| 1 | import test from "node:test"; |
| 2 | import assert from "node:assert/strict"; |
| 3 | import { |
| 4 | networkState, |
| 5 | reportForSharing, |
| 6 | isGuestLocalURL, |
| 7 | canonicalExitPolicy, |
| 8 | isSafetyCheckMessage, |
| 9 | } from "./safety-state.mjs"; |
| 10 | |
| 11 | test("check failures and incomplete evidence link to the checklist without tagging unrelated errors", () => { |
| 12 | for (const message of [ |
| 13 | "关键检查尚未通过,外网保持关闭", |
| 14 | "关键检测证据不完整,外网保持关闭", |
| 15 | "Cage 外网未放行,请在工作台完成关键检查", |
| 16 | "代理节点尚未应用或配置不一致,请重新应用节点;外网保持关闭", |
| 17 | "浏览器配置检查未通过:语言不一致", |
| 18 | ]) assert.equal(isSafetyCheckMessage(message), true, message); |
| 19 | for (const message of [undefined, "", "关键检查已通过", "关键检查进行中", "项目文件夹连接失败", "Git 设置已写入,但检查尚未通过。", "资源限制未通过核验"]) |
| 20 | assert.equal(isSafetyCheckMessage(message), false, message); |
| 21 | }); |
| 22 | |
| 23 | test("an unchanged exit policy does not restart the VM because of empty optional fields", () => { |
| 24 | assert.deepEqual( |
| 25 | canonicalExitPolicy({ |
| 26 | mode: "dynamic", |
| 27 | scope: "country", |
| 28 | country_code: "US", |
| 29 | }), |
| 30 | canonicalExitPolicy({ |
| 31 | mode: "dynamic", |
| 32 | scope: "country", |
| 33 | country_code: "US", |
| 34 | ip: "", |
| 35 | region: "", |
| 36 | city: "", |
| 37 | }), |
| 38 | ); |
| 39 | }); |
| 40 | |
| 41 | test("only a live gate lease displays networking as allowed", () => { |
| 42 | const now = Date.now(); |
| 43 | const status = { |
| 44 | vm_running: true, |
| 45 | safety: { |
| 46 | gate: { mode: "open", until: new Date(now + 1000).toISOString() }, |
| 47 | report: { checks: [{ state: "passed" }] }, |
| 48 | }, |
| 49 | }; |
| 50 | assert.equal(networkState(status, now).mode, "open"); |
| 51 | assert.equal(networkState(status, now + 2000).mode, "closed"); |
| 52 | status.safety.gate.mode = "closed"; |
| 53 | assert.equal(networkState(status, now).mode, "closed"); |
| 54 | status.safety.gate = null; |
| 55 | assert.equal(networkState(status, now).mode, "unknown"); |
| 56 | status.vm_running = false; |
| 57 | assert.equal(networkState(status, now).mode, "stopped"); |
| 58 | }); |
| 59 | |
| 60 | test("shared reports omit identifiers and secret-derived configuration hashes", () => { |
| 61 | const report = { |
| 62 | policy: "v1", |
| 63 | created_at: "today", |
| 64 | checks: [], |
| 65 | run_id: "private-run", |
| 66 | check_id: "private-check", |
| 67 | configuration: "secret-derived-hash", |
| 68 | ip: "private-ip", |
| 69 | }; |
| 70 | const text = JSON.stringify(reportForSharing(report)); |
| 71 | assert.ok(!text.includes("private") && !text.includes("secret-derived")); |
| 72 | }); |
| 73 | |
| 74 | test("offline browser previews allow guest loopback only", () => { |
| 75 | assert.equal(isGuestLocalURL("http://localhost:3000"), true); |
| 76 | assert.equal(isGuestLocalURL("http://127.0.0.1:18080"), true); |
| 77 | assert.equal(isGuestLocalURL("http://[::1]:3000"), true); |
| 78 | assert.equal(isGuestLocalURL("about:blank"), true); |
| 79 | assert.equal(isGuestLocalURL("https://localhost.attacker.example"), false); |
| 80 | assert.equal(isGuestLocalURL("http://192.168.5.2:17777"), false); |
| 81 | assert.equal(isGuestLocalURL("https://example.com"), false); |
| 82 | }); |