146 lines · 7.8 KBRaw
| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { createSession } from "../auth.ts"; |
| 4 | import { SESSION_COOKIE } from "../contract/limits.ts"; |
| 5 | import { d1, migrated } from "./d1.ts"; |
| 6 | |
| 7 | const { createBackend } = await import("../index.ts"); |
| 8 | |
| 9 | async function setup(failStorage = false) { |
| 10 | const sqlite = migrated(); |
| 11 | sqlite.exec("INSERT INTO users (id, email, name, password_hash, handle, status, created_at) VALUES ('user-id', 'private@example.test', 'Private name', 'hash', 'pilot', 'active', 1)"); |
| 12 | sqlite.exec("INSERT INTO namespaces (id, slug, name, kind, created_at) VALUES ('namespace-id', 'pilot', 'Pilot', 'user', 1)"); |
| 13 | sqlite.exec("INSERT INTO memberships (namespace_id, user_id, role, created_at) VALUES ('namespace-id', 'user-id', 'owner', 1)"); |
| 14 | const db = d1(sqlite); |
| 15 | const token = await createSession(db, "user-id"); |
| 16 | const pending: Promise<unknown>[] = []; |
| 17 | let complete: (() => void) | undefined; |
| 18 | const storage = () => failStorage ? Promise.reject(new Error("Private storage detail")) : new Promise<void>((resolve) => { complete = resolve; }); |
| 19 | const env = { |
| 20 | DB: db, |
| 21 | ARTIFACTS: { create: storage, import: storage, get: async () => ({ fork: storage, [Symbol.dispose]() {} }) }, |
| 22 | POSTHOG_ENABLED: "true", |
| 23 | POSTHOG_PROJECT_TOKEN: "project-token", |
| 24 | POSTHOG_HOST: "https://us.i.posthog.com", |
| 25 | ADMIN_EMAILS: "", |
| 26 | } as unknown as Env; |
| 27 | const ctx = { waitUntil: (promise: Promise<unknown>) => pending.push(promise) } as unknown as ExecutionContext; |
| 28 | const refs = await createBackend(env, ctx).refs(new Request("https://refs.test/new", { headers: { cookie: `${SESSION_COOKIE}=${token}` } })); |
| 29 | await Promise.all(pending.splice(0)); |
| 30 | return { refs, sqlite, pending, finishStorage: () => { assert.ok(complete); complete(); } }; |
| 31 | } |
| 32 | |
| 33 | test("creation and import are captured only after storage and the domain event succeed", async (t) => { |
| 34 | const payloads: { event: string; properties: Record<string, unknown> }[] = []; |
| 35 | t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => { |
| 36 | payloads.push(JSON.parse(String(init.body))); |
| 37 | return new Response(null, { status: 204 }); |
| 38 | }); |
| 39 | for (const importFrom of [undefined, "https://github.com/private-owner/private-repo"]) { |
| 40 | const fixture = await setup(); |
| 41 | const before = payloads.length; |
| 42 | const operation = fixture.refs.createRepo({ name: "project", description: "Private description", visibility: "private", importFrom }); |
| 43 | await new Promise<void>((resolve) => setImmediate(resolve)); |
| 44 | assert.equal(payloads.length, before); |
| 45 | fixture.finishStorage(); |
| 46 | const result = await operation; |
| 47 | assert.equal(result.ok, true); |
| 48 | await Promise.all(fixture.pending); |
| 49 | const payload = payloads.at(-1)!; |
| 50 | assert.equal(payload.event, importFrom ? "repo.imported" : "repo.created"); |
| 51 | assert.equal(payload.properties.entry_path, importFrom ? "import" : "new_project"); |
| 52 | assert.equal(payload.properties.namespace_id, "namespace-id"); |
| 53 | const row = fixture.sqlite.prepare("SELECT id FROM repos WHERE name = 'project'").get()!; |
| 54 | assert.equal(payload.properties.repo_id, row.id); |
| 55 | assert.equal(fixture.sqlite.prepare("SELECT count(*) AS n FROM events WHERE kind = ?").get(payload.event)!.n, 1); |
| 56 | assert.doesNotMatch(JSON.stringify(payload), /Private|private-owner|private-repo|private@example/); |
| 57 | fixture.sqlite.close(); |
| 58 | } |
| 59 | }); |
| 60 | |
| 61 | test("storage failure captures failure, rolls back the repository, and emits no success", async (t) => { |
| 62 | const payloads: { event: string; properties: Record<string, unknown> }[] = []; |
| 63 | t.mock.method(console, "error", () => {}); |
| 64 | t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => { |
| 65 | payloads.push(JSON.parse(String(init.body))); |
| 66 | return new Response(null, { status: 204 }); |
| 67 | }); |
| 68 | const fixture = await setup(true); |
| 69 | const result = await fixture.refs.createRepo({ name: "project", description: "", visibility: "private", importFrom: "https://github.com/owner/repo" }); |
| 70 | assert.equal(result.ok, false); |
| 71 | await Promise.all(fixture.pending); |
| 72 | assert.equal(fixture.sqlite.prepare("SELECT count(*) AS n FROM repos").get()!.n, 0); |
| 73 | assert.equal(payloads.length, 1); |
| 74 | assert.equal(payloads[0].event, "repo.provision_failed"); |
| 75 | assert.equal(payloads[0].properties.failure_reason, "storage_unavailable"); |
| 76 | fixture.sqlite.close(); |
| 77 | }); |
| 78 | |
| 79 | test("validation failures do not become storage attempts or successful adoption", async (t) => { |
| 80 | const fetch = t.mock.method(globalThis, "fetch", async () => new Response(null, { status: 204 })); |
| 81 | const fixture = await setup(); |
| 82 | const result = await fixture.refs.createRepo({ name: "invalid/name", description: "", visibility: "private" }); |
| 83 | assert.equal(result.ok, false); |
| 84 | assert.equal(fetch.mock.callCount(), 0); |
| 85 | assert.equal(fixture.pending.length, 0); |
| 86 | fixture.sqlite.close(); |
| 87 | }); |
| 88 | |
| 89 | test("a name conflict emits a conflict outcome without a second success", async (t) => { |
| 90 | const payloads: { event: string; properties: Record<string, unknown> }[] = []; |
| 91 | t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => { |
| 92 | payloads.push(JSON.parse(String(init.body))); |
| 93 | return new Response(null, { status: 204 }); |
| 94 | }); |
| 95 | const fixture = await setup(); |
| 96 | const input = { name: "project", description: "", visibility: "private" } as const; |
| 97 | const first = fixture.refs.createRepo(input); |
| 98 | await new Promise<void>((resolve) => setImmediate(resolve)); |
| 99 | fixture.finishStorage(); |
| 100 | assert.equal((await first).ok, true); |
| 101 | assert.equal((await fixture.refs.createRepo(input)).ok, false); |
| 102 | await Promise.all(fixture.pending); |
| 103 | assert.deepEqual(payloads.map((payload) => payload.event), ["$groupidentify", "repo.created", "repo.provision_failed"]); |
| 104 | assert.deepEqual(payloads[0].properties.$group_set, { kind: "user" }); |
| 105 | assert.equal(payloads[0].properties.$group_key, "namespace-id"); |
| 106 | assert.deepEqual(payloads[2].properties.$groups, { workspace: "namespace-id" }); |
| 107 | assert.equal(payloads[2].properties.failure_reason, "conflict"); |
| 108 | assert.equal(fixture.sqlite.prepare("SELECT count(*) AS n FROM repos").get()!.n, 1); |
| 109 | fixture.sqlite.close(); |
| 110 | }); |
| 111 | |
| 112 | test("a failed domain event write does not emit a provisioning success", async (t) => { |
| 113 | const fetch = t.mock.method(globalThis, "fetch", async () => new Response(null, { status: 204 })); |
| 114 | const fixture = await setup(); |
| 115 | fixture.sqlite.exec("CREATE TRIGGER fail_events BEFORE INSERT ON events BEGIN SELECT RAISE(ABORT, 'event write failed'); END"); |
| 116 | const operation = fixture.refs.createRepo({ name: "project", description: "", visibility: "private" }); |
| 117 | const rejected = assert.rejects(operation, /event write failed/); |
| 118 | await new Promise<void>((resolve) => setImmediate(resolve)); |
| 119 | fixture.finishStorage(); |
| 120 | await rejected; |
| 121 | assert.equal(fetch.mock.callCount(), 0); |
| 122 | fixture.sqlite.close(); |
| 123 | }); |
| 124 | |
| 125 | test("forks use a separate entry path rather than counting as imports or new projects", async (t) => { |
| 126 | const payloads: { event: string; properties: Record<string, unknown> }[] = []; |
| 127 | t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => { |
| 128 | payloads.push(JSON.parse(String(init.body))); |
| 129 | return new Response(null, { status: 204 }); |
| 130 | }); |
| 131 | const fixture = await setup(); |
| 132 | const first = fixture.refs.createRepo({ name: "project", description: "", visibility: "private" }); |
| 133 | await new Promise<void>((resolve) => setImmediate(resolve)); |
| 134 | fixture.finishStorage(); |
| 135 | await first; |
| 136 | const repo = await fixture.refs.repo("pilot/project"); |
| 137 | const fork = repo.fork({ name: "forked-project", description: "", visibility: "private" }); |
| 138 | await new Promise<void>((resolve) => setImmediate(resolve)); |
| 139 | fixture.finishStorage(); |
| 140 | assert.equal((await fork).ok, true); |
| 141 | await Promise.all(fixture.pending); |
| 142 | assert.equal(payloads.at(-1)?.event, "repo.forked"); |
| 143 | assert.equal(payloads.at(-1)?.properties.entry_path, "fork"); |
| 144 | assert.equal(payloads.at(-1)?.properties.import_provider, undefined); |
| 145 | fixture.sqlite.close(); |
| 146 | }); |