A person's personal account and their enterprise account (a team, or 「WorkBuddy 教育公益版」, picked on WorkBuddy's sign-in page after the phone sign-in) share the uid and the nickname. magpie's plugin host keeps one account per name and uid (settle in host.js), so signing in to the education account replaced the same person's personal account, and the two could never sit in one pool. A browser sign-in that WorkBuddy places in an enterprise is now named "nickname · enterprise name": the name from /login/account, else from the matching /v2/plugin/accounts entry, else "enterprise <last 6 of id>". A personal account's name is unchanged. Old data: an enterprise account signed in before keeps its old name until its Sign in again, which uses the same key and renames it in place; its free-model window, read from ledger rows under the old name, starts again (README says so). Run (mote, HOME=$(mktemp -d)): bun test in packages/workbuddy, 256 pass 0 fail; bun scripts/check.mjs ok. Broken by hand: accountName returning the nickname alone turns the 4 new enterprise.test.mjs tests red. Not tried: a real education account (the owner has none; the live /v2/plugin/accounts shows only a personal entry), so enterpriseName's presence in WorkBuddy's replies for 教育公益版 is from the web client's SelectAccount code, not a live reply; the id fallback covers its absence.
124 lines
5.6 KiB
JavaScript
124 lines
5.6 KiB
JavaScript
// An enterprise (team) account is asked for as the enterprise, as
|
|
// WorkBuddy's own client asks: X-Enterprise-Id and X-Tenant-Id carry the
|
|
// account's enterpriseId on every request, or WorkBuddy answers as the
|
|
// personal account and lists the personal plan's models (#75).
|
|
import { afterEach, expect, test } from "bun:test"
|
|
import { WorkBuddyAuthPlugin, _internal } from "./index.mjs"
|
|
|
|
const real = globalThis.fetch
|
|
afterEach(() => (globalThis.fetch = real))
|
|
|
|
const site = _internal.SITES.workbuddy
|
|
const later = () => Date.now() + 3600_000
|
|
const config = '{"code":0,"data":{"agents":[{"name":"cli","models":["m"]}],"models":[{"id":"m","name":"M"}]}}'
|
|
|
|
const seen = (into) => async (url, init) => {
|
|
into.push({ url: String(url), headers: new Headers(init?.headers) })
|
|
return new Response(config)
|
|
}
|
|
|
|
test("an enterprise account's model list is asked for as the enterprise (#75)", async () => {
|
|
const got = []
|
|
globalThis.fetch = seen(got)
|
|
await _internal.liveModels(site, { access: "a", uid: "u", enterpriseId: "ent-1" })
|
|
expect(got[0].url).toEndWith("/v3/config")
|
|
expect(got[0].headers.get("X-Enterprise-Id")).toBe("ent-1")
|
|
expect(got[0].headers.get("X-Tenant-Id")).toBe("ent-1")
|
|
})
|
|
|
|
test("a personal account's list carries no enterprise", async () => {
|
|
const got = []
|
|
globalThis.fetch = seen(got)
|
|
await _internal.liveModels(site, { access: "a", uid: "u" })
|
|
expect(got[0].headers.has("X-Enterprise-Id")).toBe(false)
|
|
expect(got[0].headers.has("X-Tenant-Id")).toBe(false)
|
|
})
|
|
|
|
test("an enterprise account's chats carry the enterprise too", async () => {
|
|
const got = []
|
|
globalThis.fetch = seen(got)
|
|
const auth = { type: "oauth", access: "a", refresh: "r", expires: later(), uid: "u", enterpriseId: "ent-1" }
|
|
const hooks = await WorkBuddyAuthPlugin({ client: {} })
|
|
const opts = await hooks.auth.loader(async () => auth)
|
|
await opts.fetch("https://copilot.tencent.com/v2/chat/completions", { method: "POST", body: '{"messages":[]}' })
|
|
expect(got.at(-1).headers.get("X-Enterprise-Id")).toBe("ent-1")
|
|
expect(got.at(-1).headers.get("X-Tenant-Id")).toBe("ent-1")
|
|
})
|
|
|
|
// the sign-in, with WorkBuddy's answers as its own client reads them
|
|
const signIn = async (account, accounts) => {
|
|
const asked = []
|
|
globalThis.fetch = async (url, init) => {
|
|
const u = new URL(String(url))
|
|
asked.push(u.pathname)
|
|
const data =
|
|
u.pathname.endsWith("/auth/state") ? { state: "s", authUrl: "https://www.codebuddy.cn/login" }
|
|
: u.pathname.endsWith("/auth/token") ? { accessToken: "a", refreshToken: "r", expiresIn: 3600 }
|
|
: u.pathname.endsWith("/login/account") ? account
|
|
: u.pathname.endsWith("/accounts") ? { accounts }
|
|
: {}
|
|
return new Response(JSON.stringify({ code: 0, data }))
|
|
}
|
|
const flow = await _internal.browserSignIn(site)
|
|
return { result: await flow.callback(), asked }
|
|
}
|
|
|
|
test("signing in as an enterprise keeps the enterprise WorkBuddy named", async () => {
|
|
const { result, asked } = await signIn(
|
|
{ uid: "u", nickname: "n", type: "ultimate", enterpriseId: "ent-1", enterpriseName: "WorkBuddy 教育公益版" },
|
|
[],
|
|
)
|
|
expect(result.type).toBe("success")
|
|
expect(result.enterpriseId).toBe("ent-1")
|
|
expect(result.accountId).toBe("n · WorkBuddy 教育公益版")
|
|
expect(asked.some((p) => p.endsWith("/accounts"))).toBe(false)
|
|
})
|
|
|
|
test("an enterprise named by id only takes its name from the account list", async () => {
|
|
const { result } = await signIn({ uid: "u", nickname: "n", type: "ultimate", enterpriseId: "ent-1" }, [
|
|
{ uid: "u", type: "personal" },
|
|
{ uid: "u", type: "ultimate", enterpriseId: "ent-1", enterpriseName: "某某中学" },
|
|
])
|
|
expect(result.enterpriseId).toBe("ent-1")
|
|
expect(result.accountId).toBe("n · 某某中学")
|
|
})
|
|
|
|
test("an enterprise WorkBuddy names nowhere is told apart by its id", async () => {
|
|
const { result } = await signIn({ uid: "u", nickname: "n", type: "ultimate", enterpriseId: "ent-123456789" }, [])
|
|
expect(result.enterpriseId).toBe("ent-123456789")
|
|
expect(result.accountId).toBe("n · enterprise 456789")
|
|
})
|
|
|
|
// One person's personal and education accounts share the uid and the
|
|
// nickname; magpie keeps one account per name and uid, so the two names
|
|
// must differ or the second sign-in replaces the first.
|
|
test("a person's personal and enterprise sign-ins are named apart", async () => {
|
|
const list = [
|
|
{ uid: "u", type: "personal", lastLogin: false },
|
|
{ uid: "u", type: "ultimate", enterpriseId: "ent-2", enterpriseName: "WorkBuddy 教育公益版", lastLogin: true },
|
|
]
|
|
const personal = (await signIn({ uid: "u", nickname: "n", type: "personal" }, list)).result
|
|
const edu = (await signIn({ uid: "u", nickname: "n", type: "ultimate" }, list)).result
|
|
expect(personal.accountId).toBe("n")
|
|
expect(edu.accountId).toBe("n · WorkBuddy 教育公益版")
|
|
expect(personal.uid).toBe(edu.uid)
|
|
})
|
|
|
|
test("an enterprise account named without its id is found in the account list", async () => {
|
|
const { result } = await signIn({ uid: "u", nickname: "n", type: "ultimate" }, [
|
|
{ uid: "u", type: "personal", lastLogin: false },
|
|
{ uid: "u", type: "ultimate", enterpriseId: "ent-2", lastLogin: true },
|
|
{ uid: "other", type: "ultimate", enterpriseId: "ent-3", lastLogin: true },
|
|
])
|
|
expect(result.enterpriseId).toBe("ent-2")
|
|
})
|
|
|
|
test("signing in as the personal account keeps no enterprise", async () => {
|
|
const { result, asked } = await signIn({ uid: "u", nickname: "n", type: "personal" }, [
|
|
{ uid: "u", type: "ultimate", enterpriseId: "ent-2", lastLogin: true },
|
|
])
|
|
expect(result.type).toBe("success")
|
|
expect("enterpriseId" in result).toBe(false)
|
|
expect(asked.some((p) => p.endsWith("/accounts"))).toBe(false)
|
|
})
|