Files
plugins/packages/zed/org.test.mjs
T
yetone 5a836745fb zed 0.1.10: the organization with a plan is the one asked under, not a default without one, and a 402 for the saved one moves the account (#232, ReneXiong)
ReneXiong's account has two organizations: the personal one on
zed_student and a non-personal default with no plan. orgOf() always took
default_organization_id, minted the model token for it, and /models and
/completions answered 402 "Organization does not have an active
subscription". planOf() fell back to the account's plan_v3, so the card
said Student over an organization with none.

What Zed's editor does (zed-industries/zed 56934ec): user.rs
update_authenticated_user starts in the default organization, else the
first; the title bar's organization menu calls set_current_organization,
which the LLM token's auth context (language_models cloud.rs
ClientTokenProvider) follows; plan() is plans_by_organization[current],
none when it has no entry (current_organization_has_no_active_subscription
treats a missing entry as no subscription; an unknown plan as Free). The
plugin has no switcher, so it now picks as a user switching would:
- the organization the sign-in saved, while it still has a plan that
  calls models (any but Free) and models aren't turned off for it;
- else the default, if it has one; else the personal one with one; else
  any with one;
- none with one: what Zed picks (saved, else default, else first).
planOf names the picked organization's own plan ("" without an entry);
only a reply with no organizations falls back to plan_v3.

