Files
magpie/internal/gui/tests/search-signs-in.test.cjs
T
yetone c8eb7b3877 search: a search API's keys are a list, each switched off, moved and removed on its own
Web search took one key per API in one field (辰林鸭~ on Discord: only one
Tavily key). Settings › Models › Web search now lists an API's keys under
it as a provider's accounts are: each masked on its own row with a tick
(off / on), Show, Copy and Remove; several pasted at once (commas,
semicolons, lines), the reply saying how many were new and how many it
had; the order changed by dragging or Alt+↑/↓. A resting key says why and
until when on its row ("asked last until 14:05: rate limited"), from the
gateway's rest (SearchKeyRest, SearchQuota.Ref); rotation on 401/403,
402/432/433 and 429 (#1477) is unchanged.

provider.SearchAPI keeps Key as the keys on, comma-joined, so the gateway
and an older magpie never ask one switched off; keyList is written only
while a key is off. A Key that differs from the list (an older magpie, a
sync) wins, its keys on in its order, the list's off keys after. The last
key on can't be switched off or removed. The GUI names a key by
SearchKeyRef (a hash): POST /api/settings/search-keys (add, ref+on,
ref+remove, arrange) and /api/settings/search-key with ref. Saving an API
with no key keeps its keys, off ones too. Backup and WebDAV sync carry
the off keys, or leave them out with the rest. `magpie search` says
"+N off". docs/subsystems/gateway-routing.md has "The keys as a list".

Run (mote, at this tree, base 3441035a2):
- go test -count=1 ./internal/gui ./internal/provider ./internal/backup
  ./internal/davsync ./internal/cli: ok but gui's
  TestPanelHiddenByWailsMarksPageOutOfSight (#5322, mote, untouched) and
  cli's TestEveryRouteHasACommandOrIsNamed, then fixed (search-keys named
  S2) and run again: ok. ./internal/gateway search tests ok earlier.
- node --test search-key-list, search-keys, search-api, search-signs-in,
  search-order: 84/84, Chromium and WebKit, en/zh/zh-TW/ja/de, 1100 and
  420px (search-api/order at their own widths). i18n-keys: only "a
  plugin's words are its own" red, as on main (#5332, #5426, #5482).
- go build ./..., go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./...: ok. gofmt -l: clean.

Broken by hand, each red then restored:
- SetSearchAPI drops KeyList: provider and gui tests red.
- backup clears Key only / davsync merges Key only: their tests red.
- off keys still put in Key: provider, gui, backup and gateway red.
- the whole key in /api/state: TestSearchKeyListFromSettings red.
- no last-key-on check: TestSearchKeyList red.
- settings.js at HEAD: search-key-list.test.cjs red.

Not tried: a real Tavily account with several keys (the vendor is faked;
no test reaches the network); the real app window (the GUI ran in
Playwright only); Windows and Linux runs of the tests (vet only). The
full Go and GUI suites run after the push, under mote's suite lock.

Reported-by: 辰林鸭~ (Discord)
2026-11-07 21:21:48 +08:00

105 lines
6.9 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A plugin's search API that signs in as the plugin's provider (axelburks on
// Discord) takes no key: Settings' add row says whose sign-in it uses in
// place of the key field, Add sends it with none, and each added one's row
// says how many of the provider's accounts are on, or, as an error, that
// none is signed in or none is on. In all five languages, Chromium and
// WebKit, 980px and 420px wide, with the API faked.
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 { watchSettingsReads, settingsDrawn } = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
const vendors = [
{ id: "seek-web", name: "Seek", auth: { name: "SeekCo", signedIn: true, on: 2 } },
{ id: "out-web", name: "Out", auth: { name: "OutCo", signedIn: false, on: 0 } },
{ id: "none-web", name: "None On", auth: { name: "NoneCo", signedIn: true, on: 0 } },
];
const words = {
en: { add: "Add", signs: "Signs in with SeekCo", on: "Signed in to SeekCo · accounts on: 2", out: "Not signed in to OutCo: sign in to it on Providers", none: "No account of NoneCo is on" },
zh: { add: "添加", signs: "以 SeekCo 登录", on: "已登录 SeekCo · 开启的账号:2", out: "未登录 OutCo:请在“供应商”中登录", none: "NoneCo 没有开启的账号" },
"zh-TW": { add: "新增", signs: "以 SeekCo 登入", on: "已登入 SeekCo · 開啟的帳號:2", out: "未登入 OutCo:請在「供應商」中登入", none: "NoneCo 沒有開啟的帳號" },
ja: { add: "追加", signs: "SeekCo でサインイン", on: "SeekCo にサインイン済み · オンのアカウント:2", out: "OutCo にサインインしていません:「プロバイダ」でサインインしてください", none: "NoneCo のオンのアカウントがありません" },
de: { add: "Hinzufügen", signs: "Meldet sich mit SeekCo an", on: "Bei SeekCo angemeldet · Konten an: 2", out: "Nicht bei OutCo angemeldet: unter Anbieter anmelden", none: "Kein Konto von NoneCo ist an" },
};
function serve(lang, posted) {
let apis = [
{ vendor: "out-web", name: "Out", ready: true },
{ vendor: "none-web", name: "None On", ready: true },
];
const settings = () => ({ lang, theme: "light", searchVendors: vendors, searchAPIs: apis });
return async (r) => {
const url = new URL(r.request().url());
const json = (data, status = 200) => r.fulfill({ status, json: data });
if (url.pathname === "/boot.js") return r.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` });
if (url.pathname === "/wails/runtime.js") return r.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: settings() });
if (url.pathname === "/api/settings") return json(settings());
if (url.pathname === "/api/settings/search-api") {
const b = JSON.parse(r.request().postData());
posted.push(b);
apis = [...apis, { vendor: b.vendor, name: vendors.find((v) => v.id === b.vendor).name, ready: true }];
return json(settings());
}
if (url.pathname === "/api/plugins") return json({ plugins: [] });
if (url.pathname === "/api/groups") return json({ models: [], groups: [], pools: [] });
if (url.pathname === "/api/providers") return json({ providers: [], presets: [], excluded: [], gateway: { running: true, window: true, url: "http://127.0.0.1:3999" } });
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)];
await r.fulfill({ body: await fs.readFile(file), contentType });
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of Object.keys(words)) for (const width of [980, 420]) {
const w = words[lang];
test(`${engine} ${lang} ${width}px: a search API that signs in shows its sign-in, not a key`, async (t) => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" }));
t.after(() => browser.close());
const page = await (await browser.newContext({ viewport: { width, height: 1400 } })).newPage();
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const posted = [];
await page.route("**/*", serve(lang, posted));
await watchSettingsReads(page);
await page.goto("http://magpie.test/?view=settings&tab=models");
await settingsDrawn(page);
const list = page.locator("#searchList");
const head = list.locator(".row.search-add");
await head.waitFor();
// the first vendor is picked: Seek, which signs in as SeekCo
assert.equal(await head.locator("button.search-vendor").innerText(), "Seek");
assert.equal(await head.locator("input.search-key").isVisible(), false, "no key field");
assert.equal(await head.locator("button.link").isVisible(), false, "no Get a key");
assert.equal(await head.locator(".search-signs").isVisible(), true);
assert.equal(await head.locator(".search-signs").innerText(), w.signs);
// the stored ones: not signed in, and none on, are errors
const rows = list.locator(".row.search-api");
assert.equal(await rows.nth(0).locator(".sub").innerText(), w.out);
assert.equal(await rows.nth(0).locator(".sub").evaluate((e) => e.classList.contains("err")), true);
assert.equal(await rows.nth(1).locator(".sub").innerText(), w.none);
assert.equal(await rows.nth(1).locator(".sub").evaluate((e) => e.classList.contains("err")), true);
assert.equal(await rows.nth(0).locator("button.search-eye").count(), 0, "nothing to show");
assert.equal(await list.locator(".row.search-keys").count(), 0, "no keys to list");
// Add sends it with no key, and its row says how many accounts are on
await head.locator("button.text", { hasText: w.add }).evaluate((b) => b.click());
await page.waitForFunction(() => document.querySelectorAll("#searchList .row.search-api").length === 3);
assert.deepEqual(posted, [{ vendor: "seek-web", key: "", url: "" }]);
assert.equal(await rows.nth(2).locator(".sub").innerText(), w.on);
assert.equal(await rows.nth(2).locator(".sub").evaluate((e) => e.classList.contains("err")), false);
assert(await page.evaluate(() => document.scrollingElement.scrollWidth <= innerWidth), "nothing wider than the window");
assert.deepEqual(errors, []);
});
}
}