Files
magpie/internal/gui/tests/editor-site-scheme.test.cjs
T
yetone 0ce59ee613 providers: the editor's site link opens a base's host under the base's own scheme, so a local http server opens over http (#2377)
Peterpig set a custom provider's Base URL to http://127.0.0.1:7863/v1;
the link at the top right of its editor opened https://127.0.0.1:7863
and Chrome showed ERR_SSL_PROTOCOL_ERROR.

Cause: with no website, drawEditor built the link as "https://" + p.host.
p.host is provider.Host(), a display host with the scheme already cut
off, so every provider's scheme was guessed as https. hostSite now takes
the scheme from the base the host was read from (chat, responses,
anthropic, gemini, decide, matched the way provider.HostOf reads a host),
and keeps https only when no base spells the host: a subscription moved
onto its plugin shows the vendor host it had while its base is
plugin://<id>. A plugin's own provider (host without a dot or a port)
still gets no link.

Siblings checked: every api("open") call in app.js takes a full URL from
the Go side or a preset (keysUrl, website, sign-in URLs); none builds one
from a host. POST /api/open passes http:// through. The other
"https://" + host spots in app.js (hostOf, twoKeyHeaders' host check,
the S3 endpoint label) only read a host for display or matching, never
open it. provider.faviconSites looks for an icon on https only, but it
refuses local hosts first, so the reporter's case doesn't reach it.

Ran:
- new internal/gui/tests/editor-site-scheme.test.cjs (reporter's
  127.0.0.1:7863 http base, a LAN http base in upper case, an https
  base, a moved subscription, a plugin provider; clicks the link and
  checks what is opened), en and zh at 420px: BROWSER=chromium and
  BROWSER=webkit, pass 2/2 each.
- Put the old "https://" + p.host line back by hand: red in both
  engines, actual 'https://127.0.0.1:7863' vs expected
  'http://127.0.0.1:7863'. Fix restored, green again.
- tencent-cloud, copilot-ghe, zcode-site, ux-safety .test.cjs (other
  tests over the editor head and its site link), chromium and webkit:
  all pass.
- go test -tags nogui -p 1 ./internal/gui -run 'TestPageFilesRevalidate|
  TestPageNamesItsFilesByContent|TestCoreScriptBudget|TestWebCompresses|
  TestVersionedFilesKept|TestDatesFollowGUILanguage': ok.
- No Go file changed, so no gofmt/vet run.
Not tried: the real macOS app clicking the link (the browser opened is
the system's; the URL handed to it is what the test checks).

Full suite: SUITE_LINE
2026-10-15 23:08:03 +08:00

73 lines
4.6 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// The provider editor's head links to the provider's site. With no website,
// it opens the host its base is at under that base's own scheme: a local
// server at http://127.0.0.1:7863/v1 opens over http, not https (#2377).
const assert = require("node:assert/strict");
const fs = require("node:fs/promises");
const path = require("node:path");
const { test } = require("node:test");
const playwright = require("playwright");
const engines = (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"]);
const assets = path.resolve(__dirname, "../assets");
const providers = [
// the reporter's: a custom OpenAI-compatible server on this computer
{ id: "test", name: "test", icon: "generic", host: "127.0.0.1:7863", chat: "http://127.0.0.1:7863/v1", models: [] },
{ id: "lan", name: "LAN", icon: "generic", host: "box.local:8080", anthropic: "HTTP://Box.Local:8080/anthropic", models: [] },
{ id: "far", name: "Far", icon: "generic", host: "api.example.com", chat: "https://api.example.com/v1", models: [] },
// a subscription moved onto its plugin: its base is plugin://<id>, its
// host the vendor's it had
{ id: "moved", name: "Moved", icon: "generic", host: "api.vendor.test", chat: "plugin://moved", models: [] },
// a plugin's own provider has no address to open
{ id: "plug", name: "Plug", icon: "generic", host: "plug", chat: "plugin://plug", models: [] },
].map((p) => ({ responses: "", anthropic: "", agents: [], fallback: [], headers: {}, key: { set: false }, keyList: [], ready: true, ...p }));
for (const engine of engines) for (const lang of ["en", "zh"]) {
test(`editor's site link keeps the base's scheme (${engine}, ${lang})`, async (t) => {
const browser = await playwright[engine].launch();
t.after(() => browser.close());
const page = await browser.newPage({ viewport: { width: 420, height: 720 } });
page.setDefaultTimeout(5000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
await page.route("**/*", async (route) => {
const url = new URL(route.request().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:"dark",web:true};` });
if (url.pathname === "/wails/runtime.js") return route.fulfill({ contentType: "text/javascript", body: "export const Window = {};" });
if (url.pathname === "/api/state") return json({ agents: [], profiles: [], settings: { lang, theme: "dark" } });
if (url.pathname === "/api/providers") return json({ providers, presets: [], excluded: [], gateway: {} });
if (url.pathname === "/api/groups") return json({ groups: [] });
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)];
return route.fulfill({ body: await fs.readFile(file), contentType });
});
// in a browser the app opens a link with window.open (api "open")
await page.addInitScript(() => { window.opened = []; window.open = (u) => { window.opened.push(u); return null; }; });
await page.goto("http://magpie.test/?view=providers");
await page.waitForFunction((l) => state.settings.lang === l, lang);
await page.waitForFunction(() => providers?.providers?.length === 5);
const site = async (id) => {
await page.evaluate((id) => openModal(renderEditor(providers.providers.find((p) => p.id === id))), id);
const b = page.locator("#modal .ehead .site");
if (!(await b.count())) return null;
return [await b.getAttribute("data-url"), (await b.textContent()).replace(" ↗", "")];
};
assert.deepEqual(await site("test"), ["http://127.0.0.1:7863", "127.0.0.1:7863"]);
assert.deepEqual(await site("lan"), ["http://Box.Local:8080", "box.local:8080"]);
assert.deepEqual(await site("far"), ["https://api.example.com", "api.example.com"]);
assert.deepEqual(await site("moved"), ["https://api.vendor.test", "api.vendor.test"]);
assert.equal(await site("plug"), null);
// the click opens what the link says: the reporter's http page
await site("test");
await page.locator("#modal .ehead .site").click();
await page.waitForFunction(() => window.opened.length > 0);
assert.deepEqual(await page.evaluate(() => window.opened), ["http://127.0.0.1:7863"]);
assert.deepEqual(errors, []);
});
}