diff --git a/src/cli/commands/documents.test.ts b/src/cli/commands/documents.test.ts index 60a6c3f..f9f436b 100644 --- a/src/cli/commands/documents.test.ts +++ b/src/cli/commands/documents.test.ts @@ -11,6 +11,7 @@ let dir: string let document = { id: "doc-1", title: "Fixture policy", purpose: "Testing", content: "Line one\\\r\n| a \\| b |", status: "published", enabled: true, scenarios: ["audit"], tags: ["test"] } let requests: Array<{ method: string; path: string; query: string; body?: Record }> let failReadback: boolean +let failReadbackStatus: number const original = structuredClone(document) beforeEach(() => { @@ -29,14 +30,23 @@ beforeEach(() => { document = structuredClone(original) requests = [] failReadback = false + failReadbackStatus = 400 + let writeCount = 0 + let readbackWrites = 0 globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { const request = new Request(input, init) const url = new URL(request.url) const method = request.method const body = ["PATCH", "POST"].includes(method) ? JSON.parse(await request.text()) as Record : undefined requests.push({ method, path: url.pathname, query: url.searchParams.toString(), body }) - if (failReadback && method === "GET" && requests.some(item => item.method === "PATCH")) return Response.json({ error: "offline" }, { status: 400 }) - if (method === "PATCH") document = { ...document, ...(body?.attributes as object) } + if (failReadback && method === "GET" && writeCount > readbackWrites) { + readbackWrites = writeCount + return Response.json({ error: "offline" }, { status: failReadbackStatus }) + } + if (method === "PATCH") { + writeCount++ + document = { ...document, ...(body?.attributes as object) } + } if (method === "POST") document = { ...document, ...body } return Response.json({ data: url.pathname.endsWith("/articles") && method === "GET" ? { articles: [document], total: 115 } : document }) }) as typeof fetch @@ -129,6 +139,11 @@ describe("documents CLI", () => { expect(uncertain.exitCode).toBe(1) expect(JSON.parse(uncertain.stdout)).toMatchObject({ code: "DOCUMENT_READBACK_FAILED", executed: true, verified: false }) expect(uncertain.stderr).toContain("Next action:") + + failReadbackStatus = 401 + const authFailed = await cli("update", "doc-1", "--content", "newer", "--yes") + expect(authFailed.exitCode).toBe(11) + expect(JSON.parse(authFailed.stdout)).toMatchObject({ code: "DOCUMENT_READBACK_FAILED", executed: true, verified: false }) }) test("unsupported format and path/input conflicts fail before requests or file changes", async () => { diff --git a/src/domains/seal/documents/service.test.ts b/src/domains/seal/documents/service.test.ts index daf38a5..b805ff4 100644 --- a/src/domains/seal/documents/service.test.ts +++ b/src/domains/seal/documents/service.test.ts @@ -20,6 +20,7 @@ function fixture() { let reads = 0 let failWrite = false let failReadback = false + let readbackError: unknown = undefined let corruptReadback = false const client = { get(path: string) { @@ -27,7 +28,7 @@ function fixture() { return { json: async () => { if (path === "api/v1/knowledge/articles") return { data: { articles: [document], total: 115 } } reads++ - if (failReadback && reads > 1) throw new Error("readback unavailable") + if (failReadback && reads > 1) throw (readbackError ?? new Error("readback unavailable")) return { data: structuredClone(corruptReadback && reads > 1 ? { ...document, status: "draft" } : document) } } } }, @@ -49,6 +50,7 @@ function fixture() { set document(value) { document = value }, set failWrite(value: boolean) { failWrite = value }, set failReadback(value: boolean) { failReadback = value }, + set readbackError(value: unknown) { readbackError = value }, set corruptReadback(value: boolean) { corruptReadback = value }, writes: () => requests.filter(request => request.method !== "GET") } @@ -234,6 +236,23 @@ describe("document mutations", () => { }) } + test("readback authentication failure retains exit code 11", async () => { + const f = fixture() + f.failReadback = true + f.readbackError = Object.assign(new Error("unauthorized"), { response: { status: 401 } }) + try { + await updateDocument(f.client, corp, { documentId: "doc-1", content: "new", confirm: true }) + throw new Error("expected rejection") + } catch (error) { + expect(error).toBeInstanceOf(DocumentMutationError) + const err = error as DocumentMutationError + expect(err.exitCode).toBe(11) + expect(err.payload.code).toBe("DOCUMENT_READBACK_FAILED") + expect(err.payload.executed).toBe(true) + expect(err.payload.verified).toBe(false) + } + }) + test("create and delete also disable transport retries", async () => { const f = fixture() expect(await createDocument(f.client, corp, { title: "new", purpose: "policy", content: "body", confirm: true })).toMatchObject({ verified: true }) diff --git a/src/domains/seal/documents/service.ts b/src/domains/seal/documents/service.ts index ad09075..fffa821 100644 --- a/src/domains/seal/documents/service.ts +++ b/src/domains/seal/documents/service.ts @@ -87,8 +87,9 @@ export async function createDocument(client: KyInstance, corp: CorpConfig, param async function verifyDocument(client: KyInstance, corp: CorpConfig, id: string, expected: Partial, changedFields: DocumentField[], baselineAvailable: boolean, snapshot?: { snapshotId: string; snapshotFile: string }) { const receipt = { source: documentSource(corp, id), preview: false, executed: true, changed: true, changedFields, baselineAvailable, ...snapshot } let after: ApprovalDocument - try { after = await api.getApprovalDocument(client, id) } catch { - throw new DocumentMutationError({ ...receipt, code: "DOCUMENT_READBACK_FAILED", verified: false }, "Document was written, but readback failed.", 1, nextRead(corp, id)) + try { after = await api.getApprovalDocument(client, id) } catch (error) { + const auth = classifyCliError(error).exitCode === 11 + throw new DocumentMutationError({ ...receipt, code: "DOCUMENT_READBACK_FAILED", verified: false }, "Document was written, but readback failed.", auth ? 11 : 1, nextRead(corp, id)) } const mismatchedFields = changes(after, expected) if (mismatchedFields.length) throw new DocumentMutationError({ ...receipt, code: "DOCUMENT_VERIFICATION_FAILED", verified: false, mismatchedFields }, "Document readback differs from the expected state.", 1, nextRead(corp, id))