// Run with Node's test runner and Playwright on the module path; see README.md. // treydtw on X: a local model that exists only on this computer must not be // synced to the others. A provider row's right-click has Only on this // device: it posts provider/thisdevice, and once on the item shows a tick // and posts it off again. The page doesn't move. Every language, Chromium // and WebKit, at 1100 and 420 wide; no backend, the API is faked here. 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 CHECK = "m3.5 8.5 3 3 6-7"; const now = new Date(); const ago = (minutes) => new Date(now.getTime() - minutes * 60e3).toISOString(); const prov = (id, name, extra = {}) => ({ id, name, icon: "generic", host: id + ".example.com", chat: "https://" + id + ".example.com/v1", responses: "", anthropic: "", catalog: "", models: [{ id: "gpt-5.5", name: "GPT-5.5", on: true }], agents: [], fallback: [], headers: {}, key: { set: true, masked: "sk-…" + id }, keyList: [], balanceToken: { takes: false, set: false }, proxy: "", ...extra, }); const order = [{ id: "ollama#k", provider: "ollama", name: "Ollama", who: "Ollama", kind: "key", model: "qwen3", routing: "" }]; const left = [{ id: "openai#k", provider: "openai", name: "OpenAI", who: "OpenAI", kind: "key", model: "gpt-5.5", routing: "", fallback: true, unlisted: true, standIn: true }]; function serve(lang, posts) { let list = [prov("ollama", "Ollama", { fallback: ["openai/gpt-5.5"] }), prov("openai", "OpenAI")]; const providers = () => ({ providers: list, presets: [], excluded: [], gateway: { running: true, window: true } }); const route = { id: 1, seq: 1, time: ago(1), agent: "codex", model: "ollama/qwen3", provider: "ollama", done: true, status: 503, ms: 900, order, left, tries: [{ id: "ollama#k", model: "qwen3", start: ago(1), done: true, status: 503, ms: 900 }], }; return async (request) => { const url = new URL(request.request().url()); const json = (data) => request.fulfill({ json: data }); if (url.pathname === "/boot.js") return request.fulfill({ contentType: "text/javascript", body: `window.bootPrefs = {lang:"${lang}",theme:"light",web:true};` }); if (url.pathname === "/wails/runtime.js") return request.fulfill({ contentType: "text/javascript", body: "export const Window = {};" }); if (url.pathname === "/api/state") return json({ agents: [{ id: "codex", name: "Codex", path: "/test/config.toml", fields: [] }], profiles: [], settings: { lang, theme: "light" } }); if (url.pathname === "/api/providers") return json(providers()); if (url.pathname === "/api/provider/thisdevice") { const body = request.request().postDataJSON(); posts.push(body); list = list.map((p) => p.id === body.id ? { ...p, thisDevice: body.thisDevice } : p); return json(providers()); } if (url.pathname === "/api/gateway/trace") { if (url.searchParams.get("wait")) return new Promise(() => {}); // nothing more comes return json({ mine: true, now: now.toISOString(), seq: 1, totals: { requests: 1 }, routes: [route] }); } if (url.pathname === "/api/gateway/history") return json({ cut: false, days: [], routes: [] }); if (url.pathname === "/api/groups") return json({ groups: [] }); if (url.pathname === "/api/plugins") return json({ plugins: [] }); 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 request.fulfill({ body: await fs.readFile(file), contentType }); }; } const strings = [ "Only on this device", ]; for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", "webkit"])) { for (const width of [1100, 420]) { for (const lang of ["en", "zh", "zh-TW", "ja", "de"]) { test(`${engine} ${width} ${lang}: a provider's right-click turns Only on this device on and off`, 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: 800 }, reducedMotion: "reduce" })).newPage(); page.setDefaultTimeout(5000); page.setDefaultNavigationTimeout(60000); const errors = [], posts = []; page.on("pageerror", (e) => errors.push(e.message)); await page.route("**/*", serve(lang, posts)); await page.goto("http://magpie.test/?view=providers"); const say = (key) => page.evaluate(([lang, key]) => (lang !== "en" && I18N[lang]?.[key]) || key, [lang, key]); await page.locator(".row.provider").first().waitFor(); if (lang === "en") { const missing = await page.evaluate((ks) => ["zh", "zh-TW", "ja", "de"].flatMap((l) => ks.filter((k) => !I18N[l][k]).map((k) => `${l}: ${k}`)), strings); assert.deepEqual(missing, [], "every string has its translations"); } const row = page.locator('#providers > .row.provider[data-id="ollama"]'); const menu = page.locator(".pop.row-menu"); const item = menu.locator(".rm-item", { hasText: await say("Only on this device") }); await row.locator(".name").click({ button: "right" }); await menu.waitFor(); assert.equal(await item.count(), 1); assert.notEqual(await item.locator("path").getAttribute("d"), CHECK, "off, no tick"); const box = await menu.boundingBox(); assert.ok(box.x >= 0 && box.x + box.width <= width, `the menu fits across ${width}`); await item.click(); await page.waitForFunction(() => !document.querySelector(".pop.row-menu")); assert.deepEqual(posts, [{ id: "ollama", thisDevice: true }]); await row.locator(".name").click({ button: "right" }); await menu.waitFor(); assert.equal(await item.locator("path").getAttribute("d"), CHECK, "on, a tick"); await item.click(); await page.waitForFunction(() => !document.querySelector(".pop.row-menu")); assert.deepEqual(posts, [{ id: "ollama", thisDevice: true }, { id: "ollama", thisDevice: false }]); await page.locator('#providers > .row.provider[data-id="openai"] .name').click({ button: "right" }); await menu.waitFor(); assert.notEqual(await item.locator("path").getAttribute("d"), CHECK, "another provider is off"); await page.keyboard.press("Escape"); assert.deepEqual(errors, []); }); } } }