feat(rerun): retain live playback and recover isolated viewer resources

This commit is contained in:
DCCONSTRUCTIONS
2026-11-04 00:42:54 +03:00
parent 669ada973f
commit 7a3c3c00b3
28 changed files with 1414 additions and 262 deletions
@@ -26,6 +26,7 @@ function fixture(t, { stopFails = false } = {}) {
timers.set(++timerId, fn); return timerId;
});
t.mock.method(globalThis, "clearTimeout", (id) => timers.delete(id));
t.mock.method(globalThis, "fetch", async () => ({ok: true, text: async () => "<!doctype html><div id=viewer></div>"}));
const listeners = new Map();
const viewer = { stop() { stops++; if (stopFails) throw new Error("partial start"); } };
const mount = {};
@@ -39,7 +40,10 @@ function fixture(t, { stopFails = false } = {}) {
};
const host = { dataset: {}, ownerDocument: { createElement(name) {
assert.equal(name, "iframe"); return frame;
} }, append(child) { assert.equal(child, frame); } };
} }, append(child) {
assert.equal(child, frame);
assert.equal(child.src, '/rerun-runtime.html', 'URL must precede browsing-context insertion');
} };
return { host, frame, viewer, mount, timers, listeners,
counts: () => ({ creates, stops, removals }) };
}
@@ -87,6 +91,27 @@ test("recorded realm terminates on close even if upstream stop throws", async (t
assert.equal(ready.viewer.ready, false);
});
test("playback poll publishes only the latest native eye and removes it on disposal", async (t) => {
const f = fixture(t);
let time = 1;
let position = [300, 7, 8];
f.viewer.get_current_time = () => time;
f.viewer.get_camera_eye = () => ({ position, lookTarget: [298, 9, 0], eyeUp: [0, 0, 1] });
const scope = createIsolatedRerunHost(f.host);
f.frame.onload();
const { viewer } = await scope.ready;
assert.equal(viewer.get_current_time("recording", "session_time"), 1);
assert.deepEqual(JSON.parse(f.host.dataset.rerunCamera).eye.position, [300, 7, 8]);
time = 2;
position = [310, 9, 8];
assert.equal(viewer.get_current_time("recording", "session_time"), 2);
const latest = JSON.parse(f.host.dataset.rerunCamera);
assert.equal(latest.timeNs, 2);
assert.deepEqual(latest.eye.position, position);
scope.dispose();
assert.equal(f.host.dataset.rerunCamera, undefined);
});
test("native frame input relays Escape and leaves pointer ownership inside Rerun", () => {
const listeners = new Map(), events = [];
class ParentEvent { constructor(type, init) { this.type = type; Object.assign(this, init); } }
@@ -146,6 +171,95 @@ test("close before frame load cancels startup and cannot reopen", async (t) => {
assert.equal(f.timers.size, 0);
});
test("detached observation portal navigates only after attachment and cancels pending navigation", async (t) => {
const f = fixture(t), frames = new Map(); let sequence = 0;
f.host.isConnected = f.frame.isConnected = false;
f.host.ownerDocument.defaultView = {
requestAnimationFrame() { throw new Error('Background tab does not paint'); },
setTimeout(fn) { frames.set(++sequence, fn); return sequence; },
clearTimeout(id) { frames.delete(id); },
};
const tick = () => { const [id, fn] = frames.entries().next().value; frames.delete(id); fn(); };
const scope = createIsolatedRerunHost(f.host);
tick(); tick();
assert.equal(f.frame.src, undefined);
f.host.isConnected = f.frame.isConnected = true;
tick(); assert.equal(f.frame.src, '/rerun-runtime.html');
f.frame.onload(); await scope.ready; scope.dispose();
assert.equal(frames.size, 0);
f.host.isConnected = f.frame.isConnected = false;
const pending = createIsolatedRerunHost(f.host);
const rejection = assert.rejects(pending.ready, /disposed/);
pending.dispose(); await rejection;
assert.equal(frames.size, 0);
});
test("initial blank load is ignored while the bundled document is fetched within its deadline", async (t) => {
const f = fixture(t), navigations = new Map(); let sequence = 0;
f.host.ownerDocument.defaultView = {
setTimeout(fn) { navigations.set(++sequence, fn); return sequence; },
clearTimeout(id) { navigations.delete(id); },
};
const tick = () => { const [id, fn] = navigations.entries().next().value; navigations.delete(id); fn(); };
const scope = createIsolatedRerunHost(f.host);
const deadline = [...f.timers.keys()];
tick();
const api = f.frame.contentWindow.missionCoreRerun;
delete f.frame.contentWindow.missionCoreRerun;
f.frame.contentDocument = {URL: "about:blank"};
f.frame.onload(); f.frame.onload();
assert.equal(navigations.size, 0);
assert.deepEqual([...f.timers.keys()], deadline);
assert.equal(f.frame.src, "/rerun-runtime.html");
f.frame.contentWindow.missionCoreRerun = api;
f.frame.contentDocument.URL = "http://localhost/rerun-runtime.html";
f.frame.onload(); await scope.ready;
assert.equal(navigations.size, 0);
scope.dispose();
assert.equal(f.timers.size, 0);
});
test("bundled document is fetched once and installed before the isolated realm is attached", async (t) => {
const f = fixture(t); let requests = 0, attached = 0;
t.mock.method(globalThis, 'fetch', async (url, options) => {
assert.equal(url, '/rerun-runtime.html');
assert.equal(options.redirect, 'error'); requests++;
return {ok:true, text:async()=>'<html>runtime</html>'};
});
f.host.append = frame => { assert.equal(frame.srcdoc, '<html>runtime</html>'); attached++; };
const scope = createIsolatedRerunHost(f.host);
await new Promise(setImmediate);
assert.equal(requests, 1); assert.equal(attached, 1);
f.frame.onload(); await scope.ready; scope.dispose();
assert.equal(f.timers.size, 0);
});
test("closing during document fetch cancels it and late content cannot attach a frame", async (t) => {
const f = fixture(t); let finish, signal, attached = 0;
t.mock.method(globalThis, 'fetch', (_url, options) => {
signal=options.signal; return new Promise(resolve=>{finish=resolve;});
});
f.host.append = () => attached++;
const scope = createIsolatedRerunHost(f.host);
const rejected = assert.rejects(scope.ready, /disposed/);
scope.dispose(); await rejected;
assert.equal(signal.aborted, true);
finish({ok:true,text:async()=>'<html>late</html>'});
await new Promise(setImmediate);
assert.equal(attached, 0); assert.equal(f.counts().creates, 0);
assert.equal(f.timers.size, 0);
});
test("document fetch failure releases the empty realm and its deadline", async (t) => {
const f = fixture(t);
t.mock.method(globalThis, 'fetch', async()=>({ok:false}));
const scope = createIsolatedRerunHost(f.host);
await assert.rejects(scope.ready, /failed to load/);
assert.equal(f.counts().removals, 1);
assert.equal(f.timers.size, 0);
});
test("closing during SDK startup also releases a late successful start", async () => {
let finish, stops = 0;
const owner = bridge({