import { test, expect, type Page } from "@playwright/test"; import { copyFileSync, existsSync, readFileSync, rmSync } from "node:fs"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; import { POST_IDS } from "./fixtures/intake-fixture.mjs"; // --------------------------------------------------------------------------- // Add post in Play the cut (UI notes item 13; lib/report/postCards.mjs, // postsCli.mjs addPostFromArchive, /api/report/post-cards): // // add-post-fixture play-cut-fixture's cut, no posts; the corpus's testchan-x // posts archive: a plain post (captured), one QUOTING // another account (not captured) and one with a picture // (captured). WRITES its manifest, posts-media/ and the // corpus's testchan-x/posts-media// (the stub's // capture); every test starts from the fixture's. // // The archive searched from Play; each post's warning from its own data; // Capture through the editor's capture-posts (the stub's job); Add to the clip // on screen with a crop -- the card on that clip in the live preview, its shot // cropped (data-crop, never panned) -- and the crop changed on a post in the cut. // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const DIR = path.join(FIXTURE, "reports", "add-post-fixture"); const MANIFEST = path.join(DIR, "video.manifest.json"); const PROJECT = "reports/add-post-fixture"; const STUB_LOG = path.join(FIXTURE, "editor-stub.requests.json"); const QUOTING_SHOT = path.join(FIXTURE, "channels", "testchan-x", "posts-media", POST_IDS.quoting); const SHOTS = process.env.U4_SHOTS ?? null; type Post = { id: string; attachTo?: string; shot?: string; shotMaxHeight?: number }; const posts = () => ((JSON.parse(readFileSync(MANIFEST, "utf8")) as { posts?: Post[] }).posts ?? []); const stubLog = () => { try { return JSON.parse(readFileSync(STUB_LOG, "utf8")) as { route: string; slug?: string; ids?: string[] }[]; } catch { return []; } }; 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, "posts-media"), { recursive: true, force: true }); rmSync(QUOTING_SHOT, { recursive: true, force: true }); }); const root = (page: Page) => page.locator("[data-play-cut]"); async function open(page: Page) { await page.setViewportSize({ width: 1366, height: 1000 }); await page.goto(urls.playHref(PROJECT)); await expect(root(page)).toHaveAttribute("data-play-state", "paused"); await expect(root(page)).toHaveAttribute("data-play-edges", "render", { timeout: 30_000 }); await page.locator("[data-play-add-post-open]").click(); await expect(page.getByTestId("add-post")).toBeVisible(); await expect(page.locator("[data-add-post-channel]")).toHaveValue("testchan-x"); } async function search(page: Page, q: string) { await page.locator("[data-add-post-q]").fill(q); await page.locator("[data-add-post-search]").click(); await expect(page.locator("[data-add-post-hit]").first()).toBeVisible(); } const hit = (page: Page, id: string) => page.locator(`[data-add-post-hit='${id}']`); test("the archive searched from Play; each post's warning from its own data: a quoted post, a picture, none", async ({ page }) => { await open(page); await search(page, "voice"); await expect(page.locator("[data-add-post-hit]")).toHaveCount(3); await expect(hit(page, POST_IDS.quoting).locator("[data-add-post-warning]")).toContainText("quotes a post by @someoneelse"); await expect(hit(page, POST_IDS.media).locator("[data-add-post-warning]")).toContainText("a picture or video"); await expect(hit(page, POST_IDS.plain).locator("[data-add-post-warning]")).toHaveCount(0); await expect(hit(page, POST_IDS.quoting)).toHaveAttribute("data-add-post-captured", "0"); await expect(hit(page, POST_IDS.plain)).toHaveAttribute("data-add-post-captured", "1"); await search(page, "pictured"); await expect(page.locator("[data-add-post-hit]")).toHaveCount(1); }); test("Capture through the editor, Add to the clip on screen with a crop: the card on that clip in the preview, cropped", async ({ page }) => { await open(page); await search(page, "voice"); const before = stubLog().length; await page.locator(`[data-add-post-capture='${POST_IDS.quoting}']`).click(); await expect(page.locator("[data-add-post-said]")).toContainText(`capture of ${POST_IDS.quoting}: done`, { timeout: 20_000 }); const asked = stubLog().slice(before).filter((r) => r.route === "capture-posts"); expect(asked).toEqual([expect.objectContaining({ slug: "testchan-x", ids: [POST_IDS.quoting] })]); await expect(hit(page, POST_IDS.quoting)).toHaveAttribute("data-add-post-captured", "1"); // The clip on screen is c01: the post goes on it, cropped to its own words. await expect(page.locator(`[data-add-post-add='${POST_IDS.quoting}']`)).toHaveText("Add to c01"); await page.locator(`[data-add-post-crop='${POST_IDS.quoting}']`).fill("300"); await page.locator(`[data-add-post-add='${POST_IDS.quoting}']`).click(); await expect(page.locator("[data-add-post-said]")).toContainText(`added x-${POST_IDS.quoting} on c01`); await expect.poll(() => posts().map((p) => `${p.id}:${p.attachTo}:${p.shotMaxHeight}`)).toEqual([`x-${POST_IDS.quoting}:c01:300`]); expect(existsSync(path.join(DIR, "posts-media", "testchan-x", POST_IDS.quoting, "shot.png"))).toBe(true); // The card on c01, in the live preview: its shot cropped (data-crop), never panned. const preview = page.locator("[data-testid=onscreen-posts-preview][data-segment='c01']"); await expect(preview).toHaveAttribute("data-posts-ready", "1", { timeout: 60_000 }); const frame = page.frameLocator("[data-testid=onscreen-posts-preview-iframe]"); await expect(frame.locator(".shot-view[data-crop='300']")).toHaveCount(1); // In the cut, the post still says what its screenshot carries. await expect(page.locator(`[data-add-post-cut-warning='x-${POST_IDS.quoting}']`)).toContainText("cropped to 300 px"); if (SHOTS) { await page.waitForTimeout(800); await page.screenshot({ path: path.join(SHOTS, "13-add-post-card-on-clip.png") }); } }); test("a post in the cut: Save crop changes its own shotMaxHeight; empty takes the crop off", async ({ page }) => { await open(page); await search(page, "pictured"); await page.locator(`[data-add-post-add='${POST_IDS.media}']`).click(); await expect.poll(() => posts().map((p) => p.id)).toEqual([`x-${POST_IDS.media}`]); const crop = page.locator(`[data-add-post-cut-crop='x-${POST_IDS.media}']`); await crop.fill("420"); await page.locator(`[data-add-post-cut-crop-save='x-${POST_IDS.media}']`).click(); await expect.poll(() => posts()[0].shotMaxHeight).toBe(420); if (SHOTS) { await page.getByTestId("add-post").screenshot({ path: path.join(SHOTS, "13-add-post-panel-warnings.png") }); } await crop.fill(""); await page.locator(`[data-add-post-cut-crop-save='x-${POST_IDS.media}']`).click(); await expect.poll(() => "shotMaxHeight" in posts()[0]).toBe(false); // A crop out of range is refused in the build's words. await crop.fill("50"); await page.locator(`[data-add-post-cut-crop-save='x-${POST_IDS.media}']`).click(); await expect(page.locator("[data-add-post-error]")).toContainText("from 120 to 1080"); });