Files
magpie/internal/gui/tests/session-identity.test.cjs
T
yetone 98c4b2e814 usage: a request opens on a page of its own
geesehowardt7 on X clicked a request on the Usage page to see where it
came from. The click on its time opened Routing, which they hadn't
asked for, and the row's details had no source address.

A row's click, or Enter or Space on it, now pushes that request's own
page (#ledReqPane, ledReqOpen/renderLedReq) over the list. The page
gives the model and status, then ledDetail: time, agent, source IP
(the previous commit's client_ip: loopback reads "This computer ·
127.0.0.1", a gateway row from before the field "Not recorded", rows
read from session files, other computers and magpie's own calls none),
requested, provider, account, sent, served, effort, then status,
error, duration, tokens, cost, credits, speed and the archive. Routing
is the View routing link in it, and the time cell no longer links
there. "Requests ‹" or Escape (not while a menu, dialog or field takes
it) goes back to the list where it was left, with the row focused
(ledReqClose). Leaving the Usage page closes it (ledLeave(true)), and
a refresh keeps it. The in-table expanding row (ledOpen, tr.led-detail)
and its narrow-window lc-opt copies are gone, since the page shows
every field at every width. It slides in 200ms with the ease-out
curve, and only fades with reduced motion. "Source IP" and "Not
recorded" are added in zh, zh-TW, ja and de. The 20 ledger test files
that opened a row's details now open the page. usage-request-page.test.cjs
covers the page, the address, the way back, focus and a 560px window.
docs/subsystems/gui-shell.md describes it.

Run at this commit's tree (rebased on origin/main b4a0a2a1a, with the
two commits beside it):
- mote (macOS 27): gofmt -l clean; go vet ./..., GOOS=linux -tags
  nogui go vet and GOOS=windows go vet ok. go test internal/gui (with
  TestCoreScriptBudget) ok in 164s, internal/usage, internal/usageapi,
  internal/cli and the root package ok, internal/gateway -run
  "Usage|ClientIP" ok, internal/provider -race -count=5 -run Workspace
  ok.
- WSL VM (magpie-desktop), whole suite through the suite lock at
  be9dbce02 plus this change: go test ./... -timeout 45m ok apart from
  the GTK packages, which need a display the VM run lacks (they pass
  on mote), TestGrokInstalled (#3548) and the provider workspace test
  the next commit fixes (#5800). GUI suite: 540 of 23297 red in 114
  files, WebKit timeouts mostly (#4121); those 114 files run again
  give 378 reds here and 365 at be9dbce02, the same files, and
  own-requests was the one this change broke (fixed: a row's click now
  opens the page, and its way back keeps the list's place).
- WSL VM, the 119 Usage and Routing GUI files at the rebased head,
  both engines: 21 red, 20 of them red the same at origin/main
  b4a0a2a1a: click-scroll (#4385), routing-cline-route and
  routing-upstream (#5156), usage-agents-hide (#2276, #3414),
  usage-heatmap (#4718), usage-ledger-ttft (#4489), usage-ledger-chart
  (#4263), usage-focus-steady (#3501), usage-fold (#3415, #2163),
  usage-ledger Requests (#3104, #5048), account-refresh-holds (#5814)
  and usage-speed-all-models (#5834, red since the 24 hours period).
  The 21st, usage-compact-counts chromium "zh 万 at 420px", is the
  flake in #5648 and passes 3 of 3 alone.
Again after rebasing on origin/main b3a57b3d8 (main's WebSocket row in
the ledger, 7c1dc198d): mote go vet, internal/gui, internal/usage,
internal/usageapi, internal/cli, the root package, internal/provider
and gateway -run "Usage|ClientIP|Socket|Memory" ok, apart from
TestCoreScriptBudget, red on main since b3a57b3d8 and fixed there by
4894332d7, which this is now rebased on; the VM's 119 files plus usage-ledger-bytes and
codex-app-back: 1708 of 1782 pass, no red that isn't in the list
above, usage-ledger-bytes passing with the request page.
Not tried: a request from another device over a real LAN share.
2026-11-10 04:44:13 +08:00

156 lines
13 KiB
JavaScript

// Provider attribution requires route evidence. Models and session identities
// never promote unknown requests to official providers. Legacy model_vendor
// hints in fixtures are deliberately ignored. The API is a fixture.
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 reader = require("./reader.cjs");
const assets = path.resolve(__dirname, "../assets");
const now = new Date().toISOString();
const rows = [
{ t:now, agent:"claude-desktop", agentName:"Claude Desktop", provider:"session-anthropic", providerName:"Anthropic", session_account:"historical@example.com", session_official_login:true, model:"claude-opus-5", source:"log", in:10, out:2, status:0, cost:0, priced:false },
{ t:now, agent:"codex", agentName:"Codex", provider:"session-openai", providerName:"OpenAI", session_account:"recorded@example.com", session_official_login:true, session_provider:"openai", model:"gpt-6-sol", source:"log", in:10, out:2, status:0, cost:0, priced:false },
{ t:now, agent:"codex", agentName:"Codex", provider:"session-unknown", providerName:"Local session", model:"gpt-6-astra", model_vendor:"OpenAI", session_provider:"relay", session_account:"creator@example.com", source:"log", in:10, out:2, status:0, cost:0, priced:false },
{ t:now, agent:"claude", agentName:"Claude Code", provider:"session-unknown", providerName:"Local session", model:"claude-opus-5", req:"claude-opus-5[1m]", model_vendor:"Anthropic", source:"log", in:10, out:2, status:0, cost:0, priced:false },
{ t:now, agent:"opencode", agentName:"OpenCode", provider:"relay", providerName:"My Relay", host:"relay.example", access:"api", model:"gpt-6-astra", in:10, out:2, status:200, cost:0, priced:false },
{ t:now, agent:"claude", agentName:"Claude Code", provider:"session-unknown", providerName:"Local session", model:"unknown-alias", source:"log", in:10, out:2, status:0, cost:0, priced:false },
{ t:now, agent:"opencode", agentName:"OpenCode", provider:"unknown-relay", providerName:"Archived relay", model:"gpt-6-astra", in:10, out:2, status:200, cost:0, priced:false },
{ t:now, agent:"codex", agentName:"Codex", provider:"session-unknown", providerName:"Local session", session_provider:"custom", session_account:"reviewer@example.com", session_official_login:true, req:"codex-auto-review", model:"codex-auto-review", source:"log", in:10, out:2, status:0, cost:0.003, priced:true, pricing_model:"gpt-5.6-luna" },
{ t:now, agent:"codex", agentName:"Codex", provider:"session-unknown", providerName:"Local session", session_provider:"my-custom-route", model:"gpt-6-astra", source:"log", in:10, out:2, status:0, cost:0, priced:false },
];
[10000,10001,30000,30001,1250,0,undefined,900,-1].forEach((ms,i) => rows[i].ms=ms);
rows.push({t:now,agent:"codex",agentName:"Codex",provider:"codex",providerName:"ChatGPT",access:"subscription",model:"gpt-6-sol",in:10,out:2,status:200,ms:4000,cost:0,priced:false});
for (const engine of ["chromium", "webkit"]) {
test(engine + ": session account identities", async t => {
const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({channel:"chromium"}));
t.after(() => browser.close());
for (const lang of ["en", "zh"]) for (const theme of ["light", "dark"]) {
await t.test(lang + " / " + theme, async () => {
const page = await browser.newPage({viewport:{width:1200,height:900},reducedMotion:"reduce"});
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:"${theme}",web:false};`});
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}});
if (url.pathname === "/api/usage/requests") return json({period:"30d",rows,calls:rows.length,total:rows.length,offset:0,errors:0,input:100,output:20,cache_read:0,cache_write:0,cost:0,unpriced:rows.length,series:[],by:{},agents:[],providers:[]});
if (url.pathname === "/api/usage/quotas") return json([]);
if (url.pathname === "/api/usage") return json({calls:0,cost:0,series:[],agents:[],models:[]});
if (url.pathname === "/api/sessions") return json({sessions:[],dirs:[]});
if (url.pathname === "/api/sessions/stats") return json({days:[],agents:{}});
if (url.pathname === "/api/groups") return json({groups:[],models:[]});
if (url.pathname.startsWith("/api/")) return json({});
const file = path.join(assets, url.pathname === "/" ? "index.html" : url.pathname);
await route.fulfill({body:await fs.readFile(file),contentType:{".html":"text/html",".css":"text/css",".js":"text/javascript",".svg":"image/svg+xml"}[path.extname(file)]});
});
await page.goto("http://magpie.test/");
await page.locator('[data-view="usage"]').first().click();
await page.locator(":is(#usageTab .opt, .nav-s):visible").nth(1).click();
await page.locator(".led-row").first().waitFor();
const cells = page.locator(".led-row .where");
const localName = lang === "zh" ? "本地会话" : "Local session";
for (const [i, account] of [[0,"historical@example.com"],[1,"recorded@example.com"],[2,"creator@example.com"],[7,"reviewer@example.com"]]) {
const text = await cells.nth(i).textContent();
const official = [0,1,7].includes(i);
assert.equal(await cells.nth(i).locator(".where-name").textContent(),account);
assert.equal(await cells.nth(i).locator(".official").count(),official ? 1 : 0);
if (official) assert.equal(await cells.nth(i).locator(".official").textContent(),lang === "zh" ? "官方" : "OFFICIAL");
assert(text.includes(localName));
assert.equal(await cells.nth(i).locator(".access").count(),0,"account login does not establish a supplier route");
for (const inferred of ["OpenAI","Anthropic","custom","relay","官方登录","ChatGPT login"]) assert(!text.includes(inferred));
}
// A row whose file names both an account and a provider shows the
// account: local metadata identifies it, and the provider id is not
// read as the row's supplier (#557)
assert.equal(await cells.nth(7).locator(".where-name").textContent(),"reviewer@example.com");
assert((await cells.nth(7).textContent()).includes(localName));
for (const i of [3,5]) {
assert.equal(await cells.nth(i).textContent(),localName,"a file that names no provider says only Local session");
}
// A session file names the provider it was set up to call. That is
// the file's own record, not a route magpie saw, so the id is shown
// as the file carries it — never resolved to a provider's name,
// which would promote an unknown request to an official provider —
// and the row keeps saying it is a local session. An account, when
// local metadata identifies one, is shown instead (rows 0,1,2,7).
{
const cell = cells.nth(8);
assert.equal(await cell.locator(".where-name").textContent(),"my-custom-route","the recorded provider ID, as the file carries it");
assert((await cell.textContent()).includes(localName),"and it is still a local session");
assert.equal(await cell.locator(".local").count(),1);
assert((await cell.locator(".local").getAttribute("title")).includes(lang === "zh" ? "不是 magpie 看到的路由" : "not a route magpie saw"));
}
assert((await cells.nth(4).textContent()).includes("My Relay · relay.example"));
assert.equal(await cells.nth(4).locator(".access").textContent(),"API");
assert((await cells.nth(6).textContent()).startsWith("Archived relay"));
assert.equal(await cells.nth(6).locator(".src").count(),0,"an unknown gateway provider is not a local session");
for (const [i,band] of [[0,"fast"],[1,"slow"],[2,"slow"],[3,"long"],[4,"fast"],[7,"fast"]]) {
const cell=page.locator(".led-row .duration").nth(i);
assert((await cell.getAttribute("class")).includes("duration-"+band));
assert((await cell.getAttribute("title")).includes(lang === "zh" ? "耗时颜色" : "Duration colors"));
if (rows[i].source === "log") assert((await cell.textContent()).startsWith("≈"),"color must preserve estimated timing");
}
for (const i of [5,6,8]) assert.equal(await page.locator(".led-row .duration").nth(i).textContent(),"—");
const palette=await page.evaluate(() => {
const selectors=[".src.official",".src.local",".access-api",".access-subscription"];
const canvas=document.createElement("canvas"),ctx=canvas.getContext("2d");canvas.width=canvas.height=1;
const rgb=color=>{ctx.clearRect(0,0,1,1);ctx.fillStyle=color;ctx.fillRect(0,0,1,1);return [...ctx.getImageData(0,0,1,1).data].slice(0,3);};
const luminance=c=>c.map(v=>{v/=255;return v<=.04045?v/12.92:((v+.055)/1.055)**2.4}).reduce((a,v,i)=>a+v*[.2126,.7152,.0722][i],0);
return selectors.map(selector=>{const s=getComputedStyle(document.querySelector(selector)),a=luminance(rgb(s.color)),b=luminance(rgb(s.backgroundColor));return {color:s.color,contrast:(Math.max(a,b)+.05)/(Math.min(a,b)+.05)};});
});
assert.equal(new Set(palette.map(p=>p.color)).size,4,"source types have distinct colors");
assert(palette.every(p=>p.contrast>=4.5),"small badge text remains readable: "+JSON.stringify(palette));
if (process.env.MAGPIE_STYLE_SHOTS && lang === "zh" && engine === "chromium") {
await page.setViewportSize({width:1900,height:950});
await page.locator(".led-wrap").screenshot({path:process.env.MAGPIE_STYLE_SHOTS+"/badges-"+theme+".png"});
await page.setViewportSize({width:1200,height:900});
}
// beside the sidebar the cards above wrap and the row sits under the
// footer, where WebKit's click leaves it: the reader's wheel brings
// it up first
const rowTop = (await page.locator(".led-row").nth(2).boundingBox()).y;
await page.mouse.move(700, 400);
await page.mouse.wheel(0, rowTop - 400);
await page.waitForFunction((y) => document.querySelectorAll(".led-row")[2].getBoundingClientRect().top < y - 100, rowTop);
const detail = await reader.openRequest(page, page.locator(".led-row").nth(2));
assert((await detail.textContent()).includes("relay"),"raw provider ID remains available in details");
assert((await detail.textContent()).includes("creator@example.com"));
assert((await detail.textContent()).includes(lang === "zh" ? "不推断供应商" : "no service provider is inferred"));
await reader.closeRequest(page);
const review = page.locator(".led-row").nth(7);
assert.equal(await review.locator("td").nth(5).textContent(),"—","an estimate must not claim a served model");
assert.equal(await review.locator(".swap").count(),0,"price alias is not observed forwarding");
assert((await review.locator(".price-reference").textContent()).includes("gpt-5.6-luna"));
for (const width of [1200,440]) {
await page.setViewportSize({width,height:900});
// the table is fitted to the new width on a frame after the resize
// (ledFit, which may cut names shorter): look once it has been
await page.evaluate(() => new Promise(r => requestAnimationFrame(() => requestAnimationFrame(() => setTimeout(r, 100)))));
const layout = await cells.nth(7).evaluate(cell => {
const badges = [...cell.querySelectorAll(".source-badges .src")].map(n => n.getBoundingClientRect());
const bounds = cell.getBoundingClientRect();
const name = cell.querySelector(".where-name").getBoundingClientRect();
return badges.length === 2 && Math.abs(badges[0].top - badges[1].top) < 1 && badges[0].right < badges[1].left && badges.every(b => b.top >= name.bottom && b.left >= bounds.left && b.right <= bounds.right && b.bottom <= bounds.bottom);
});
assert(layout,"both badges share one row below the account at " + width);
}
await page.setViewportSize({width:1200,height:900});
await review.focus();
await page.keyboard.press("Enter");
const officialDetail = page.locator("#ledReqPane .led-detail");
await officialDetail.waitFor();
assert((await officialDetail.textContent()).includes(lang === "zh" ? "官方登录" : "Official login"));
assert((await cells.nth(7).locator(".official").getAttribute("title")).includes(lang === "zh" ? "不代表已确认这次请求" : "does not establish the route"));
assert.deepEqual(errors,[]);
await page.close();
});
}
});
}