146 lines
6.2 KiB
JavaScript
146 lines
6.2 KiB
JavaScript
import { describe, expect, it } from "bun:test";
|
|
import {
|
|
createHudFocusStack,
|
|
DYNAMIC_Z_BASE,
|
|
PINNED_Z_BASE,
|
|
} from "./hud-focus-stack.js";
|
|
|
|
function makeMockCard(id, classes = ["hud-panel", "is-spotlight"]) {
|
|
const classSet = new Set(classes);
|
|
const listeners = new Map();
|
|
return {
|
|
id,
|
|
isConnected: true,
|
|
style: { zIndex: "" },
|
|
dataset: {},
|
|
classList: {
|
|
add(...names) {
|
|
names.forEach((n) => classSet.add(n));
|
|
},
|
|
remove(...names) {
|
|
names.forEach((n) => classSet.delete(n));
|
|
},
|
|
toggle(name, force) {
|
|
const shouldHave = force === undefined ? !classSet.has(name) : Boolean(force);
|
|
if (shouldHave) classSet.add(name);
|
|
else classSet.delete(name);
|
|
return shouldHave;
|
|
},
|
|
contains(name) {
|
|
return classSet.has(name);
|
|
},
|
|
},
|
|
addEventListener(type, fn) {
|
|
if (!listeners.has(type)) listeners.set(type, []);
|
|
listeners.get(type).push(fn);
|
|
},
|
|
trigger(type, payload = {}) {
|
|
for (const fn of listeners.get(type) || []) fn(payload);
|
|
},
|
|
};
|
|
}
|
|
|
|
describe("createHudFocusStack", () => {
|
|
it("promotes user-clicked News card above Compute card, then lets idle Stage Act re-focus Compute card", () => {
|
|
let clock = 1000;
|
|
const stack = createHudFocusStack({
|
|
nowFn: () => clock,
|
|
stageGraceMs: 1200,
|
|
ambientGraceMs: 3500,
|
|
});
|
|
|
|
const newsCard = makeMockCard("news-asia", ["hud-panel", "news-region-card", "is-spotlight"]);
|
|
const computeCard = makeMockCard("compute-cn", ["hud-panel", "compute-card", "is-spotlight"]);
|
|
|
|
stack.register(newsCard);
|
|
stack.register(computeCard);
|
|
|
|
// Initially computeCard was registered second, so computeCard is on top
|
|
expect(Number(computeCard.style.zIndex)).toBeGreaterThan(Number(newsCard.style.zIndex));
|
|
|
|
// User clicks newsCard -> newsCard immediately rises above computeCard
|
|
newsCard.trigger("pointerdown", { pointerId: 1 });
|
|
stack.notifyPointerRelease(1);
|
|
expect(Number(newsCard.style.zIndex)).toBeGreaterThan(Number(computeCard.style.zIndex));
|
|
expect(newsCard.classList.contains("is-hud-focused")).toBe(true);
|
|
expect(computeCard.classList.contains("is-hud-focused")).toBe(false);
|
|
|
|
// 300ms later (within grace window), passive system step does NOT steal focus from user's click
|
|
clock += 300;
|
|
const blocked = stack.focus(computeCard, { source: "system", kind: "stage_act" });
|
|
expect(blocked).toBe(false);
|
|
expect(Number(newsCard.style.zIndex)).toBeGreaterThan(Number(computeCard.style.zIndex));
|
|
|
|
// User stays idle (clock advances past grace window) and Compute Stage Tour advances to next act
|
|
clock += 2000;
|
|
const promoted = stack.focus(computeCard, { source: "system", kind: "stage_act" });
|
|
expect(promoted).toBe(true);
|
|
expect(Number(computeCard.style.zIndex)).toBeGreaterThan(Number(newsCard.style.zIndex));
|
|
expect(computeCard.classList.contains("is-hud-focused")).toBe(true);
|
|
expect(newsCard.classList.contains("is-hud-focused")).toBe(false);
|
|
});
|
|
|
|
it("never steals focus while the user is actively holding pointer down", () => {
|
|
let clock = 5000;
|
|
const stack = createHudFocusStack({ nowFn: () => clock });
|
|
const newsCard = makeMockCard("news-eu", ["hud-panel", "news-region-card", "is-spotlight"]);
|
|
const computeCard = makeMockCard("compute-us", ["hud-panel", "compute-card", "is-spotlight"]);
|
|
|
|
stack.register(newsCard);
|
|
stack.register(computeCard);
|
|
|
|
// User presses and holds newsCard for 5 seconds (e.g. dragging or reading while holding)
|
|
newsCard.trigger("pointerdown", { pointerId: 7 });
|
|
clock += 5000;
|
|
expect(stack.focus(computeCard, { source: "system", kind: "stage_act" })).toBe(false);
|
|
expect(Number(newsCard.style.zIndex)).toBeGreaterThan(Number(computeCard.style.zIndex));
|
|
|
|
// Once user releases pointer and stays idle, next system act acquires focus
|
|
stack.notifyPointerRelease(7);
|
|
expect(stack.focus(computeCard, { source: "system", kind: "stage_act" })).toBe(true);
|
|
expect(Number(computeCard.style.zIndex)).toBeGreaterThan(Number(newsCard.style.zIndex));
|
|
});
|
|
|
|
it("keeps Always-On-Top pinned surfaces (#stage-narrative-bar, pinned Copilot) above all dynamic cards", () => {
|
|
let clock = 1000;
|
|
const stack = createHudFocusStack({ nowFn: () => clock });
|
|
|
|
const stageBar = makeMockCard("stage-narrative-bar", ["hud-panel", "stage-narrative-bar"]);
|
|
const copilot = makeMockCard("situational-copilot", ["hud-panel", "situational-copilot"]);
|
|
const computeCard = makeMockCard("compute-cn", ["hud-panel", "compute-card", "is-spotlight"]);
|
|
const newsCard = makeMockCard("news-na", ["hud-panel", "news-region-card", "is-spotlight"]);
|
|
|
|
stack.register(stageBar, { pinned: true, priority: 2 });
|
|
stack.register(copilot, { pinned: false });
|
|
stack.register(computeCard);
|
|
stack.register(newsCard);
|
|
|
|
// User clicks computeCard, then newsCard -> stageBar remains strictly above all of them
|
|
stack.focus(computeCard, { source: "user" });
|
|
clock += 100;
|
|
stack.focus(newsCard, { source: "user" });
|
|
|
|
expect(Number(stageBar.style.zIndex)).toBeGreaterThanOrEqual(PINNED_Z_BASE);
|
|
expect(Number(stageBar.style.zIndex)).toBeGreaterThan(Number(newsCard.style.zIndex));
|
|
expect(Number(newsCard.style.zIndex)).toBeGreaterThan(Number(computeCard.style.zIndex));
|
|
expect(Number(computeCard.style.zIndex)).toBeGreaterThan(Number(copilot.style.zIndex));
|
|
expect(Number(copilot.style.zIndex)).toBeGreaterThanOrEqual(DYNAMIC_Z_BASE);
|
|
|
|
// User pins Copilot -> Copilot enters Always-On-Top tier, and clicking newsCard cannot cover Copilot or stageBar
|
|
stack.setPinned(copilot, true, { priority: 1 });
|
|
clock += 200;
|
|
stack.focus(newsCard, { source: "user" });
|
|
|
|
expect(stack.isPinned(copilot)).toBe(true);
|
|
expect(Number(copilot.style.zIndex)).toBeGreaterThan(Number(newsCard.style.zIndex));
|
|
expect(Number(stageBar.style.zIndex)).toBeGreaterThan(Number(copilot.style.zIndex));
|
|
|
|
// Unpinning Copilot returns it to top of dynamic band, so a subsequent click on newsCard rises above Copilot
|
|
stack.setPinned(copilot, false);
|
|
expect(stack.isPinned(copilot)).toBe(false);
|
|
clock += 200;
|
|
stack.focus(newsCard, { source: "user" });
|
|
expect(Number(newsCard.style.zIndex)).toBeGreaterThan(Number(copilot.style.zIndex));
|
|
});
|
|
});
|