Files
magpie/internal/gui/tests/codex-access-only.test.cjs
T
yetone ffc69f0244 codex: an account can keep its access token only, never renewed (EastonXvii on X)
A Codex account can now be imported, or ticked later, as "Access token
only". magpie uses the access token it was given and never refreshes it.
The refresh token is never spent or rotated, so another computer signed
in to the same ChatGPT account stays signed in.

- Every renewal of a saved account goes through renewSaved. For an
  access-only account it calls accessOnlyToken, which hands out the
  token until it runs out and never calls codexRefresh. That covers
  requests, usage reads, the idle read, models, resets, warm-up, the
  401 recovery and the keep-alive, which also skips such accounts.
- Once the token runs out, the account is marked Lapsed with
  refreshRefused. The provider editor shows "Access expired" and
  "Import again", which opens the import with the tick set. A new
  import or sign-in replaces the entry and clears both.
- Such an account is never switched into Codex, since Codex would
  renew it. That applies to switchable, switchSavedLogin, NextLogin,
  keep-as and signInToFirst. The live or held account can't be made
  access-only (errAccessOnlyHeld). Its row says "magpie only".
- Import: ImportCodexAccessOnly accepts files that have only an access
  token, refuses an expired token or a missing id_token, and makes no
  refresh call. CLI: `magpie accounts import codex --access-only`,
  `magpie accounts access-only codex <email> [on|off]`. GUI: the
  import box tick, the row tick and POST /api/login/access-only|renewed.
- The core scripts stay within budget: app.js is smaller than main's.
  This change's own comments and nine sign-in/import comment blocks were
  cut, with no raise.

Run (on mote):
- gofmt -l clean; go vet for darwin, linux (nogui) and windows ok.
- go test ./internal/provider ./internal/cli ./internal/gui ok.
- GUI files in chromium and webkit, en and zh: codex-access-only (new),
  login-import, account-return, i18n-keys, account-row-line,
  agent-switch-accounts and signin-paste-import: 64/64 at the final
  tree. In an earlier grouped run, signin-paste-import failed once on its
  Import button's wait. It then passed 6/6 alone and in the rerun.

Broken by hand:
- With the renewSaved branch and the keep-alive skip taken out,
  TestCodexAccessOnlyNeverRefreshed went red: the keep-alive renewed
  the account. TestCodexAccessOnlyRunOutLapses went red too: a run-out
  token was handed out.
- With the branch and the import path taken out, a forced renewal went
  through and an expired-token import was "added". Both tests went red.

Not tried: a real ChatGPT account, so I did not check that another
computer stays signed in. No Windows run, since nothing here differs by
OS.
2026-11-10 09:49:30 +08:00

