import { test, expect, 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 SCENES (report-to-video/post-join.mjs, post-conversation.mjs, post-item.mjs postLayout; knobs.md, "The join",
// "Manual threads", "Wide attachments"; operator, 4 Nov, mid-6: "I'd like a thread-like smooth transition here rather
// than a cut", "do all of them chronologically as if a thread", "for these super wide images a top-bottom layout"):
// - a run whose items switch layout (a card beside a video, then a card alone) JOINS in Play: the item before's card
// as it ended in a frame of its own riding the morph, this card's box eased into its own;
// - a manual thread (thread.group) stacks unlinked posts as one scene, each reply saying what it answers;
// - a wide attachment is laid at full width above the card.
// Fixture: e2e/fixtures/post-narration-fixture.mjs's items, rewritten here.
//
// THREAD_SCENES_SHOTS=
screenshots there
// ---------------------------------------------------------------------------
const HERE = path.dirname(fileURLToPath(import.meta.url));
const REPORTS = path.join(HERE, "..", ".e2e-song", "reports");
const DIR = path.join(REPORTS, "post-narration-fixture");
const MANIFEST = path.join(DIR, "video.manifest.json");
const PROJECT = "reports/post-narration-fixture";
const SHOTS = process.env.THREAD_SCENES_SHOTS ?? null;
const ROOT_TEXT = "Many have questioned my methods in dealing with the whole mess, and I will say it once more for everyone.";
const REPLY = "I can't help but feel like I've failed everyone, they all got exactly what they wanted out of it.";
const ROOT = { id: "11", author: "Narr Subject", handle: "narrsubject", date: "2026-10-30T06:00:00.000Z", text: ROOT_TEXT };
function takeOf(pieces: string[]) {
let at = 0;
const parts = pieces.map((text, pi) => {
const seconds = Math.round((text.split(/\s+/).length / 3) * 100) / 100;
if (pi) at += 0.6;
const p = { n: pi + 1, para: pi * 100, text, start: Math.round(at * 100) / 100, seconds };
at += seconds;
return p;
});
return { file: "narration/posts/11/take-1.orpheus-zac.wav", engine: "orpheus", voice: "leo+zac", seconds: Math.round(at * 100) / 100, script: pieces.join("\n\n"), parts };
}
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 });
const m = JSON.parse(readFileSync(MANIFEST, "utf8"));
const byId = (id: string) => m.timeline.find((x: { id: string }) => x.id === id);
const narr = (e: Record, text: string, pieces: string[]) => Object.assign(e, { text, narration: { script: text, takes: [takeOf(pieces)], pick: 0 } });
// A run that switches layout: his root beside its video, then his reply under it, a card alone.
const root = narr(byId("p-11"), ROOT_TEXT, [ROOT_TEXT]);
root.date = ROOT.date;
const reply = narr(byId("p-12"), REPLY, [REPLY]);
delete reply.media;
reply.date = "2026-10-30T07:00:00.000Z";
reply.parents = [ROOT];
// A manual thread: a post he answered (shown), then two of his, unlinked on X.
const opener: Record = { ...structuredClone(reply), id: "p-21", post: "othchan-x/21", author: "Movail", handle: "movail_two", text: "Ready for more lessons?", date: "2026-10-30T08:00:00.000Z", thread: { group: "lessons" } };
delete opener.parents;
delete opener.narration;
const g1: Record = narr({ ...structuredClone(reply), id: "p-22", post: "narrchan-x/22", date: "2026-10-30T09:00:00.000Z", thread: { group: "lessons" } }, "It wobbles, oh my word.", ["It wobbles, oh my word."]);
delete g1.parents;
const g2 = narr({ ...structuredClone(reply), id: "p-23", post: "narrchan-x/23", date: "2026-10-30T09:05:00.000Z", thread: { group: "lessons" }, parents: [{ id: "21", author: "Movail", handle: "movail_two", date: "2026-10-30T08:00:00.000Z", text: "Ready for more lessons?" }] }, "It wobbles, I cannot even.", ["It wobbles, I cannot even."]);
// A wide attachment.
const wide = narr({ ...structuredClone(byId("p-13")), id: "p-31", post: "narrchan-x/31", parents: undefined, media: [{ src: "posts-media/narrchan-x/12/12_1.png", kind: "image", width: 692, height: 141 }] }, "If anything happens I am holding them accountable.", ["If anything happens I am holding them accountable."]);
delete wide.parents;
m.timeline = [root, reply, opener, g1, g2, wide];
writeFileSync(MANIFEST, JSON.stringify(m, null, 2) + "\n");
});
async function playAt(page: Page, id: string, at: number) {
await page.goto(urls.playHref(PROJECT, { bare: true, clip: id, at }));
// The deck composed first (post-narration.spec waits the same): its frame mounts the player afresh, and a frame
// read across that is detached.
await expect(page.locator("[data-play-cut]")).toHaveAttribute("data-play-compose", "ready", { timeout: 60_000 });
const player = page.locator(`[data-post-play="${id}"]`);
await expect(player).toBeVisible({ timeout: 15_000 });
const frame = page.frameLocator('[data-post-card-frame="read"]');
await expect(frame.locator("body")).toHaveAttribute("data-post-ready", "1", { timeout: 15_000 });
await expect.poll(async () => Math.abs(Number(await player.getAttribute("data-post-s")) - at)).toBeLessThan(0.3);
return { player, frame };
}
test("THE JOIN in Play: his reply (a card alone) opens on his root as it ended beside its video -- the item before's card in a frame of its own riding the morph, this card's box eased from that one's -- and is its own once the join is over", async ({ page }) => {
await page.setViewportSize({ width: 1366, height: 1000 });
{
const { player, frame } = await playAt(page, "p-12", 0.25);
await expect(page.locator('[data-post-join-frame="p-11"]')).toHaveCount(1);
await expect.poll(async () => player.getAttribute("data-post-join")).toMatch(/^0\.\d+$/);
// The page knows where the item before's card was; its own card moved and scaled toward its own box.
await expect(frame.locator("body")).toHaveAttribute("data-join", /"A":\{/);
await expect.poll(async () => frame.locator("[data-post-card]").evaluate((el) => (el as HTMLElement).style.transform)).toMatch(/translate\(.*scale\(/);
// The carried root is drawn as his post was (head and words), the join's anchor.
await expect(frame.locator('[data-thread-tile="parent:0"]')).toHaveAttribute("data-join-anchor", "");
if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "join-mid.png") });
}
{
const { player, frame } = await playAt(page, "p-12", 1.5);
await expect.poll(async () => player.getAttribute("data-post-join")).toBe("done");
await expect.poll(async () => frame.locator("[data-post-card]").evaluate((el) => (el as HTMLElement).style.transform)).not.toMatch(/scale\(/);
if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "join-after.png") });
}
});
test("A MANUAL THREAD (thread.group): the post he answered, then two of his unlinked on X, one scene -- every earlier post kept on screen, each reply saying what it answers", async ({ page }) => {
await page.setViewportSize({ width: 1366, height: 1000 });
const { frame } = await playAt(page, "p-23", 2.5);
await expect(frame.locator('[data-thread-slot="conv:22"]')).toHaveAttribute("data-thread-scene", "keep");
await expect(frame.locator('[data-thread-slot="parent:0"]')).toHaveCount(1);
// His last reply answers the post three tiles up: "replying to @movail_two" (blurred: a private account).
await expect(frame.locator('[data-thread-slot="post"] [data-thread-rel="branch"]')).toContainText("replying to");
if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "group.png") });
});
test("A WIDE ATTACHMENT (692×141) is laid at full width above the card, never a side panel", async ({ page }) => {
await page.setViewportSize({ width: 1366, height: 1000 });
const { player, frame } = await playAt(page, "p-31", 1);
await expect(player).toHaveAttribute("data-post-panel", "top");
const slot = await frame.locator("[data-post-slot]").boundingBox();
const card = await frame.locator("[data-post-card]").boundingBox();
expect(slot && card && slot.y + slot.height <= card.y + 1).toBe(true);
expect(slot && card && slot.width > card.width).toBe(true);
if (SHOTS) await page.screenshot({ path: path.join(SHOTS, "wide.png") });
});