import { test, expect } from "@playwright/test";
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import * as urls from "./urls";
// Release 34: the phrase finder (/tools/phrases). Two phrases over the fixture
// corpus (testchan's cue files say "The sweep fixture opens.", "The deliver
// fixture opens.", "The editor fetch fixture speaks.", "The reuse fixture
// begins here." …) in one search, run as a job: counts, the year bars, the
// contexts with their transcript read in place, the comparison, the last
// search's settings on the next visit, a suggestion from a candidates file,
// and Create montage landing on a new project seeded from that search.
const HERE = path.dirname(fileURLToPath(import.meta.url));
const FIXTURE = path.join(HERE, "..", ".e2e-song");
test("the phrase finder searches two phrases in one pass, shows the numbers and contexts, and makes a montage", async ({ page }) => {
const slug = "phrase-finder-probe";
const dir = path.join(FIXTURE, slug);
// A catchphrase report's candidates, as the report writes them (REPORTS_ROOT/
/candidates.json).
const cand = path.join(FIXTURE, "phrase-finder-candidates");
rmSync(dir, { recursive: true, force: true });
mkdirSync(cand, { recursive: true });
writeFileSync(path.join(cand, "candidates.json"), JSON.stringify({ scanned: [{ key: "whole sentence", regex: String.raw`\bwhole sentence\b`, instances: 1 }] }));
try {
await page.goto(urls.phraseFinderHref());
await expect(page.locator("[data-rail-sub=tools] a[aria-current=page]")).toHaveText("phrases");
// Plain words, and an expression.
await page.locator("[data-phrase-text]").first().fill("fixture opens");
await expect(page.locator("[data-phrase-source]").first()).toContainText("as words");
await page.locator("[data-phrase-add]").click();
await page.locator("[data-phrase-text]").nth(1).fill("fixture (speaks|begins)");
await expect(page.locator("[data-phrase-source]").nth(1)).toContainText("as written: /fixture (speaks|begins)/");
// A bad expression says so and cannot be searched.
await page.locator("[data-phrase-add]").click();
await page.locator("[data-phrase-text]").nth(2).fill("fixture (opens");
await expect(page.locator("[data-phrase-error]")).toContainText("not a regular expression");
await page.locator("[data-phrase-remove]").nth(2).click();
// A suggestion adds a row.
await page.locator("[data-phrase-suggestions] summary").click();
await page.locator("[data-phrase-suggestion='whole sentence']").click();
await expect(page.locator("[data-phrase-text]")).toHaveCount(3);
await expect(page.locator("[data-phrase-source]").nth(2)).toContainText("whole sentence · as written");
await expect(page.locator("[data-phrase-search]")).toBeDisabled();
await page.locator("[data-phrase-channel-filter]").fill("testch");
await page.locator("[data-phrase-channel=testchan] input").check();
await expect(page.locator("[data-phrase-picked=testchan]")).toBeVisible();
await page.locator("[data-phrase-search]").click();
const done = page.locator("[data-phrase-state=done]");
await expect(done).toBeVisible({ timeout: 60_000 });
expect(page.url()).toContain("/tools/phrases?scan=ps-");
await expect(page.locator("[data-phrase-compare-row]")).toHaveCount(3);
const opens = page.locator("[data-phrase-result=p1]");
const n = Number(await opens.locator("[data-phrase-count]").getAttribute("data-phrase-count"));
expect(n).toBeGreaterThanOrEqual(2);
await expect(opens.locator("[data-phrase-year='2025']")).toHaveAttribute("data-count", String(n));
await expect(opens.locator("[data-phrase-other]")).toHaveAttribute("data-phrase-other", String(n));
await expect(opens.locator("[data-phrase-sample]")).toHaveCount(Math.min(n, 10));
await expect(opens.locator("[data-phrase-sample]").first()).toContainText(/fixture opens/i);
await expect(opens.locator("[data-phrase-sample]").first()).toContainText("1 Jan 2025");
// The transcript where it was said, its cue lit.
await opens.locator("[data-phrase-transcript]").first().click();
await expect(opens.locator("[data-phrase-cue=lit]").first()).toContainText(/fixture opens/i);
// Play mounts the cited clip (a window on disk, or Fetch clip).
await opens.locator("[data-phrase-play]").first().click();
await expect(opens.locator("[data-cited-clip]").first()).toBeVisible();
const speaks = page.locator("[data-phrase-result=p2]");
expect(Number(await speaks.locator("[data-phrase-count]").getAttribute("data-phrase-count"))).toBeGreaterThanOrEqual(2);
// The next visit opens on this search's settings.
await page.goto(urls.phraseFinderHref());
await expect(page.locator("[data-phrase-text]").first()).toHaveValue("fixture opens");
await expect(page.locator("[data-phrase-picked=testchan]")).toBeVisible();
await expect(page.locator("[data-phrase-recent]").first()).toBeVisible();
await page.locator("[data-phrase-recent]").first().click();
await expect(done).toBeVisible();
// Create montage: the slug prefilled, a project seeded from this search.
const form = page.locator("[data-phrase-montage=p1]");
await expect(form.locator("[data-phrase-slug]")).toHaveValue("fixture-opens-montage");
await form.locator("[data-phrase-slug]").fill(slug);
await form.locator("[data-phrase-create]").click();
await page.waitForURL(`**/projects/${slug}`);
const doc = JSON.parse(readFileSync(path.join(dir, "video.manifest.json"), "utf8"));
const clips = doc.timeline as Array<{ id: string; channel: string; montage: { n: number; of: number; match: string } }>;
expect(clips).toHaveLength(n);
clips.forEach((c, k) => {
expect(c.id).toBe(`s${String(k + 1).padStart(2, "0")}`);
expect(c.channel).toBe("testchan");
expect(c.montage.match).toMatch(/fixture opens/i);
});
const scan = JSON.parse(readFileSync(path.join(dir, "phrase-scan.json"), "utf8"));
expect(scan.channels).toEqual(["testchan"]);
expect(scan.instances).toHaveLength(n);
} finally {
rmSync(dir, { recursive: true, force: true });
rmSync(cand, { recursive: true, force: true });
}
expect(existsSync(dir)).toBe(false);
});