diff --git a/packages/qoder/busy.test.mjs b/packages/qoder/busy.test.mjs index e433701..1725efc 100644 --- a/packages/qoder/busy.test.mjs +++ b/packages/qoder/busy.test.mjs @@ -78,3 +78,36 @@ test("Qoder's other codes on a 403 aren't a lapsed sign-in either; 105 and no co expect(f("105", "login expired")).toEqual({ status: 401, message: "the sign-in lapsed — sign in again (Qoder said 403: login expired)" }) expect(_internal.failure(403, "")).toEqual({ status: 401, message: "the sign-in lapsed — sign in again" }) }) + +test("a BOM in an SSE error body must not turn a resource limit into expired auth", async () => { + const { fetch, sets } = await chat(() => sse({headers:{},body:"\uFEFF"+QUEUED,statusCodeValue:403,statusCode:"FORBIDDEN"})) + expect(await busy(await fetch(API_CHAT,ASK))).toEqual(WANT) + expect(sets).toEqual([]) +}) + +// #167 lists these outer message wrappers as additional ways a structured +// busy response can still reach the sign-in fallback. They are synthetic +// wrappers of the saved upstream queue fixture, not captured live bytes. +for (const [label, outer] of [ + ["message only", { message: QUEUED }], + ["null code", { code: null, message: QUEUED }], + ["HTTP code", { code: 403, message: QUEUED }], + ["empty queue", { queue: {}, message: QUEUED }], +]) for (const stream of [false, true]) { + test(`a ${label} wrapper preserves the resource failure (SSE=${stream})`, async () => { + const { fetch, sets } = await chat(() => stream + ? sse({ headers: {}, body: JSON.stringify(outer), statusCodeValue: 403, statusCode: "FORBIDDEN" }) + : new Response(JSON.stringify(outer), { status: 403, headers: { "Content-Type": "application/json" } })) + expect(await busy(await fetch(API_CHAT, ASK))).toEqual(WANT) + expect(sets).toEqual([]) + }) +} + +test("an explicit authentication, credit or request code is not replaced by text inside it", () => { + for (const [code, status] of [["105", 401], ["110", 429], ["406", 400]]) { + expect(_internal.failure(403, JSON.stringify({ code, message: QUEUED })).status).toBe(status) + } + expect(_internal.failure(403, JSON.stringify({ message: '{"code":"10605"' })).status).toBe(401) + expect(_internal.failure(403, JSON.stringify({ message: '[{"code":"10605"}]' })).status).toBe(401) + expect(_internal.failure(403, JSON.stringify({ message: JSON.stringify({ code: "110", message: "credits" }) })).status).toBe(401) +}) diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index 026e46b..1d7bc80 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -799,16 +799,26 @@ function failure(status, text) { let code = "" let queue try { - const j = JSON.parse(text) + let j = JSON.parse(msg) + // #167: an HTTP/message wrapper can hold the actual 10605 body. + // Unwrap only that known queue code; a real auth/credit/request code + // on the outer body remains authoritative. + const outerCode = j?.code == null ? "" : String(j.code) + if (["", "401", "403"].includes(outerCode) && typeof j?.message === "string") { + try { + const inner = JSON.parse(j.message.trim()) + if (inner && !Array.isArray(inner) && String(inner.code) === QUEUED) j = inner + } catch {} + } code = j?.code == null ? "" : String(j.code) if (j?.message) msg = String(j.message) // details is a JSON string, or the object itself (gjson reads either) - const d = typeof j?.details === "string" ? JSON.parse(j.details) : j?.details + const d = typeof j?.details === "string" ? JSON.parse(j.details.trim()) : j?.details if (d?.error?.message) msg += ": " + d.error.message // the queue's state: the message itself, as a JSON string, or a field queue = j?.queue && typeof j.queue === "object" ? j.queue : undefined if (!queue && typeof j?.message === "string" && j.message.trim().startsWith("{")) { - const q = JSON.parse(j.message) + const q = JSON.parse(j.message.trim()) if (q && typeof q === "object" && ("isQueued" in q || "serviceAvailable" in q || "retryAfterSeconds" in q)) queue = q } } catch {} @@ -1029,7 +1039,7 @@ async function* events(body) { if (inner.trim() === "[DONE]") break let chunk try { - chunk = JSON.parse(inner) + chunk = JSON.parse(inner.trim()) } catch { continue }