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)
193 lines
12 KiB
JavaScript
193 lines
12 KiB
JavaScript
// Run with Node's test runner and Playwright on the module path; see README.md.
|
|
// Settings' Web search section, a search API's keys as a list (辰林鸭~ on
|
|
// Discord: "only one Tavily key can be entered"): each key on a row of its
|
|
// own, masked, with its tick, Show, Copy and Remove; a key switched off
|
|
// stays, dimmed, and says so; a key refused lately says why and until when
|
|
// it is asked last. Several are added at once, pasted split by commas or
|
|
// lines, and the list is put in order with Alt+↑/↓. Every change is sent
|
|
// by the key's ref, never the key. In five languages, Chromium and WebKit,
|
|
// 1100px 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, reach } = require("./reader.cjs");
|
|
|
|
const assets = path.resolve(__dirname, "../assets");
|
|
const LANGS = ["en", "zh", "zh-TW", "ja", "de"];
|
|
const I18N = (() => {
|
|
const src = require("node:fs").readFileSync(path.join(assets, "i18n.js"), "utf8");
|
|
return new Function(src.slice(0, src.indexOf("\n};") + 3) + "; return I18N;")();
|
|
})();
|
|
const tr = (lang, s, vars = {}) => {
|
|
const got = lang === "en" ? s : I18N[lang][s];
|
|
assert(got, `"${s}" has no ${lang} translation`);
|
|
return got.replace(/\{(\w+)\}/g, (_, k) => String(vars[k]));
|
|
};
|
|
|
|
const vendors = [{ id: "tavily", name: "Tavily", keysURL: "https://app.tavily.com/home", base: "https://api.tavily.com" }];
|
|
const restUntil = new Date(Date.now() + 40 * 60 * 1000).toISOString();
|
|
const mask = (k) => "tvly-…" + k.slice(-4);
|
|
const refOf = (k) => "ref-" + k.slice(-4);
|
|
|
|
function serve(lang, posted) {
|
|
// as the real list: keys in order, each on or off; the rest is the
|
|
// gateway's, for a key on
|
|
let list = [
|
|
{ key: "tvly-dev-first-1111" },
|
|
{ key: "tvly-dev-limit-2222", rest: "rate limited" },
|
|
{ key: "tvly-dev-spare-3333", off: true },
|
|
];
|
|
const settings = () => ({
|
|
lang, theme: "light", searchVendors: vendors,
|
|
searchAPIs: [{
|
|
vendor: "tavily", name: "Tavily", ready: true, keys: list.filter((k) => !k.off).length,
|
|
key: list.filter((k) => !k.off).map((k) => mask(k.key)).join(", "),
|
|
keyList: list.map((k) => ({ ref: refOf(k.key), masked: mask(k.key), off: k.off || undefined, ...(k.rest && !k.off ? { rest: k.rest, restUntil } : {}) })),
|
|
}],
|
|
});
|
|
const at = (ref) => list.findIndex((k) => refOf(k.key) === ref);
|
|
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-keys") {
|
|
const b = JSON.parse(r.request().postData());
|
|
posted.push(b);
|
|
let added = 0, had = 0;
|
|
if (b.arrange) list = b.arrange.map((ref) => list[at(ref)]);
|
|
else if (b.add) {
|
|
for (const k of b.add.split(/[\s,;]+/).filter(Boolean)) {
|
|
if (list.some((x) => x.key === k)) had++;
|
|
else { list.push({ key: k }); added++; }
|
|
}
|
|
} else if (b.remove) list.splice(at(b.ref), 1);
|
|
else if ("on" in b) list[at(b.ref)] = { ...list[at(b.ref)], off: !b.on };
|
|
return json({ settings: settings(), added, had });
|
|
}
|
|
if (url.pathname === "/api/settings/search-key") {
|
|
const b = JSON.parse(r.request().postData());
|
|
posted.push({ asked: b });
|
|
return json({ key: list[at(b.ref)].key });
|
|
}
|
|
if (url.pathname === "/api/copy") { posted.push({ copied: JSON.parse(r.request().postData()).text }); return json({}); }
|
|
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 LANGS) for (const width of [1100, 420]) {
|
|
test(`${engine} ${lang} ${width}px: a search API's keys are a list, each switched, shown, removed and moved`, 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 }, reducedMotion: "reduce" })).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 keys = page.locator('#searchList .row.search-keys[data-vendor="tavily"]');
|
|
await keys.waitFor();
|
|
const rows = keys.locator(".search-key-list .acc[data-ref]");
|
|
const codes = () => rows.locator("code").allInnerTexts();
|
|
assert.deepEqual(await codes(), ["tvly-…1111", "tvly-…2222", "tvly-…3333"]);
|
|
// nothing in the page holds a whole key
|
|
assert.equal(await page.evaluate(() => document.body.innerHTML.includes("tvly-dev-")), false);
|
|
assert.deepEqual(await rows.evaluateAll((rs) => rs.map((r) => r.className.includes("off") ? "off" : "on")), ["on", "on", "off"]);
|
|
assert.deepEqual(await rows.locator("button.tick").evaluateAll((bs) => bs.map((b) => b.getAttribute("aria-pressed"))), ["true", "true", "false"]);
|
|
assert.equal(await rows.nth(0).locator("button.tick").getAttribute("aria-label"), tr(lang, "Stop using this key"));
|
|
assert.equal(await rows.nth(2).locator("button.tick").getAttribute("aria-label"), tr(lang, "Use this key too"));
|
|
// why a key is asked last, and until when; one off says Off
|
|
const until = await page.evaluate((u) => hhmm(u), restUntil);
|
|
assert.equal(await rows.nth(1).locator(".key-rest").innerText(), tr(lang, "asked last until {time}: {why}", { time: until, why: tr(lang, "rate limited") }));
|
|
assert.equal(await rows.locator(".key-rest").count(), 1);
|
|
assert.equal(await rows.nth(2).locator(".key-state").innerText(), tr(lang, "Off"));
|
|
assert(await page.evaluate(() => document.scrollingElement.scrollWidth <= innerWidth), "nothing wider than the window");
|
|
const past = await keys.evaluate((r) => [...r.querySelectorAll("button, code, .key-rest")]
|
|
.filter((e) => e.getBoundingClientRect().right > innerWidth + 0.5).map((e) => e.className + " " + e.textContent));
|
|
assert.deepEqual(past, [], "nothing past the window's edge");
|
|
|
|
const where = () => page.evaluate(() => [scrollX, scrollY, document.scrollingElement.scrollTop, document.querySelector("#view-settings")?.scrollTop]);
|
|
// reached as a reader does (the wheel), then clicked where it is:
|
|
// the click itself never moves the page. Settings is drawn again
|
|
// after each change, so the box is taken from the row that stays.
|
|
const stays = [];
|
|
const click = async (loc) => {
|
|
await reach(page, loc);
|
|
const b = await loc.boundingBox();
|
|
assert(b, "the button is in the page");
|
|
const at = await where();
|
|
await page.mouse.click(b.x + b.width / 2, b.y + b.height / 2);
|
|
stays.push(async () => assert.deepEqual(await where(), at, "a click doesn't scroll"));
|
|
};
|
|
const drawn = (want) => page.waitForFunction((w) => [...document.querySelectorAll('#searchList .search-keys[data-vendor="tavily"] .acc[data-ref] code')].map((c) => c.textContent).join() === w, want.join());
|
|
const settle = async () => { for (const f of stays.splice(0)) await f(); };
|
|
|
|
// Show reads that one key, by its ref; Hide masks it again
|
|
await click(rows.nth(2).locator("button.search-eye"));
|
|
await page.waitForFunction(() => document.querySelector("#searchList .search-key-list code.revealed"));
|
|
assert.equal(await rows.nth(2).locator("code").innerText(), "tvly-dev-spare-3333");
|
|
assert.equal(await rows.nth(2).locator("button.search-eye").innerText(), tr(lang, "Hide"));
|
|
await click(rows.nth(2).locator("button.search-eye"));
|
|
assert.equal(await rows.nth(2).locator("code").innerText(), "tvly-…3333");
|
|
await settle();
|
|
await click(rows.nth(0).locator("button.search-copy"));
|
|
for (let i = 0; i < 100 && !posted.some((p) => p.copied); i++) await page.waitForTimeout(50);
|
|
assert.deepEqual(posted.splice(0), [{ asked: { vendor: "tavily", ref: "ref-3333" } }, { asked: { vendor: "tavily", ref: "ref-1111" } }, { copied: "tvly-dev-first-1111" }]);
|
|
|
|
// switched on, then the first off
|
|
await click(rows.nth(2).locator("button.tick"));
|
|
await page.waitForFunction(() => !document.querySelector('#searchList .search-keys[data-vendor="tavily"] .acc.off'));
|
|
await click(rows.nth(0).locator("button.tick"));
|
|
await page.waitForFunction(() => document.querySelector('#searchList .search-keys[data-vendor="tavily"] .acc[data-ref="ref-1111"]')?.classList.contains("off"));
|
|
assert.deepEqual(posted.splice(0), [{ vendor: "tavily", ref: "ref-3333", on: true }, { vendor: "tavily", ref: "ref-1111", on: false }]);
|
|
await settle();
|
|
|
|
// several pasted at once, one already there: said how many
|
|
await click(keys.locator(".search-key-list .acc.add"));
|
|
const field = keys.locator("input.search-key-new");
|
|
await field.waitFor();
|
|
await settle();
|
|
assert.equal(await field.getAttribute("type"), "password", "a key typed is masked");
|
|
assert.equal(await field.getAttribute("placeholder"), tr(lang, "Paste a key, or several split by commas or lines"));
|
|
await field.fill("tvly-dev-new-4444, tvly-dev-first-1111, tvly-dev-more-5555");
|
|
await field.press("Enter");
|
|
await drawn(["tvly-…1111", "tvly-…2222", "tvly-…3333", "tvly-…4444", "tvly-…5555"]);
|
|
assert.deepEqual(posted.splice(0), [{ vendor: "tavily", add: "tvly-dev-new-4444, tvly-dev-first-1111, tvly-dev-more-5555" }]);
|
|
await page.waitForFunction((w) => document.querySelector("#status")?.textContent === w, tr(lang, "{n} keys added, {m} already there", { n: 2, m: 1 }));
|
|
|
|
// Alt+↓ on the second puts it third: the order is sent as refs
|
|
await rows.nth(1).focus();
|
|
await page.keyboard.press("Alt+ArrowDown");
|
|
await drawn(["tvly-…1111", "tvly-…3333", "tvly-…2222", "tvly-…4444", "tvly-…5555"]);
|
|
assert.deepEqual(posted.splice(0), [{ vendor: "tavily", arrange: ["ref-1111", "ref-3333", "ref-2222", "ref-4444", "ref-5555"] }]);
|
|
await page.waitForFunction(() => document.activeElement?.dataset?.ref === "ref-2222");
|
|
|
|
// Remove
|
|
await click(rows.nth(3).locator("button.search-key-remove"));
|
|
await drawn(["tvly-…1111", "tvly-…3333", "tvly-…2222", "tvly-…5555"]);
|
|
assert.deepEqual(posted.splice(0), [{ vendor: "tavily", ref: "ref-4444", remove: true }]);
|
|
assert.equal(await rows.nth(0).locator("button.search-key-remove").innerText(), tr(lang, "Remove"));
|
|
|
|
await settle();
|
|
assert(await page.evaluate(() => document.scrollingElement.scrollWidth <= innerWidth), "nothing wider than the window");
|
|
assert.equal(await page.evaluate(() => document.body.innerHTML.includes("tvly-dev-")), false, "no key left in the page");
|
|
assert.deepEqual(errors, []);
|
|
});
|
|
}
|
|
}
|