liukaybee adds Claude accounts with `claude setup-token` and wants magpie
to switch only which one Claude Code itself uses, without connecting it
to magpie. Such an account has no sign-in to put in Claude Code's
keychain or .credentials.json, so Use was refused ("magpie only").
Claude Code takes CLAUDE_CODE_OAUTH_TOKEN from the env of its user
settings.json ahead of its stored sign-in. Use on a setup-token account
now writes the token there (claude_token_env.go). Claude Code's stored
sign-in stays as it is and stays Active; the token account is listed
with Uses. A switch to a signed-in account, Use on the signed-in row,
or removing the token's account takes the token out again.
- A value the user set is theirs: magpie keeps only the sha256 of what
it wrote (claude-token-env.json), never writes over another value and
never removes one whose hash differs, even if it once wrote it.
- An unreadable settings.json is "unknown": nothing is written, the
record is kept, and the switch fails without moving the sign-in.
- NextLogin (Switch accounts only) leaves Claude Code on a token in use,
as its allowance can't be read.
- magpie's own runs pass --setting-sources "" and are unaffected.
- GUI: the token row's Use and In use; the signed-in row says Signed in
with a Use back. Every new string in zh, zh-TW, ja and de.
- app.js +864 bytes over its budget, raised with the reason in
core_budget_test.go: one swUse helper shared by both buttons, packed;
renderAccounts draws every account row, so no page to move it to.
Real use: real Claude Code 2.1.316 in a sandbox HOME/CLAUDE_CONFIG_DIR
(never the live ~/.claude) with a deliberately broken .credentials.json
access token. The settings.json magpie's switch writes (dumped from a
test) gave a PONG with a real account's token; the file after switching
back gave "401 OAuth access token is invalid", i.e. Claude Code was on
its stored sign-in again. Sandbox homes holding real tokens deleted.
Run (mote/mica, at f00e2059d rebased on 593517bf0, then this head):
- Rebased on effdd1beb (internal/gui and internal/provider untouched
since, but for this): TestCoreScriptBudget and the provider TestClaude*,
NextLogin and Forget tests rerun at the pushed head, ok.
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go
vet ./...: ok. gofmt -l: clean.
- go test ./... -timeout 45m on mote: 102 packages ok, internal/gui
FAIL: TestCoreScriptBudget (fixed above, then green) and
TestQuitKeepsTheMenuBarIconsPlace, killed at its 30s AppKit bar under
load; passes alone in 1.1s here and on origin/main 00b2c11a6. Filed
#6919.
- GUI, both engines, every language, on mica: the 45 account, Claude,
switch, sign-in and i18n files (1275 tests, 68 red). The same 9 red
files on origin/main effdd1beb give 67 of the same reds (#5925, #6660,
#6179, #6424, #6010, #5998, #5814, #6877). The one red only at this
head, accounts-own-signin webkit zh (access-control on /api/autobackup)
is #6038 and passes 20/20 alone. claude-setup-token: green in all 10
engine x language runs.
Broken by hand, each red, then restored: the own-value check
(TestClaudeOwnEnvTokenLeftAlone), clearing on an OAuth switch
(TestClaudeCodeSwitchedToSetupToken, ...UnreadableSettings), clearing
on Use back (Switched, InUseNotMovedOff), clearing on remove
(TestClaudeSetupTokenRemovedLeavesSettings), the NextLogin guard
(InUseNotMovedOff), unreadable settings read as empty
(TestClaudeEnvTokenUnreadableSettings).
Not tried: the whole GUI suite (both machines were running other
sessions' full suites for hours); a magpie GUI build clicking Use
against the real binary (the switch's file was checked instead);
Windows and Linux Claude Code.
152 lines
10 KiB
JavaScript
152 lines
10 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// sfss5362 on X: "支持 CLAUDE_CODE_OAUTH_TOKEN 吗". Claude's sign-in offers
|
|
// "Use a setup-token instead…", which ends the browser sign-in and asks for
|
|
// the token `claude setup-token` printed, said whole in the page's
|
|
// language. A refused token says why and stays to be fixed; one taken is
|
|
// added. Its row's Use switches Claude Code itself to it (in its
|
|
// settings.json, liukaybee on X); in use, the signed-in account's Use
|
|
// switches back.
|
|
// Chromium and WebKit, English, Chinese (both), Japanese and German, at
|
|
// 420px; no backend, the API is faked here.
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs/promises");
|
|
const path = require("node:path");
|
|
const { test } = require("node:test");
|
|
const playwright = require("playwright");
|
|
|
|
const engines = (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"]);
|
|
const assets = path.resolve(__dirname, "../assets");
|
|
const TOKEN = "sk-ant-oat01-guitoken-0123456789";
|
|
|
|
for (const engine of engines) for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
|
|
test(`claude setup-token account (${engine}, ${lang})`, async (t) => {
|
|
const browser = await playwright[engine].launch();
|
|
t.after(() => browser.close());
|
|
const page = await browser.newPage({ viewport: { width: 420, height: 720 } });
|
|
page.setDefaultTimeout(5000);
|
|
page.setDefaultNavigationTimeout(60000);
|
|
const errors = [];
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
const tokens = [];
|
|
const canceled = [];
|
|
await page.route("**/*", async (route) => {
|
|
const url = new URL(route.request().url());
|
|
const json = (data, status) => route.fulfill({ json: data, status: status || 200 });
|
|
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"dark",web:true};` });
|
|
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
|
|
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "dark" } });
|
|
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: {} });
|
|
if (url.pathname === "/api/groups") return json({ groups: [] });
|
|
if (url.pathname === "/api/signin/claude-token") {
|
|
const body = route.request().postDataJSON();
|
|
tokens.push(body.token);
|
|
if (body.token.startsWith("sk-ant-api")) return json({ error: "this is an Anthropic API key, not a setup-token: add it as an API key provider instead" }, 400);
|
|
return json({ user: "Claude setup-token 3204d0" });
|
|
}
|
|
if (url.pathname === "/api/signin") return json({ id: "c1", agent: "claude", state: "waiting", url: "https://claude.ai/oauth/authorize?x=1" });
|
|
if (/^\/api\/signin\/c\d+\/cancel$/.test(url.pathname)) { canceled.push(url.pathname.split("/")[3]); return json({}); }
|
|
if (/^\/api\/signin\/c\d+$/.test(url.pathname)) return new Promise(() => {}); // stays waiting
|
|
if (url.pathname.startsWith("/api/")) return json({});
|
|
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
|
|
const contentType = { ".html": "text/html", ".js": "text/javascript", ".css": "text/css", ".svg": "image/svg+xml", ".png": "image/png" }[path.extname(file)];
|
|
return route.fulfill({ body: await fs.readFile(file), contentType });
|
|
});
|
|
await page.context().route((url) => url.hostname !== "magpie.test", (route) => route.fulfill({ contentType: "text/html", body: "" }));
|
|
await page.goto("http://magpie.test/");
|
|
await page.waitForFunction((l) => state.settings.lang === l, lang);
|
|
await page.evaluate(() => {
|
|
providers = providers || { providers: [], presets: [], plugins: [] };
|
|
window.signedInWith = [];
|
|
window.signedIn = async (st) => { window.signedInWith.push(st); signing = null; };
|
|
window.renderProviders = () => {
|
|
const host = document.getElementById("csign") || Object.assign(document.createElement("div"), { id: "csign" });
|
|
host.style.cssText = "position:fixed;inset:0 auto auto 0;width:100%;z-index:1000;";
|
|
host.replaceChildren(signing ? renderSigning(subOf("claude") || { agent: "claude" }) : "");
|
|
document.body.append(host);
|
|
};
|
|
startSignIn("claude", true);
|
|
});
|
|
const box = page.locator("#csign");
|
|
const said = (s, v) => page.evaluate(([s, v]) => t(s, v), [s, v]);
|
|
const translated = async (s, v) => {
|
|
const got = await said(s, v);
|
|
if (lang !== "en") assert.notEqual(got, s.replace("{cmd}", "claude setup-token"), `${s} isn't in ${lang}`);
|
|
return got;
|
|
};
|
|
|
|
// the browser sign-in offers the token
|
|
await page.waitForFunction(() => signing?.state === "waiting");
|
|
const offer = box.getByRole("button", { name: await translated("Use a setup-token instead…") });
|
|
await offer.click();
|
|
await page.waitForFunction(() => signing?.state === "token");
|
|
assert.deepEqual(canceled, ["c1"]);
|
|
const text = await box.innerText();
|
|
assert.ok(text.includes(await translated("Add a Claude account by setup-token")), text);
|
|
assert.ok(text.includes(await translated("Paste the token {cmd} printed. magpie gives it to Claude Code for this account's requests only; Claude Code itself stays signed in as it is.", { cmd: "claude setup-token" })), text);
|
|
assert.ok(text.includes(await translated("A setup-token can't tell its email or allowance: the account is named by the token, and its allowance shows once a reply tells it.")), text);
|
|
const field = box.locator(".claude-token input");
|
|
const add = box.locator('.claude-token button[type="submit"]');
|
|
assert.equal(await field.getAttribute("type"), "password");
|
|
assert.equal(await add.isDisabled(), true);
|
|
assert.equal(await box.locator("select").count(), 0);
|
|
// inside the sheet at 420px
|
|
const w = await box.evaluate((root) => Math.max(...[...root.querySelectorAll("*")].map((e) => e.getBoundingClientRect().right)));
|
|
assert.ok(w <= 420, `sheet reaches ${w}px`);
|
|
|
|
// a refused token says why and stays
|
|
await field.fill("sk-ant-api03-key");
|
|
await add.click();
|
|
await page.waitForFunction(() => document.querySelector("#csign .claude-token .why")?.textContent);
|
|
assert.match(await box.locator(".why").textContent(), /API key/);
|
|
assert.equal(await page.evaluate(() => signing?.state), "token");
|
|
|
|
// a setup-token is added
|
|
await box.locator(".claude-token input").fill(TOKEN);
|
|
await box.locator('.claude-token button[type="submit"]').click();
|
|
await page.waitForFunction(() => window.signedInWith.length === 1);
|
|
assert.deepEqual(tokens, ["sk-ant-api03-key", TOKEN]);
|
|
assert.deepEqual(await page.evaluate(() => window.signedInWith), [{ agent: "claude", user: "Claude setup-token 3204d0", token: true }]);
|
|
assert.equal(await box.innerText().then((s) => s.includes(TOKEN)), false);
|
|
|
|
// its row's Use switches Claude Code itself to it (liukaybee on X)
|
|
const rowsOf = (uses) => page.evaluate((uses) => {
|
|
document.getElementById("accts-under-test")?.remove();
|
|
const ls = [{ user: "pro@x.com", active: true, plan: "max" }, { user: "Claude setup-token 3204d0", on: true, token: true, uses }];
|
|
const a = { agent: "claude", agentName: "Claude Code", logins: ls };
|
|
const list = renderAccounts(a, { id: "claude", account: { agent: "claude", user: "pro@x.com" } });
|
|
list.id = "accts-under-test";
|
|
document.body.append(list);
|
|
const of = (u) => {
|
|
const r = list.querySelector(`[data-account-id="${u}"] .acc-line`);
|
|
return { text: r.innerText, buttons: [...r.querySelectorAll("button.text")].map((b) => ({ text: b.textContent, title: b.title })), spans: [...r.querySelectorAll(".using")].map((x) => ({ text: x.textContent, title: x.title })) };
|
|
};
|
|
return { pro: of("pro@x.com"), tok: of("Claude setup-token 3204d0") };
|
|
}, uses);
|
|
const use = await said("Use");
|
|
let rows = await rowsOf(false);
|
|
const tokUse = rows.tok.buttons.find((b) => b.text === use);
|
|
assert.ok(tokUse, JSON.stringify(rows.tok));
|
|
assert.equal(tokUse.title, await translated("Switch {agent} itself to this setup-token, in its settings.json; its own sign-in stays", { agent: "Claude Code" }));
|
|
assert.ok(!rows.tok.text.includes(await said("magpie only")), rows.tok.text);
|
|
assert.ok(!rows.pro.buttons.some((b) => b.text === use), JSON.stringify(rows.pro));
|
|
const posted = page.waitForRequest((r) => new URL(r.url()).pathname === "/api/login/switch");
|
|
await page.locator('#accts-under-test [data-account-id="Claude setup-token 3204d0"] button.text', { hasText: use }).click();
|
|
assert.deepEqual((await posted).postDataJSON(), { agent: "claude", user: "Claude setup-token 3204d0" });
|
|
|
|
// in use: said on it, and the signed-in account's Use switches back
|
|
rows = await rowsOf(true);
|
|
assert.deepEqual(rows.tok.spans.map((x) => x.text), [await said("In use")]);
|
|
assert.equal(rows.tok.spans[0].title, await translated("{agent} itself uses this setup-token, set in its settings.json", { agent: "Claude Code" }));
|
|
assert.ok(!rows.tok.buttons.some((b) => b.text === use), JSON.stringify(rows.tok));
|
|
assert.deepEqual(rows.pro.spans.map((x) => x.text), [await said("Signed in")]);
|
|
assert.equal(rows.pro.spans[0].title, await translated("{agent} is signed in to this account, but uses {user}'s setup-token over it", { agent: "Claude Code", user: "Claude setup-token 3204d0" }));
|
|
const back = rows.pro.buttons.find((b) => b.text === use);
|
|
assert.ok(back, JSON.stringify(rows.pro));
|
|
assert.equal(back.title, await translated("Switch {agent} itself back to this account", { agent: "Claude Code" }));
|
|
await translated("{agent} now uses {user}", { agent: "Claude Code", user: "pro@x.com" });
|
|
const w2 = await page.evaluate(() => Math.max(...[...document.querySelectorAll("#accts-under-test *")].map((e) => e.getBoundingClientRect().right)));
|
|
assert.ok(w2 <= 420, `accounts reach ${w2}px`);
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|