import assert from "node:assert/strict"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import { isJsonRecordName, normalizeBranch, normalizeCodeChannelRepository, normalizeReceipt, parseArgs, safeCacheRelative, selfTest, sha256, waitForPublicSnapshotAcceptance, } from "./finalize-development.mjs"; test("the finalizer accepts only the registered Fifth Domain code channel", () => { assert.equal( normalizeCodeChannelRepository( "https://guanghulab.com/code/bingshuo/guanghu-ice-heart.git", ).identity, "repo://guanghulab.com/code/bingshuo/guanghu-ice-heart", ); for (const invalid of [ "https://guanghulab.com/fifth-domain/bingshuo/fifth-domain.git", "https://example.com/code/bingshuo/guanghu-ice-heart.git", "https://user:secret@guanghulab.com/code/bingshuo/guanghu-ice-heart.git", ]) { assert.throws(() => normalizeCodeChannelRepository(invalid)); } }); test("receipt and branch validation fail closed", () => { assert.equal(normalizeBranch("main"), "main"); assert.equal( normalizeReceipt("deployment/receipts/EXACT.json"), "deployment/receipts/EXACT.json", ); assert.throws(() => normalizeReceipt("../EXACT.json")); assert.throws(() => normalizeReceipt("docs/EXACT.json")); assert.throws(() => normalizeBranch("../main")); }); test("repeated receipts and cleanup paths are retained", () => { const parsed = parseArgs([ "--development-id", "DEV-20260805-003", "--receipt", "deployment/receipts/A.json", "--receipt", "deployment/receipts/B.json", "--cleanup-path", "target", ]); assert.deepEqual(parsed.receipts, [ "deployment/receipts/A.json", "deployment/receipts/B.json", ]); assert.deepEqual(parsed.cleanupPaths, ["target"]); }); test("cache deletion scope accepts only exact allowlisted descendants", () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), "finalizer-cache-test-")); try { assert.equal(safeCacheRelative(root, "target").relative, "target"); assert.equal( safeCacheRelative(root, "src/__pycache__").relative, "src/__pycache__", ); assert.throws(() => safeCacheRelative(root, ".")); assert.throws(() => safeCacheRelative(root, "../outside")); assert.throws(() => safeCacheRelative(root, "outputs")); } finally { fs.rmSync(root, { recursive: true, force: true }); } }); test("embedded finalizer self-test passes", () => { assert.deepEqual(selfTest(), { result: "PASS_100" }); }); test("macOS AppleDouble sidecars are never parsed as JSON records", () => { assert.equal(isJsonRecordName("lease.json"), true); assert.equal(isJsonRecordName("._lease.json"), false); assert.equal(isJsonRecordName("lease.txt"), false); }); test("receipt hashes preserve exact trailing bytes", () => { assert.notEqual(sha256(Buffer.from("{}")), sha256(Buffer.from("{}\n"))); }); test("public snapshot acceptance returns the accepted exact commit", async () => { let calls = 0; const result = await waitForPublicSnapshotAcceptance({ expectedCommit: "abc123", expectedAnchorVersion: "test.3", attempts: 2, delayMs: 0, probe: async () => { calls += 1; return calls === 1 ? { status: 503, body: { navigation_source: { source_degraded: true } } } : { status: 200, body: { navigation_source: { source_degraded: false, source_commit: "abc123", anchor_version: "test.3" } } }; }, }); assert.equal(result.result, "PASS_100"); assert.equal(result.attempt, 2); }); test("public snapshot acceptance fails closed with actionable source feedback", async () => { await assert.rejects( waitForPublicSnapshotAcceptance({ expectedCommit: "new", expectedAnchorVersion: "test.3", attempts: 1, delayMs: 0, probe: async () => ({ status: 503, body: { navigation_source: { source_degraded: true, source_commit: "old", anchor_version: "test.2", source_error_code: "snapshot_map_version_mismatch:lighthouse_paths" } }, }), }), /PUBLIC_SNAPSHOT_NOT_ACCEPTED.*snapshot_map_version_mismatch:lighthouse_paths/, ); });