chirp-sync-static.test.ts

4.5 KB · raw

1// IV.7 — Static safety assertions for Chirp Sync.2// These tests read source files to enforce structural invariants that3// are cheaper to verify statically than to exercise at runtime.45import { describe, expect, it } from "vitest";6import { readFileSync } from "node:fs";7import { resolve } from "node:path";89const here = __dirname;10const src = (rel: string) => readFileSync(resolve(here, rel), "utf8");1112describe("Chirp Sync — static safety assertions", () => {13  it("no WebSocket server in any browser-extension sync source file", () => {14    const files = [15      "./sync-trigger.ts",16      "./chirp-sync-runner.ts",17      "./chirp-sync-coordinator.ts",18      "./chirp-sync-vault-adapter.ts",19      "../shared/chirp-sync-transport.ts",20    ];21    const forbidden = /WebSocketServer|wss\.listen|createServer|http\.listen|net\.listen/;22    for (const f of files) {23      expect(src(f), `${f} must not start a WebSocket server`).not.toMatch(forbidden);24    }25  });2627  it("applyMergedRemoteEntries does not call fireSyncTrigger or TRIGGER_SYNC", () => {28    const vaultSrc = src("./vault.ts");29    const fnStart = vaultSrc.indexOf("applyMergedRemoteEntries");30    expect(fnStart, "applyMergedRemoteEntries must exist in vault.ts").toBeGreaterThan(-1);31    // Grab a generous slice starting from the function — large enough to cover32    // the implementation but not so large that it bleeds into unrelated code.33    const fnBody = vaultSrc.slice(fnStart, fnStart + 4000);34    expect(fnBody).not.toMatch(/triggerSyncIfReachable|TRIGGER_SYNC|fireSyncTrigger/);35  });3637  it("addEntryDirect / updateEntryDirect / deleteEntryDirect do not call fireSyncTrigger", () => {38    const vaultSrc = src("./vault.ts");39    for (const fn of ["addEntryDirect", "updateEntryDirect", "deleteEntryDirect"]) {40      const start = vaultSrc.indexOf(`function ${fn}`);41      if (start === -1) continue;42      // Slice up to the start of the very next export declaration so we don't43      // bleed into adjacent function bodies or their JSDoc comments.44      const nextExport = vaultSrc.indexOf("\nexport ", start + fn.length);45      const end = nextExport > start ? nextExport : start + 600;46      const body = vaultSrc.slice(start, end);47      expect(body, `${fn} must not call fireSyncTrigger`).not.toMatch(48        /triggerSyncIfReachable|TRIGGER_SYNC|fireSyncTrigger/,49      );50    }51  });5253  it("device registry is stored in chrome.storage.local, not chrome.storage.sync", () => {54    const registrySrc = src("./paired-devices.ts");55    expect(registrySrc).toContain("storage.local");56    expect(registrySrc).not.toMatch(57      /storage\.sync[^;]*peach_paired_devices|peach_paired_devices[^;]*storage\.sync/,58    );59  });6061  it("setSyncRunner and productionSyncRunner are called in entry.ts", () => {62    const entrySrc = src("./entry.ts");63    expect(entrySrc).toContain("setSyncRunner");64    expect(entrySrc).toContain("productionSyncRunner");65  });6667  it("sync-trigger filters to trustSource:'direct' — vouched devices excluded", () => {68    const triggerSrc = src("./sync-trigger.ts");69    expect(triggerSrc).toContain('"direct"');70    expect(triggerSrc).toMatch(/trustSource.*direct|direct.*trustSource/);71  });7273  it("App.tsx wires startMDNSListener with onCleanup registered before the import resolves", () => {74    const appSrc = src("../popup/App.tsx");75    expect(appSrc).toContain("startMDNSListener");76    expect(appSrc).toContain("onCleanup");77    // onCleanup must appear BEFORE the .then() call that starts the listener,78    // i.e. stopMDNS must be captured into the closure before dynamic import.79    const cleanupIdx = appSrc.indexOf("onCleanup(() => stopMDNS");80    const thenIdx = appSrc.indexOf("startMDNSListener");81    expect(cleanupIdx).toBeGreaterThan(-1);82    expect(thenIdx).toBeGreaterThan(-1);83    expect(cleanupIdx).toBeLessThan(thenIdx);84  });8586  it("vault_id uses the full 64-char BLAKE3 hex digest", () => {87    const identitySrc = src("../shared/sync-identity.ts");88    expect(identitySrc).toContain("VAULT_ID_HEX_LENGTH = 64");89    expect(identitySrc).not.toContain("VAULT_ID_HEX_LENGTH = 16");90  });9192  it("per-entry key derivation uses entryId as part of HKDF info", () => {93    const adapterSrc = src("./chirp-sync-vault-adapter.ts");94    expect(adapterSrc).toContain("ENTRY_KEY_INFO_SUFFIX");95    expect(adapterSrc).toContain("sync-entry-v1");96    // The info must be derived from entryId — look for the concat pattern.97    expect(adapterSrc).toMatch(/entryId.*ENTRY_KEY_INFO_SUFFIX|ENTRY_KEY_INFO_SUFFIX.*entryId/);98  });99});