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 fetchVegetationShadowResult; let vegetationFullRouteMaskUrl; before(async () => { server = await createServer({ appType: "custom", logLevel: "silent", server: { middlewareMode: true }, }); ({ fetchVegetationBenchmarkResult, fetchVegetationShadowResult, vegetationFullRouteMaskUrl, } = await server.ssrLoadModule( "/src/core/laboratory/vegetationShadow.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", 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, frame_source_times_ns: Array.from({ length: 6830 }, (_, index) => 39_215_263_458 + index * 100_000_000), 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 result = await fetchVegetationShadowResult(resultId, { fetcher: async () => 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( 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 realtime LAB and archival benchmark use separate admitted instruments", async () => { const [resultSource, benchmarkSource] = 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", ), ]); assert.doesNotMatch(resultSource, /M48MaskComparisonVisual/); assert.match(resultSource, /M4ReplayThreatVisual/); assert.match(resultSource, /M49TgsFullShadowEvidence/); assert.match(resultSource, /semanticOverride/); assert.match(resultSource, /EoMT CITY \/ DDRNet VEGETATION/); assert.equal(resultSource.match(/