|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { createTestEnv } from "../helpers/d1"; |
| 3 | +import { buildAnchorLogLine, submitToGitAnchor, type GitHubContentsRequester } from "../../src/review/ledger-anchor-git"; |
| 4 | +import { buildLedgerAnchorPayload, canonicalJson, type SignedLedgerAnchor } from "../../src/review/ledger-anchor"; |
| 5 | +import { loadPublicLedgerAnchors } from "../../src/review/ledger-anchor-persistence"; |
| 6 | + |
| 7 | +// #9273 (epic #9267). octokit is ALWAYS injected -- never a real GitHub write. The property under test is |
| 8 | +// this module's own append/error/persistence logic, exercised against a scripted GitHubContentsRequester. |
| 9 | + |
| 10 | +const TARGET = { owner: "acme", repo: "loopover-anchors", branch: "main", path: "anchors.jsonl" }; |
| 11 | + |
| 12 | +function makeSignedAnchor(seq = 1): SignedLedgerAnchor { |
| 13 | + return { payload: buildLedgerAnchorPayload({ seq, rowHash: "a".repeat(64), totalCount: seq }, "2026-07-27T12:00:00.000Z"), signature: "c2ln", keyId: "key1" }; |
| 14 | +} |
| 15 | + |
| 16 | +function decodeBase64(base64: string): string { |
| 17 | + return Buffer.from(base64, "base64").toString("utf8"); |
| 18 | +} |
| 19 | + |
| 20 | +describe("buildAnchorLogLine (#9273)", () => { |
| 21 | + it("commits the SAME canonicalized payload and signature as the Rekor backend anchors -- the two never diverge", () => { |
| 22 | + const signed = makeSignedAnchor(); |
| 23 | + const line = buildAnchorLogLine(signed); |
| 24 | + expect(line.endsWith("\n")).toBe(true); |
| 25 | + expect(JSON.parse(line)).toEqual({ payload: JSON.parse(canonicalJson(signed.payload)), signature: signed.signature, keyId: signed.keyId }); |
| 26 | + }); |
| 27 | +}); |
| 28 | + |
| 29 | +describe("submitToGitAnchor (#9273)", () => { |
| 30 | + it("creates the file on first anchor ever (no prior sha to compare-and-swap against)", async () => { |
| 31 | + const env = createTestEnv(); |
| 32 | + const signed = makeSignedAnchor(1); |
| 33 | + const request = vi.fn(async (route: string, params: Record<string, unknown>) => { |
| 34 | + if (route.startsWith("GET")) { |
| 35 | + const error = new Error("Not Found") as Error & { status: number }; |
| 36 | + error.status = 404; |
| 37 | + throw error; |
| 38 | + } |
| 39 | + expect(params["sha"]).toBeUndefined(); // no sha on a brand-new file |
| 40 | + expect(decodeBase64(params["content"] as string)).toBe(buildAnchorLogLine(signed)); |
| 41 | + return { data: { commit: { sha: "deadbeef1" } } }; |
| 42 | + }); |
| 43 | + |
| 44 | + await submitToGitAnchor(env, signed, { request } as GitHubContentsRequester, TARGET); |
| 45 | + |
| 46 | + const { anchors } = await loadPublicLedgerAnchors(env); |
| 47 | + expect(anchors[0]).toMatchObject({ |
| 48 | + seq: 1, |
| 49 | + backend: "git", |
| 50 | + status: "ok", |
| 51 | + backendRef: { owner: "acme", repo: "loopover-anchors", branch: "main", path: "anchors.jsonl", sha: "deadbeef1" }, |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + it("APPENDS to existing content rather than overwriting it, using the existing sha as a compare-and-swap guard", async () => { |
| 56 | + const env = createTestEnv(); |
| 57 | + const signed = makeSignedAnchor(2); |
| 58 | + const priorLine = '{"prior":"entry"}\n'; |
| 59 | + const request = vi.fn(async (route: string, params: Record<string, unknown>) => { |
| 60 | + if (route.startsWith("GET")) return { data: { content: Buffer.from(priorLine).toString("base64"), sha: "old-sha" } }; |
| 61 | + expect(params["sha"]).toBe("old-sha"); |
| 62 | + expect(decodeBase64(params["content"] as string)).toBe(priorLine + buildAnchorLogLine(signed)); |
| 63 | + return { data: { commit: { sha: "newsha2" } } }; |
| 64 | + }); |
| 65 | + |
| 66 | + await submitToGitAnchor(env, signed, { request } as GitHubContentsRequester, TARGET); |
| 67 | + |
| 68 | + const { anchors } = await loadPublicLedgerAnchors(env); |
| 69 | + expect(anchors[0]?.backendRef).toMatchObject({ sha: "newsha2" }); |
| 70 | + }); |
| 71 | + |
| 72 | + it("treats a GET response with a sha but no inline content (GitHub omits it for files >1MB) as empty existing content, not a crash", async () => { |
| 73 | + const env = createTestEnv(); |
| 74 | + const signed = makeSignedAnchor(9); |
| 75 | + const request = vi.fn(async (route: string, params: Record<string, unknown>) => { |
| 76 | + if (route.startsWith("GET")) return { data: { sha: "large-file-sha" } }; // no `content` field |
| 77 | + expect(params["sha"]).toBe("large-file-sha"); // still used for compare-and-swap |
| 78 | + expect(decodeBase64(params["content"] as string)).toBe(buildAnchorLogLine(signed)); // not prefixed with garbage |
| 79 | + return { data: { commit: { sha: "afterlarge" } } }; |
| 80 | + }); |
| 81 | + |
| 82 | + await submitToGitAnchor(env, signed, { request } as GitHubContentsRequester, TARGET); |
| 83 | + const { anchors } = await loadPublicLedgerAnchors(env); |
| 84 | + expect(anchors[0]).toMatchObject({ status: "ok", backendRef: { sha: "afterlarge" } }); |
| 85 | + }); |
| 86 | + |
| 87 | + it("records status:'failed' (with the real error, not a thrown one) on a rate-limit / auth error", async () => { |
| 88 | + const env = createTestEnv(); |
| 89 | + const signed = makeSignedAnchor(3); |
| 90 | + const request = vi.fn(async (route: string) => { |
| 91 | + if (route.startsWith("GET")) { |
| 92 | + const error = new Error("Not Found") as Error & { status: number }; |
| 93 | + error.status = 404; |
| 94 | + throw error; |
| 95 | + } |
| 96 | + const error = new Error("API rate limit exceeded") as Error & { status: number }; |
| 97 | + error.status = 403; |
| 98 | + throw error; |
| 99 | + }); |
| 100 | + |
| 101 | + await expect(submitToGitAnchor(env, signed, { request } as GitHubContentsRequester, TARGET)).resolves.toBeUndefined(); |
| 102 | + |
| 103 | + const { anchors } = await loadPublicLedgerAnchors(env); |
| 104 | + expect(anchors[0]).toMatchObject({ backend: "git", status: "failed", error: "API rate limit exceeded" }); |
| 105 | + }); |
| 106 | + |
| 107 | + it("records status:'failed' when a non-404 GET error occurs (never conflated with 'file does not exist yet')", async () => { |
| 108 | + const env = createTestEnv(); |
| 109 | + const signed = makeSignedAnchor(4); |
| 110 | + const request = vi.fn(async () => { |
| 111 | + const error = new Error("Server Error") as Error & { status: number }; |
| 112 | + error.status = 500; |
| 113 | + throw error; |
| 114 | + }); |
| 115 | + |
| 116 | + await submitToGitAnchor(env, signed, { request } as GitHubContentsRequester, TARGET); |
| 117 | + const { anchors } = await loadPublicLedgerAnchors(env); |
| 118 | + expect(anchors[0]).toMatchObject({ status: "failed", error: "Server Error" }); |
| 119 | + }); |
| 120 | + |
| 121 | + it("records status:'failed' if the PUT response is missing a commit sha", async () => { |
| 122 | + const env = createTestEnv(); |
| 123 | + const signed = makeSignedAnchor(5); |
| 124 | + const request = vi.fn(async (route: string) => { |
| 125 | + if (route.startsWith("GET")) { |
| 126 | + const error = new Error("Not Found") as Error & { status: number }; |
| 127 | + error.status = 404; |
| 128 | + throw error; |
| 129 | + } |
| 130 | + return { data: {} }; // no commit.sha |
| 131 | + }); |
| 132 | + |
| 133 | + await submitToGitAnchor(env, signed, { request } as GitHubContentsRequester, TARGET); |
| 134 | + const { anchors } = await loadPublicLedgerAnchors(env); |
| 135 | + expect(anchors[0]).toMatchObject({ status: "failed", error: "GitHub Contents API response did not include a commit sha" }); |
| 136 | + }); |
| 137 | + |
| 138 | + it("never throws past the caller, whatever the failure", async () => { |
| 139 | + const env = createTestEnv(); |
| 140 | + const signed = makeSignedAnchor(6); |
| 141 | + const request = vi.fn(async () => { |
| 142 | + throw new Error("anything"); |
| 143 | + }); |
| 144 | + await expect(submitToGitAnchor(env, signed, { request } as GitHubContentsRequester, TARGET)).resolves.toBeUndefined(); |
| 145 | + }); |
| 146 | +}); |
0 commit comments