Files
magpie/internal/gui/tests/copilot-ghe.test.cjs
T
yetone ce8b67de37 gui tests: a page's first load gets a minute, not a click's 5s (#4470)
claude-switch-only set page.setDefaultTimeout(5000) and then called
page.goto with no timeout of its own, so Playwright held the first load
of app.js, i18n.js and the plugins to the 5s bar meant for clicks. On a
busy box (a whole-suite run at load 130-160) that load took longer and
goto timed out. The same pattern is in 988 GUI test files (1174 pages):
every one of them now also sets setDefaultNavigationTimeout(60000) on
the same page, right after its default, so goto, reload and waitForURL
wait on the load itself with room, while clicks and waits keep their
short bar. group-from-model-add's own 20s navigation bar is raised to the
same minute. claude-switch-only also gives its first provider row (drawn
from the page's first reads) a minute before clicking it.

nav-timeout.test.cjs (no browser) reads every GUI test file and names
each page given a default under a minute without a navigation timeout of
at least a minute, so a new file can't bring the pattern back.

What was run (WSL magpie-desktop, Playwright 1.62.0, load 70-260 from
other sessions):
- node --check on every file in internal/gui/tests: all parse.
- nav-timeout.test.cjs against origin/main's test files: red (names the
  1174 pages); against this tree: its only reds were my own probe copies
  of the old files, since removed.
- By hand: claude-switch-only with app.js served 7s late (a busy box's
  first load), --test-name-pattern "1100 en": origin/main's file 4 of 4
  red with "page.goto: Timeout 5000ms exceeded"; this file 4 of 4 green.
- claude-switch-only.test.cjs, both engines, en/zh/ja/de, 1100 and 420:
  32 of 32. group-from-model-add, usage-access-key-card and i18n-keys:
  green. agent-plugin-agents: Chromium green; WebKit's de 420 subtest red
  on a 5s waitForFunction at line 115 (a wait after load, not the
  navigation), at load ~230.

Not tried: the whole GUI suite (about 1000 files; the change is one
mechanical line per page), Windows or mote (mote's browsers, #4125).
2026-10-31 02:09:18 +08:00

119 lines
6.9 KiB
JavaScript

// Run with Node's test runner and Playwright on the module path; see README.md.
// A Copilot account on GitHub Enterprise Cloud with data residency (#723):
// the device code's panel offers "Account on GHE.com?", which asks the
// enterprise's <name>.ghe.com and posts it as the sign-in's site; a host
// that isn't one can't be submitted, and github.com is one click back.
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");
for (const engine of engines) for (const lang of ["en", "zh"]) {
test(`copilot GHE.com sign-in (${engine}, ${lang})`, async (t) => {
const browser = await playwright[engine].launch();
t.after(() => browser.close());
const page = await browser.newPage({ viewport: { width: 420, height: 640 } });
page.setDefaultTimeout(5000);
page.setDefaultNavigationTimeout(60000);
const errors = [];
page.on("pageerror", (e) => errors.push(e.message));
const posted = [];
const canceled = [];
let n = 0;
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 === "/api/signin") {
const body = route.request().postDataJSON();
posted.push(body);
const host = body.site || "github.com";
return json({ id: "c" + ++n, agent: "copilot", state: "waiting", code: "ABCD-1234", url: `https://${host}/login/device` });
}
if (/^\/api\/signin\/c\d+\/cancel$/.test(url.pathname)) { canceled.push(url.pathname.split("/")[3]); return json({}); }
if (/^\/api\/signin\/c\d+$/.test(url.pathname)) return new Promise(() => {}); // stays waiting
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 });
});
// the sign-in opens GitHub's device page in a new window: it gets an
// empty page, not the real github.com or acme.ghe.com, whose pages a
// test can't control (ChatGPT's Cloudflare challenge hung clicks, #1307)
await page.context().route((url) => url.hostname !== "magpie.test", (route) => route.fulfill({ contentType: "text/html", body: "" }));
await page.goto("http://magpie.test/");
await page.waitForFunction((l) => state.settings.lang === l, lang);
await page.evaluate(() => {
providers = providers || { providers: [], presets: [], plugins: [] };
window.renderProviders = () => {
const host = document.getElementById("csign") || Object.assign(document.createElement("div"), { id: "csign" });
host.style.cssText = "position:fixed;inset:0 auto auto 0;width:100%;z-index:1000;";
host.replaceChildren(signing ? renderSigning(subOf("copilot")) : "");
document.body.append(host);
};
startSignIn("copilot", true);
});
const box = page.locator("#csign");
const noAccents = async () => {
const bad = await box.evaluate((root) => [...root.querySelectorAll("*")].filter((e) => {
const s = getComputedStyle(e);
return parseFloat(s.borderLeftWidth) > 0 && s.borderLeftWidth !== s.borderRightWidth;
}).map((e) => e.outerHTML.slice(0, 80)));
assert.deepEqual(bad, []);
assert.equal(await box.locator("select").count(), 0);
};
// github.com's sign-in, as before, with the way to an enterprise's
await page.waitForFunction(() => signing?.state === "waiting");
assert.deepEqual(posted, [{ agent: "copilot" }]);
const toGHE = box.locator('button[data-ghe="ghe"]');
assert.equal(await toGHE.textContent(), lang === "zh" ? "账号在 GHE.com 上?" : "Account on GHE.com?");
assert.ok((await toGHE.getAttribute("title")).length > 0);
await noAccents();
// it asks the host, ending the github.com sign-in
await toGHE.click();
await page.waitForFunction(() => signing?.state === "ghe");
await page.waitForFunction(() => document.querySelector("#csign .copilot-ghe input"));
assert.deepEqual(canceled, ["c1"]);
const text = await box.innerText();
assert.match(text, lang === "zh" ? /你的企业在 GHE\.com 上的地址/ : /Your enterprise on GHE\.com/);
assert.match(text, lang === "zh" ? /github\.com 账号无需填写/ : /An account on github\.com doesn't need it/);
const field = box.locator(".copilot-ghe input");
const go = box.locator('.copilot-ghe button[type="submit"]');
assert.equal(await field.getAttribute("placeholder"), "acme.ghe.com");
assert.equal(await go.textContent(), lang === "zh" ? "登录" : "Sign in");
assert.equal(await go.isDisabled(), true);
for (const bad of ["evil.com", "acme.ghe.com.evil.com", "a.b.ghe.com", "acme.ghe.com:8443", "github.example.com"]) {
await field.fill(bad);
assert.equal(await go.isDisabled(), true, bad);
}
await noAccents();
await field.fill("https://Acme.ghe.com/");
assert.equal(await go.isDisabled(), false);
await field.press("Enter");
await page.waitForFunction(() => signing?.state === "waiting" && signing.site);
assert.deepEqual(posted[1], { agent: "copilot", site: "acme.ghe.com" });
assert.equal(await page.evaluate(() => signing.site), "acme.ghe.com");
assert.match(await box.innerText(), /https:\/\/acme\.ghe\.com\/login\/device/);
// and back to github.com
const back = box.locator('button[data-ghe="github"]');
assert.equal(await back.textContent(), lang === "zh" ? "改为在 github.com 登录" : "Sign in on github.com instead");
assert.equal(await back.getAttribute("title"), lang === "zh" ? "正在 acme.ghe.com 登录" : "Signing in on acme.ghe.com");
await back.click();
await page.waitForFunction(() => signing?.state === "waiting" && !signing.site && signing.id === "c3");
assert.deepEqual(posted[2], { agent: "copilot" });
assert.deepEqual(canceled, ["c1", "c2"]);
assert.deepEqual(errors, []);
});
}