A relay's "Add to magpie" link can now name the URL that tells a key its balance, where the amount sits in the reply, and GET or POST. magpie takes it only on the host of one of the link's own base URLs, over https, with no userinfo or fragment, so a link can't send the key anywhere its requests don't already go; anything else refuses the whole link. balance_path or balance_method without balance is an error. No header or token is taken from a link: an account-wide token is worth more than a key, and a URL stays in browser history. - provider.ParseImport: linkBalance checks and sets BalanceURL, BalancePath and BalanceMethod. - CLI: the import preview shows the balance URL and doesn't ask it before yes (showProviderAsking). - GUI: the import dialog shows method, URL and field (.imp-bal). - Docs: docs/import in en, zh and ja (table rows, a note on headers, the builder's three fields with the same host check, a security bullet); docs/reference.md's parameter table. Run on mote at this tree (rebased on8fa3c60f8): - go test -count=1 ./internal/provider ./internal/gui: ok (TestParseImportBalance, TestCoreScriptBudget in it). - go test ./internal/cli: ok but TestEveryRouteHasACommandOrIsNamed, red on main sincef418edffc(GET /api/context/session); fixed in the next commit, and ok there with -run 'TestEveryRoute…|Import' (TestImportPreviewAsksNoBalance). - GUI import-link-balance, import-link-gemini, i18n-keys: 43/44. The red is i18n-keys "a plugin's words are its own", the same three strings on an origin/main8fa3c60f8copy (#5327, #5426, #5482). - GOOS=linux go vet -tags nogui and GOOS=windows go vet of provider, cli, gui: ok. gofmt -l: clean. Not tried: a real link with balance= imported into the app and read against a real relay's balance endpoint.
73 lines
4.7 KiB
JavaScript
73 lines
4.7 KiB
JavaScript
// A magpie://import link naming its balance query (balance=, balance_path=,
|
|
// balance_method=): the dialog shows where the balance will be asked, the
|
|
// dialog asks nothing of it before Add, and Add saves the query with the
|
|
// provider. 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 linked = {
|
|
id: "bal-relay", name: "Bal Relay", chat: "https://bal-relay.example/v1", key: "sk-fixture-bal", models: ["gpt-6"],
|
|
balanceURL: "https://bal-relay.example/api/usage/token/a-rather-long-path-for-a-narrow-window", balancePath: "data.total_available", balanceMethod: "POST",
|
|
};
|
|
|
|
for (const engine of engines) {
|
|
for (const lang of langs) {
|
|
for (const width of [1100, 420]) {
|
|
test(`${engine} ${lang} ${width}px: a link's balance query is shown, and saved with Add`, 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);
|
|
page.setDefaultNavigationTimeout(60000);
|
|
const errors = [], saves = [], foreign = [];
|
|
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 });
|
|
const state = { providers: [], presets: [], excluded: [], gateway: { running: true, window: true } };
|
|
if (url.host !== "magpie.test") { foreign.push(req.url()); return route.abort(); }
|
|
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(state);
|
|
if (url.pathname === "/api/groups") return json({ groups: [], pools: [] });
|
|
if (url.pathname === "/api/import/bal1") return json({ provider: linked });
|
|
if (url.pathname === "/api/provider/save") { saves.push(req.postDataJSON()); return json(state); }
|
|
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 () => {
|
|
if (process.env.ARTIFACT_DIR) {
|
|
await fs.mkdir(process.env.ARTIFACT_DIR, { recursive: true });
|
|
await page.screenshot({ path: path.join(process.env.ARTIFACT_DIR, `${engine}-${lang}-${width}-import-link-balance.png`) });
|
|
}
|
|
await browser.close();
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
await page.goto("http://magpie.test/?view=providers&import=bal1");
|
|
const ed = page.locator(".editor.import");
|
|
await ed.waitFor();
|
|
const bal = ed.locator("code.imp-bal");
|
|
assert.equal(await bal.textContent(), `POST ${linked.balanceURL} data.total_available`);
|
|
const label = await page.evaluate(() => t("Balance URL"));
|
|
assert(await ed.getByText(label, { exact: true }).isVisible(), "labelled as the editor labels it");
|
|
const fits = await ed.evaluate((e) => e.getBoundingClientRect().right <= innerWidth && e.scrollWidth <= e.clientWidth + 1);
|
|
assert(fits, "the dialog fits the window");
|
|
assert(await bal.evaluate((e) => e.getBoundingClientRect().right <= e.closest(".editor").getBoundingClientRect().right), "the URL wraps inside the dialog");
|
|
await ed.locator(".bar button.primary").click();
|
|
await page.waitForFunction(() => !importing);
|
|
assert.equal(saves.length, 1);
|
|
assert.deepEqual([saves[0].balanceURL, saves[0].balancePath, saves[0].balanceMethod], [linked.balanceURL, "data.total_available", "POST"]);
|
|
assert.deepEqual(foreign, [], "nothing asked of another host");
|
|
});
|
|
}
|
|
}
|
|
}
|