|
| 1 | +// #157: the exact latest-turn provider accounting rows collapse by default in |
| 2 | +// the right panel behind a one-line gist; the exact context percentage and the |
| 3 | +// estimated visible-content bar stay visible; the toggle is remembered per client. |
| 4 | +import { createRequire } from "node:module" |
| 5 | +import { mkdtemp, rm } from "node:fs/promises" |
| 6 | +import { tmpdir } from "node:os" |
| 7 | +import path from "node:path" |
| 8 | +import { startPanelPreview } from "./composer-panel-preview.mjs" |
| 9 | +import { directory } from "./composer-fixture.mjs" |
| 10 | + |
| 11 | +const { chromium, _electron, expect } = createRequire(new URL("../../app/package.json", import.meta.url))("@playwright/test") |
| 12 | +const preview = await startPanelPreview() |
| 13 | +const fixture = preview.fixture |
| 14 | +fixture.messages[1].info.time.completed = 3 |
| 15 | +fixture.messages[1].info.tokens = { input: 3, output: 608, reasoning: 46, cache: { read: 471919, write: 491 } } |
| 16 | +fixture.messagesB = [ |
| 17 | + { info: { id: "b_1", sessionID: "ses_b", role: "user", agent: "build", model: { providerID: "fixture", modelID: "test" }, time: { created: 1 } }, |
| 18 | + parts: [{ id: "bp_1", messageID: "b_1", sessionID: "ses_b", type: "text", text: "hello" }] }, |
| 19 | + { info: { id: "b_2", sessionID: "ses_b", role: "assistant", agent: "build", providerID: "fixture", modelID: "test", parentID: "b_1", |
| 20 | + path: { cwd: directory, root: directory }, cost: 0, tokens: { input: 0, output: 0, reasoning: 0, cache: { read: 0, write: 0 } }, time: { created: 2, completed: 3 } }, |
| 21 | + parts: [{ id: "bp_2", messageID: "b_2", sessionID: "ses_b", type: "text", text: "hi" }] }, |
| 22 | +] |
| 23 | +const providers = { providers: [{ id: "fixture", models: { test: { id: "test", limit: { context: 500000 } } } }], default: {} } |
| 24 | +const profile = process.env.ES_DESKTOP_EXECUTABLE ? await mkdtemp(path.join(tmpdir(), "context-accounting-")) : undefined |
| 25 | +const app = profile ? await _electron.launch({ executablePath: process.env.ES_DESKTOP_EXECUTABLE, |
| 26 | + env: { ...process.env, ELECTRON_RUN_AS_NODE: "", ES_DESKTOP_USER_DATA: profile, ES_DESKTOP_RENDERER: "", |
| 27 | + OPENCODE_URL: fixture.url, ES_DASHBOARD_URL: fixture.url, OPENCODE_SERVER_PASSWORD: "" } }) : undefined |
| 28 | +const browser = app ? undefined : await chromium.launch({ channel: "chrome", headless: true }) |
| 29 | +const page = app ? await app.firstWindow() : await browser.newPage({ viewport: { width: 1440, height: 900 } }) |
| 30 | +if (app) { |
| 31 | + await page.waitForURL("http://127.0.0.1:*/") |
| 32 | + preview.origin = new URL(page.url()).origin |
| 33 | + preview.url = `${preview.origin}/session/ses_a?directory=${encodeURIComponent(directory)}` |
| 34 | +} |
| 35 | +await page.context().route("**/*", (route) => new URL(route.request().url()).origin === preview.origin ? route.continue() : route.abort()) |
| 36 | +await page.route("**/config/providers*", (route) => route.fulfill({ json: providers })) |
| 37 | +const panel = page.locator(".session-info") |
| 38 | +const disclosure = panel.locator(".ctx-accounting-disclosure") |
| 39 | +const summary = disclosure.locator("> summary") |
| 40 | +const rows = panel.locator(".ctx-provider li") |
| 41 | +const shot = async (name) => { if (process.env.CONTEXT_ACCOUNTING_SHOTS) await page.screenshot({ path: `${process.env.CONTEXT_ACCOUNTING_SHOTS}-${name}.png` }) } |
| 42 | +try { |
| 43 | + await page.goto(preview.url, { waitUntil: "domcontentloaded" }) |
| 44 | + // exact percent, bar and legend remain visible while the rows are collapsed |
| 45 | + await expect(panel.locator(".ctx-exact")).toContainText("473k / 500k (95%)") |
| 46 | + await expect(panel.locator(".ctx-exact.high")).toHaveCount(1) |
| 47 | + await expect(panel.locator(".ctx-bar")).toBeVisible() |
| 48 | + await expect(panel.locator(".ctx-legend li", { hasText: "assistant text" })).toBeVisible() |
| 49 | + await expect(disclosure).not.toHaveAttribute("open") |
| 50 | + await expect(summary).toHaveAttribute("aria-label", "exact latest-turn provider accounting") |
| 51 | + await expect(summary.locator(".ctx-gist")).toHaveText("cache read 100% · output 608") |
| 52 | + await expect(panel.locator(".ctx-provider")).not.toBeVisible() |
| 53 | + expect(await panel.evaluate((el) => el.innerText.includes("471,919"))).toBe(false) |
| 54 | + const collapsedHeight = await panel.locator("[data-section='context']").evaluate((el) => el.getBoundingClientRect().height) |
| 55 | + await shot("collapsed") |
| 56 | + |
| 57 | + // keyboard expand reveals every exact class with numeric and percentage detail |
| 58 | + await summary.focus() |
| 59 | + await page.keyboard.press("Enter") |
| 60 | + await expect(disclosure).toHaveAttribute("open", "") |
| 61 | + await expect(rows).toHaveCount(5) |
| 62 | + await expect(rows.filter({ hasText: "cache read" })).toContainText("471,919") |
| 63 | + await expect(rows.filter({ hasText: "cache read" })).toContainText("100%") |
| 64 | + await expect(rows.filter({ hasText: "cache write" })).toContainText("491") |
| 65 | + await expect(rows.filter({ hasText: "uncached input" })).toContainText("3") |
| 66 | + await expect(rows.filter({ hasText: "generated output" })).toContainText("608") |
| 67 | + await expect(rows.filter({ hasText: "reasoning" })).toContainText("46") |
| 68 | + await expect(rows.filter({ hasText: "cache read" }).locator(".mono").first()).toHaveAttribute("title", "471,919 tokens") |
| 69 | + await expect(panel.locator(".ctx-accounting .ctx-caption")).toContainText("provider accounting classes, not content sources") |
| 70 | + expect(await panel.locator("[data-section='context']").evaluate((el) => el.getBoundingClientRect().height)).toBeGreaterThan(collapsedHeight + 40) |
| 71 | + await shot("expanded") |
| 72 | + |
| 73 | + // the open state persists per client across reload and across sessions |
| 74 | + await page.reload({ waitUntil: "domcontentloaded" }) |
| 75 | + await expect(disclosure).toHaveAttribute("open", "") |
| 76 | + await expect(rows.filter({ hasText: "cache read" })).toBeVisible() |
| 77 | + expect(await page.evaluate(() => localStorage.getItem("es-app-ctx-accounting-open"))).toBe("1") |
| 78 | + await summary.click() |
| 79 | + await expect(disclosure).not.toHaveAttribute("open") |
| 80 | + await page.reload({ waitUntil: "domcontentloaded" }) |
| 81 | + await expect(disclosure).not.toHaveAttribute("open") |
| 82 | + await expect(summary.locator(".ctx-gist")).toHaveText("cache read 100% · output 608") |
| 83 | + |
| 84 | + // unknown usage: gist says so, no fabricated rows |
| 85 | + await page.goto(`${preview.origin}/session/ses_b?directory=${encodeURIComponent(directory)}`, { waitUntil: "domcontentloaded" }) |
| 86 | + await expect(panel.locator(".ctx-exact")).toContainText("latest turn context unknown") |
| 87 | + await expect(summary.locator(".ctx-gist")).toHaveText("unknown") |
| 88 | + await summary.click() |
| 89 | + await expect(panel.locator(".ctx-accounting .ctx-caption")).toHaveText("provider usage unknown") |
| 90 | + await expect(rows).toHaveCount(0) |
| 91 | + await summary.click() |
| 92 | + await expect(disclosure).not.toHaveAttribute("open") |
| 93 | + |
| 94 | + // mobile + RTL: gist stays on one line without horizontal overflow |
| 95 | + await page.goto(preview.url, { waitUntil: "domcontentloaded" }) |
| 96 | + await page.setViewportSize({ width: 390, height: 844 }) |
| 97 | + await page.evaluate(() => { document.documentElement.dir = "rtl" }) |
| 98 | + await expect(summary.locator(".ctx-gist")).toBeVisible() |
| 99 | + expect(await panel.evaluate((el) => el.scrollWidth <= el.clientWidth + 1)).toBe(true) |
| 100 | + await shot("mobile-rtl") |
| 101 | + |
| 102 | + expect(fixture.calls.filter((call) => !["GET", "HEAD", "OPTIONS"].includes(call.method))).toEqual([]) |
| 103 | + console.log(`PASS context accounting ${app ? "native" : "browser"}: collapsed by default with gist, exact percent/bar/legend visible, keyboard expand with exact rows, persisted toggle, unknown handling, mobile RTL, zero mutations`) |
| 104 | +} finally { |
| 105 | + await app?.close() |
| 106 | + await browser?.close() |
| 107 | + await preview.close() |
| 108 | + if (profile) await rm(profile, { recursive: true, force: true }) |
| 109 | +} |
0 commit comments