Preserve auth exit codes when document readback fails
This commit is contained in:
1 parent
b017e1fc48
commit
a2963c736c
3 files changed
+40
-5
No files matched your search
@@ -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 () => {
|
||||
|
||||
Reference in new issue
Block a user