136 lines
8.0 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// EastonXvii on X: a ChatGPT account can be kept "access token only", so
// magpie never renews its sign-in and another computer signed in to it
// stays signed in. The import box offers it for ChatGPT and says the
// source stays signed in; the Import posts it. A saved account that isn't
// the one Codex is on offers it under ⋯ and posts login/access-only; one
// kept so says "magpie only" in place of Use, and once its token has run
// out, "Access expired" with Import again, which opens the import with it
// ticked. In English and Chinese, Chromium and WebKit, every string in
// every language.
const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const path = require("node:path");
const { test } = require("node:test");
const { chromium, webkit } = require("playwright");
const assets = path.resolve(__dirname, "../assets");
const file = '{"auth_mode":"chatgpt","tokens":{"access_token":"a","id_token":"i"}}';
const lapse = "kept's access token has expired, and it is kept access token only, so magpie doesn't renew it — import it again, or sign in to it again";
const account = () => ({
id: "codex", name: "ChatGPT", icon: "openai", chat: "", responses: "", anthropic: "", catalog: "",
models: [{ id: "gpt-6", name: "GPT-6", on: true }], agents: [], fallback: [], headers: {}, keyList: [],
account: {
agent: "codex", agentName: "Codex", user: "live@example.com", plan: "plus", logins: [
{ user: "live@example.com", plan: "plus", active: true, on: true },
{ user: "spare@example.com", plan: "plus", on: true },
{ user: "kept@example.com", plan: "plus", on: true, accessOnly: true },
{ user: "gone@example.com", plan: "plus", on: true, accessOnly: true, lapsed: lapse },
],
},
});
function serve(lang, posted) {
const providers = { providers: [account()], presets: [], excluded: [], gateway: { running: true, window: true } };
const state = { agents: [], profiles: [], settings: { lang, theme: "light" } };
return async (route) => {
const url = new URL(route.request().url());
const json = (data) => route.fulfill({ json: data });
if (url.pathname === "/boot.js") return route.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",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(state);
if (url.pathname === "/api/providers") return json(providers);
if (url.pathname === "/api/groups") return json({ groups: [] });
if (route.request().method() === "POST" && (url.pathname === "/api/signin/import" || url.pathname.startsWith("/api/login/"))) {
posted.push([url.pathname, route.request().postDataJSON()]);
if (url.pathname === "/api/signin/import") return json({ providers, results: [{ user: "new@example.com", status: "added", plan: "plus" }] });
return json(providers);
}
if (url.pathname.startsWith("/api/")) return json({});
const f = 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(f)];
await route.fulfill({ body: await fs.readFile(f), contentType });
};
}
const strings = [
"Access expired", "Import again", "Access token only",
"Kept access token only: requests through magpie use it until it expires; Codex itself isn't signed in to it, as Codex would renew it",
"magpie never renews this sign-in, so another computer signed in to it stays signed in. Once its access token expires, import it again.",
"Each account is kept with the file's access token and never renewed, so the tool it came from stays signed in. Once that token expires, import it again.",
];
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
const open = async (t, posted) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await (await browser.newContext({ viewport: { width: 900, height: 760 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", serve(lang, posted));
await page.goto("http://magpie.test/?view=providers");
await page.locator(".row.provider", { hasText: "ChatGPT" }).click();
await page.locator(".editor .acc").first().waitFor();
const T = (s) => page.evaluate((s) => t(s), s);
return { page, errors, T };
};
test(`${engine} ${lang}: the import keeps ChatGPT accounts access token only when ticked`, async (t) => {
const posted = [];
const { page, errors, T } = await open(t, posted);
const missing = await page.evaluate((ks) => ks.flatMap((k) => ["zh", "zh-TW", "ja", "de"].filter((l) => !I18N[l][k]).map((l) => l + ": " + k)), strings);
assert.deepEqual(missing, [], "every string in every language");
await page.locator(".editor .accts .acc.add").nth(1).click();
const box = page.locator(".signing.import");
await box.waitFor();
const tick = box.locator("label.import-access-only");
assert.equal(await tick.locator("span").textContent(), await T("Access token only"));
const spent = box.locator(".tt > .s").nth(1);
assert.match(await spent.textContent(), lang === "en" ? /signed out of that account/ : /退出该账号/);
await tick.locator("input").check();
assert.equal(await spent.textContent(), await T(strings[5]));
await box.locator("textarea").fill(file);
await box.locator("button.primary").click();
await box.locator(".results").waitFor();
assert.deepEqual(posted, [["/api/signin/import", { agent: "codex", files: [file], accessOnly: true }]]);
assert.deepEqual(errors, []);
});
test(`${engine} ${lang}: a saved account is kept access token only from its row`, async (t) => {
const posted = [];
const { page, errors, T } = await open(t, posted);
const row = (u) => page.locator(".editor .acc", { hasText: u });
// the one Codex is on doesn't offer it; a spare does, under ⋯
assert.equal(await row("live@example.com").locator(".acc-access-only").count(), 0);
const spare = row("spare@example.com");
await spare.locator(".acc-more").click();
const tick = spare.locator(".acc-tools .acc-access-only");
assert.equal(await tick.locator("input").isChecked(), false);
// clicked, not check(): the fake's answer draws it unticked again
await tick.locator("input").click();
for (let i = 0; i < 100 && !posted.length; i++) await page.waitForTimeout(20);
assert.deepEqual(posted[0], ["/api/login/access-only", { agent: "codex", user: "spare@example.com" }]);
// kept so: "magpie only", never Use, and its tick shows on its line
const kept = row("kept@example.com");
assert.equal(await kept.locator(".acc-line .using").textContent(), await T("magpie only"));
assert.equal(await kept.locator(".acc-line button.text:not(.quiet)").count(), 0, "no Use");
assert.equal(await kept.locator(".acc-line .acc-access-only input").isChecked(), true);
// run out: Access expired, and Import again opens the import ticked
const gone = row("gone@example.com");
assert.equal(await gone.locator(".acc-line .using").textContent(), await T("Access expired"));
await gone.getByRole("button", { name: await T("Import again") }).click();
const box = page.locator(".signing.import");
await box.waitFor();
assert.equal(await box.locator("label.import-access-only input").isChecked(), true);
assert.deepEqual(errors, []);
});
}
}