Files
yetone 779388ba04 zed 0.1.9: a signed-in account whose model list was never read says why, instead of showing the six pre-sign-in models as its own (#229, ReneXiong)
ReneXiong's Zed Student account (an organization without an active
subscription) showed "zed (Zed, 6 models)" with no error: the six
models the config hook declares before sign-in, with their hard-coded
limits. provider.models handed those back whenever /models failed and no
list had been read for the account. The likely path for this account is
a 200 whose models are all disabled: parseModels throws "Zed lists no
models for this account", the hook returned the declared list, and
magpie's host, seeing the last fetch answer 200, didn't mark the list as
fallen back, so nothing said it wasn't the account's.

Now, with no list ever read for the account, the hook throws the reason
(Zed's own "Zed: ... (status)", "Zed lists no models for this account
...", or "Zed: this account's models couldn't be read: <cause>"). magpie's
host takes a thrown models error as fellBack with that message as
listError, and the editor and `magpie providers` show "couldn't list its
models: <reason>". A list read before is still kept on a later failure,
as it was. A 401 for the model token still marks the sign-in expired.

Tests (bun 1.4.2, packages/zed): models.test.mjs 7 pass. With the old
index.mjs and the new tests, 3 fail: "the list refused with the token
just minted", "Zed down before any list", "a 200 listing nothing
callable" (each got the declared models back). Whole package: 33 pass,
0 fail.

Siblings: minimax, qoder, trae, raccoon, workbuddy and opendesign also
return provider.models on a failed list. Each of those checks a
response status magpie's host sees, so a failed read is marked as
fallen back with its HTTP status; not changed here. Not tried against a
real Zed account.
2026-10-18 21:45:42 +08:00

114 lines
4.9 KiB
JavaScript

// provider.models says what the built-in's model list did to the account's
// mark (internal/provider/zed.go zedFetchModels): Zed refusing the sign-in
// while the list is read marked it lapsed; anything else left it alone.
import { afterEach, expect, test } from "bun:test"
import { ZedAuthPlugin, _internal } from "./index.mjs"
const real = globalThis.fetch
afterEach(() => {
globalThis.fetch = real
_internal.tokens.clear()
_internal.lists.clear()
})
let n = 0
const auth = () => ({
type: "oauth",
access: "plain-access-" + ++n,
refresh: JSON.stringify({ userId: String(5000 + n), systemId: "", org: "org-me", plan: "zed_pro" }),
expires: 0,
accountId: "octo",
})
const list = JSON.stringify({ models: [{ provider: "anthropic", id: "claude-sonnet-5", display_name: "Claude Sonnet 5", max_token_count: 1000, max_output_tokens: 100 }] })
async function models(a, route) {
const seen = []
globalThis.fetch = async (url) => {
const u = new URL(String(url))
seen.push(u.pathname)
return route(u.pathname)
}
const hooks = await ZedAuthPlugin({})
return { run: () => hooks.provider.models({ models: { declared: {} } }, { auth: a }), seen }
}
test("the model token refused: the sign-in expired, as zedLapse marked it", async () => {
const { run, seen } = await models(auth(), () => new Response("unauthorized", { status: 401 }))
const err = await run().catch((e) => e)
expect(err).toBeInstanceOf(Error)
expect(err.signIn).toBe("expired")
expect(err.message).toContain("the Zed sign-in has expired")
expect(seen).toEqual(["/client/llm_tokens"])
})
test("the list refused with the token just minted: no word on the sign-in, and no list", async () => {
// a stale token is minted again once; /models refusing both is no word on
// the account's own sign-in, which the built-in left unmarked. The
// declared models aren't handed back as the account's (#229)
const { run, seen } = await models(auth(), (p) =>
p === "/client/llm_tokens" ? new Response(JSON.stringify({ token: "tok" })) : new Response("nope", { status: 401 }),
)
const err = await run().catch((e) => e)
expect(err).toBeInstanceOf(Error)
expect(err.signIn).toBeUndefined()
expect(err.message).toContain("(401)")
expect(seen).toEqual(["/client/llm_tokens", "/models", "/client/llm_tokens", "/models"])
})
test("Zed down before any list: the failure, not the declared models", async () => {
const { run } = await models(auth(), () => new Response("bad gateway", { status: 502 }))
const err = await run().catch((e) => e)
expect(err).toBeInstanceOf(Error)
expect(err.signIn).toBeUndefined()
expect(err.message).toContain("bad gateway")
})
// #229 (ReneXiong): an organization without an active subscription gets a
// 200 whose models are all disabled. The plugin handed back the six
// declared before sign-in, with their limits, and a host judging by the
// 200 showed them as the account's list with no error
test("a 200 listing nothing callable: says so, not the declared models", async () => {
const off = JSON.stringify({ models: [{ provider: "anthropic", id: "claude-sonnet-5", is_disabled: true, max_token_count: 1000, max_output_tokens: 100 }] })
const { run } = await models(auth(), (p) => (p === "/client/llm_tokens" ? new Response(JSON.stringify({ token: "tok" })) : new Response(off)))
const err = await run().catch((e) => e)
expect(err).toBeInstanceOf(Error)
expect(err.message).toContain("Zed lists no models for this account")
})
test("Zed down after a list was read: the list read last", async () => {
const a = auth()
let down = false
const { run } = await models(a, (p) => {
if (down) return new Response("bad gateway", { status: 502 })
return p === "/client/llm_tokens" ? new Response(JSON.stringify({ token: "tok" })) : new Response(list)
})
expect(Object.keys(await run())).toEqual(["claude-sonnet-5"])
const uid = JSON.parse(a.refresh).userId
_internal.lists.set(uid, { at: 0, models: _internal.lists.get(uid).models })
down = true
expect(Object.keys(await run())).toEqual(["claude-sonnet-5"])
})
test("a list read: the account's models", async () => {
const { run } = await models(auth(), (p) => (p === "/client/llm_tokens" ? new Response(JSON.stringify({ token: "tok" })) : new Response(list)))
expect(Object.keys(await run())).toEqual(["claude-sonnet-5"])
})
test("refused after a list was read: still says expired", async () => {
const a = auth()
let refuse = false
const { run } = await models(a, (p) => {
if (refuse) return new Response("unauthorized", { status: 401 })
return p === "/client/llm_tokens" ? new Response(JSON.stringify({ token: "tok" })) : new Response(list)
})
expect(Object.keys(await run())).toEqual(["claude-sonnet-5"])
// the list goes stale and the token with it
const uid = JSON.parse(a.refresh).userId
_internal.lists.set(uid, { at: 0, models: _internal.lists.get(uid).models })
_internal.tokens.clear()
refuse = true
const err = await run().catch((e) => e)
expect(err.signIn).toBe("expired")
})