import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import { after, before, test } from "node:test"; import { createServer } from "vite"; let server; let claimExclusiveLiveViewer; let createRecordedOpenWatchdog; let createReentrantViewerDisposer; let isLiveRerunPresentationReady; let liveTimelineNeedsSynchronization; let liveRerunReceiverBindingIdentity; let recordedOpenWatchdogTimeoutMs; let rerunViewerInitialSource; let resolveRecordedViewerSourceUrl; let isRecordedRrdSource; let resolveRecordedBlueprintUrl; before(async () => { server = await createServer({ appType: "custom", logLevel: "silent", server: { middlewareMode: true }, }); ({ claimExclusiveLiveViewer, createRecordedOpenWatchdog, createReentrantViewerDisposer, isLiveRerunPresentationReady, liveTimelineNeedsSynchronization, liveRerunReceiverBindingIdentity, recordedOpenWatchdogTimeoutMs, rerunViewerInitialSource, resolveRecordedViewerSourceUrl, isRecordedRrdSource, resolveRecordedBlueprintUrl, } = await server.ssrLoadModule("/src/components/RerunViewport.tsx")); }); after(async () => { await server?.close(); }); const sourceUrl = "/api/v1/observation-sessions/session-atomic/recording.rrd"; const sha256 = "a".repeat(64); const viewerSourceUrl = `${sourceUrl}?generation=${sha256}`; test("only the canonical digest-bound generation URL reaches the native Rerun receiver", () => { const descriptor = { sourceUrl, viewerSourceUrl, byteLength: 246_331_680, sha256, }; const resolved = resolveRecordedViewerSourceUrl(descriptor, "http://mission-core.test"); assert.equal(resolved, `http://mission-core.test${viewerSourceUrl}`); assert.equal(rerunViewerInitialSource(resolved), resolved); for (const unsafeViewerSourceUrl of [ sourceUrl, `${sourceUrl}?generation=${"b".repeat(64)}`, `${viewerSourceUrl}&extra=true`, `https://foreign.test${viewerSourceUrl}`, ]) { assert.throws( () => resolveRecordedViewerSourceUrl({ ...descriptor, viewerSourceUrl: unsafeViewerSourceUrl, }, "http://mission-core.test"), /Unsafe recorded RRD viewer descriptor/, ); } }); test("one canonical LAB replay generation reaches the same native receiver", () => { const labSource = `/api/v1/laboratory/vegetation-shadow/lab-v1-vegetation-shadow-${"c".repeat(64)}` + "/canonical-replay.rrd"; const descriptor = { sourceUrl: labSource, viewerSourceUrl: `${labSource}?generation=${sha256}`, byteLength: 300_000_000, sha256, }; assert.equal( resolveRecordedViewerSourceUrl(descriptor, "http://mission-core.test"), `http://mission-core.test${descriptor.viewerSourceUrl}`, ); }); for (const prefix of [ "m49-tgs-portable-review", "lab-v1-eomt-ddrnet", "ai-layer-ddrnet", "ai-layer-eomt", "ai-layer-rf-detr", "ai-layer-object-distance", ]) test(`${prefix} replay uses recorded admission and the shared source blueprint`, () => { const source = `/api/v1/observatory/portable-results/${prefix}-${"c".repeat(64)}/replays/${"d".repeat(64)}/recording.rrd`; const blueprint = "/api/v1/observation-sessions/source/blueprint.rrd"; assert.equal(isRecordedRrdSource(source), true); assert.equal(isRecordedRrdSource(source.replace("/replays/", "/untrusted/")), false); assert.equal(resolveRecordedViewerSourceUrl({ sourceUrl: source, viewerSourceUrl: `${source}?generation=${sha256}`, sha256, byteLength: 100 }, "http://mission-core.test"), `http://mission-core.test${source}?generation=${sha256}`); assert.equal(resolveRecordedBlueprintUrl(source, "http://mission-core.test", blueprint), `http://mission-core.test${blueprint}`); }); test("live presentation waits for the exact receiver to expose a usable range", () => { assert.equal(isLiveRerunPresentationReady(false, { min: 1, max: 2 }, 1), false); assert.equal(isLiveRerunPresentationReady(true, null, 1), false); assert.equal(isLiveRerunPresentationReady(true, { min: 2, max: 1 }, 1), false); assert.equal(isLiveRerunPresentationReady(true, { min: 1, max: 2 }, 0), false); assert.equal(isLiveRerunPresentationReady(true, { min: 1, max: 1 }, 1), true); }); test("live timeline is retried only after stream_time exists and until it is active", () => { assert.equal(liveTimelineNeedsSynchronization(false, undefined, { min: 1, max: 2 }), false); assert.equal(liveTimelineNeedsSynchronization(true, undefined, null), false); assert.equal(liveTimelineNeedsSynchronization(true, undefined, { min: 1, max: 2 }), true); assert.equal(liveTimelineNeedsSynchronization(true, "log_time", { min: 1, max: 2 }), true); assert.equal(liveTimelineNeedsSynchronization(true, "stream_time", { min: 1, max: 2 }), false); }); test("live receiver binding stays stable across recovery authority projections", () => { const sourceUrl = "rerun+http://127.0.0.1:9877/proxy"; const streamId = "acq-001"; assert.equal( liveRerunReceiverBindingIdentity(sourceUrl, streamId, true), liveRerunReceiverBindingIdentity(` ${sourceUrl} `, streamId, true), ); assert.notEqual( liveRerunReceiverBindingIdentity(sourceUrl, streamId, true), liveRerunReceiverBindingIdentity(sourceUrl, "acq-002", true), ); assert.notEqual( liveRerunReceiverBindingIdentity(sourceUrl, streamId, true), liveRerunReceiverBindingIdentity(sourceUrl, streamId, false), ); }); test("recorded admission watchdog is size-aware and exits an incomplete load", () => { const smallDelay = recordedOpenWatchdogTimeoutMs(4); const currentDelay = recordedOpenWatchdogTimeoutMs(246_331_680); const largeDelay = recordedOpenWatchdogTimeoutMs(805_687_122); assert.ok(smallDelay >= 120_000); assert.ok(currentDelay >= smallDelay); assert.ok(largeDelay > currentDelay); assert.ok(largeDelay <= 1_800_000); assert.ok(largeDelay > 12_000); let scheduledCallback; let scheduledDelay; let timeoutCount = 0; const cancelled = []; const watchdog = createRecordedOpenWatchdog({ byteLength: 246_331_680, schedule(callback, timeoutMs) { scheduledCallback = callback; scheduledDelay = timeoutMs; return 17; }, cancel(handle) { cancelled.push(handle); }, onTimeout() { timeoutCount += 1; }, }); watchdog.arm(); assert.equal(watchdog.pending(), true); assert.equal(scheduledDelay, currentDelay); scheduledCallback(); assert.equal(timeoutCount, 1); assert.equal(watchdog.pending(), false); assert.deepEqual(cancelled, []); }); test("complete recorded admission clears its watchdog", () => { let timeoutCount = 0; const cancelled = []; const watchdog = createRecordedOpenWatchdog({ byteLength: 805_687_122, schedule() { return 23; }, cancel(handle) { cancelled.push(handle); }, onTimeout() { timeoutCount += 1; }, }); watchdog.arm(); watchdog.clear(); assert.equal(watchdog.pending(), false); assert.equal(timeoutCount, 0); assert.deepEqual(cancelled, [23]); }); test("deferred viewer start cannot reopen after stale unmount", async () => { let resolveStart; const start = new Promise((resolve) => { resolveStart = resolve; }); let disposed = false; let cleanupCount = 0; let closeCount = 0; let stopCount = 0; const diagnostics = []; const disposeViewer = createReentrantViewerDisposer( () => { cleanupCount += 1; }, () => { closeCount += 1; stopCount += 1; }, ); const pendingMount = (async () => { await start; if (disposed) { disposeViewer(); return; } diagnostics.push("admitted"); })(); disposed = true; disposeViewer(); resolveStart(); await pendingMount; assert.equal(cleanupCount, 1); assert.equal(closeCount, 2); assert.equal(stopCount, 2); assert.deepEqual(diagnostics, []); }); test("recorded RRD bytes are never split across LogChannel.send_rrd calls", async () => { const source = await readFile( new URL("../src/components/RerunViewport.tsx", import.meta.url), "utf8", ); assert.doesNotMatch(source, /streamVerifiedRecordedRrd/); assert.doesNotMatch(source, /missioncore\/recorded-recording/); assert.doesNotMatch(source, /recordedChannel/); assert.match(source, /recordedPerceptionLayers\.costmap,/); assert.match(source, /recordedPerceptionLayers\.costmap !== undefined && status !== "ready"/); assert.match(source, /perceptionLayers\.costmap === undefined \? \{\} : \{\s*show_costmap: perceptionLayers\.costmap,\s*reactivate_updates: true/s); assert.match(source, /viewer\.start\(\s*rerunViewerInitialSource\(resolvedSource\)/s); assert.doesNotMatch(source, /rerunViewerOpenOptions/); assert.match( source, /recordingOpened = true;[\s\S]*diagnosticLifecycle\.markAdmitted\(\);/, ); assert.match( source, /viewer\.get_active_recording_id\(\)[\s\S]*admitRecording\(\{[\s\S]*application_id: "nodedc_mission_core_spatial"/, ); assert.match( source, /const readyToRender = followLive\s*\? liveTimelineSynchronized && isLiveRerunPresentationReady\(/, ); assert.match( source, /viewer\.get_active_timeline\(event\.recording_id\)[\s\S]*viewer\.set_active_timeline\(event\.recording_id, timeline\)/, ); assert.match( source, /if \(readyToRender && !readyPublished\)[\s\S]*eventCode: "live_receiver_active_store_admitted"[\s\S]*diagnosticLifecycle\.markAdmitted\(\)/, ); assert.match( source, /liveOpenWatchdog = initialLiveReceiverOpenWatchdogState\(null, Date\.now\(\)\)/, ); assert.doesNotMatch( source, /initialLiveReceiverOpenWatchdogState\(\s*liveActivitySequenceRef\.current/, ); assert.doesNotMatch( source, /\], \[followLive, liveRecoveryAuthorityIdentity, liveStreamId, sourceUrl\]\);/, ); assert.match( source, /viewerStartResolved = true;\s*if \(isRecordedSource && !recordedSceneAdmitted\) recordedOpenWatchdog\?\.arm\(\);/, ); assert.match( source, /if \(readyToRender && !readyPublished\)[\s\S]*clearRecordedAdmissionWatchdog\(\);/, ); assert.match( source, /reloadRecordedViewerAfterStaleModuleFailure\(\{[\s\S]*loadedUiBuildId: diagnosticLifecycle\.lineage\.uiBuildId/, ); assert.match( source, /!recordedPerceptionUrl \|\| !recordedPerceptionLayers\.enabled/, ); }); test("one live document owns one native Rerun receiver", async () => { const releases = []; const releaseFirstClaim = claimExclusiveLiveViewer(() => releases.push("first")); const releaseSecondClaim = claimExclusiveLiveViewer(() => releases.push("second")); assert.deepEqual(releases, ["first"]); releaseFirstClaim(); assert.deepEqual(releases, ["first"]); releaseSecondClaim(); const releaseThirdClaim = claimExclusiveLiveViewer(() => releases.push("third")); assert.deepEqual(releases, ["first"]); releaseThirdClaim(); }); test("raw replay exercises the same streaming receiver lifecycle as a live scan", async () => { const source = await readFile( new URL("../src/workspaces/Workspaces.tsx", import.meta.url), "utf8", ); assert.match( source, /const liveRerunSource = !recordedSource && \/\^rerun\\\+https\?:\\\/\\\//, ); assert.match( source, /const livePresentationActivitySequence = metrics\?\.publishedFrameCount \?\?[\s\S]*liveRerunSource && !streamActive \? 1 : null/, ); assert.match( source, /liveAcquisitionRerunProfile\(\{[\s\S]*liveActivitySequence: livePresentationActivitySequence/, ); assert.match(source, / { const source = await readFile( new URL("../src/workspaces/Workspaces.tsx", import.meta.url), "utf8", ); assert.match( source, /const intentionalSourceEnd = !recordedSource &&\s*state\?\.sourceMode === "idle" && \[/, ); assert.match( source, /"awaiting_external_stop",\s*"stopping",\s*"finalizing",\s*"completed",/, ); assert.doesNotMatch( source, /const intentionalSourceEnd = !recordedSource && \[\s*"awaiting_external_stop"/, ); }); test("the spatial header reports raw replay as an active source", async () => { const source = await readFile( new URL("../src/App.tsx", import.meta.url), "utf8", ); assert.match( source, /sourceMode === "replay"[\s\S]*\? "Повтор записи"[\s\S]*: "Ожидание эфира"/, ); }); test("the complete vendor canvas host is hidden during partial and failed admission", async () => { const css = await readFile(new URL("../src/styles/spatial.css", import.meta.url), "utf8"); assert.match( css, /\.rerun-viewport:is\(\[data-status="loading"\], \[data-status="error"\]\)\s+\.rerun-viewport__canvas\s*\{[^}]*visibility:\s*hidden;[^}]*pointer-events:\s*none;/s, ); assert.doesNotMatch( css, /data-status="loading"[^}]*\.rerun-viewport__canvas\s*>\s*div/s, ); });