diff --git a/e2e/console/fixtures.ts b/e2e/console/fixtures.ts index 6a18d430a..d279368b6 100644 --- a/e2e/console/fixtures.ts +++ b/e2e/console/fixtures.ts @@ -10,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 { DatabaseSync } from "node:sqlite"; import { test as base, expect, type Page } from "@playwright/test"; import { exportJWK, generateKeyPair, SignJWT } from "jose"; @@ -53,6 +54,8 @@ export interface RunningDaemon extends ConsoleDaemon { start(): Promise; /** Stops the daemon and deletes its home. */ dispose(options?: { readonly preservePages?: readonly Page[] }): Promise; + /** why: stats read completed minutes only, so this moves the usage rows two minutes back. */ + prepareHistoricalUsage(): Promise; } export interface StartOptions { @@ -388,6 +391,27 @@ export async function startDaemon( socketPath, start, stop, + async prepareHistoricalUsage() { + await stop(); + // why: the daemon writes history.db until its process exits, not until its socket goes. + await waitFor( + async () => + !(await Promise.all([...daemonPids].map((pid) => isProcessAlive(pid)))).some(Boolean), + { timeout: 20_000, interval: 100, label: "daemon process exited" }, + ); + 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..14cfc9e28 100644 --- a/e2e/console/usage.spec.ts +++ b/e2e/console/usage.spec.ts @@ -60,27 +60,19 @@ 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 +125,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);