Files
plugins/packages/factory/lapsed.test.mjs
T
yetone c8f4d448df Parity with magpie's built-ins: a failed sign-in says why, CLI-backed Command Code and Devin accounts follow the CLI's key, Factory's refused renewal is a 401 in the caller's API shape
- every {type:"failed"} carries an error reason magpie shows
- commandcode/devin: accounts taken from the CLI are marked metadata.cli and re-read its credentials on each request, usage and models
- factory: a refused refresh answers 401 authentication_error (Anthropic shape on /llm/a/, OpenAI's elsewhere) instead of throwing; the whoami back-off is per account; 403 hints name the account to sign in again
- versions: factory/devin/commandcode/cursor/kiro/mimo/zed/zcode 0.1.2, workbuddy 0.1.3
2026-10-01 00:55:50 +08:00

55 lines
1.9 KiB
JavaScript

// An account whose sign-in Factory won't renew answers as the built-in's
// does: a 401 in the shape of the API the request was for, not a throw.
import { afterEach, expect, test } from "bun:test"
import { FactoryAuthPlugin } from "./index.mjs"
const real = globalThis.fetch
afterEach(() => (globalThis.fetch = real))
const expired = {
type: "oauth",
access: "tok-old",
refresh: "r-old",
expires: Date.now() - 1000,
accountId: "ada",
activeOrganizationId: "fac_A",
region: "",
premBaseHost: "",
}
async function loaded() {
const seen = []
globalThis.fetch = async (url) => {
const u = new URL(String(url))
seen.push(u.pathname)
if (u.pathname.endsWith("/authenticate")) return new Response(JSON.stringify({ error: "invalid_grant" }), { status: 400 })
return new Response("upstream reached", { status: 500 })
}
let auth = { ...expired }
const client = { auth: { set: async ({ body }) => (auth = body) } }
const hooks = await FactoryAuthPlugin({ client })
const l = await hooks.auth.loader(async () => auth)
return { l, seen }
}
test("a refused renewal is Anthropic's 401 on /llm/a/", async () => {
const { l, seen } = await loaded()
const res = await l.fetch("https://api.factory.ai/api/llm/a/v1/messages", { method: "POST", body: JSON.stringify({ model: "claude-x" }) })
expect(res.status).toBe(401)
const b = await res.json()
expect(b.type).toBe("error")
expect(b.error.type).toBe("authentication_error")
expect(b.error.message).toContain("sign in")
expect(seen.some((p) => p.includes("/llm/"))).toBe(false)
})
test("and OpenAI's on /llm/o/", async () => {
const { l } = await loaded()
const res = await l.fetch("https://api.factory.ai/api/llm/o/v1/responses", { method: "POST", body: JSON.stringify({ model: "gpt-x" }) })
expect(res.status).toBe(401)
const b = await res.json()
expect(b.error.type).toBe("authentication_error")
expect(b.error.code).toBe(null)
expect(b.type).toBeUndefined()
})