@hu_xiaomi on X asked whether magpie could pick up OpenCode Go's API key
by itself. OpenCode keeps what `opencode auth login` signs in to in
auth.json, in $XDG_DATA_HOME/opencode (~/.local/share/opencode when that
is unset, on Windows too). Its format and path were checked against
anomalyco/opencode v1.19.0, packages/opencode/src/auth/index.ts.
- OpenCode is now a source in the import picker (appReaders,
import_opencode.go). An "api" key comes in on the magpie preset with
the same id: opencode-go on OpenCode Go, opencode (Zen) on OpenCode
Zen, and a vendor's key (openai, anthropic, deepseek, openrouter, ...)
on that vendor's preset, the same way Alma's almaPresets work.
- auth.json keeps no address beside a key, so a key for a provider with
no preset is listed and skipped ("it has no base URL"). Sign-ins
(oauth: Claude Pro/Max, Copilot; wellknown: an enterprise server's
token) are listed and skipped with the reason the picker already
translates. No new GUI strings.
- OpenCode is read only when the user opens the picker. The Providers
page's automatic discovery leaves it out (DiscoverSources, the `asked`
flag), because the file holds every sign-in OpenCode has. magpie never
writes to it. OPENCODE_AUTH_CONTENT is not read.
- presetAt: the first preset at a host won even when a later one matched
the URL exactly. OpenCode Zen and Go share opencode.ai, so a Zen key
would have landed on Go. An exact match now wins.
- importSources: a source whose stat fails with anything but not-exist
(a folder magpie can't search) is found with an error, which means
unknown. Before, it read as "Not found".
- The picker's tab shows OpenCode's own mark (icons/opencode.svg).
- docs/reference.md describes the source.
Not added: an "use the key OpenCode has" offer in the OpenCode Go add
sheet, because no other app has one there (no sibling to follow).
Run (macOS, go1.26.8, worktree at dd25997a4 before the rebase):
- go test -tags nogui -timeout 40m ./...: every package ok
(63 ok, none failed; gateway 1806s, agent 1769s at load ~160-230)
- gofmt -l internal/provider internal/gui: clean
- go vet ./...; GOOS=linux go vet -tags nogui ./...;
GOOS=windows go vet ./...: clean
- GUI, playwright 1.62.0: import-opencode.test.cjs in chromium and webkit,
en/zh/zh-TW/ja/de, at 900 and 390px: 20/20. import-collision,
import-row, provider-discovery and key-import pass. login-import fails
in chromium (fill/click timeouts) at load ~230. It fails the same way
with HEAD's app.js (dd25997a4 = origin/main, just fetched), and #1307
already tracks it.
- Broken by hand, each went red and was then put back:
1. presetAt's old first-hit rule: TestImportOpenCodeAuth, Zen came in
as opencode-go-2.
2. no sign-in skip: TestImportOpenCodeAuth, anthropic's oauth entry.
3. stat errors read as not found: TestImportOpenCodeAuthUnknown,
unreadable folder.
4. discovery reads OpenCode: TestImportOpenCodeAuth and
TestOpenCodeWaitsForThePicker.
5. XDG_DATA_HOME ignored: TestImportOpenCodeAuth, TestImportOpenCodeAuthUnknown
and TestOpenCodeWaitsForThePicker.
6. tab icon left at app-opencode.png: import-opencode.test.cjs.
Fixtures are auth.json as OpenCode v1.19.0 writes it
(JSON.stringify(data, null, 2), 0600), under a sandbox HOME and
XDG_DATA_HOME. Not tried: a real OpenCode install with a real key, and
the Windows box (the path logic is the same there; GOOS=windows vet only).
87 lines
5.5 KiB
JavaScript
87 lines
5.5 KiB
JavaScript
// OpenCode in the import picker (@hu_xiaomi on X): its tab carries
|
|
// OpenCode's own mark, OpenCode Go's key is picked to come in on its
|
|
// preset, and a sign-in (Claude Pro/Max) is listed, said why, and can't be
|
|
// picked. All APIs and keys are fixtures.
|
|
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 = process.env.ASSET_DIR || path.resolve(__dirname, "../assets");
|
|
const engines = process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"];
|
|
const langs = ["en", "zh", "zh-TW", "ja", "de"];
|
|
|
|
const sources = () => [{
|
|
id: "opencode", name: "OpenCode", path: "/fixture/.local/share/opencode/auth.json", found: true,
|
|
items: [
|
|
{ ref: "opencode-go", from: "auth.json", fingerprint: "go", status: "new",
|
|
provider: { id: "opencode-go", name: "OpenCode Go", icon: "opencode", preset: "opencode-go", chat: "https://opencode.ai/zen/go/v1", key: "sk-g…7c" } },
|
|
{ ref: "anthropic", from: "auth.json", fingerprint: "ant", skip: "it is a sign-in, not a key; add the subscription in magpie",
|
|
provider: { name: "anthropic" } },
|
|
],
|
|
}];
|
|
|
|
for (const engine of engines) {
|
|
for (const lang of langs) {
|
|
for (const width of [900, 390]) {
|
|
test(`${engine} ${lang} ${width}px: OpenCode's key is picked, its sign-in is not`, async (t) => {
|
|
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
|
|
const page = await browser.newPage({ viewport: { width, height: 900 }, reducedMotion: "reduce" });
|
|
page.setDefaultTimeout(7000);
|
|
const errors = [], posts = [];
|
|
page.on("pageerror", (e) => errors.push(e.stack || e.message));
|
|
await page.route("**/*", async (route) => {
|
|
const req = route.request(), url = new URL(req.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 === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "light" } });
|
|
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true } });
|
|
if (url.pathname === "/api/groups") return json({ groups: [], pools: [] });
|
|
if (url.pathname === "/api/importapps/discovery") return json([]);
|
|
if (url.pathname === "/api/importapps") {
|
|
if (req.method() === "GET") return json(sources());
|
|
posts.push(req.postDataJSON());
|
|
return json({ added: ["OpenCode Go"], state: { providers: [], presets: [], excluded: [], gateway: { running: true, window: true } } });
|
|
}
|
|
if (url.pathname === "/api/gateway/trace") return new Promise(() => {});
|
|
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: "" }); }
|
|
});
|
|
t.after(async () => {
|
|
await browser.close();
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
await page.goto("http://magpie.test/?view=providers");
|
|
await page.waitForFunction(() => typeof openImportApps === "function");
|
|
await page.evaluate(() => openImportApps());
|
|
const picker = page.locator("#modal .importapps");
|
|
await picker.locator(".approw").first().waitFor();
|
|
const T = (s, v) => page.evaluate(([s, v]) => t(s, v), [s, v]);
|
|
|
|
// the tab's mark is OpenCode's, and it loads
|
|
const logo = picker.locator(".apptab .applogo");
|
|
assert.match(await logo.getAttribute("src"), /icons\/opencode\.svg$/);
|
|
await page.waitForFunction(() => document.querySelector("#modal .apptab .applogo").complete);
|
|
assert((await logo.evaluate((img) => img.naturalWidth)) > 0, "OpenCode's mark loads");
|
|
|
|
const go = picker.locator(".approw", { hasText: "OpenCode Go" });
|
|
const ant = picker.locator(".approw", { hasText: "anthropic" });
|
|
assert.equal(await go.locator('input[type="checkbox"]').isChecked(), true);
|
|
assert.equal(await ant.locator('input[type="checkbox"]').isDisabled(), true);
|
|
assert.equal(await ant.locator(".sub", { hasText: await T("it is a sign-in, not a key; add the subscription in magpie") }).count(), 1);
|
|
assert.equal(await ant.locator(".apptag").textContent(), await T("Can't import"));
|
|
if (lang !== "en") assert.notEqual(await T("it is a sign-in, not a key; add the subscription in magpie"), "it is a sign-in, not a key; add the subscription in magpie");
|
|
|
|
const fits = await picker.evaluate((ed) => [...ed.querySelectorAll(".approw")].every((r) => r.scrollWidth <= r.clientWidth + 1) && ed.getBoundingClientRect().right <= innerWidth);
|
|
assert(fits, "rows fit the window");
|
|
|
|
await picker.locator(".bar button.primary").click();
|
|
await page.waitForFunction(() => !importingApps);
|
|
assert.deepEqual(posts, [{ picks: [{ source: "opencode", ref: "opencode-go", mode: "add" }] }]);
|
|
});
|
|
}
|
|
}
|
|
}
|