import { mkdir, rm, symlink, writeFile } from "node:fs/promises"; import { join } from "node:path"; import { test, expect, type Page } from "@playwright/test"; import { baseUrl } from "./baseUrl"; import { readJson, resetData, resolvePath, testPaths, writeSettings } from "./helpers"; // A DRIVE THAT IS NOT ANSWERING, ONE THAT IS SLOW, ONE THAT IS NOT MOUNTED — // and that /storage and the dashboard tell them apart (lane drive-stall). // // NO FIXTURE HANGS A MOUNT. `/api/test/drive-health` records what a detector // would have said about a configured location (a stall, or the watchdog's // slow refusal) into the editor's health record, which every gate, page and // poll reads; the health pass then sees the fixture's drive answering and // clears a stall after two clean passes (15–30 s), so each step marks the // drive again just before it looks. "Not mounted" is real: the location's // root is removed. The detectors and the state machine are unit-tested // (common/lib/storageHealth.test.ts, controller/storageWatch.test.ts). const AUTH = { authorization: "Bearer test-worker-token" }; const SLUG = "test-youtube"; const VIDEO = "20240101_test1234567"; const channelDir = () => resolvePath(`${testPaths().transcriptsDir}/channels/${SLUG}`); // The fixture channel's media tier on `root`, as a finished move leaves it // (storage-locations.spec.ts builds it the same way). async function relocateOnDisk(root: string): Promise { const target = join(root, SLUG, "media"); await mkdir(join(target, VIDEO), { recursive: true }); await writeFile(join(target, VIDEO, "audio.mp3"), "ID3audio"); await symlink( join("..", "..", "media", VIDEO, "audio.mp3"), join(channelDir(), "data", VIDEO, "audio.mp3"), ); await symlink(target, join(channelDir(), "media")); const config = await readJson>( `${testPaths().transcriptsDir}/channels/${SLUG}/config.json`, ); await writeFile( join(channelDir(), "config.json"), JSON.stringify({ ...config, mediaDir: target }, null, 2) + "\n", ); await fetch(`${baseUrl}/api/test/invalidate-cache`).catch(() => {}); } async function mark(id: string, state: "stalled" | "slow"): Promise { const res = await fetch(`${baseUrl}/api/test/drive-health`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ id, state }), }); expect(res.ok, await res.clone().text()).toBe(true); } async function setUp(root: string): Promise { await resetData("one-youtube-channel-with-data"); await mkdir(root, { recursive: true }); await relocateOnDisk(root); await writeSettings({ adminTitle: "Test Admin", minFreeDiskGB: 0, storage: { locations: [{ id: "cold", label: "Cold", root, autoRepoint: false }], defaultLocationId: "cold", }, }); } function row(page: Page, id: string) { return page.getByLabel(`storage location: ${id}`); } test("a stalled drive reads 'not answering' on /storage and the dashboard; an unmounted one 'not mounted'", async ({ page, }, testInfo) => { test.setTimeout(120_000); const root = testInfo.outputPath("platter"); await setUp(root); // --- not answering ------------------------------------------------------ await mark("cold", "stalled"); await page.goto("/storage"); const cold = row(page, "cold"); await expect(cold.getByLabel("location status")).toHaveText("Not answering"); await expect(cold.getByLabel("location drive")).toHaveText(/^not answering since \d\d:\d\d — /); await expect(cold.getByLabel("location slow")).toHaveCount(0); await page.setViewportSize({ width: 1366, height: 768 }); await cold.screenshot({ path: testInfo.outputPath("storage-not-answering.png") }); await mark("cold", "stalled"); await page.goto("/"); const card = page.getByLabel("drive cold", { exact: true }); await expect(card).toBeVisible(); await expect(card).toHaveAttribute("data-state", "stalled"); await expect(card).toHaveAttribute("role", "alert"); await expect(card).toContainText("The media drive Cold is not answering"); await expect(page.getByLabel("drive cold health")).toHaveText(/^not answering since \d\d:\d\d/); await expect(card.getByRole("link", { name: "1 channel(s) on it" })).toHaveAttribute( "href", /^\/channels\?location=cold(&|$)/, ); await page.setViewportSize({ width: 1366, height: 768 }); await page.screenshot({ path: testInfo.outputPath("dashboard-not-answering.png") }); // --- the drive answers again: no banner ----------------------------------- await fetch(`${baseUrl}/api/test/invalidate-cache`); await page.goto("/"); await expect(page.getByRole("heading", { name: "Dashboard" })).toBeAttached(); await expect(page.getByLabel("drive health")).toHaveCount(0); // --- not mounted (the root is gone; its channel's media was on it) -------- await rm(root, { recursive: true, force: true }); await fetch(`${baseUrl}/api/test/invalidate-cache`); await page.goto("/"); const gone = page.getByLabel("drive cold", { exact: true }); await expect(gone).toHaveAttribute("data-state", "unmounted"); await expect(gone).toContainText("The media drive Cold is not mounted"); await expect(page.getByLabel("drive cold health")).toHaveText("not mounted"); await page.goto("/storage"); await expect(row(page, "cold").getByLabel("location status")).not.toHaveText("Not answering"); await expect(row(page, "cold").getByLabel("location drive")).toHaveText("not mounted"); }); test("a slow drive stays Available with a slow chip, and its dashboard card is a status, not an alert", async ({ page, }, testInfo) => { test.setTimeout(120_000); const root = testInfo.outputPath("platter"); await setUp(root); await mark("cold", "slow"); await page.goto("/storage"); const cold = row(page, "cold"); await expect(cold.getByLabel("location status")).toHaveText("Available"); await expect(cold.getByLabel("location slow")).toHaveText("slow"); await expect(cold.getByLabel("location drive")).toHaveText( /^slow at \d\d:\d\d — a read in the editor took 2.4 s$/, ); await page.setViewportSize({ width: 1366, height: 768 }); await cold.screenshot({ path: testInfo.outputPath("storage-slow.png") }); await mark("cold", "slow"); await page.goto("/"); const card = page.getByLabel("drive cold", { exact: true }); await expect(card).toHaveAttribute("data-state", "slow"); await expect(card).toHaveAttribute("role", "status"); await expect(card).toContainText("The media drive Cold is answering slowly"); await page.setViewportSize({ width: 1366, height: 768 }); await page.screenshot({ path: testInfo.outputPath("dashboard-slow.png") }); // `pnpm ops get storage` reports the same view per location. await mark("cold", "slow"); const res = await page.request.get(`${baseUrl}/api/ops/storage`, { headers: AUTH }); expect(res.ok(), await res.text()).toBe(true); const body = (await res.json()) as { storage: { rows: Array<{ id: string; drive?: { state: string; text: string; lastOkAt?: number } }> }; }; const drive = body.storage.rows.find((r) => r.id === "cold")?.drive; expect(drive?.state).toBe("slow"); expect(drive?.text).toMatch(/^slow at /); expect(body.storage.rows.find((r) => r.id === "internal")?.drive).toBeUndefined(); });