An OpenCode plugin's "api" auth method with its own prompts and an authorize (inkstone's API key ways, Devin's session token in magpie-community/plugins#928) asks the key in its prompts and returns it from authorize(inputs). The GUI asked those prompts and then always showed a key field of its own, so the user typed the same token twice and the second entry went nowhere. The host's save() also copied every answer into the account's metadata, so the token sat there a second time in plain text. - host.js marks such a method keyAsked (type api, prompts, authorize). The GUI's startPluginSignIn signs in with no key field for it, as for keyless, and a rejected answer asks the prompts again on Try again. PluginAPIKey and `magpie plugin` login skip the empty-key check for it (Method.NoKey). - save() keeps only select answers (choices, never secrets) beside the authorize result's own metadata. A method with no authorize still keeps every answer, as OpenCode does. - An authorize that gives no key, with no key typed, is now an error instead of an "ok" that saved nothing. OpenCode itself differs: its v1 CLI (providers.ts) asks the prompts, then always asks "Enter your API key", saves result.key ?? the typed key, and merges the answers into metadata; its TUI never calls authorize for api methods. magpie-community plugins are written for magpie's flow and read the key from inputs, so this follows them. Plugins that read prompt answers back from metadata (grep of magpie-community/plugins packages): zcode (site, a select), joycode (tenant) and agnes (keyProfile, consoleToken). None has an authorize, so none changes. Every api+authorize plugin (inkstone, cloudflare-workers-ai, sensenova, agnes-code, astudio, sap-aicore, Devin #928) puts what it needs in its result's metadata itself; sensenova's apiKey: undefined workaround is now moot. Run on mote (macOS 27, Go 1.26.8, Playwright 1.62.0) at this change on origin/main9c39edb92: gofmt -l clean; go vet of internal/plugin/... internal/gui/... internal/provider internal/cli, also GOOS=windows and GOOS=linux -tags nogui: ok. go test ./internal/plugin/... ./internal/gui/... ./internal/provider: ok. ./internal/cli: ok but TestEveryRouteHasACommandOrIsNamed, red the same alone on9c39edb92(POST /api/gateway/pause frome8fbee60ehas no command), filed as #5946. node --test, chromium and webkit, of plugin-api-prompt-key (new), plugin-keyless-signin, plugin-password-prompt, plugin-signin, plugin-ask-ways, plugin-key-hint, plugin-other-ways, signin-paste-key, plugin-signin-opened, plugin-signin-scrolls, plugin-pick-sub, plugin-two-subs, zcode-site-layout and i18n-keys: 176 of 176 ok. The new GUI test passed 3 of 3 alone. plugin-signin-no-url is red 22 of 22 at the base too (987eac0aa); that is #5938, commented with the macOS run. Broken by hand: GUI back to keyless-only and host without keyAsked -> TestAPIPromptsKeyAskedAndMetadata and plugin-api-prompt-key 22 of 22 red; save() copying every answer again -> the Go test red (token kept twice, the note answer kept). Not tried: a real plugin sign-in (inkstone, Devin), the CLI path (no test), a run on the Windows or Linux boxes, the full ./... suite.
140 lines
7.5 KiB
JavaScript
140 lines
7.5 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// An "api" sign-in method whose own question asks the key (keyAsked: it has
|
|
// prompts and an authorize that turns the answers into the key, as Devin's
|
|
// session token and inkstone's API key ways do) is signed in with the
|
|
// answer alone: no second key field after the question, whose entry went
|
|
// nowhere. A rejected answer is asked again on Try again. An "api" way
|
|
// with a question but no keyAsked still asks its key after it. Every
|
|
// language, both engines, 1100 and 420 wide; 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 { reach } = require("./reader.cjs");
|
|
|
|
const assets = path.resolve(__dirname, "../assets");
|
|
|
|
const plugin = { id: "fakeco", pid: "fakeco", name: "FakeCo", icon: "generic", spec: "opencode-fakeco-auth", signedIn: false, models: 3,
|
|
methods: [{ type: "api", label: "FakeCo session token", keyAsked: true }, { type: "api", label: "FakeCo site key" }] };
|
|
|
|
function server(lang, asked, state) {
|
|
return async (route) => {
|
|
const url = new URL(route.request().url());
|
|
const json = (data) => route.fulfill({ json: data });
|
|
const body = () => route.request().postDataJSON();
|
|
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({ providers: [{ id: "openai", name: "OpenAI", icon: "openai", preset: "openai", models: [], agents: [], key: { set: true, masked: "sk-…ab12" } }], presets: [], excluded: [], gateway: { running: true, window: true }, plugins: [plugin] });
|
|
if (url.pathname === "/api/groups") return json({ groups: [], pools: [] });
|
|
if (url.pathname === "/api/gateway/trace") return json({ routes: [] });
|
|
if (url.pathname === "/api/plugin-signin/prompt") {
|
|
const b = body();
|
|
asked.push(["prompt", b]);
|
|
const inputs = { ...(b.inputs || {}) };
|
|
if (b.key) inputs[b.key] = b.value;
|
|
if (b.method === 0 && !inputs.token) return json({ prompt: { type: "password", key: "token", message: "Your FakeCo session token", placeholder: "fs-..." }, inputs });
|
|
if (b.method === 1 && !inputs.site) return json({ prompt: { type: "text", key: "site", message: "Your FakeCo site" }, inputs });
|
|
return json({ prompt: null, inputs });
|
|
}
|
|
if (url.pathname === "/api/plugin-signin") {
|
|
const b = body();
|
|
asked.push(["signin", b]);
|
|
if (b.inputs?.token === "fs-bad") return route.fulfill({ status: 400, json: { error: "that token is revoked" } });
|
|
state.signed = true;
|
|
return json({ agent: "fakeco", state: "done", user: "fakeco", using: true });
|
|
}
|
|
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: "" }); }
|
|
};
|
|
}
|
|
|
|
async function open(page, way) {
|
|
await page.goto("http://magpie.test/?view=providers");
|
|
await page.locator("#addProvider").click();
|
|
const sheet = page.locator("#addSheet");
|
|
const tile = sheet.locator('.tile[data-pick="FakeCo"]');
|
|
await reach(page, tile);
|
|
await tile.click();
|
|
const box = sheet.locator(".signing");
|
|
const b = box.locator("button", { hasText: way });
|
|
await reach(page, b);
|
|
await b.click();
|
|
return box;
|
|
}
|
|
|
|
// until waits for what the fake API was sent, not for the page: the
|
|
// field goes as soon as it is answered, before the sign-in is posted
|
|
async function until(ok, what) {
|
|
for (const end = Date.now() + 5000; !ok(); ) {
|
|
if (Date.now() > end) throw new Error("waited for " + what);
|
|
await new Promise((r) => setTimeout(r, 20));
|
|
}
|
|
}
|
|
|
|
async function answer(page, box, label, value) {
|
|
const f = box.getByLabel(label, { exact: true });
|
|
await f.waitFor();
|
|
await reach(page, f);
|
|
await f.fill(value);
|
|
await f.press("Enter");
|
|
}
|
|
|
|
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
|
|
test(engine + ": an api way whose question asks the key asks no second key", async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
t.after(() => browser.close());
|
|
for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) {
|
|
for (const width of [1100, 420]) {
|
|
await t.test(`${lang} ${width}`, async () => {
|
|
const ctx = await browser.newContext({ viewport: { width, height: 800 } });
|
|
const page = await ctx.newPage();
|
|
page.setDefaultTimeout(5000);
|
|
page.setDefaultNavigationTimeout(60000);
|
|
const errors = [], asked = [], state = {};
|
|
page.on("pageerror", (e) => errors.push(e.message));
|
|
await page.route("**/*", server(lang, asked, state));
|
|
let box = await open(page, "FakeCo session token");
|
|
|
|
// a rejected token: the sign-in is sent with the answer alone,
|
|
// and Try again asks the question again
|
|
await answer(page, box, "Your FakeCo session token", "fs-bad");
|
|
await box.locator(".s", { hasText: "that token is revoked" }).waitFor();
|
|
assert.equal(await box.getByLabel("FakeCo session token", { exact: true }).count(), 0, "a key field after the question");
|
|
const again = box.locator("button.primary");
|
|
await reach(page, again);
|
|
await again.click();
|
|
await answer(page, box, "Your FakeCo session token", "fs-good");
|
|
await until(() => state.signed, "the second sign-in");
|
|
const signins = asked.filter(([k]) => k === "signin").map(([, b]) => b);
|
|
assert.deepEqual(signins, [
|
|
{ provider: "fakeco", method: 0, inputs: { token: "fs-bad" }, key: "" },
|
|
{ provider: "fakeco", method: 0, inputs: { token: "fs-good" }, key: "" },
|
|
]);
|
|
assert.ok(state.signed);
|
|
|
|
// a way with a question and no keyAsked still asks its key
|
|
await page.keyboard.press("Escape");
|
|
await page.locator("#modal").waitFor({ state: "hidden" });
|
|
box = await open(page, "FakeCo site key");
|
|
await answer(page, box, "Your FakeCo site", "eu");
|
|
const key = box.getByLabel("FakeCo site key", { exact: true });
|
|
await key.waitFor();
|
|
assert.equal(await key.getAttribute("type"), "password");
|
|
assert.equal(asked.filter(([k]) => k === "signin").length, 2, "signed in before the key was typed");
|
|
await key.fill("sk-site");
|
|
await key.press("Enter");
|
|
await until(() => asked.filter(([k]) => k === "signin").length === 3, "the site key's sign-in");
|
|
assert.deepEqual(asked.filter(([k]) => k === "signin").at(-1)[1], { provider: "fakeco", method: 1, inputs: { site: "eu" }, key: "sk-site" });
|
|
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth));
|
|
assert.deepEqual(errors, []);
|
|
await ctx.close();
|
|
});
|
|
}
|
|
}
|
|
});
|
|
}
|