Preserve auth exit codes when document readback fails

This commit is contained in:
gitea committed 2026-10-20 17:40:06 +08:00
1 parent b017e1fc48
commit a2963c736c
3 files changed
+40 -5

No files matched your search

+17 -2
View File
@@ -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<string, unknown> }>
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<string, unknown> : 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 () => {
+20 -1
View File
@@ -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 })
+3 -2
View File
@@ -87,8 +87,9 @@ export async function createDocument(client: KyInstance, corp: CorpConfig, param
async function verifyDocument(client: KyInstance, corp: CorpConfig, id: string, expected: Partial<DocumentAttributes>, 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))