Files
magpie/internal/gui/tests/plugin-import.test.cjs
T
yetone 04776ec6cb providers: a plugin subscription imports accounts from a file through its own auth.import, WorkBuddy's from 0.2.32 (marchen_cc on X)
marchen_cc on X: 「从文件导入账号」 took no WorkBuddy sign-in file. The cause
was not WorkBuddy's: magpie's import reached only built-ins (Codex,
Claude, Antigravity, Factory), and a plugin had no way to say it could
read a file. This adds that for every plugin subscription:

- A plugin's auth may have import(texts, {signal}), returning sign-in
  results, with importFrom (and importFroms by language) saying which
  file to choose. The host's importFiles op keeps each one as a sign-in
  is kept (keptAs, saved under signInKey, settled), so an account
  already there comes back "updated"; it is cancellable (signInSteps)
  and takes at most 100.
- /api/signin/import and `magpie accounts import <plugin> <files>` go
  to provider.ImportPluginFiles when PluginImportable(id); the editor's
  「从文件导入账号…」 row and the import sheet show for such a plugin, in
  its own words (importSay), moved subscriptions included (subOf).
- docs/subsystems/provider-plugins.md: "Accounts from a file".
- WorkBuddy's mover min goes to 0.2.32, which reads the desktop app's
  .info sign-in or a JSON credential, and sends a cloud-hosted
  enterprise account (*.sso.copilot.tencent.com, *.copilot.qq.com,
  *.sso.codebuddy.cn, iOA apart) to its enterprise's address (emilochen
  on X), published and checked with npm view --prefer-online.
- TestCoreScriptBudget +468 for the import branch, packed to one
  statement. importSay is read as the Providers rows draw, so it has
  no lazy page; nothing else was moved out (#2710). The budget's
  arithmetic was checked over this tree: 1,267,762, at the new cap.

Run on mote (mica's disk is full; mote over its cores) at this change
on 3f6b1b0c9; ac1702c33 (the library plugin and its GUI test, no core
script) landed after, and the core budget's arithmetic was redone at
this head:
gofmt -l clean; go vet darwin, GOOS=linux -tags nogui, GOOS=windows on
internal/{gui,plugin,provider,cli}; go test -count=1 of those four:
all ok. GUI (chromium + webkit, en and zh): plugin-import,
login-import, factory-key, plugin-moved-signin, i18n-keys: 23 pass, 1 fail: i18n-keys "a plugin's words are its own",
memory_mcp.go's "No folder", red on main since 72facf72e (#6422).
Plugins repo: workbuddy import.test.mjs 6 of 6, bun scripts/check.mjs
ok, root bun test 8534 pass / 1 fail (agy-cli smoke under load, green on a rerun: 8535 pass, 0 fail;
unrelated, plugins #1070).

Broken by hand: without the host's importFiles handler,
TestPluginImportFiles goes red; without importable in pluginSubs,
plugin-import.test.cjs goes red ("WorkBuddy lists no import").

Not tried: a real WorkBuddy desktop .info file or a real enterprise
account on a cloud-hosted domain; the fixtures follow WorkBuddy's own
client source. Not tried on Windows or Linux beyond vet.
2026-11-13 13:32:32 +08:00

98 lines
5.5 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A plugin subscription takes accounts from a file when its plugin has
// auth.import (marchen_cc on X: WorkBuddy's had no "Import accounts from
// a file…"). A moved built-in (WorkBuddy) and a plugin's own provider
// both list it, with the plugin's own words for which file, in the
// window's language; a plugin with no import lists none. English and
// Chinese; 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 { chromium, webkit } = require("playwright");
const assets = path.resolve(__dirname, "../assets");
const from = { en: "Choose WorkBuddy's sign-in file", zh: "选择 WorkBuddy 的登录文件" };
const plugin = (id, name, importable) => ({
id, pid: id, name, icon: "generic", spec: "@magpie-community/opencode-" + id + "-auth", signedIn: true, models: 1,
methods: [{ type: "oauth", label: name + " (browser)" }],
...(importable ? { importable: true, importFrom: from.en, importFroms: { zh: from.zh } } : {}),
});
const plugins = [plugin("workbuddy", "WorkBuddy", true), plugin("acme", "Acme", true), plugin("plain", "Plain", false)];
const row = (id, name) => ({ id, name, icon: "generic", chat: "plugin://" + id + "/v1", models: [], agents: [], key: {},
account: { agent: id, agentName: name, agentIcon: "generic", user: "a@" + id, logins: [{ user: "a@" + id, active: true, on: true }] } });
function server(lang, posted) {
const payload = () => ({
providers: [row("workbuddy", "WorkBuddy"), row("acme", "Acme"), row("plain", "Plain")],
presets: [], excluded: [], gateway: { running: true, window: true }, plugins, onPlugins: ["workbuddy"],
});
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:false};` });
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: "light" } });
if (url.pathname === "/api/providers") return json(payload());
if (url.pathname === "/api/groups") return json({ groups: [], pools: [] });
if (url.pathname === "/api/signin/import") {
posted.push(route.request().postDataJSON());
return json({ providers: payload(), results: [{ user: "b@workbuddy", status: "added" }] });
}
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)];
try { await route.fulfill({ body: await fs.readFile(file), contentType }); } catch { await route.fulfill({ status: 404, body: "" }); }
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
test(engine + ": a plugin subscription imports from a file", async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
for (const lang of ["en", "zh"]) {
await t.test(lang, async () => {
const page = await (await browser.newContext({ viewport: { width: 900, height: 800 }, reducedMotion: "reduce" })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [], posted = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", server(lang, posted));
await page.goto("http://magpie.test/?view=providers");
const T = (s, v) => page.evaluate(([s, v]) => t(s, v), [s, v || {}]);
const imp = await T("Import accounts from a file…");
const ed = page.locator("#modal .editor");
const open = async (name) => {
await page.locator(".row.provider", { hasText: name }).first().click();
await ed.locator(".accts").waitFor();
};
const importRow = () => ed.locator(".accts .acc.add", { hasText: imp });
for (const name of ["WorkBuddy", "Acme"]) {
await open(name);
assert.equal(await importRow().count(), 1, name + " lists no import");
assert.equal(await importRow().getAttribute("title"), from[lang]);
await page.keyboard.press("Escape");
await ed.waitFor({ state: "detached" });
}
await open("Plain");
assert.equal(await importRow().count(), 0, "a plugin with no import lists one");
await page.keyboard.press("Escape");
await ed.waitFor({ state: "detached" });
// WorkBuddy's: the plugin's words, what is pasted posted as it is
await open("WorkBuddy");
await importRow().click();
const box = page.locator(".signing.import");
await box.waitFor();
assert.equal(await box.locator(".tt > .s").first().textContent(), from[lang]);
await box.locator("textarea").fill('{"auth":{}}');
await box.locator("button.primary").click();
await box.locator(".results").waitFor();
assert.deepEqual(posted, [{ agent: "workbuddy", files: ['{"auth":{}}'] }]);
assert.deepEqual(errors, []);
});
}
});
}