A sign-in saved before this under the unsubscribed default is moved on
the first 402 (llm_tokens, /models, /completions) or empty list: the
account is read again, moved if another organization has a plan, the
move saved through client.auth.set (and kept in memory in case the save
doesn't land), and the request asked once more. The usage hook re-picks
the same way. The model-token cache and the model list are keyed by
organization now, so a move mints and lists anew. There is no explicit
organization choice in the plugin or in magpie to respect; the saved
organization is the sticky one.

Run (bun 1.4.2, macOS, worktree at origin/main 8cdf2d2 + this):
- packages/zed/org.test.mjs (new, no network: fetch stubbed): 11 pass.
  With the old index.mjs and the new tests: 10 fail, 1 pass (the
  no-plan fallback the old code also did).
- HOME=$(mktemp -d) bun test (whole repo): 1802 pass, 0 fail.
- bun scripts/check.mjs: every package ok.
Not tried against a real Zed account; the reporter's /users/me shape is
from the issue's sanitized read.
2026-10-19 01:04:57 +08:00

189 lines
9.8 KiB
JavaScript

// The organization an account's models are asked under (#232, ReneXiong):
// Zed's default organization can be one without a subscription while
// another (the personal one, on a Student plan) has one. Zed's editor lets
// the user switch to it (title_bar.rs → user.rs set_current_organization);
// the plugin has no switcher, so it picks the one with a plan, and moves a
// sign-in saved under the wrong one when Zed answers 402.
import { afterEach, expect, test } from "bun:test"
import crypto from "node:crypto"
import { ZedAuthPlugin, _internal } from "./index.mjs"
const real = globalThis.fetch
afterEach(() => {
globalThis.fetch = real
_internal.tokens.clear()
_internal.lists.clear()
_internal.picked.clear()
})
// the reporter's /client/users/me, as the issue gives it sanitized: the
// personal organization has the Student plan, the default one none
const reporter = (over = {}) => ({
user: { legacy_user_id: 77, github_login: "rene" },
organizations: [
{ id: "org-personal", name: "rene", is_personal: true },
{ id: "org-team", name: "Some Team", is_personal: false },
],
default_organization_id: "org-team",
plans_by_organization: { "org-personal": "zed_student" },
plan: { plan_v3: "zed_student" },
...over,
})
const orgs = (...ids) => ids.map((id) => ({ id, name: id, is_personal: id.startsWith("personal") }))
test("the reporter's account: the personal organization with the Student plan, not the default without one", () => {
expect(_internal.orgOf(reporter())).toBe("org-personal")
// and its plan is that organization's, not the account's in its place
expect(_internal.planOf(reporter(), "org-team")).toBe("")
expect(_internal.planOf(reporter(), "org-personal")).toBe("zed_student")
})
test("a default organization with a plan is kept, as Zed picks it", () => {
const me = { organizations: orgs("personal", "biz"), default_organization_id: "biz", plans_by_organization: { personal: "zed_pro", biz: "zed_business" } }
expect(_internal.orgOf(me)).toBe("biz")
})
test("an organization the sign-in saved is kept while it has a plan, over the default", () => {
const me = { organizations: orgs("personal", "biz"), default_organization_id: "biz", plans_by_organization: { personal: "zed_pro", biz: "zed_business" } }
expect(_internal.orgOf(me, "personal")).toBe("personal")
// it lost its plan: the default, which has one
me.plans_by_organization = { biz: "zed_business" }
expect(_internal.orgOf(me, "personal")).toBe("biz")
// it is gone from the account
expect(_internal.orgOf({ ...me, organizations: orgs("biz") }, "personal")).toBe("biz")
})
test("with the default unsubscribed: the personal organization with a plan first, then any with one", () => {
const me = { organizations: orgs("team-a", "team-b", "personal"), default_organization_id: "team-a", plans_by_organization: { "team-b": "zed_business", personal: "zed_pro" } }
expect(_internal.orgOf(me)).toBe("personal")
me.plans_by_organization = { "team-b": "zed_business", personal: "zed_free" }
expect(_internal.orgOf(me)).toBe("team-b")
})
test("an organization with Zed's hosted models turned off isn't picked over one that has them", () => {
const me = {
organizations: orgs("team-a", "team-b"),
default_organization_id: "team-a",
plans_by_organization: { "team-a": "zed_business", "team-b": "zed_business" },
configuration_by_organization: { "team-a": { is_zed_model_provider_enabled: false } },
}
expect(_internal.orgOf(me)).toBe("team-b")
})
test("no organization with a plan: what Zed picks, the saved one first", () => {
const me = { organizations: orgs("personal", "team"), default_organization_id: "team", plans_by_organization: { personal: "zed_free" } }
expect(_internal.orgOf(me)).toBe("team")
expect(_internal.orgOf(me, "personal")).toBe("personal")
expect(_internal.orgOf({ organizations: orgs("a", "b") })).toBe("a")
// a reply without organizations names the plan of the account itself
expect(_internal.planOf({ plan: { plan_v3: "zed_pro" } }, "")).toBe("zed_pro")
})
// a fake cloud.zed.dev: /client/users/me answers me; a model token names
// the organization it was minted for, and /models and /completions refuse
// one minted for an organization without a plan with Zed's 402
function cloud(me) {
const seen = []
globalThis.fetch = async (url, init = {}) => {
const path = new URL(String(url)).pathname
const auth = init.headers?.Authorization ?? ""
if (path === "/client/users/me") {
seen.push(path)
return Response.json(me)
}
if (path === "/client/llm_tokens") {
const org = JSON.parse(init.body).organization_id
seen.push(path + " " + org)
return Response.json({ token: "tok-" + org })
}
const org = auth.replace(/^Bearer tok-/, "")
seen.push(path + " " + org)
if (!me.plans_by_organization?.[org]) return Response.json({ code: "payment_required", message: "Organization does not have an active subscription." }, { status: 402 })
if (path === "/models") return Response.json({ models: [{ provider: "anthropic", id: "claude-sonnet-5", display_name: "Claude Sonnet 5", max_token_count: 1000, max_output_tokens: 100 }] })
if (path === "/completions") {
const lines = [
{ status: "started" },
{ event: { type: "message_start", message: { id: "m", type: "message", role: "assistant", content: [], model: "claude-sonnet-5", usage: { input_tokens: 1, output_tokens: 0 } } } },
{ event: { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } } },
{ event: { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "hi" } } },
{ event: { type: "content_block_stop", index: 0 } },
{ event: { type: "message_delta", delta: { stop_reason: "end_turn" }, usage: { output_tokens: 1 } } },
{ event: { type: "message_stop" } },
{ status: "stream_ended" },
]
return new Response(lines.map((l) => JSON.stringify(l)).join("\n") + "\n", { headers: { "x-zed-server-supports-status-messages": "true" } })
}
return new Response("", { status: 404 })
}
return seen
}
// a sign-in saved before #232: under the default organization, which has no plan
const savedUnder = (org, uid = "77") => ({
type: "oauth",
access: "plain-access-" + uid,
refresh: JSON.stringify({ userId: uid, systemId: "", org, plan: "zed_student", planName: "Student", login: "rene" }),
expires: 0,
accountId: "rene",
})
test("sign-in: the model token is minted for the organization with the plan, and that one is saved", async () => {
const seen = cloud(reporter())
const { privateKey, publicKey } = crypto.generateKeyPairSync("rsa", { modulusLength: 2048 })
const ct = crypto.publicEncrypt({ key: publicKey, padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, oaepHash: "sha256" }, Buffer.from("plain-access-s")).toString("base64url")
const out = await _internal.signedInWith(privateKey, "78", ct, "sys")
expect(out.type).toBe("success")
expect(JSON.parse(out.refresh)).toMatchObject({ org: "org-personal", plan: "zed_student", planName: "Student" })
expect(seen).toEqual(["/client/users/me", "/client/llm_tokens org-personal", "/models org-personal"])
})
test("a sign-in saved under the unsubscribed default: the 402 moves it, the list is read, and the move is saved", async () => {
const seen = cloud(reporter())
const saved = []
const hooks = await ZedAuthPlugin({ client: { auth: { set: async (x) => saved.push(x.body) } } })
const a = savedUnder("org-team")
const models = await hooks.provider.models({ models: {} }, { auth: a })
expect(Object.keys(models)).toEqual(["claude-sonnet-5"])
expect(seen).toEqual(["/client/llm_tokens org-team", "/models org-team", "/client/users/me", "/client/llm_tokens org-personal", "/models org-personal"])
expect(saved.length).toBe(1)
expect(JSON.parse(saved[0].refresh)).toMatchObject({ userId: "77", org: "org-personal", plan: "zed_student", login: "rene" })
// the next request goes under it, even when the save didn't reach the sign-in
expect(_internal.stateOf(a).org).toBe("org-personal")
expect(_internal.stateOf(saved[0]).org).toBe("org-personal")
})
test("a request under the unsubscribed default: moved once, answered under the organization with the plan", async () => {
const seen = cloud(reporter())
const s = _internal.stateOf(savedUnder("org-team", "79"))
const res = await _internal.complete(s, "https://cloud.zed.dev/v1/messages", {
method: "POST",
body: JSON.stringify({ model: "claude-sonnet-5", stream: false, max_tokens: 10, messages: [{ role: "user", content: "hi" }] }),
})
expect(res.status).toBe(200)
expect((await res.json()).content).toEqual([{ type: "text", text: "hi" }])
expect(seen).toEqual(["/client/llm_tokens org-team", "/completions org-team", "/client/users/me", "/client/llm_tokens org-personal", "/completions org-personal"])
})
test("no organization with a plan: the 402 is answered, after one read of the account", async () => {
const me = reporter({ plans_by_organization: {} })
const seen = cloud(me)
const s = _internal.stateOf(savedUnder("org-team", "80"))
const res = await _internal.complete(s, "https://cloud.zed.dev/v1/messages", {
method: "POST",
body: JSON.stringify({ model: "claude-sonnet-5", stream: false, max_tokens: 10, messages: [{ role: "user", content: "hi" }] }),
})
expect(res.status).toBe(402)
expect(seen).toEqual(["/client/llm_tokens org-team", "/completions org-team", "/client/users/me"])
})
test("the plan card: the organization with the plan, picked again and saved, not the account's plan shown over the wrong one", async () => {
cloud(reporter())
const saved = []
const hooks = await ZedAuthPlugin({ client: { auth: { set: async (x) => saved.push(x.body) } } })
const u = await hooks.auth.usage(async () => savedUnder("org-team", "81"))
expect(u.plan).toBe("Student")
expect(saved.length).toBe(1)
expect(JSON.parse(saved[0].refresh)).toMatchObject({ org: "org-personal", plan: "zed_student" })
})