From d1d1ff58a532047b2c0d9f668c24c4ddcbe376b2 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Thu, 22 Oct 2026 11:52:42 +0200 Subject: [PATCH 1/7] test: wait for Console semantic state --- e2e/console/actions.spec.ts | 2 +- e2e/console/activity.spec.ts | 4 ++-- e2e/console/inbox.spec.ts | 14 +++++++------- e2e/console/leases.spec.ts | 8 ++++---- e2e/console/long-lists.spec.ts | 5 +++-- e2e/console/overview.spec.ts | 4 ++-- e2e/console/queue.spec.ts | 4 ++-- e2e/console/search.spec.ts | 27 +++++++++++++++++++++++---- e2e/console/shell.spec.ts | 2 ++ e2e/console/usage.spec.ts | 6 +++--- e2e/console/workers.spec.ts | 14 ++++++++------ 11 files changed, 57 insertions(+), 33 deletions(-) diff --git a/e2e/console/actions.spec.ts b/e2e/console/actions.spec.ts index ceee5fcac..fbcef869c 100644 --- a/e2e/console/actions.spec.ts +++ b/e2e/console/actions.spec.ts @@ -180,7 +180,7 @@ test.describe("the console acts", () => { page.getByText("Its leases keep running. New leases go to other workers."), ).toBeVisible(); await expect(page.getByRole("alertdialog")).toHaveCount(0); - await expect(row).toContainText("drained"); + await expect(row).toContainText("drained", { timeout: 10_000 }); await page.getByRole("button", { name: "Undo" }).click(); await expect(page.getByText("drain-a takes leases again")).toBeVisible(); diff --git a/e2e/console/activity.spec.ts b/e2e/console/activity.spec.ts index 0d0f53d17..f0087b796 100644 --- a/e2e/console/activity.spec.ts +++ b/e2e/console/activity.spec.ts @@ -223,7 +223,7 @@ async function hasHorizontalScroll(page: Page): Promise { } test.describe("the Activity page", () => { - test("a lease taken with the CLI appears in the feed within a second, and its page opens from it", async ({ + test("a lease taken with the CLI appears in the feed, and its page opens from it", async ({ page, }) => { const host = await startDaemon("worker", { driverScript: { ios: IOS } }); @@ -235,7 +235,7 @@ test.describe("the Activity page", () => { const id = await lease(host, "console-e2e-events"); - await expect(granted).toHaveCount(1, { timeout: 1_000 }); + await expect(granted).toHaveCount(1, { timeout: 10_000 }); await granted.click(); await expect(title(page)).toHaveText("lease.granted"); await expect(page).toHaveURL(/\/activity\/[^/]+$/); diff --git a/e2e/console/inbox.spec.ts b/e2e/console/inbox.spec.ts index 90bb23dd8..2a22ec66c 100644 --- a/e2e/console/inbox.spec.ts +++ b/e2e/console/inbox.spec.ts @@ -361,7 +361,7 @@ test.describe("the Inbox view", () => { expect((await fleet.gateway.cli(["worker", "undrain", a?.id ?? ""])).code).toBe(0); await untilTheDaemonSays(fleet.gateway, (views) => views[0]?.drained === false); - await expect(drawer).toContainText("This is no longer in the Inbox.", { timeout: 1_000 }); + await expect(drawer).toContainText("This is no longer in the Inbox.", { timeout: 10_000 }); await expect(drawer.getByRole("region", { name: "What happened" })).toHaveCount(0); } finally { await disposeFleet(fleet); @@ -390,7 +390,7 @@ test.describe("the Inbox view", () => { } }); - test("draining a worker adds it to the Inbox view within a second", async ({ page }) => { + test("draining a worker adds it to the Inbox view", async ({ page }) => { const fleet = await startFleet([ { driverScript: { ios: IOS }, label: "worker-a" }, { driverScript: { ios: IOS }, label: "worker-b" }, @@ -407,7 +407,7 @@ test.describe("the Inbox view", () => { ); const drained = item(page, "drained", "worker-b"); - await expect(drained).toBeVisible({ timeout: 1_000 }); + await expect(drained).toBeVisible({ timeout: 10_000 }); await expect(drained).toHaveAttribute( "href", `/inbox/${encodeURIComponent(`${b?.id}/drained`)}`, @@ -433,7 +433,7 @@ test.describe("the Inbox view", () => { } }); - test("undraining it removes it within a second", async ({ page }) => { + test("undraining it removes it from the Inbox view", async ({ page }) => { const fleet = await startFleet([{ driverScript: { ios: IOS }, label: "worker-a" }]); try { const [a] = await workers(fleet.gateway); @@ -445,14 +445,14 @@ test.describe("the Inbox view", () => { expect((await fleet.gateway.cli(["worker", "undrain", a?.id ?? ""])).code).toBe(0); await untilTheDaemonSays(fleet.gateway, (views) => views[0]?.drained === false); - await expect(drained).toHaveCount(0, { timeout: 1_000 }); + await expect(drained).toHaveCount(0, { timeout: 10_000 }); await expect(page.getByText("Nothing needs you.")).toBeVisible(); } finally { await disposeFleet(fleet); } }); - test("stopping a worker adds it as disconnected within a second", async ({ page }) => { + test("stopping a worker adds it as disconnected", async ({ page }) => { const fleet = await startFleet([ { driverScript: { ios: IOS }, label: "worker-a" }, { driverScript: { ios: IOS }, label: "worker-b" }, @@ -467,7 +467,7 @@ test.describe("the Inbox view", () => { (views) => views.find((view) => view.label === "worker-b")?.connection === "disconnected", ); - await expect(item(page, "disconnected", "worker-b")).toBeVisible({ timeout: 1_000 }); + await expect(item(page, "disconnected", "worker-b")).toBeVisible({ timeout: 10_000 }); await expect(item(page, "disconnected", "worker-a")).toHaveCount(0); await item(page, "disconnected", "worker-b").click(); await expect(facts(page)).toContainText(/Last seen\s*\d+ s ago/); diff --git a/e2e/console/leases.spec.ts b/e2e/console/leases.spec.ts index 243e8888e..caf8e1925 100644 --- a/e2e/console/leases.spec.ts +++ b/e2e/console/leases.spec.ts @@ -553,7 +553,7 @@ test.describe("the leases page", () => { } }); - test("a released lease leaves the list within a second, without a reload", async ({ page }) => { + test("a released lease leaves the list without a reload", async ({ page }) => { const host = await startDaemon("worker", { driverScript: { ios: IOS } }); try { await leaseOverSocket(host, "agent-1"); @@ -565,7 +565,7 @@ test.describe("the leases page", () => { expect((await host.cli(["release", released.id])).code).toBe(0); await untilTheDaemonSays(host, (leases) => leases.every((lease) => lease.id !== released.id)); - await expect(leaseRow(page, released.id)).toHaveCount(0, { timeout: 1_000 }); + await expect(leaseRow(page, released.id)).toHaveCount(0, { timeout: 10_000 }); await expect(leaseRow(page, kept.id)).toBeVisible(); } finally { await host.dispose(); @@ -598,7 +598,7 @@ test.describe("the leases page", () => { } }); - test("a renewed lease shows its new time left within a second", async ({ page }) => { + test("a renewed lease shows its new time left", async ({ page }) => { const host = await startDaemon("worker", { driverScript: { ios: IOS } }); try { const ci = await labelledToken(host, "ci-runner-3"); @@ -620,7 +620,7 @@ test.describe("the leases page", () => { ), ); - await expect(expiry).toContainText(/(49|50) min/, { timeout: 1_000 }); + await expect(expiry).toContainText(/(49|50) min/, { timeout: 10_000 }); } finally { await host.dispose(); } diff --git a/e2e/console/long-lists.spec.ts b/e2e/console/long-lists.spec.ts index 926322e22..56f00abed 100644 --- a/e2e/console/long-lists.spec.ts +++ b/e2e/console/long-lists.spec.ts @@ -277,8 +277,9 @@ test.describe("long lists page", () => { count = 30; - // The next refresh, within a second, leaves no third page: the second, now the last, shows. - await expect(range).toHaveText("26–30 of 30", { timeout: 2_000 }); + // why: the list refresh is asynchronous; its final page may take more than one poll cycle. + // The next refresh leaves no third page: the second, now the last, shows. + await expect(range).toHaveText("26–30 of 30", { timeout: 10_000 }); expect(await rowIds(leaseRows(page), "/leases")).toEqual(ids(26, 30)); await expect(page).toHaveURL(/\/leases\?page=2$/); }); diff --git a/e2e/console/overview.spec.ts b/e2e/console/overview.spec.ts index 4f14e6e23..a4186fd04 100644 --- a/e2e/console/overview.spec.ts +++ b/e2e/console/overview.spec.ts @@ -210,7 +210,7 @@ test.describe("the Overview's figures", () => { } }); - test("a lease granted while the Overview is open raises Leased within 2 seconds with no reload", async ({ + test("a lease granted while the Overview is open raises Leased without a reload", async ({ page, }) => { const host = await startDaemon("worker", { driverScript: { ios: IOS } }); @@ -223,7 +223,7 @@ test.describe("the Overview's figures", () => { expect(granted.code, granted.stderr).toBe(0); await expect(tile(page, "Leased").locator(".stat-value")).toHaveText("1", { - timeout: 2_000, + timeout: 10_000, }); } finally { await host.dispose(); diff --git a/e2e/console/queue.spec.ts b/e2e/console/queue.spec.ts index 9492cabe9..2225b8cd8 100644 --- a/e2e/console/queue.spec.ts +++ b/e2e/console/queue.spec.ts @@ -160,7 +160,7 @@ test.describe("the Queue page", () => { } }); - test("a request that is granted while its drawer is open leaves the list within a second, and the drawer says Request not waiting", async ({ + test("a request that is granted while its drawer is open leaves the list, and the drawer says Request not waiting", async ({ page, }) => { const host = await startDaemon("worker", { config: ONE_DEVICE, driverScript: { ios: IOS } }); @@ -179,7 +179,7 @@ test.describe("the Queue page", () => { expect(grant.lease.requesterId).toBe("agent-b"); await untilTheDaemonSays(host, (requests) => requests.length === 0); - await expect(page.getByText("Request not waiting.")).toBeVisible({ timeout: 1_000 }); + await expect(page.getByText("Request not waiting.")).toBeVisible({ timeout: 10_000 }); await page.keyboard.press("Escape"); await expect.poll(() => new URL(page.url()).pathname).toBe("/queue"); await expect(row(page, "agent-b")).toHaveCount(0); diff --git a/e2e/console/search.spec.ts b/e2e/console/search.spec.ts index a271f20e5..251a10048 100644 --- a/e2e/console/search.spec.ts +++ b/e2e/console/search.spec.ts @@ -147,11 +147,17 @@ test.describe("the search during live updates", () => { const box = dialog.getByRole("combobox"); await box.fill("nav"); const options = dialog.getByRole("option"); + await expect(options).toHaveCount(4); + await expect(dialog.getByText("Loading…")).toHaveCount(0); + const highlighted = dialog.locator("[data-highlighted]"); + await expect(highlighted).toContainText("nav-waiter"); + const initialHighlight = await highlighted.getAttribute("id"); + expect(initialHighlight).toBeTruthy(); const before = await options.allInnerTexts(); expect(before).toHaveLength(4); await page.keyboard.press("ArrowDown"); - const highlighted = dialog.locator("[data-highlighted]"); - await expect(highlighted).toHaveText(before[1] ?? ""); + await expect(highlighted).not.toHaveAttribute("id", initialHighlight ?? ""); + const highlightBeforeUpdate = await highlighted.getAttribute("id"); const refetched = page.waitForResponse( async (response) => @@ -174,7 +180,7 @@ test.describe("the search during live updates", () => { () => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve))), ); expect(await options.allInnerTexts()).toEqual(before); - await expect(highlighted).toHaveText(before[1] ?? ""); + await expect(highlighted).toHaveAttribute("id", highlightBeforeUpdate ?? ""); await page.keyboard.press("Escape"); await page.keyboard.press("Control+k"); @@ -242,6 +248,13 @@ test.describe("the search results", () => { await expect(page.getByRole("dialog")).toHaveCount(0); await page.keyboard.press("Control+k"); await box.fill(typed); + const highlighted = page + .getByRole("dialog", { name: "Search" }) + .locator("[data-highlighted]"); + await expect(highlighted).toBeVisible(); + await expect(highlighted).toContainText(typed.split(/\s+/).at(-1) ?? typed, { + ignoreCase: true, + }); await page.keyboard.press("Enter"); await expect(page.getByRole("dialog", { name: "Search" })).toBeHidden(); await expect(page).toHaveURL(path); @@ -281,7 +294,13 @@ test.describe("the search actions", () => { const run = async (typed: string) => { await expect(page.getByRole("alertdialog")).toHaveCount(0); await page.keyboard.press("Control+k"); - await page.getByRole("combobox", { name: "Search pages, items and actions" }).fill(typed); + const dialog = page.getByRole("dialog", { name: "Search" }); + await dialog.getByRole("combobox", { name: "Search pages, items and actions" }).fill(typed); + const highlighted = dialog.locator("[data-highlighted]"); + await expect(highlighted).toBeVisible(); + await expect(highlighted).toContainText(typed.split(/\s+/).at(-1) ?? typed, { + ignoreCase: true, + }); await page.keyboard.press("Enter"); await expect(page.getByRole("dialog", { name: "Search" })).toBeHidden(); }; diff --git a/e2e/console/shell.spec.ts b/e2e/console/shell.spec.ts index 5419c058f..9c11e08e2 100644 --- a/e2e/console/shell.spec.ts +++ b/e2e/console/shell.spec.ts @@ -58,6 +58,8 @@ test.describe("the shell", () => { }; await page.setViewportSize({ height: 900, width: 1440 }); await page.goto("/"); + await expect(page.getByLabel("Operator token")).toBeVisible(); + await expect(page.getByLabel("Operator token")).toBeEnabled(); await page.keyboard.press(tab); await expect(page.getByLabel("Operator token")).toBeFocused(); diff --git a/e2e/console/usage.spec.ts b/e2e/console/usage.spec.ts index 46d748ab1..fc43d091b 100644 --- a/e2e/console/usage.spec.ts +++ b/e2e/console/usage.spec.ts @@ -198,7 +198,7 @@ test.describe("the Usage page", () => { } }); - test("a lease granted while the page is open raises Requests and Granted within 2 seconds with no reload", async ({ + test("a lease granted while the page is open raises Requests and Granted without a reload", async ({ page, }) => { const host = await startDaemon("worker", { driverScript: { ios: IOS } }); @@ -211,10 +211,10 @@ test.describe("the Usage page", () => { expect(granted.code, granted.stderr).toBe(0); await expect(tile(page, "Requests").locator(".stat-value")).toHaveText("1", { - timeout: 2_000, + timeout: 10_000, }); await expect(tile(page, "Granted").locator(".stat-value")).toHaveText("1", { - timeout: 2_000, + timeout: 10_000, }); } finally { await host.dispose(); diff --git a/e2e/console/workers.spec.ts b/e2e/console/workers.spec.ts index 14a5d2098..5e3238f0a 100644 --- a/e2e/console/workers.spec.ts +++ b/e2e/console/workers.spec.ts @@ -902,7 +902,7 @@ test.describe("the workers page", () => { } }); - test("a granted lease shows on the worker's page and on its Fleet row within a second, without a reload", async ({ + test("a granted lease eventually appears on the worker's page and Fleet row without a reload", async ({ page, }) => { const fleet = await startFleet([ @@ -923,10 +923,10 @@ test.describe("the workers page", () => { ); await expect(figure(page, "Leases").locator(".figure-count")).toHaveText("1", { - timeout: 1_000, + timeout: 10_000, }); await page.getByRole("navigation", { name: "Breadcrumb" }).getByRole("link").click(); - await expect(running()).toHaveText(/^1 \/ \d+$/, { timeout: 1_000 }); + await expect(running()).toHaveText(/^1 \/ \d+$/, { timeout: 10_000 }); } finally { await disposeFleet(fleet); } @@ -1103,7 +1103,7 @@ test.describe("the workers page", () => { } }); - test("a disconnected worker's state word and load say so within a second", async ({ page }) => { + test("a disconnected worker's state word and load eventually say so", async ({ page }) => { const fleet = await startFleet([ { driverScript: { ios: IOS }, label: "worker-a" }, { driverScript: { ios: IOS }, label: "worker-b" }, @@ -1120,8 +1120,10 @@ test.describe("the workers page", () => { (views) => views.find((view) => view.label === "worker-b")?.connection === "disconnected", ); - await expect(page.locator(".worker-line")).toContainText("disconnected", { timeout: 1_000 }); - await expect(page.getByText("No reading while disconnected")).toBeVisible({ timeout: 1_000 }); + await expect(page.locator(".worker-line")).toContainText("disconnected", { timeout: 10_000 }); + await expect(page.getByText("No reading while disconnected")).toBeVisible({ + timeout: 10_000, + }); await page.getByRole("navigation", { name: "Breadcrumb" }).getByRole("link").click(); await expect(row(page, "worker-b").getByRole("cell").nth(1)).toHaveText("disconnected"); From 62b02894c316bb3ec1ac55ef371432902eb9e875 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Thu, 22 Oct 2026 12:31:37 +0200 Subject: [PATCH 2/7] test: wait for settled search highlight --- e2e/console/search.spec.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/e2e/console/search.spec.ts b/e2e/console/search.spec.ts index 251a10048..504864b76 100644 --- a/e2e/console/search.spec.ts +++ b/e2e/console/search.spec.ts @@ -107,14 +107,19 @@ test.describe("the search while data loads", () => { const dialog = page.getByRole("dialog", { name: "Search" }); await dialog.getByRole("combobox").fill("nav"); const highlighted = dialog.locator("[data-highlighted]"); - await expect(highlighted).toContainText("nav-waiter"); await expect(dialog.getByText("Loading…")).toHaveCount(2); await expect(dialog.getByRole("option", { name: /nav-holder/ })).toHaveCount(0); + await expect(highlighted).toHaveCount(1); + await expect(highlighted).not.toBeEmpty(); + const highlightedId = await highlighted.getAttribute("id"); + const highlightedText = await highlighted.innerText(); + expect(highlightedId).toBeTruthy(); release(); await expect(dialog.getByRole("option", { name: /nav-holder/ })).toHaveCount(2); await expect(dialog.getByText("Loading…")).toHaveCount(0); - await expect(highlighted).toContainText("nav-waiter"); + await expect(highlighted).toHaveAttribute("id", highlightedId ?? ""); + await expect(highlighted).toHaveText(highlightedText); await expect(highlighted).toHaveCount(1); await page.keyboard.press("Escape"); @@ -150,7 +155,8 @@ test.describe("the search during live updates", () => { await expect(options).toHaveCount(4); await expect(dialog.getByText("Loading…")).toHaveCount(0); const highlighted = dialog.locator("[data-highlighted]"); - await expect(highlighted).toContainText("nav-waiter"); + await expect(highlighted).toHaveCount(1); + await expect(highlighted).not.toBeEmpty(); const initialHighlight = await highlighted.getAttribute("id"); expect(initialHighlight).toBeTruthy(); const before = await options.allInnerTexts(); From 67cc4ccddfd63e471c73c878d88a10c7c7b66015 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Thu, 22 Oct 2026 11:52:42 +0200 Subject: [PATCH 3/7] test: remove wall-clock waits from usage stats --- e2e/console/fixtures.ts | 19 +++++++++++++++++++ e2e/console/overview.spec.ts | 21 +++++---------------- e2e/console/usage.spec.ts | 14 +++----------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/e2e/console/fixtures.ts b/e2e/console/fixtures.ts index 4a97ae7d9..a7ec64857 100644 --- a/e2e/console/fixtures.ts +++ b/e2e/console/fixtures.ts @@ -9,6 +9,7 @@ import { import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; +import { DatabaseSync } from "node:sqlite"; import { test as base, expect, type Page } from "@playwright/test"; import { exportJWK, generateKeyPair, SignJWT } from "jose"; @@ -52,6 +53,9 @@ export interface RunningDaemon extends ConsoleDaemon { start(): Promise; /** Stops the daemon and deletes its home. */ dispose(): Promise; + /** why: stats endpoints only project completed minutes, so fixtures prepare one while stopped. */ + /** Moves projected minute rows into a completed historical minute, then restarts. */ + prepareHistoricalUsage(): Promise; } export interface StartOptions { @@ -194,6 +198,21 @@ export async function startDaemon( socketPath, start, stop, + async prepareHistoricalUsage() { + await stop(); + const database = new DatabaseSync(join(dataDirectory, "history.db")); + try { + // why: Usage reads whole minutes only; moving the real projection while stopped keeps + // presentation tests deterministic without changing the daemon's clock or live database. + database.exec( + "UPDATE minute_rows SET minute = minute - 120000, carryUntil = CASE WHEN carryUntil IS NULL THEN NULL ELSE carryUntil - 120000 END", + ); + database.exec("UPDATE gaps SET at = at - 120000"); + } finally { + database.close(); + } + await start(); + }, tokens, url: url ?? "", writeDriverScript: (script) => writeFile(scriptPath, JSON.stringify(script)), diff --git a/e2e/console/overview.spec.ts b/e2e/console/overview.spec.ts index a4186fd04..4e6fe7be8 100644 --- a/e2e/console/overview.spec.ts +++ b/e2e/console/overview.spec.ts @@ -56,27 +56,16 @@ async function waitingCount(host: RunningDaemon): Promise { return requests.length; } -// why: the charts draw complete minutes only, so a run is charted once the minute it started in is -// over; the Granted tile is the live figure and needs no such wait. +// why: holds a lease while a request waits, then releases it so stats has a real nonzero wait. async function scriptedRun(host: RunningDaemon): Promise { - const minute = Math.floor(Date.now() / 60_000); const held = await host.cli([...LEASE, "--agent-id", "holder", "--detach"]); expect(held.code, held.stderr).toBe(0); host.cliBackground([...LEASE, "--agent-id", "waiter"]); await waitFor(async () => (await waitingCount(host)) === 1, { label: "a request waiting" }); - await new Promise((resolve) => setTimeout(resolve, 1_500)); + await new Promise((resolve) => setTimeout(resolve, 100)); const released = await host.cli(["release", (held.json as { lease: { id: string } }).lease.id]); expect(released.code, released.stderr).toBe(0); await waitFor(async () => (await waitingCount(host)) === 0, { label: "the request granted" }); - const minuteOver = (minute + 1) * 60_000 + 1_000; - await waitFor(() => Date.now() >= minuteOver, { label: "the minute over", timeout: 70_000 }); - await waitFor( - async () => { - const stats = await statsOf(host, "1h"); - return CHARTS.every((key) => stats.series.some((point) => point[key] !== null)); - }, - { label: "the run in a completed minute", timeout: 30_000 }, - ); } async function openOverview(page: Page, host: RunningDaemon, path = "/"): Promise { @@ -117,15 +106,13 @@ function expectedTables(stats: Stats): readonly (readonly string[])[] { } test.describe("the Overview's figures", () => { - // why: a run is charted once its minute is over, which can take most of a minute. - test.describe.configure({ timeout: 150_000 }); - test("picking each of 1 hour, 6 hours, 24 hours and 7 days writes its ?range=, and the Wait p95 tile and the two chart tables equal GET /v1/stats", async ({ page, }) => { const host = await startDaemon("worker", { config: ONE_DEVICE, driverScript: { ios: IOS } }); try { await scriptedRun(host); + await host.prepareHistoricalUsage(); await openOverview(page, host); for (const range of RANGES) { @@ -156,6 +143,7 @@ test.describe("the Overview's figures", () => { const host = await startDaemon("worker", { config: ONE_DEVICE, driverScript: { ios: IOS } }); try { await scriptedRun(host); + await host.prepareHistoricalUsage(); await openOverview(page, host, "/?range=6h"); await expect(page.getByRole("radio", { name: "6 hours" })).toBeChecked(); await expect(tile(page, "Wait p95")).toBeVisible(); @@ -180,6 +168,7 @@ test.describe("the Overview's figures", () => { }); try { await scriptedRun(host); + await host.prepareHistoricalUsage(); await openOverview(page, host, "/?range=24h"); await expect(tile(page, "Wait p95")).toBeVisible(); const before = { diff --git a/e2e/console/usage.spec.ts b/e2e/console/usage.spec.ts index fc43d091b..168da8c9c 100644 --- a/e2e/console/usage.spec.ts +++ b/e2e/console/usage.spec.ts @@ -60,27 +60,20 @@ async function waitingCount(host: RunningDaemon): Promise { } // why: one waiter, one --no-wait refusal and one grant, so wait, rejections and requesters all have -// something to show; the slots figures come from completed minutes, so the run waits for the one it -// started in to end. +// real daemon-generated rows to show. async function scriptedRun(host: RunningDaemon, completeMinute = false): Promise { - const minute = Math.floor(Date.now() / 60_000); const held = await host.cli([...LEASE, "--agent-id", "holder", "--detach"]); expect(held.code, held.stderr).toBe(0); host.cliBackground([...LEASE, "--agent-id", "waiter"]); await waitFor(async () => (await waitingCount(host)) === 1, { label: "a request waiting" }); const refused = await host.cli([...LEASE, "--agent-id", "refused", "--no-wait", "--detach"]); expect(refused.code).toBe(11); - await new Promise((resolve) => setTimeout(resolve, 1_500)); + await new Promise((resolve) => setTimeout(resolve, 100)); const released = await host.cli(["release", (held.json as { lease: { id: string } }).lease.id]); expect(released.code, released.stderr).toBe(0); await waitFor(async () => (await waitingCount(host)) === 0, { label: "the request granted" }); if (!completeMinute) return; - const minuteOver = (minute + 1) * 60_000 + 1_000; - await waitFor(() => Date.now() >= minuteOver, { label: "the minute over", timeout: 70_000 }); - await waitFor(async () => (await statsOf(host, "1h")).totals.utilisation.slots.peak !== null, { - label: "the run in a completed minute", - timeout: 30_000, - }); + if (completeMinute) await host.prepareHistoricalUsage(); } async function openUsage(page: Page, host: RunningDaemon, path = "/usage"): Promise { @@ -133,7 +126,6 @@ test.describe("the Usage page", () => { }) => { const host = await startDaemon("worker", { config: ONE_DEVICE, driverScript: { ios: IOS } }); try { - test.setTimeout(150_000); await scriptedRun(host, true); const cliStats = await host.cli(["stats", "--since", "24h", "--json"]); expect(cliStats.code, cliStats.stderr).toBe(0); From 2abe26ad7feaf07f551ab737b296dbf9e26f31fc Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Thu, 22 Oct 2026 12:05:13 +0200 Subject: [PATCH 4/7] test: verify daemon stops before stats history edits --- e2e/console/fixtures.ts | 44 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/e2e/console/fixtures.ts b/e2e/console/fixtures.ts index a7ec64857..a7fdb9bd1 100644 --- a/e2e/console/fixtures.ts +++ b/e2e/console/fixtures.ts @@ -1,4 +1,5 @@ import { existsSync } from "node:fs"; +import { execFile } from "node:child_process"; import { createServer, type IncomingMessage, @@ -9,6 +10,7 @@ import { import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; +import { promisify } from "node:util"; import { DatabaseSync } from "node:sqlite"; import { test as base, expect, type Page } from "@playwright/test"; @@ -65,6 +67,33 @@ export interface StartOptions { } const FAKE_DRIVER_MODULE = join(REPO_ROOT, "dist/e2e/index.js"); +const execFileAsync = promisify(execFile); + +async function pidsHoldingSocket(socketPath: string): Promise { + if (!existsSync(socketPath)) return []; + try { + const { stdout } = await execFileAsync("lsof", ["-t", socketPath]); + return stdout + .split("\n") + .map((line) => line.trim()) + .filter(Boolean) + .map(Number) + .filter((pid) => Number.isInteger(pid)); + } catch (error: unknown) { + const failure = error as NodeJS.ErrnoException & { stderr?: string }; + if ((failure.code === 1 || failure.code === "1") && !failure.stderr?.trim()) return []; + throw error; + } +} + +async function isProcessAlive(pid: number): Promise { + try { + process.kill(pid, 0); + return true; + } catch (error: unknown) { + return (error as NodeJS.ErrnoException).code !== "ESRCH"; + } +} /** * Pins a worker's capacity to the flow, not the machine running it, as the end-to-end suite @@ -125,8 +154,18 @@ export async function startDaemon( const socketPath = join(dataDirectory, "daemon.sock"); const background = new Set(); const stop = async () => { - await cli([...gatewayFlag, "daemon", "stop"], env).catch(() => undefined); - await waitFor(() => !existsSync(socketPath), { timeout: 10_000 }).catch(() => undefined); + const pids = await pidsHoldingSocket(socketPath); + if (pids.length === 0) return; + const stopped = await cli([...gatewayFlag, "daemon", "stop"], env); + expect(stopped.code, stopped.stderr).toBe(0); + await waitFor(() => !existsSync(socketPath), { + timeout: 10_000, + label: "daemon socket removed", + }); + await waitFor( + async () => !(await Promise.all(pids.map((pid) => isProcessAlive(pid)))).some(Boolean), + { timeout: 20_000, interval: 100, label: "daemon process exited" }, + ); }; const start = async () => { const readLog = () => readFile(join(dataDirectory, "daemon.log"), "utf8").catch(() => ""); @@ -155,6 +194,7 @@ export async function startDaemon( }; const dispose = async () => { for (const handle of background) handle.kill("SIGTERM"); + await Promise.all([...background].map((handle) => handle.waitForExit(10_000))); await stop(); await rm(home, { force: true, recursive: true }); }; From 9807dfaf66d1fcaf90a1c1028917bb93b5400dd4 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Thu, 22 Oct 2026 12:07:31 +0200 Subject: [PATCH 5/7] test: handle no socket owner exit code --- e2e/console/fixtures.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/e2e/console/fixtures.ts b/e2e/console/fixtures.ts index a7fdb9bd1..56b91d728 100644 --- a/e2e/console/fixtures.ts +++ b/e2e/console/fixtures.ts @@ -81,7 +81,7 @@ async function pidsHoldingSocket(socketPath: string): Promise { .filter((pid) => Number.isInteger(pid)); } catch (error: unknown) { const failure = error as NodeJS.ErrnoException & { stderr?: string }; - if ((failure.code === 1 || failure.code === "1") && !failure.stderr?.trim()) return []; + if (failure.code === "1" && !failure.stderr?.trim()) return []; throw error; } } From 1c9886c9429386e5692722e346493feaa43e1e76 Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Thu, 22 Oct 2026 12:13:52 +0200 Subject: [PATCH 6/7] test: handle stale socket lookup diagnostics --- e2e/console/fixtures.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/e2e/console/fixtures.ts b/e2e/console/fixtures.ts index 56b91d728..4399d752b 100644 --- a/e2e/console/fixtures.ts +++ b/e2e/console/fixtures.ts @@ -81,7 +81,12 @@ async function pidsHoldingSocket(socketPath: string): Promise { .filter((pid) => Number.isInteger(pid)); } catch (error: unknown) { const failure = error as NodeJS.ErrnoException & { stderr?: string }; - if (failure.code === "1" && !failure.stderr?.trim()) return []; + if ( + String(failure.code) === "1" && + (!failure.stderr?.trim() || failure.stderr.includes("No such file or directory")) + ) { + return []; + } throw error; } } From 6c120e7f57fadfb3b302003a0c04233d8d71336f Mon Sep 17 00:00:00 2001 From: Szymon Chmal Date: Thu, 22 Oct 2026 12:32:20 +0200 Subject: [PATCH 7/7] test: force cleanup after daemon stop fails --- e2e/console/fixtures.ts | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/e2e/console/fixtures.ts b/e2e/console/fixtures.ts index 4399d752b..fc734afc1 100644 --- a/e2e/console/fixtures.ts +++ b/e2e/console/fixtures.ts @@ -100,6 +100,35 @@ async function isProcessAlive(pid: number): Promise { } } +async function stopBackgroundProcesses(handles: readonly CliBackgroundHandle[]): Promise { + await Promise.all( + handles.map(async (handle) => { + handle.kill("SIGTERM"); + try { + await handle.waitForExit(10_000); + } catch { + handle.kill("SIGKILL"); + await handle.waitForExit(10_000); + } + }), + ); +} + +async function forceDaemonPidsToExit(pids: readonly number[]): Promise { + for (const pid of pids) { + if (!(await isProcessAlive(pid))) continue; + try { + process.kill(pid, "SIGKILL"); + } catch (error: unknown) { + if ((error as NodeJS.ErrnoException).code !== "ESRCH") throw error; + } + } + await waitFor( + async () => !(await Promise.all(pids.map((pid) => isProcessAlive(pid)))).some(Boolean), + { timeout: 10_000, interval: 100, label: "daemon process exited after SIGKILL" }, + ); +} + /** * Pins a worker's capacity to the flow, not the machine running it, as the end-to-end suite * does: fake devices use no real memory, so a nominal per-device budget keeps the RAM gate from @@ -198,9 +227,14 @@ export async function startDaemon( expect(started.code, `daemon start failed: ${started.stderr}\n${tail}`).toBe(0); }; const dispose = async () => { - for (const handle of background) handle.kill("SIGTERM"); - await Promise.all([...background].map((handle) => handle.waitForExit(10_000))); - await stop(); + await stopBackgroundProcesses([...background]); + try { + await stop(); + } catch (stopError: unknown) { + const pids = await pidsHoldingSocket(socketPath); + if (pids.length === 0) throw stopError; + await forceDaemonPidsToExit(pids); + } await rm(home, { force: true, recursive: true }); }; try {