Files
plugins/packages/commandcode/usage.test.mjs
T
yetone 9410e2a510 commandcode 0.1.19, devin 0.1.26, kiro 0.1.19: a refused sign-in marks the account signed out
blue12345655 (magpie Discord #help): a Command Code account signed in
through the plugin was signed out, and magpie offered no 重新登录, on
v0.1.1844 and still on v0.1.2021.

Cause: commandcode said X-Magpie-Sign-In "kept" on every answer and
signIn "kept" on every usage read, so a revoked key came back as a card
saying "Unauthorized" while magpie kept the account signed in, routed to
it, and never showed Sign in again on its rows. Devin and Kiro did the
same in their usage reads: their errors already said "sign in again",
but "kept" stops magpie from reading those words.

- commandcode: a usage read whose credits are refused (401/403), or a
  request answered 401, asks /alpha/whoami; when whoami refuses the key
  too, the usage says signIn "expired" with "Command Code's sign-in has
  expired; sign in again", and the answer carries X-Magpie-Sign-In:
  expired, its body unchanged. A 403 for a model the plan lacks, a 401
  whoami still takes, and a whoami 500 keep the account. A clean usage
  read says "renewed", which takes a mark off.
- devin: a usage read Devin answers 401 says "expired"; a clean read
  "renewed"; other failures "kept". Requests unchanged.
- kiro: a usage read whose sign-in Kiro refused (the errors made by
  gone) says "expired"; a clean read "renewed"; other failures "kept".
  auth.refresh and requests unchanged.

The response bytes in commandcode/signed-out.test.mjs are what
api.commandcode.ai answered an unknown key on 2026-10-29 (whoami,
billing/credits, billing/subscriptions, provider/v1/chat/completions).

Run (WSL magpie-desktop, Bun 1.4.2, HOME=$(mktemp -d)):
- bun test in packages/commandcode: 82 pass, 0 fail; packages/devin:
  142 pass; packages/kiro: 67 pass.
- bun scripts/check.mjs: every package ok.

Broken by hand: signed-out.test.mjs against the 0.1.18 index.mjs, 4 of
9 red (usage expired, clean read renewed, Provider API expired, Go
expired). kiro's and devin's changed usage tests against their old
index.mjs: red ("kept" received where "expired" expected).

Not tried: a real revoked Command Code, Devin or Kiro account; the
whoami check is only against an unknown key's real answers.
2026-10-29 17:04:52 +08:00

212 lines
10 KiB
JavaScript

// auth.usage tells what magpie's built-in Command Code account shows
// (internal/provider/commandcode_plan.go, cmdQuota), against the replies its
// tests give (commandcode_plan_test.go, commandcode_go_test.go).
import { afterEach, expect, test } from "bun:test"
import { CommandCodePlugin, _internal } from "./index.mjs"
// talking leaves out the decision model (Jev, jev.test.mjs), which every
// plan's list has beside the models it talks to
const talking = (ms) => {
delete ms[_internal.JEV]
return ms
}
const real = globalThis.fetch
afterEach(() => {
globalThis.fetch = real
_internal.subsSeen.clear()
_internal.waits.subWait = 9_000
})
const api = { type: "api", key: "own-key" }
async function run(a, replies) {
const seen = []
globalThis.fetch = async (url, init) => {
const path = new URL(String(url)).pathname
seen.push({ path, auth: init.headers.Authorization })
const r = replies[path]
return r === undefined ? new Response("", { status: 404 }) : typeof r === "number" ? new Response("", { status: r }) : Response.json(r)
}
const hooks = await CommandCodePlugin()
return { u: await hooks.auth.usage(async () => a), seen }
}
test("a Max account: its 5 hours, its credits second, where magpie shows two windows, its week, and its period", async () => {
const reset = Date.now() + 3600_000
const week = Date.now() + 72 * 3600_000
const { u, seen } = await run(api, {
"/alpha/billing/subscriptions": { success: true, data: { planId: "individual-max-monthly", status: "active", currentPeriodEnd: "2026-10-28T00:00:00Z", cancelAtPeriodEnd: true } },
"/alpha/billing/credits": {
credits: { monthlyCredits: 100.5, purchasedCredits: 10, freeCredits: 0 },
windowLimits: { limited: true, fiveHour: { used: 25, cap: 100, resetAt: reset }, weekly: { used: 30, cap: 300, resetAt: week } },
},
})
expect(u).toEqual({
signIn: "renewed",
plan: "Max",
until: "2026-10-28T00:00:00.000Z",
renew: "off",
windows: [
{ name: "5 hours", used: 25, span: 18000, resetsAt: new Date(reset).toISOString() },
{ name: "Credits", used: 30.9375, display: "$49.50 / $160.00", resetsAt: "2026-10-28T00:00:00.000Z" },
{ name: "Weekly", used: 10, span: 604800, resetsAt: new Date(week).toISOString() },
],
})
expect(seen.every((x) => x.auth === "Bearer own-key")).toBe(true)
})
test("no plan: the credits bought are a balance", async () => {
const { u } = await run(api, {
"/alpha/billing/subscriptions": { success: true, data: null },
"/alpha/billing/credits": { credits: { monthlyCredits: 0, purchasedCredits: 7.25, freeCredits: 0 } },
})
expect(u).toEqual({ signIn: "renewed", plan: "No plan", balance: "$7.25", windows: [] })
})
test("Go: the month's $10 is the pool", async () => {
const { u } = await run(api, {
"/alpha/billing/subscriptions": { data: { planId: "individual-go", status: "active" } },
"/alpha/billing/credits": { credits: { monthlyCredits: 4 } },
})
expect(u).toEqual({ signIn: "renewed", plan: "Go", windows: [{ name: "Credits", used: 60, display: "$6.00 / $10.00" }] })
})
test("credits that can't be read keep the plan and say why", async () => {
const { u } = await run(api, {
"/alpha/billing/subscriptions": { data: { planId: "individual-pro-monthly", status: "active", cancelAtPeriodEnd: false } },
"/alpha/billing/credits": 503,
})
expect(u).toEqual({ signIn: "kept", plan: "Pro", renew: "auto", error: "Service Unavailable", windows: [] })
})
test("a canceled subscription is No plan; an unread one leaves the plan unsaid", async () => {
let { u } = await run(api, { "/alpha/billing/subscriptions": { data: { planId: "individual-pro", status: "canceled" } }, "/alpha/billing/credits": 413 })
expect(u).toEqual({ signIn: "kept", plan: "No plan", error: "Request Entity Too Large", windows: [] })
_internal.subsSeen.clear() // a plan read is kept ten minutes
// a 401 whoami doesn't confirm (404 here) keeps the account; one it
// does is signed out (signed-out.test.mjs)
;({ u } = await run(api, { "/alpha/billing/subscriptions": 401, "/alpha/billing/credits": 401 }))
expect(u).toEqual({ signIn: "kept", error: "Unauthorized", windows: [] })
})
test("seconds and string amounts read as magpie reads them; a half cent goes to the even one", async () => {
const { u } = await run(api, {
"/alpha/billing/subscriptions": { data: { planId: "individual-pro", status: "active", currentPeriodEnd: 1790000000 } },
"/alpha/billing/credits": { credits: { monthlyCredits: "29.875" }, windowLimits: { fiveHour: { used: "5", cap: 0 }, weekly: { used: 150, cap: "100", resetAt: 0 } } },
})
expect(u).toEqual({
signIn: "renewed",
plan: "Pro",
until: new Date(1790000000 * 1000).toISOString(),
windows: [{ name: "Weekly", used: 100, span: 604800 }, { name: "Credits", used: 0.4166666666666667, display: "$0.12 / $30.00", resetsAt: new Date(1790000000 * 1000).toISOString() }],
})
})
test("a sign-in that isn't a key is no account", async () => {
expect((await run({ type: "oauth" }, {})).u).toEqual({ signIn: "kept", error: "not signed in" })
})
test("a slow subscription: the windows go without it, and the next card has it", async () => {
_internal.waits.subWait = 50
let answer
const slow = new Promise((r) => (answer = r))
const credits = { credits: { monthlyCredits: 35 }, windowLimits: { limited: true, fiveHour: { used: 1, cap: 100 } } }
globalThis.fetch = async (url) => {
const path = new URL(String(url)).pathname
if (path === "/alpha/billing/subscriptions") {
await slow
return Response.json({ data: { planId: "individual-goat", status: "active", cancelAtPeriodEnd: true } })
}
return Response.json(credits)
}
const hooks = await CommandCodePlugin()
const first = await hooks.auth.usage(async () => api)
expect(first).toEqual({ signIn: "renewed", windows: [{ name: "5 hours", used: 1, span: 18000 }] })
answer()
await new Promise((r) => setTimeout(r, 20))
const next = await hooks.auth.usage(async () => api)
expect(next).toEqual({ signIn: "renewed", plan: "GOAT", renew: "off", windows: [{ name: "5 hours", used: 1, span: 18000 }, { name: "Credits", used: 50, display: "$35.00 / $70.00" }] })
})
test("a plan read is saved with the key, once", async () => {
const sets = []
globalThis.fetch = async (url) =>
new URL(String(url)).pathname === "/alpha/billing/subscriptions"
? Response.json({ data: { planId: "individual-goat", status: "active" } })
: Response.json({ credits: { monthlyCredits: 70 } })
const hooks = await CommandCodePlugin({ client: { auth: { set: async (x) => sets.push(x) } } })
const a = { type: "api", key: "own-key", metadata: { email: "me", keyName: "laptop" } }
expect((await hooks.auth.usage(async () => a)).plan).toBe("GOAT")
expect(sets).toEqual([
{ path: { id: "commandcode-plan" }, body: { type: "api", key: "own-key", metadata: { email: "me", keyName: "laptop", plan: "GOAT", planId: "individual-goat" } } },
])
await hooks.auth.usage(async () => ({ ...a, metadata: { ...a.metadata, plan: "GOAT", planId: "individual-goat" } }))
expect(sets.length).toBe(1)
})
test("a plan saved stands in for a subscription slow to read: waited for a moment, the credits pooled by it", async () => {
let answer
const slow = new Promise((r) => (answer = r))
globalThis.fetch = async (url) => {
if (new URL(String(url)).pathname === "/alpha/billing/subscriptions") {
await slow
return Response.json({ success: false, error: "write CONNECTION_CLOSED" })
}
return Response.json({ credits: { monthlyCredits: 35 } })
}
const hooks = await CommandCodePlugin()
const t = Date.now()
const u = await hooks.auth.usage(async () => ({ ...api, metadata: { plan: "GOAT", planId: "individual-goat" } }))
expect(Date.now() - t).toBeLessThan(3_000)
expect(u).toEqual({ signIn: "renewed", plan: "GOAT", windows: [{ name: "Credits", used: 50, display: "$35.00 / $70.00" }] })
answer()
})
test("a subscription Command Code couldn't read is unread, not No plan", async () => {
const { u } = await run(api, {
"/alpha/billing/subscriptions": { success: false, error: "write CONNECTION_CLOSED db.local:5432" },
"/alpha/billing/credits": { credits: { monthlyCredits: 0, purchasedCredits: 0 } },
})
expect(u.plan).toBeUndefined()
const hooks = await CommandCodePlugin()
expect((await hooks.auth.usage(async () => ({ ...api, metadata: { plan: "GOAT" } }))).plan).toBe("GOAT")
})
test("a model served on Responses and not chat completions is asked on Responses, relayed as it is", async () => {
const sent = []
globalThis.fetch = async (url, init = {}) => {
const path = new URL(String(url)).pathname
if (path === "/provider/v1/models")
return Response.json({ data: [
{ id: "gpt-6-sol", supported_endpoints: ["/v1/responses"] },
{ id: "moonshotai/Kimi-K3", supported_endpoints: ["/v1/chat/completions", "/v1/responses"] },
{ id: "claude-sonnet-5", supported_endpoints: ["/v1/messages"] },
{ id: "both", supported_endpoints: ["/v1/responses", "/v1/messages"] },
] })
if (path === "/provider/v1/responses") {
sent.push({ path, key: new Headers(init.headers).get("x-api-key"), body: init.body })
return Response.json({ id: "resp_1" })
}
return new Response("", { status: 404 })
}
const a = { ...api, metadata: { plan: "Max" } }
const hooks = await CommandCodePlugin()
const ms = talking(await hooks.provider.models({ models: {} }, { auth: a }))
expect(Object.fromEntries(Object.entries(ms).map(([k, m]) => [k, m.api.npm]))).toEqual({
"gpt-6-sol": "@ai-sdk/openai",
"moonshotai/Kimi-K3": "@ai-sdk/openai-compatible",
"claude-sonnet-5": "@ai-sdk/anthropic",
both: "@ai-sdk/openai",
})
const l = await hooks.auth.loader(async () => a)
const res = await l.fetch("https://api.commandcode.ai/provider/v1/responses", { method: "POST", body: '{"model":"gpt-6-sol"}' })
expect(await res.json()).toEqual({ id: "resp_1" })
expect(sent).toEqual([{ path: "/provider/v1/responses", key: "own-key", body: '{"model":"gpt-6-sol"}' }])
})
test("errors don't name Command Code, which magpie adds", () => {
expect(_internal.failure(500, JSON.stringify({ error: { message: "boom" } }))).toEqual({ status: 500, message: "boom" })
expect(_internal.failure(403, "MODEL_NOT_IN_PLAN: gpt-6-sol")).toEqual({ status: 403, message: "Model not in plan: gpt-6-sol" })
})