Files
vibemake/tests/walking-lifecycle.test.js
T

37 lines
1.8 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { disposeWalkingFrame, walkingErrorMessage } from '../js/walking-lifecycle.js';
function fixture() {
const target = new EventTarget(), posted = [];
let removed = 0;
const child = { postMessage: (...args) => posted.push(args) };
const frame = { contentWindow: child, getAttribute: () => '/play/robots/index.html', remove: () => removed++ };
const message = (channel, origin = 'https://example.com', source = child) => {
const event = new Event('message');
Object.assign(event, { origin, source, data: { type: 'vibemake:walk-disposed', channel } });
target.dispatchEvent(event);
};
return { frame, message, posted, removed: () => removed, options: { eventTarget: target, origin: 'https://example.com', timeoutMs: 25 } };
}
test('iframe is removed only after a trusted disposal acknowledgment', async () => {
const f = fixture();
const pending = disposeWalkingFrame(f.frame, 'test', f.options);
assert.equal(f.posted[0][0].type, 'vibemake:walk-dispose');
f.message('other'); f.message('test', 'https://other.example'); f.message('test', 'https://example.com', {});
assert.equal(f.removed(), 0);
f.message('test');
await pending;
assert.equal(f.removed(), 1);
});
test('failed startup cannot retain its iframe indefinitely', async () => {
const f = fixture();
await disposeWalkingFrame(f.frame, 'test', f.options);
f.message('test');
assert.equal(f.removed(), 1);
});
test('memory failure is explained as browser allocation failure, other errors preserved', () => {
assert.match(walkingErrorMessage('no available backend found. ERR: [wasm] RangeError: Out of memory'), /浏览器.*分配内存/);
assert.equal(walkingErrorMessage('robot.json invalid'), 'robot.json invalid');
});