import assert from "node:assert/strict"; import { access, readFile } from "node:fs/promises"; import { after, before, test } from "node:test"; import { createServer } from "vite"; let server; let fetchVegetationBenchmarkResult; let fetchCanonicalRecordedLabSpatialFrame; let fetchVegetationShadowResult; let fetchVegetationRouteTgsAnchor; let vegetationFullRouteMaskUrl; before(async () => { server = await createServer({ appType: "custom", logLevel: "silent", server: { middlewareMode: true }, }); ({ fetchVegetationBenchmarkResult, fetchVegetationShadowResult, fetchVegetationRouteTgsAnchor, vegetationFullRouteMaskUrl, } = await server.ssrLoadModule( "/src/core/laboratory/vegetationShadow.ts", )); ({ fetchCanonicalRecordedLabSpatialFrame } = await server.ssrLoadModule( "/src/core/laboratory/canonicalRecordedLabSpatial.ts", )); }); after(async () => { await server?.close(); }); const resultId = `lab-v1-vegetation-shadow-${"a".repeat(64)}`; const benchmarkResultId = `lab-v1-vegetation-benchmark-${"d".repeat(64)}`; function candidate(candidateKey, vegetationIou) { return { loaded_model_name: candidateKey === "ddrnet" ? "ddrnet_39" : "pp_lite_t_seg", checkpoint_sha256: (candidateKey === "ddrnet" ? "b" : "c").repeat(64), validation_metrics: { mean_iou_percent: 44.2, published_mean_iou_percent: 46.53, vegetation_mean_iou: vegetationIou, }, validation_timing: { latency_ms_p95: 22.4, throughput_fps_from_mean_inference: 48.1, }, shadow_timing: { prewarm_latency_ms: 612.4, latency_ms_p95: 21.8, throughput_fps_from_mean_inference: 49.2, }, resource: { peak_reserved_vram_bytes: 2_000_000_000, gpu_name: "NVIDIA GeForce RTX 4090", }, }; } function visualCase(sourceKind, index) { const caseId = `case-${index}`; const keys = sourceKind === "goose" ? ["source", "truth", "ddrnet", "ppliteseg", "ddrnet_error", "ppliteseg_error"] : ["source", "ddrnet", "ppliteseg", "urban", "rural", "offroad"]; return { case_id: caseId, source_kind: sourceKind, width: sourceKind === "goose" ? 512 : 800, height: sourceKind === "goose" ? 512 : 600, center_crop_xyxy: sourceKind === "goose" ? [0, 0, 512, 512] : [100, 0, 700, 600], outside_crop_state: sourceKind === "goose" ? "not-applicable" : "undefined", focus: sourceKind === "goose" ? { class_name: "high_grass", label_id: 51, truth_pixels: 16384, truth_fraction: 0.0625, stratum_rank: index + 1, } : null, assets: Object.fromEntries(keys.map((key) => [key, { path: `visual/${sourceKind}/${caseId}/${key}.png`, sha256: "d".repeat(64), }])), }; } function routeVideo() { return { worker_result_id: `lab-v1-ravnoves-video-ddrnet-${"e".repeat(64)}`, m47_reference_graph_result_id: `m47-reference-graph-lab-${"f".repeat(64)}`, base_m4_result_id: `m4-threat-replay-${"1".repeat(64)}`, frame_count: 4489, width: 800, height: 600, center_crop_xyxy: [100, 0, 700, 600], outside_crop_state: "undefined", sequence_binding: "sequence-0-to-masks/frame-000001.png", taxonomy: { schema_version: "missioncore.lab-v1-vegetation-taxonomy/v1", classes: Array.from({ length: 64 }, (_, classId) => ({ class_id: classId, label: classId === 0 ? "undefined" : `class-${classId}`, color_rgb: [classId, classId, classId], disposition: classId === 0 ? "undefined" : "prediction", })), }, aggregate_prediction_pixels: Array(64).fill(0), mask_archive: { path: "video/ddrnet-semantic-masks.zip", sha256: "9".repeat(64), byte_length: 1024, }, }; } function coarseRouteVideo() { return { ...routeVideo(), view_kind: "coarse-material-policy-review", linked_tgs_result_id: `m49-tgs-full-shadow-${"2".repeat(64)}`, taxonomy: { schema_version: "missioncore.lab-v1-terrain-policy-taxonomy/v1", classes: Array.from({ length: 10 }, (_, classId) => ({ class_id: classId, label: `policy-${classId}`, color_rgb: [classId, classId, classId], disposition: classId === 0 ? "ambiguous" : classId === 9 ? "undefined" : "prediction", material_class: classId === 0 || classId === 9 ? null : "grass", evidence_state: classId === 0 || classId === 9 ? "UNOBSERVED" : "SUPPORTED_GROUND", })), }, aggregate_prediction_pixels: Array(10).fill(0), mask_archive: { path: "video/coarse-material-policy-masks.zip", sha256: "8".repeat(64), byte_length: 2048, }, valid_fov: { mask_path: "video/valid-fov-mask.png", mask_sha256: "7".repeat(64), outside_valid_fov_class_id: 9, }, policy: { presets: { urban: { grass: "NO_GO" }, rural: { grass: "HIGH_COST" }, offroad: { grass: "HIGH_COST" }, }, }, fusion: { mode: "synchronised-multilayer-review", pixel_raster_fusion: false, camera_semantic_temporal_filter: "none", }, }; } function fullRouteReview() { const layer = (kind) => ({ name: kind === "city" ? "EoMT Cityscapes" : "ddrnet_39", result_id: kind === "city" ? `result-${"2".repeat(64)}` : `lab-v1-ravnoves-video-ddrnet-${"3".repeat(64)}`, frame_count: 6830, taxonomy: { schema_version: kind === "city" ? "missioncore.recorded-eomt-taxonomy/v1" : "missioncore.lab-v1-vegetation-taxonomy/v1", classes: Array.from({ length: kind === "city" ? 16 : 64 }, (_, classId) => ({ class_id: classId, label: classId === 0 ? "undefined" : `${kind}-${classId}`, color_rgb: [classId, classId, classId], disposition: classId === 0 ? "undefined" : "prediction", })), }, mask_archive: { path: kind === "city" ? "video/eomt-semantic-masks.zip" : "video/ddrnet-semantic-masks.zip", sha256: "4".repeat(64), byte_length: 4096, }, inference_fps: 9.5, latency_p95_ms: 101.2, peak_reserved_vram_bytes: 3_000_000_000, }); return { source_id: "RAVNOVES004TREE", session_id: "20260828T130511Z_viewer_live", linked_route_review_result_id: `lab-v1-vegetation-shadow-${"9".repeat(64)}`, source_job_id: "recorded-camera-eb2783c5480d56bda07c8af0", source_job_input_sha256: "eb2783c5480d56bda07c8af008dff5344d19dc550ef70fe2075d6f098f7cc715", source_stream_sha256: "e5eb017e2cc0f546736eda5235ca157b501913093cb64af5e548e335417e1bac", recorded_media_source_id: "recorded.camera.6a3945242828a038", recorded_media_generation_sha256: "b073ea1e7babf1c77a664e1a5b95e3702d0e05b0e34c1e85a7c67a6f8b392ded", frame_count: 6830, width: 800, height: 600, timeline_start_seconds: 39.215263458, timeline_end_seconds: 757.260263458, timeline: { path: "video/frame-source-times-ns.bin", sha256: "5".repeat(64), byte_length: 6830 * 8, encoding: "uint64-le-nanoseconds", frame_count: 6830, }, ground_truth: false, decode_repair: { repaired_frame_count: 1, sequence: 6092, method: "duplicate-previous-decoded-frame", proofs: { eomt: { path: "proofs/decode_repair.json", sha256: "7".repeat(64) }, ddrnet: { path: "proofs/ddrnet_decode_repair.json", sha256: "8".repeat(64) }, }, }, layers: { city: layer("city"), vegetation: layer("vegetation") }, }; } function labPayload(route = routeVideo()) { return { schema_version: "missioncore.lab-v1-vegetation-shadow/v1", result_id: resultId, created_at_utc: "2026-08-27T20:00:00Z", status: "visual-shadow-ready-policy-not-authorized", ground_truth: false, identity: { selected_candidate: "ddrnet" }, metrics: { candidates: { ddrnet: candidate("ddrnet", 0.64), ppliteseg: candidate("ppliteseg", 0.61), }, }, decision: { selected_candidate: "ddrnet", visual_shadow_ready: true, mission_policy_ready_for_configuration: true, navigation_accepted: false, production_accepted: false, }, limitations: ["shadow only"], authority: { commands_enabled: false, navigation_or_safety_accepted: false, actuation_accepted: false, camera_semantics_can_clear_rigid_geometry: false, }, catalogs: { goose: Array.from({ length: 12 }, (_, index) => visualCase("goose", index)), ravnoves: [], }, route_video: route, access: "read-only", }; } test("vegetation LAB keeps autonomous assets and fail-closed authority", async () => { let requestedUrl = ""; const result = await fetchVegetationShadowResult(resultId, { fetcher: async (url) => { requestedUrl = String(url); return new Response(JSON.stringify(labPayload()), { status: 200, headers: { "Content-Type": "application/json" }, }); }, }); assert.equal( requestedUrl, `/api/v1/laboratory/vegetation-shadow/${resultId}`, ); assert.equal(result.selectedCandidate, "ddrnet"); assert.equal(result.candidates[0].vegetationMeanIouPercent, 64); assert.equal(result.routeCases.length, 0); assert.equal(result.validationCases.length, 12); assert.equal(result.routeVideo.frameCount, 4489); assert.equal(result.routeVideo.viewKind, "fine-semantic-prediction"); assert.equal(result.routeVideo.linkedTgsResultId, null); assert.equal(result.routeVideo.taxonomy[0].disposition, "undefined"); assert.equal(result.validationCases[0].focus.className, "high_grass"); assert.match(result.validationCases[0].assets.ddrnet_error, /\/assets\/visual\/goose\//); assert.deepEqual(result.authority, { commandsEnabled: false, navigationOrSafetyAccepted: false, actuationAccepted: false, cameraSemanticsCanClearRigidGeometry: false, }); }); test("vegetation LAB parses coarse material policy and sealed TGS binding", async () => { const result = await fetchVegetationShadowResult(resultId, { fetcher: async () => new Response(JSON.stringify(labPayload(coarseRouteVideo())), { status: 200, headers: { "Content-Type": "application/json" }, }), }); assert.equal(result.routeVideo.viewKind, "coarse-material-policy-review"); assert.match(result.routeVideo.linkedTgsResultId, /^m49-tgs-full-shadow-/); assert.equal(result.routeVideo.taxonomy.length, 10); assert.equal(result.routeVideo.taxonomy[0].evidenceState, "UNOBSERVED"); assert.equal(result.routeVideo.policyPresets.urban.grass, "NO_GO"); assert.equal(result.routeVideo.fusionMode, "synchronised-multilayer-review"); }); test("vegetation LAB parses the full 004 pass inside the existing result contract", async () => { const payload = { ...labPayload(null), catalogs: { goose: [], ravnoves: [] }, route_full_review: fullRouteReview(), }; const timeline = new ArrayBuffer(6830 * 8); const timelineView = new DataView(timeline); for (let index = 0; index < 6830; index += 1) { timelineView.setBigUint64( index * 8, BigInt(39_215_263_458 + index * 100_000_000), true, ); } const result = await fetchVegetationShadowResult(resultId, { fetcher: async (url) => String(url).endsWith("/route-timeline") ? new Response(timeline, { status: 200, headers: { "Content-Type": "application/octet-stream", ETag: `"${"5".repeat(64)}"`, }, }) : new Response(JSON.stringify(payload), { status: 200, headers: { "Content-Type": "application/json" }, }), }); assert.equal(result.routeVideo, null); assert.equal(result.routeFullReview.frameCount, 6830); assert.equal(result.routeFullReview.city.taxonomy.length, 16); assert.equal(result.routeFullReview.vegetation.taxonomy.length, 64); assert.equal(result.routeFullReview.decodeRepair.sequence, 6092); assert.equal(result.routeFullReview.frameSourceTimesNs.length, 6830); assert.equal( vegetationFullRouteMaskUrl(resultId, "vegetation", 6829), `/api/v1/laboratory/vegetation-shadow/${resultId}/route-masks/vegetation/6829`, ); }); test("vegetation GOOSE benchmark opens through its separate archival endpoint", async () => { let requestedUrl = ""; const result = await fetchVegetationBenchmarkResult(benchmarkResultId, { fetcher: async (url) => { requestedUrl = String(url); return new Response(JSON.stringify({ ...labPayload(null), result_id: benchmarkResultId, }), { status: 200, headers: { "Content-Type": "application/json" }, }); }, }); assert.equal( requestedUrl, `/api/v1/laboratory/vegetation-benchmark/${benchmarkResultId}`, ); assert.equal(result.routeVideo, null); assert.equal(result.validationCases.length, 12); }); test("vegetation route TGS anchor keeps exact sealed metric shapes", async () => { let requestedUrl = ""; const anchor = await fetchVegetationRouteTgsAnchor(resultId, 409, { fetcher: async (url) => { requestedUrl = String(url); return new Response(JSON.stringify({ schema_version: "missioncore.lab-v1-route-tgs-anchor/v1", source_sequence: 409, slot: 1, current_points_xyz_m: [[1, 2, 3], [4, 5, 6]], costmap: { cell_size_m: 0.45, centers_xy_m: Array.from({ length: 2244 }, (_, index) => [index, -index]), state_codes: Array.from({ length: 2244 }, (_, index) => index % 4), z_bounds_m: Array.from({ length: 2244 }, () => [null, null]), }, }), { status: 200, headers: { "Content-Type": "application/json" } }); }, }); assert.equal( requestedUrl, `/api/v1/laboratory/vegetation-shadow/${resultId}/route-tgs-anchor/409`, ); assert.equal(anchor.sourceSequence, 409); assert.equal(anchor.currentPointsXyzM.length, 2); assert.equal(anchor.costmap.centersXyM.length, 2244); assert.deepEqual(new Set(anchor.costmap.stateCodes), new Set([0, 1, 2, 3])); }); test("canonical recorded LAB spatial frame keeps source, SLAM and body identity on one clock", async () => { const generation = "e".repeat(64); let requestedUrl = ""; const frame = await fetchCanonicalRecordedLabSpatialFrame("session-004", generation, 82_770_000_000, { fetcher: async (url) => { requestedUrl = String(url); return new Response(JSON.stringify({ schema_version: "missioncore.canonical-recorded-lab-spatial-frame/v3", target_time_ns: 82_770_000_000, source_time_ns: 82_769_535_708, pose_time_ns: 82_769_535_708, trajectory_time_ns: 82_700_000_000, coordinate_frame: "body-ground", sensor_height: { meters: 0.32, source: "local-source-cloud-ground-quantile-median", sample_count: 20, mad_m: 0.03, authority: "visual-derived", }, spatial_profile: { profile_id: "source-paced-ground-v3", local_slam_history_seconds: 5, local_slam_radius_m: 30, local_slam_voxel_size_m: 0.12, local_slam_point_limit: 27000, }, source_point_count: 2, source_points_body_xyz_m: [[1, 2, 3], [4, 5, 6]], local_slam_source_frame_count: 2, local_slam_source_point_count: 4, local_slam_point_count: 2, local_slam_body_xyz_m: [[0, 0, 0], [1, 0, 0]], body_frame: { origin_map_xyz_m: [33, 4, 1], sensor_origin_map_xyz_m: [33, 4, 1.32], basis_map_from_body: [[1, 0, 0], [0, 1, 0], [0, 0, 1]], }, }), { status: 200, headers: { "Content-Type": "application/json" } }); }, }); assert.equal( requestedUrl, `/api/v1/observation-sessions/session-004/canonical-lab/spatial-frame?generation=${generation}&time_ns=82770000000&profile=source-paced-ground-v3`, ); assert.equal(frame.sourcePointCount, 2); assert.equal(frame.localSlamBodyXyzM.length, 2); assert.equal(frame.sensorHeight.meters, 0.32); assert.deepEqual(frame.bodyFrame.originMapXyzM, [33, 4, 1]); }); test("vegetation realtime LAB uses one upstream Rerun clock and keeps archival review separate", async () => { const [resultSource, benchmarkSource, m49Source, canonicalSource, rerunSource, replayStyles] = await Promise.all([ readFile( new URL("../src/workspaces/laboratory/VegetationShadowResult.tsx", import.meta.url), "utf8", ), readFile( new URL("../src/workspaces/laboratory/VegetationBenchmarkResult.tsx", import.meta.url), "utf8", ), readFile( new URL("../src/workspaces/laboratory/M49TgsFullShadowEvidence.tsx", import.meta.url), "utf8", ), readFile( new URL("../src/components/laboratory/CanonicalRecordedLabReplay.tsx", import.meta.url), "utf8", ), readFile( new URL("../src/components/laboratory/CanonicalVegetationRerunReplay.tsx", import.meta.url), "utf8", ), readFile( new URL("../src/styles/m4-replay-threat.css", import.meta.url), "utf8", ), ]); assert.doesNotMatch(resultSource, /M48MaskComparisonVisual/); assert.match(resultSource, /M49TgsFullShadowEvidence/); assert.match(resultSource, /semanticOverride/); assert.match(m49Source, /spatialSemantic=\{spatialSemantic\}/); assert.match(m49Source, /controlLabel: "SEMANTICS"/); assert.equal(resultSource.match(/