import { mkdir, stat, utimes, writeFile } from "node:fs/promises"; import path from "node:path"; import { test, expect } from "@playwright/test"; import { resetData, resolvePath, testPaths } from "./helpers"; import { baseUrl } from "./baseUrl"; // /cleanup/space (lane space-suggestions): Refresh runs the space scan as a // job; the disks render, fullest marked; caption compression leads the corpus // disk; a suggestion's button runs the existing writer (here evict-clips, by // age) and a rescan no longer lists what it freed. A dry run answers in place // (compress-text's). Nothing here reaches a platform. const SLUG = "test-transcribe"; const DATA = `${testPaths().transcriptsDir}/channels/${SLUG}/data`; const RESULT = `${testPaths().transcriptsDir}/.space-suggestions.json`; const DAY = 86_400_000; async function seed(): Promise { await writeFile(resolvePath(`${DATA}/vidA/transcript.en.vtt`), "WEBVTT\n\n" + "00:00.000 --> 00:01.000\nhello\n\n".repeat(4000)); const clip = resolvePath(`${DATA}/vidB/clips/10.00-20.00.mp4`); await mkdir(path.dirname(clip), { recursive: true }); await writeFile(clip, Buffer.alloc(200_000, 1)); const old = new Date(Date.now() - 60 * DAY); await utimes(clip, old, old); return clip; } async function scan(page: import("@playwright/test").Page): Promise { const before = await stat(resolvePath(RESULT)).then((s) => s.mtimeMs, () => 0); await page.getByRole("region", { name: "space scan" }).getByRole("button", { name: "Refresh" }).click(); await expect .poll(async () => stat(resolvePath(RESULT)).then((s) => s.mtimeMs, () => 0), { timeout: 60_000 }) .toBeGreaterThan(before); // The file lands before the job's record says done: reload until the page // offers Refresh again rather than naming a running scan. await expect .poll(async () => { await page.reload(); return page.getByLabel("space scan running").count(); }, { timeout: 30_000 }) .toBe(0); } test("free up space: a scan, the disks, caption compression first, an eviction through its writer", async ({ page }) => { await resetData("one-transcribe-channel-with-audio"); const clip = await seed(); await fetch(`${baseUrl}/api/test/invalidate-cache`).catch(() => {}); await page.goto("/cleanup"); await expect(page.getByLabel("space summary")).toContainText("No space scan yet."); await page.getByRole("link", { name: "Free up space" }).first().click(); await expect(page).toHaveURL(/\/cleanup\/space$/); await expect(page.getByText("No scan yet.")).toBeVisible(); await scan(page); const corpus = page.getByLabel("disk corpus"); await expect(corpus).toBeVisible(); await expect(corpus).toContainText("fullest"); await expect(corpus).toContainText("The archive's:"); // Caption compression leads the corpus disk's list. const groups = page.getByRole("region", { name: "suggestions" }).locator("[aria-label^='group ']"); await expect(groups.first()).toHaveAttribute("aria-label", "group compress-captions"); const evict = page.getByLabel("group evict-clips"); await expect(evict).toContainText("clip window(s) untouched 30+ days"); // A dry run answers in place, and runs nothing. const captions = page.getByLabel("group compress-captions"); await captions.getByRole("button", { name: "Compress…" }).first().click(); await captions.getByRole("button", { name: "Dry run" }).click(); await expect(captions.getByLabel("dry run")).toContainText(SLUG); await captions.getByRole("button", { name: "Cancel" }).click(); // The eviction: the confirm names the ops call; Confirm runs evict-clips. await evict.getByRole("button", { name: "Free it…" }).first().click(); await expect(evict.getByRole("group")).toContainText("pnpm ops evict-clips"); await evict.getByRole("button", { name: "Confirm" }).click(); await expect(evict.getByRole("status")).toContainText("rescan to update"); await expect.poll(() => existsAbs(clip), { timeout: 30_000 }).toBe(false); // A rescan no longer lists it; the summary line on /cleanup names the disk. await page.goto("/cleanup/space"); await scan(page); await expect(page.getByLabel("group evict-clips")).toHaveCount(0); await page.goto("/cleanup"); await expect(page.getByLabel("space summary")).toContainText("Corpus disk:"); }); async function existsAbs(p: string): Promise { return stat(p).then(() => true, () => false); }