Lines 1–10 at 162 (were 1–8 at 161)
11import assert from "node:assert/strict"; 22import { registerHooks } from "node:module"; 33import { test } from "node:test"; 4+import git from "isomorphic-git"; 45import { Project } from "../../worker/project/project.ts"; 56import { EngineProject } from "../src/engine-project.ts"; 7+import { MemoryFS } from "../src/memory-fs.ts"; 68import { memoryStorage } from "./helpers.ts"; 810const hooks = registerHooks({ Lines 47–54 at 162 (were 45–50 at 161)
4547 const values = new Map<string, unknown>(); 4648 let alarm: number | null = null; 4749 let onReaders: (() => Promise<void>) | undefined; 50+ let failGitAcknowledgement = false; 51+ let failNamePersistence = false; 4953 id: { toString: () => "project-1" }, Lines 62–75 at 162 (were 58–63 at 161)
5862 return values.get(key); 6064 put: async (key: string | Record<string, unknown>, value?: unknown) => { 65+ if (key === "name" && failNamePersistence) { 66+ failNamePersistence = false; 67+ throw Error("injected name persistence failure"); 69+ if (failGitAcknowledgement && typeof key === "object" && "git:head" in key) { 70+ failGitAcknowledgement = false; 71+ throw Error("injected Git cursor persistence failure"); 6173 if (typeof key === "string") values.set(key, value); 6274 else for (const [name, v] of Object.entries(key)) values.set(name, v); Lines 88–96 at 162 (were 76–84 at 161)
7688 t.mock.method(Project, "load", () => { 7789 throw Error("full history replay forbidden"); 79− const call = async (op: string, agent: string | undefined, body: unknown) => {
91+ const call = async (op: string, agent: string | undefined, body: unknown, requestId?: string) => { 8092 for (let page = 0; page < 100; page++) { 81− const result = await object().call("project", op, agent, body);
93+ const result = await object().call("project", op, agent, body, false, undefined, undefined, requestId); 8294 if (result.status !== 503) { 8395 assert.notEqual(result.status, 500, JSON.stringify({ op, result })); Lines 100–213 at 162 (were 88–99 at 161)
89101 throw Error("adoption did not finish"); 91− assert.equal((await call("init", undefined, { files: { "a.txt": "one\ntwo\n" } })).status, 200);
103+ const initId = "00000000-0000-4000-8000-000000000008"; 104+ const initBody = { files: { "a.txt": "one\ntwo\n" } }; 105+ failNamePersistence = true; 106+ await assert.rejects( 107+ () => object().call("project", "init", undefined, initBody, false, undefined, undefined, initId), 108+ /name persistence failure/, 110+ const initializedLog = storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all(); 111+ assert.equal((await call("init", undefined, initBody, initId)).status, 200); 112+ assert.deepEqual(storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all(), initializedLog); 92113 assert.equal((await call("sync", "alice", {})).status, 200); 93− assert.equal((await call("record", "alice", { files: { "a.txt": "changed\n" } })).status, 200);
114+ const requestId = "00000000-0000-4000-8000-000000000007"; 115+ const recordBody = { files: { "a.txt": "changed\n" } }; 116+ failNamePersistence = true; 117+ await assert.rejects( 118+ () => object().call("project", "record", "alice", recordBody, false, undefined, undefined, requestId), 119+ /name persistence failure/, 121+ const recordedLog = storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all(); 123+ (await object().call("project", "record", "alice", recordBody, false, undefined, undefined, requestId)).status, 126+ assert.deepEqual(storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all(), recordedLog); 129+ await object().call( 133+ { files: { "a.txt": "different\n" } }, 94142 const published = await call("publish", "alice", { message: "Disk first" }); 95143 assert.equal(published.status, 200); 96144 assert.equal((published.body as { ok: boolean }).ok, true); 145+ const issued = await object().issueAgentKey("alice", "test-address", null, "writer"); 146+ assert.ok("key" in issued); 147+ const keyCall = { presented: issued.key, address: "test-address", write: true, route: "record" }; 148+ const crossPrincipal = await object().call( 158+ assert.equal(crossPrincipal.status, 200, "each checked principal owns its own request ID namespace"); 159+ const principalReceipts = storage.db 160+ .prepare("SELECT DISTINCT key FROM writer_state WHERE collection = 'runtime:receipts' AND key LIKE ?") 161+ .all(`http:%:${requestId}`); 162+ assert.equal(principalReceipts.length, 2); 163+ const beforeSharedReplay = storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all(); 164+ assert.equal((await call("record", "alice", recordBody, requestId)).status, 200); 165+ assert.deepEqual(storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all(), beforeSharedReplay); 166+ const keyRequestId = "00000000-0000-4000-8000-000000000010"; 168+ (await object().call("project", "record", "alice", recordBody, false, undefined, keyCall, keyRequestId)).status, 171+ await object().revokeAgentKey(issued.id, "test-address"); 172+ const beforeRevokedRetry = storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all(); 173+ const revokedRetry = await object().call( 183+ assert.equal(revokedRetry.status, 403); 184+ assert.deepEqual(storage.db.prepare("SELECT * FROM log ORDER BY seq,part").all(), beforeRevokedRetry); 185+ const readerId = "00000000-0000-4000-8000-000000000009"; 186+ failNamePersistence = true; 187+ await assert.rejects( 188+ () => object().call("project", "reader", "alice", { reader: true }, false, undefined, undefined, readerId), 189+ /name persistence failure/, 191+ const nowRefused = await object().call( 202+ (nowRefused.body as { ok: boolean }).ok, 204+ "current reader authority overrides an earlier successful receipt", 207+ (await object().call("project", "reader", "alice", { reader: true }, false, undefined, undefined, readerId)).status, 210+ assert.equal((await call("reader", "alice", { reader: false })).status, 200); 97211 const file = await call("file", undefined, { path: "a.txt", version: "1" }); 98212 assert.equal(file.status, 200); 99213 assert.match(JSON.stringify(file.body), /changed/); Lines 279–449 at 162 (were 165–168 at 161)
165279 /injected after record/, 167281 assert.deepEqual(rows(), stable); 282+ // Exercise the real alarm and commit builder; only remote transport is mocked. 283+ t.mock.method(git, "listServerRefs", async () => []); 284+ t.mock.method(git, "clone", async () => { 285+ throw Error("empty test repository"); 288+ let remoteFiles: MemoryFS | undefined; 289+ t.mock.method(git, "push", async (options: Parameters<typeof git.push>[0]) => { 290+ const oid = await git.resolveRef({ fs: options.fs, dir: options.dir, ref: "main" }); 291+ const commit = await git.readCommit({ fs: options.fs, dir: options.dir, oid }); 292+ assert.match(commit.commit.message, /abso-version: 2/); 293+ const blob = await git.readBlob({ fs: options.fs, dir: options.dir, oid, filepath: "a.txt" }); 294+ assert.equal(new TextDecoder().decode(blob.blob), "Alice's change\n"); 296+ remoteFiles = options.fs as MemoryFS; 297+ return { ok: true }; 301+ info: async () => ({ remote: "https://artifacts.invalid/test" }), 302+ createToken: async () => ({ plaintext: "invented-test-token" }), 303+ [Symbol.dispose]() {}, 306+ const bound = () => new ProjectObject(context as never, { ABSO_DISK_FIRST: "on", ARTIFACTS: artifacts } as never); 307+ await bound().alarm(); 308+ assert.equal(pushed, 1); 309+ assert.equal((await bound().out()).pushed?.version, 2); 310+ assert.deepEqual(rows(), stable, "pushing shared does not mutate canonical project state"); 311+ await bound().alarm(); 312+ assert.equal(pushed, 1, "a cold alarm does not push the same version again"); 313+ assert.ok(remoteFiles); 314+ const remote = remoteFiles; 315+ const parent = await git.resolveRef({ fs: remote, dir: "/repo", ref: "main" }); 316+ const blob = await git.writeBlob({ fs: remote, dir: "/repo", blob: new TextEncoder().encode("From Git\n") }); 317+ const tree = await git.writeTree({ 320+ tree: [{ mode: "100644", path: "a.txt", oid: blob, type: "blob" }], 322+ const incoming = await git.commit({ 327+ message: "Plain Git edit", 328+ author: { name: "Git writer", email: "test@example.invalid" }, 330+ await git.writeRef({ fs: remote, dir: "/repo", ref: "main", value: incoming, force: true }); 331+ t.mock.method(git, "listServerRefs", async () => [{ ref: "refs/heads/main", oid: incoming }]); 332+ t.mock.method(git, "clone", async (options: Parameters<typeof git.clone>[0]) => { 333+ (options.fs as MemoryFS).entries = structuredClone(remote.entries); 335+ const beforeIntake = rows(); 336+ const originalIngest = EngineProject.prototype.ingest; 337+ const failedIngest = t.mock.method( 338+ EngineProject.prototype, 340+ function (this: EngineProject, ...args: Parameters<EngineProject["ingest"]>) { 341+ originalIngest.apply(this, args); 342+ throw Error("injected after Git intake"); 345+ await assert.rejects(() => bound().gitSync(), /injected after Git intake/); 346+ assert.deepEqual(rows(), beforeIntake); 347+ assert.equal((await bound().out()).git.head, parent, "failed intake must not acknowledge the remote head"); 348+ failedIngest.mock.restore(); 349+ failGitAcknowledgement = true; 350+ await assert.rejects(() => bound().gitSync(), /Git cursor persistence failure/); 351+ const committedIntake = rows(); 352+ assert.equal((await bound().out()).version, 3); 353+ assert.equal((await bound().out()).git.head, parent); 354+ const intake = await bound().gitSync(); 355+ assert.deepEqual(rows(), committedIntake, "retry after cursor failure returns the atomic intake receipt"); 357+ assert.equal(intake?.head, incoming); 358+ assert.equal(intake?.commits, 1); 359+ assert.equal((intake.answer as { landed: boolean }).landed, true, JSON.stringify(intake)); 360+ const imported = await call("file", undefined, { path: "a.txt", version: "3" }); 361+ assert.equal(imported.status, 200); 362+ assert.match(JSON.stringify(imported.body), /From Git/); 363+ const afterIntake = rows(); 364+ assert.equal(await bound().gitSync(), null); 365+ assert.deepEqual(rows(), afterIntake, "cold intake does not apply the same commit twice"); 368+for (const disk of [false, true]) { 369+ test(`Git refusal permits a later fixing commit with disk mode ${disk}`, async (t) => { 370+ const storage = memoryStorage(); 371+ t.after(() => storage.db.close()); 372+ const values = new Map<string, unknown>(); 374+ id: { toString: () => "refusal" }, 377+ get: async (key: string) => values.get(key), 378+ put: async (key: string | Record<string, unknown>, value?: unknown) => { 379+ if (typeof key === "string") values.set(key, value); 380+ else for (const [k, v] of Object.entries(key)) values.set(k, v); 382+ getAlarm: async () => null, 383+ setAlarm: async () => {}, 388+ info: async () => ({ remote: "https://artifacts.invalid/refusal" }), 389+ createToken: async () => ({ plaintext: "invented-test-token" }), 390+ [Symbol.dispose]() {}, 393+ const object = () => 394+ new ProjectObject(context as never, { ABSO_DISK_FIRST: disk ? "on" : "off", ARTIFACTS: artifacts } as never); 395+ const call = async (op: string, body: unknown) => { 396+ for (let page = 0; page < 100; page++) { 397+ const result = await object().call("refusal", op, undefined, body); 398+ if (result.status !== 503) return result; 400+ throw Error("preparation did not finish"); 402+ assert.equal((await call("init", { files: { "a.txt": "original\n" } })).status, 200); 403+ assert.equal((await call("status", {})).status, 200); 404+ const remote = new MemoryFS(); 405+ await git.init({ fs: remote, dir: "/repo", defaultBranch: "main" }); 406+ const commit = async (files: Record<string, string>, message: string, parent: string[]) => { 408+ for (const [path, text] of Object.entries(files)) 412+ type: "blob" as const, 413+ oid: await git.writeBlob({ fs: remote, dir: "/repo", blob: new TextEncoder().encode(text) }), 415+ const tree = await git.writeTree({ fs: remote, dir: "/repo", tree: entries }); 416+ const oid = await git.commit({ 422+ author: { name: "Git writer", email: "test@example.invalid" }, 424+ await git.writeRef({ fs: remote, dir: "/repo", ref: "main", value: oid, force: true }); 427+ const base = await commit({ "a.txt": "original\n" }, "Initial\n\nabso-version: 0", []); 428+ values.set("git:head", base); 429+ let head = await commit({ "A.TXT": "clash\n", "a.txt": "original\n" }, "Refused", [base]); 430+ t.mock.method(git, "listServerRefs", async () => [{ ref: "refs/heads/main", oid: head }]); 431+ t.mock.method(git, "clone", async (options: Parameters<typeof git.clone>[0]) => { 432+ (options.fs as MemoryFS).entries = structuredClone(remote.entries); 434+ const refused = await object().gitSync(); 436+ assert.match(JSON.stringify(refused.answer), /one file|clash/i); 437+ assert.equal(values.get("git:head"), head); 438+ assert.equal(await object().gitSync(), null); 439+ head = await commit({ "a.txt": "fixed\n" }, "Fixed", [head]); 440+ const fixed = await object().gitSync(); 442+ assert.equal((fixed.answer as { landed: boolean }).landed, true, JSON.stringify(fixed)); 443+ assert.equal(values.get("git:head"), head); 445+ JSON.stringify((await call("file", { path: "a.txt", version: String((await object().out()).version) })).body),