Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions e2e/console/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -53,6 +54,8 @@ export interface RunningDaemon extends ConsoleDaemon {
start(): Promise<void>;
/** Stops the daemon and deletes its home. */
dispose(options?: { readonly preservePages?: readonly Page[] }): Promise<void>;
/** why: stats read completed minutes only, so this moves the usage rows two minutes back. */
prepareHistoricalUsage(): Promise<void>;
}

export interface StartOptions {
Expand Down Expand Up @@ -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)),
Expand Down
21 changes: 5 additions & 16 deletions e2e/console/overview.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,16 @@ async function waitingCount(host: RunningDaemon): Promise<number> {
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<void> {
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<void> {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand All @@ -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 = {
Expand Down
15 changes: 3 additions & 12 deletions e2e/console/usage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,19 @@ async function waitingCount(host: RunningDaemon): Promise<number> {
}

// 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<void> {
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<void> {
Expand Down Expand Up @@ -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);
Expand Down
Loading