|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { readFile } from 'node:fs/promises'; |
| 3 | +import vm from 'node:vm'; |
| 4 | +import test from 'node:test'; |
| 5 | + |
| 6 | +// Click-through and the hover animation both depend on state the page keeps in `app`. |
| 7 | +// These tests drive the production functions with a fake bridge and record the patches. |
| 8 | +const source = await readFile(new URL('../miniapp/client/pet-app.js', import.meta.url), 'utf8'); |
| 9 | + |
| 10 | +function slice(from, to) { |
| 11 | + const start = source.indexOf(from); |
| 12 | + assert.ok(start >= 0, `missing ${from}`); |
| 13 | + const end = source.indexOf(to, start); |
| 14 | + assert.ok(end > start, `missing ${to}`); |
| 15 | + return source.slice(start, end); |
| 16 | +} |
| 17 | + |
| 18 | +const geometry = slice('function isOverPet(', 'function isOverOverlay('); |
| 19 | +const overlays = slice('function isOverOverlay(', 'function refreshInteractivity('); |
| 20 | +const interactivity = slice('function refreshInteractivity()', 'function armForwardingWatchdog()'); |
| 21 | +const animation = slice('function desiredAnimation()', 'function availabilityText()'); |
| 22 | + |
| 23 | +function harness({ overrides = {} } = {}) { |
| 24 | + const updates = []; |
| 25 | + const app = { |
| 26 | + mode: 'window', modeSwitching: false, restoringPosition: false, awaitingPointer: false, |
| 27 | + workbenchOpen: false, menuOpen: false, dragging: false, hovered: false, pet: {}, |
| 28 | + pointer: null, clickThrough: null, ctForwarding: 'verified', ctSafeHold: false, ctRetryAt: 0, |
| 29 | + source: 'real', availability: 'ready', demoStatus: 'idle', |
| 30 | + ...overrides, |
| 31 | + }; |
| 32 | + const elements = { |
| 33 | + bubble: { hidden: true, getBoundingClientRect: () => ({ x: 0, y: 0, width: 0, height: 0 }) }, |
| 34 | + notificationStack: { hidden: true, getBoundingClientRect: () => ({ x: 0, y: 0, width: 0, height: 0 }) }, |
| 35 | + quickbar: { hidden: true, getBoundingClientRect: () => ({ x: 0, y: 0, width: 0, height: 0 }) }, |
| 36 | + menu: { hidden: true, getBoundingClientRect: () => ({ x: 0, y: 0, width: 0, height: 0 }) }, |
| 37 | + workbench: { hidden: true, getBoundingClientRect: () => ({ x: 8, y: 209, width: 284, height: 147 }) }, |
| 38 | + }; |
| 39 | + const context = vm.createContext({ |
| 40 | + app, els: elements, win: { update: async (patch) => { updates.push(patch); } }, |
| 41 | + performance: { now: () => 0 }, clearTimeout() {}, setTimeout: () => 1, |
| 42 | + console: { warn() {}, info() {}, error() {} }, errCode: (error) => error?.code ?? 'unknown', |
| 43 | + armForwardingWatchdog() {}, clearForwardingWatchdog() {}, |
| 44 | + callBridge: (run) => Promise.resolve(run()), |
| 45 | + player: { isOpaqueAt: (x, y) => x >= 78 && x <= 222 && y >= 360 && y <= 516 }, |
| 46 | + pointInRect: (rect, x, y) => x >= rect.x && x <= rect.x + rect.width && y >= rect.y && y <= rect.y + rect.height, |
| 47 | + reminders: { getState: () => ({ primary: null, animationKey: 'k' }) }, |
| 48 | + activity: () => ({ status: 'idle', session: null, key: 'k' }), |
| 49 | + activityAnim: () => 'idle', animationGate: { apply: () => {} }, stopLook() {}, |
| 50 | + }); |
| 51 | + vm.runInContext(`${geometry}\n${overlays}\n${interactivity}\n${animation} |
| 52 | + globalThis.api = { refreshInteractivity, desiredAnimation, isOverOverlay };`, context); |
| 53 | + return { app, elements, updates, api: context.api }; |
| 54 | +} |
| 55 | + |
| 56 | +test('an open card keeps only its own pixels interactive', () => { |
| 57 | + const h = harness({ overrides: { workbenchOpen: true, clickThrough: false } }); |
| 58 | + h.elements.workbench.hidden = false; |
| 59 | + h.app.pointer = { x: 150, y: 250 }; |
| 60 | + h.api.refreshInteractivity(); |
| 61 | + assert.equal(h.app.clickThrough, false, 'the card itself stays clickable'); |
| 62 | + // Above the card is transparent: clicks there must reach whatever is behind the window. |
| 63 | + h.app.pointer = { x: 150, y: 40 }; |
| 64 | + h.api.refreshInteractivity(); |
| 65 | + assert.equal(h.app.clickThrough, true, 'the margin above an open card forwards clicks'); |
| 66 | + assert.equal(h.api.isOverOverlay(150, 40), false); |
| 67 | +}); |
| 68 | + |
| 69 | +test('the transparent margin forwards clicks, and the pet does not', () => { |
| 70 | + const h = harness({ overrides: { clickThrough: false } }); |
| 71 | + h.app.pointer = { x: 150, y: 432 }; |
| 72 | + h.api.refreshInteractivity(); |
| 73 | + assert.equal(h.app.clickThrough, false, 'the pet is opaque and stays interactive'); |
| 74 | + h.app.pointer = { x: 5, y: 5 }; |
| 75 | + h.api.refreshInteractivity(); |
| 76 | + assert.equal(h.app.clickThrough, true, 'empty space forwards clicks'); |
| 77 | +}); |
| 78 | + |
| 79 | +test('a card opened without a pointer sample stays clickable', () => { |
| 80 | + // Enter on the focused pet opens the card with no mousemove behind it. |
| 81 | + const h = harness({ overrides: { workbenchOpen: true, pointer: null, clickThrough: true } }); |
| 82 | + h.api.refreshInteractivity(); |
| 83 | + assert.equal(h.app.clickThrough, false); |
| 84 | +}); |
| 85 | + |
| 86 | +test('a drag or an open menu keeps the whole window interactive', () => { |
| 87 | + for (const flag of ['dragging', 'menuOpen']) { |
| 88 | + const h = harness({ overrides: { [flag]: true, clickThrough: false } }); |
| 89 | + h.app.pointer = { x: 5, y: 5 }; |
| 90 | + h.api.refreshInteractivity(); |
| 91 | + assert.equal(h.app.clickThrough, false, flag); |
| 92 | + } |
| 93 | +}); |
| 94 | + |
| 95 | +test('opening the card does not cut the hover animation short', () => { |
| 96 | + const h = harness({ overrides: { hovered: true, workbenchOpen: true } }); |
| 97 | + // Reverting to the base state restarts the sprite at frame 0, which reads as a flicker. |
| 98 | + // The objects come from the sandbox realm, so compare the fields rather than the prototype. |
| 99 | + const state = () => { const a = h.api.desiredAnimation(); return `${a.state}:${a.key}`; }; |
| 100 | + assert.equal(state(), 'jumping:hover'); |
| 101 | + h.app.menuOpen = true; |
| 102 | + assert.equal(state(), 'idle:k', 'the menu still owns the pet'); |
| 103 | + h.app.menuOpen = false; |
| 104 | + h.app.hovered = false; |
| 105 | + assert.equal(state(), 'idle:k'); |
| 106 | +}); |
0 commit comments