import { test, expect, type FrameLocator, type Page } from "@playwright/test";
import { copyFileSync, existsSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import * as urls from "./urls";
// ---------------------------------------------------------------------------
// THREAD READS (report-to-video/post-thread.mjs; knobs.md, "Thread reads";
// operator, 4 Nov: "when narrating a reply parent you're just narrating hidden
// text ... show the parent in full then transition in the reply as the parent
// shrinks and goes dim"). Fixture: e2e/fixtures/post-narration-fixture.mjs's
// p-13, made a reply here: the post it answers read first (three parts), then
// his reply (two parts) -- as the narration batch assembles a read.
//
// THREAD_READS_SHOTS=
screenshots there
// ---------------------------------------------------------------------------
const HERE = path.dirname(fileURLToPath(import.meta.url));
const DIR = path.join(HERE, "..", ".e2e-song", "reports", "post-narration-fixture");
const MANIFEST = path.join(DIR, "video.manifest.json");
const PROJECT = "reports/post-narration-fixture";
const SHOTS = process.env.THREAD_READS_SHOTS ?? null;
const say = (i: number) => `Paragraph ${i} of the neighbour's post goes on about the noise from the street, ${"and the lights in the hall that keep going out without any warning at all, ".repeat(2)}until it stops at mark ${i}.`;
const PARENT = [1, 2, 3].map(say).join("\n\n");
const REPLY = "That is exactly what I was saying yesterday morning to everyone.\n\nWe should have bought the generator back in the spring.";
test.beforeAll(() => {
if (!existsSync(`${MANIFEST}.pristine`)) copyFileSync(MANIFEST, `${MANIFEST}.pristine`);
});
test.beforeEach(() => {
test.setTimeout(120_000);
copyFileSync(`${MANIFEST}.pristine`, MANIFEST);
rmSync(path.join(DIR, "revisions"), { recursive: true, force: true });
rmSync(path.join(DIR, "notes.json"), { force: true });
// p-13 a reply, read [the post it answers] + [his words], part by part (3 s a part, the batch's pauses).
const m = JSON.parse(readFileSync(MANIFEST, "utf8"));
const e = m.timeline.find((x: { id: string }) => x.id === "p-13");
e.text = REPLY;
e.parents = [{ id: "900", author: "Some Neighbour", handle: "neighbour_one", date: "2026-10-29T10:00:00.000Z", text: PARENT }];
const paras = [...PARENT.split("\n\n").map((t, k) => ({ para: k, text: t })), ...REPLY.split("\n\n").map((t, k) => ({ para: 100 + k, text: t }))];
const starts = [0, 3.6, 7.2, 11, 14.6];
e.narration = {
script: REPLY,
takes: [{ file: "narration/posts/11/take-1.orpheus-zac.wav", engine: "orpheus", voice: "leo+zac", seconds: 17.6, script: `${PARENT}\n\n${REPLY}`, parts: paras.map((p, i) => ({ n: i + 1, ...p, start: starts[i], seconds: 3 })) }],
pick: 0,
};
writeFileSync(MANIFEST, JSON.stringify(m, null, 2) + "\n");
});
/** A block's box against the card's window (the words' window), in the card page. */
const inWindow = (frame: FrameLocator, k: string) =>
frame.locator("[data-post-words]").evaluate((w, k) => {
const r = w.getBoundingClientRect();
const b = (w.querySelector(`[data-post-block="${k}"]`) as HTMLElement).getBoundingClientRect();
return { top: b.top - r.top, bottom: b.bottom - r.top, height: r.height, op: (w.querySelector(`[data-post-block="${k}"]`) as HTMLElement).style.opacity };
}, k);
const tileOf = (frame: FrameLocator, scope: string) =>
frame.locator(`[data-thread-slot="${scope}"]`).evaluate((el) => {
const t = el.querySelector("[data-thread-tile]") as HTMLElement;
const w = document.querySelector("[data-post-words]")!.getBoundingClientRect();
const r = el.getBoundingClientRect();
return { state: el.getAttribute("data-thread-state"), transform: t.style.transform, opacity: t.style.opacity, top: r.top - w.top, bottom: r.bottom - w.top };
});
/** Play opened paused `at` seconds into p-13. */
async function playAt(page: Page, at: number) {
await page.goto(urls.playHref(PROJECT, { bare: true, clip: "p-13", at }));
const player = page.locator('[data-post-play="p-13"]');
await expect(player).toHaveAttribute("data-post-thread", "2", { timeout: 15_000 });
const frame = page.frameLocator('[data-post-card-frame="read"]');
await expect(frame.locator("body")).toHaveAttribute("data-post-thread", "2", { timeout: 15_000 });
// Play opens there (give or take its first tick).
await expect.poll(async () => Math.abs(Number(await player.getAttribute("data-post-s")) - at)).toBeLessThan(0.3);
return { player, frame };
}
test("NEVER NARRATE HIDDEN TEXT, in Play: the post it answers in full and lit while its parts play -- in the window, panned to -- his reply hidden under it", async ({ page }) => {
await page.setViewportSize({ width: 1366, height: 1000 });
for (const [at, part] of [[1.5, 0], [5.5, 1], [9.2, 2]] as const) {
const { player, frame } = await playAt(page, at);
// The block being read is the parent's, part by part.
const k = await player.getAttribute("data-post-follow-block");
await expect(frame.locator(`[data-thread-tile="parent:0"] [data-post-block="${k}"]`)).toHaveCount(1);
const blocks = await frame.locator('[data-thread-tile="parent:0"] [data-post-block]').evaluateAll((els) => els.map((el) => el.getAttribute("data-post-block")));
expect(blocks[part]).toBe(k);
await expect(player).toHaveAttribute("data-post-thread-tiles", /parent:0=1\/1 post=1\/0/);
await expect.poll(async () => (await tileOf(frame, "post")).opacity).toBe("0");
const t = await tileOf(frame, "parent:0");
expect(t.state).toBe("focus");
expect(t.transform).toBe("");
// Lit (full strength) and inside the window.
await expect.poll(async () => (await inWindow(frame, k!)).op).toBe("");
const b = await inWindow(frame, k!);
expect(b.top).toBeGreaterThanOrEqual(-1);
expect(b.bottom).toBeLessThanOrEqual(b.height + 1);
if (SHOTS) await page.screenshot({ path: path.join(SHOTS, `thread-parent-part-${part + 1}.png`) });
}
});
test("THE REPLY COMES IN: as his first part starts the parent shrinks and dims (it stays above), his reply is revealed, lit and in the window", async ({ page }) => {
await page.setViewportSize({ width: 1366, height: 1000 });
// Mid-change (his part at 0.4 + 11 = 11.4 s; the change from 11.0 s over 0.7 s).
{
const { player, frame } = await playAt(page, 11.35);
const tiles = (await player.getAttribute("data-post-thread-tiles"))!;
const [, sc, op] = /parent:0=([\d.]+)\/([\d.]+)/.exec(tiles)!;
expect(Number(sc)).toBeGreaterThan(0.62);
expect(Number(sc)).toBeLessThan(1);
expect(Number(op)).toBeLessThan(1);
await expect.poll(async () => (await tileOf(frame, "parent:0")).transform).toMatch(/^scale\(0\.\d+\)$/);
if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "thread-transition.png") });
}
for (const [at, part] of [[12.5, 0], [16, 1]] as const) {
const { player, frame } = await playAt(page, at);
await expect(player).toHaveAttribute("data-post-thread-tiles", "parent:0=0.62/0.5 post=1/1");
const k = await player.getAttribute("data-post-follow-block");
const mine = await frame.locator('[data-thread-tile="post"] [data-post-block]').evaluateAll((els) => els.map((el) => el.getAttribute("data-post-block")));
expect(mine[part]).toBe(k);
// The page set to the state Play is at (each tick applies it).
await expect.poll(async () => (await tileOf(frame, "parent:0")).transform).toBe("scale(0.62)");
const parent = await tileOf(frame, "parent:0");
expect(parent.state).toBe("context");
expect(parent.opacity).toBe("0.5");
await expect.poll(async () => (await inWindow(frame, k!)).op).toBe("");
const b = await inWindow(frame, k!);
expect(b.top).toBeGreaterThanOrEqual(-1);
expect(b.bottom).toBeLessThanOrEqual(b.height + 1);
// Older up top: the parent's foot is still in the window, above his post.
const post = await tileOf(frame, "post");
expect(parent.bottom).toBeGreaterThan(0);
expect(parent.bottom).toBeLessThanOrEqual(post.top + 1);
if (SHOTS) await page.screenshot({ path: path.join(SHOTS, `thread-reply-part-${part + 1}.png`) });
}
});
test("the bench: the read piece by piece, what each post is on screen; hiding the post it reads is shown and refused by Save", async ({ page }) => {
await page.setViewportSize({ width: 1500, height: 1100 });
await page.goto(urls.postBenchHref(PROJECT, "p-13"));
const panel = page.locator("[data-post-thread-panel]");
await expect(panel).toHaveAttribute("data-threaded", "1");
await expect(panel.locator("[data-post-thread-piece]")).toHaveCount(2);
await expect(panel.locator('[data-post-thread-piece="parent:0"]')).toContainText("the post it answers");
await expect(panel.locator('[data-post-thread-piece="post"]')).toContainText("his words");
// A piece's row puts the playhead at its start (his first part: 0.4 + 11).
await panel.locator('[data-post-thread-piece="post"] button').click();
await expect(page.locator("[data-post-bench-clock]")).toHaveText(/^11\.4 \//);
// Hide the post it reads: the guard says so here, and Save is refused.
await panel.locator('[data-post-thread-mode="parent:0"]').selectOption("hidden");
await expect(panel.locator("[data-post-thread-errors]")).toContainText("drawn hidden");
await page.locator("[data-post-bench-save]").click();
await expect(page.locator("[data-post-bench-error]")).toContainText("drawn hidden");
expect(JSON.parse(readFileSync(MANIFEST, "utf8")).timeline.find((x: { id: string }) => x.id === "p-13").thread).toBeUndefined();
// A shrink of its own saves.
await panel.locator('[data-post-thread-mode="parent:0"]').selectOption("");
await panel.locator('[data-post-thread-knob="shrink"]').fill("0.5");
await page.locator("[data-post-bench-save]").click();
await expect.poll(() => JSON.parse(readFileSync(MANIFEST, "utf8")).timeline.find((x: { id: string }) => x.id === "p-13").thread).toEqual({ shrink: 0.5 });
if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "thread-bench.png") });
});