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 canonicalMapGravityLocalPointToBodyGround; let canonicalRecordedLabPackedTgsCells; let canonicalRecordedLabTgsIsCurrent; let resolveCanonicalLabReplay; before(async () => { server = await createServer({ appType: "custom", logLevel: "silent", server: { middlewareMode: true }, }); ({ canonicalMapGravityLocalPointToBodyGround, canonicalRecordedLabPackedTgsCells, canonicalRecordedLabTgsIsCurrent, } = await server.ssrLoadModule("/src/core/laboratory/canonicalRecordedLab.ts")); ({ resolveCanonicalLabReplay } = await server.ssrLoadModule( "/src/core/laboratory/canonicalLabReplay.ts", )); }); after(async () => { await server?.close(); }); const identity = [ [1, 0, 0], [0, 1, 0], [0, 0, 1], ]; test("canonical TGS validity never retains a sparse anchor beyond its sealed history", () => { assert.equal(canonicalRecordedLabTgsIsCurrent(2_000_000_000, 1_000_000_000), true); assert.equal(canonicalRecordedLabTgsIsCurrent(2_000_000_001, 1_000_000_000), false); assert.equal(canonicalRecordedLabTgsIsCurrent(999_999_999, 1_000_000_000), false); }); test("map-gravity-local TGS uses sensor translation and current ground body exactly once", () => { const anchor = { originMapXyzM: [10, 20, 0.68], sensorOriginMapXyzM: [10, 20, 1], basisMapFromBody: identity, }; const current = { originMapXyzM: [8, 20, 0], sensorOriginMapXyzM: [8, 20, 0.32], basisMapFromBody: identity, }; assert.deepEqual( canonicalMapGravityLocalPointToBodyGround([1, 2, -1], anchor, current), [3, 2, 0], ); const packed = canonicalRecordedLabPackedTgsCells({ centersXyM: [[1, 2]], stateCodes: [2], zBoundsM: [[-1, 0]], }, anchor, current); assert.deepEqual([...packed.centersBodyXyM], [3, 2]); assert.deepEqual([...packed.zBoundsM], [0, 1]); assert.deepEqual([...packed.stateCodes], [2]); }); test("recorded LAB spatial loading is shared, profile-bound and experiment-neutral", async () => { const [contract, scheduler, vegetation] = await Promise.all([ readFile(new URL("../src/core/laboratory/canonicalRecordedLabSpatial.ts", import.meta.url), "utf8"), readFile(new URL("../src/components/laboratory/useCanonicalRecordedLabSpatialFrame.ts", import.meta.url), "utf8"), readFile(new URL("../src/core/laboratory/vegetationShadow.ts", import.meta.url), "utf8"), ]); assert.match(contract, /CANONICAL_RECORDED_LAB_SPATIAL_PROFILE/); assert.match(contract, /profile: CANONICAL_RECORDED_LAB_SPATIAL_PROFILE/); assert.match(scheduler, /Shared latest-request-wins scheduler/); assert.match(scheduler, /identityRef\.current !== requestIdentity/); assert.doesNotMatch(scheduler, /RAVNOVES|vegetation|DDRNet/); assert.doesNotMatch(vegetation, /fetchCanonicalRecordedLabSpatialFrame|CanonicalRecordedLabSpatialFrame/); }); test("canonical LAB resolves one generation-bound merged RRD", async () => { const baseGeneration = "a".repeat(64); const replayGeneration = "b".repeat(64); const resultId = `lab-v1-vegetation-shadow-${"c".repeat(64)}`; let request; const replay = await resolveCanonicalLabReplay(resultId, { kind: "rerun-recording", sessionId: "session-001", sourceUrl: "/api/v1/observation-sessions/session-001/recording.rrd", viewerSourceUrl: `/api/v1/observation-sessions/session-001/recording.rrd?generation=${baseGeneration}`, mediaType: "application/vnd.rerun.rrd", timeline: "session_time", timelineStartSeconds: 0, timelineEndSeconds: 10, seekable: true, byteLength: 100, sha256: baseGeneration, playback: { speed: 1, loop: false }, mediaSources: [], }, { origin: "http://mission-core.test", fetcher: async (url, options) => { request = { url, options }; return new Response(null, { status: 200, headers: { "Content-Type": "application/vnd.rerun.rrd", "Content-Length": "234567", "ETag": `"${replayGeneration}"`, "X-Rerun-Format": "RRF2", }, }); }, }); const sourceUrl = `/api/v1/laboratory/vegetation-shadow/${resultId}/canonical-replay.rrd`; assert.equal( request.url, `http://mission-core.test${sourceUrl}?base_generation=${baseGeneration}`, ); assert.equal(request.options.method, "HEAD"); assert.deepEqual(replay, { sourceUrl, viewerSourceUrl: `${sourceUrl}?generation=${replayGeneration}`, byteLength: 234567, sha256: replayGeneration, blueprintSourceUrl: "/api/v1/observation-sessions/session-001/blueprint.rrd", }); }); for (const [prefix, sourceKind] of [ ["m49-tgs-portable-review", "portable-tgs"], ["lab-v1-eomt-ddrnet", "portable-semantic"], ]) test(`${sourceKind} resolves Core cache for the exact result and base, never a legacy LAB`, async () => { const base = "a".repeat(64), generation = "b".repeat(64); const resultId = `${prefix}-${"c".repeat(64)}`; const launch = { sourceUrl: "/api/v1/observation-sessions/source/recording.rrd", viewerSourceUrl: `/api/v1/observation-sessions/source/recording.rrd?generation=${base}`, sha256: base }; let calls = 0; const fetcher = async (url, options) => { calls++; assert.equal(url, `http://mission-core.test/api/v1/observatory/portable-results/${resultId}/replays/${base}/recording.rrd`); assert.equal(options.method, "HEAD"); return new Response(null, { headers: { "Content-Type": "application/vnd.rerun.rrd", "Content-Length": "100", "ETag": `"${generation}"`, "X-Rerun-Format": "RRF2" } }); }; const options = { sourceKind, origin: "http://mission-core.test", fetcher }; const replay = await resolveCanonicalLabReplay(resultId, launch, options); assert.equal(replay.viewerSourceUrl, `${replay.sourceUrl}?generation=${generation}`); assert.equal(replay.blueprintSourceUrl, "/api/v1/observation-sessions/source/blueprint.rrd"); await assert.rejects(resolveCanonicalLabReplay(`lab-v1-vegetation-shadow-${"c".repeat(64)}`, launch, options)); await assert.rejects(resolveCanonicalLabReplay(resultId, launch, { ...options, sourceKind: sourceKind === "portable-tgs" ? "portable-semantic" : "portable-tgs", })); await assert.rejects(resolveCanonicalLabReplay(resultId, { ...launch, sha256: "d".repeat(64) }, options)); assert.equal(calls, 1); });