Files
magpie/internal/gui/tests/searcher-pick.test.cjs
T
yetone 06a2e9cff5 search: which provider searches the web for a model that can't is picked in Settings → Web search, Automatic as before (01huadalang on Discord: 能不能在供应商联网搜索里加一个入口,能用自己能联网搜索的地方来替代不能联网搜索的供应商) — magpie picked it by itself (a Claude account, then a Codex account, then a provider that searches by itself on Anthropic's API, the Responses API, Chat, each by its small model) and Settings only said who. A "Searches for other models" row now comes first in the section: its picker offers Automatic (magpie's pick, named), and each provider able to search — on, and searching by itself or a Claude/Codex account, in the order magpie would pick them — by its small model or by any model of it. The pick is saved as settings' searcher ("<provider>" or "<provider>/<model>"), sent with every other setting saved (prefsKeep), and refused when the provider can't search. searcher() takes it while it can (chosenSearcher); one gone, turned off or no longer able to search gives way to magpie's pick, and the row says which and why, showing Automatic; a model it no longer lists gives way to its small model. A relay said to search is still never asked for other models (#359: it would spend the relay's quota, and one that serves only Claude Code refuses magpie's own request), so it isn't offered, and the row names such relays and says why. TestSearcherChosen (the automatic order, a provider, a model of it, a model gone, one that can't search, a relay said to search, one gone, one turned off and back on, Automatic again) and searcher-pick.test.cjs (WebKit and Chromium, en and zh: Automatic shown with magpie's pick, the relay not offered and named, a model and a provider picked and saved, another setting saved keeping the pick, one turned off said and Automatic shown, Automatic sent back, no click moving the page) are new; it passes in WebKit (this machine's Playwright Chromium fails to launch, icudtl.dat missing, for every test). search-api, settings-sections, provider-search, click-scroll pass in WebKit. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass (TestDevinSignedInOnlyInMagpie failed once, passes 3/3 alone).
2026-10-02 23:33:31 +08:00

136 lines
8.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Run with Node's test runner and Playwright on the module path; see README.md.
// Settings' Web search section: which provider searches for a model that
// can't (01huadalang on Discord). The row shows magpie's own pick while none
// is named; the picker offers Automatic, each provider that can search by
// its small model, and each of its models; a pick is saved as searcher
// ("<provider>" or "<provider>/<model>") and shown; one named that magpie
// can't use (turned off) is said in the row, magpie's pick shown instead;
// relays said to search are said not to be offered. Another setting saved
// keeps the pick (prefsKeep). No click moves the page. English and Chinese, Chromium and
// WebKit, 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 assets = path.resolve(__dirname, "../assets");
const choices = [
{ id: "claude", name: "Claude", icon: "claude", small: "claude-haiku-4-5", models: [
{ id: "claude/claude-haiku-4-5", name: "Claude Haiku 4.5", provider: "claude", providerName: "Claude" },
{ id: "claude/claude-opus-4-5", name: "Claude Opus 4.5", provider: "claude", providerName: "Claude" }] },
{ id: "openai", name: "OpenAI", icon: "openai", small: "gpt-5-mini", models: [
{ id: "openai/gpt-5-mini", name: "GPT-5 mini", provider: "openai", providerName: "OpenAI" },
{ id: "openai/gpt-5.5", name: "GPT-5.5", provider: "openai", providerName: "OpenAI" }] },
];
const words = {
en: { name: "Searches for other models", auto: "Automatic", small: "gpt-5-mini, its small model", unused: "isn't used: it is turned off", relays: "Relays said to search (MyRelay)" },
zh: { name: "代搜供应商", auto: "自动", small: "gpt-5-mini(它的小模型)", unused: "没有用 OpenAI · gpt-5-mini:它已关闭", relays: "标为能搜索的中转站(MyRelay)" },
};
function serve(lang, posted, st) {
const settings = () => ({ lang, theme: "light", searchVendors: [], searchAPIs: [], searchChoices: choices,
searchAuto: "Claude · claude-haiku-4-5", searchProvider: st.unused || !st.searcher ? "Claude · claude-haiku-4-5" : st.searcher,
searchRelays: ["MyRelay"], searcher: st.searcher, searchUnused: st.unused });
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") {
if (r.request().method() === "POST") {
const b = JSON.parse(r.request().postData());
posted.push(b);
st.searcher = b.searcher || "";
}
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)];
const body = await fs.readFile(file).catch(() => null);
await (body ? r.fulfill({ body, contentType }) : r.fulfill({ status: 404, body: "" }));
};
}
for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) {
for (const lang of ["en", "zh"]) {
const w = words[lang];
test(`${engine} ${lang}: the provider that searches for other models is picked in Settings`, 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: 1000, height: 900 } })).newPage();
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const posted = [];
const st = { searcher: "", unused: "" };
await page.route("**/*", serve(lang, posted, st));
await page.goto("http://magpie.test/?view=settings&tab=models");
const row = page.locator("#searchList .row.searcher-row");
await row.waitFor();
await row.scrollIntoViewIfNeeded();
assert.equal(await row.locator(".name").innerText(), w.name);
assert.equal(await row.locator("button.searcher-pick").innerText(), `${w.auto} · Claude · claude-haiku-4-5`);
assert((await row.locator(".sub").innerText()).includes(w.relays), "the relays said to search are named");
// the Search APIs come after it
assert.equal(await page.locator("#searchList .row").nth(1).evaluate((e) => e.classList.contains("search-add")), true);
const where = () => page.evaluate(() => [scrollX, scrollY, document.scrollingElement.scrollTop, document.querySelector("main")?.scrollTop ?? 0]);
const click = async (loc) => {
const b = await loc.boundingBox();
await page.mouse.click(b.x + b.width / 2, b.y + b.height / 2);
};
const before = await where();
// a model of a provider
await click(row.locator("button.searcher-pick"));
await page.locator("#pop").waitFor({ state: "visible" });
const items = await page.locator("#list li:not(.group)").allInnerTexts();
assert(items[0].includes(w.auto), "Automatic comes first");
assert(items.some((x) => x.includes("GPT-5.5")) && items.some((x) => x.includes("Claude Opus 4.5")));
assert(!items.some((x) => x.includes("MyRelay")), "a relay said to search isn't offered");
await click(page.locator("#list li:not(.group)", { hasText: "GPT-5.5" }));
await page.waitForFunction(() => document.querySelector("#searchList button.searcher-pick")?.innerText.includes("GPT-5.5"));
assert.equal(posted.at(-1).searcher, "openai/gpt-5.5");
assert.equal(await row.locator("button.searcher-pick").innerText(), "GPT-5.5 · OpenAI");
// a provider, by its small model
await click(row.locator("button.searcher-pick"));
await page.locator("#pop").waitFor({ state: "visible" });
await click(page.locator("#list li:not(.group)", { hasText: w.small }));
await page.waitForFunction(() => document.querySelector("#searchList button.searcher-pick")?.innerText === "OpenAI · gpt-5-mini");
assert.equal(posted.at(-1).searcher, "openai");
// another setting saved keeps the pick
const n = posted.length;
await page.evaluate(() => savePrefs({ ...prefsKeep(prefs), noStats: true }));
await page.waitForTimeout(200);
assert.equal(posted.length, n + 1);
assert.equal(posted.at(-1).searcher, "openai", "another setting saved sends the pick as it was");
// the one named turned off: said, and magpie's pick shown
st.unused = "off";
await page.evaluate(() => renderSettings && fetch("/api/settings").then((r) => r.json()).then((s) => { prefs = s; state.settings = s; renderSettings(); }));
await page.waitForFunction(() => document.querySelector("#searchList .searcher-unused"));
assert((await row.locator(".searcher-unused").innerText()).includes(w.unused));
assert.equal(await row.locator("button.searcher-pick").innerText(), `${w.auto} · Claude · claude-haiku-4-5`);
st.unused = "";
// back to Automatic
await click(row.locator("button.searcher-pick"));
await page.locator("#pop").waitFor({ state: "visible" });
await click(page.locator("#list li:not(.group)", { hasText: w.auto }).first());
await page.waitForFunction(() => !document.querySelector("#searchList .searcher-unused") &&
document.querySelector("#searchList button.searcher-pick")?.innerText.includes("claude-haiku"));
assert.equal(posted.at(-1).searcher, "");
assert.deepEqual(await where(), before, "the clicks moved nothing");
assert.deepEqual(errors, []);
});
}
}