// Capture posts for a Bluesky channel (release 29 E1), end to end over the // fake Bluesky (common/testing/social/fakeBluesky.mjs): the AppView, the // bsky.app-shaped post page and the image CDN all on one loopback origin, // named to the test server through E2E_BLUESKY_ORIGIN_FILE (e2e/servers.ts). // The real Bluesky is never contacted. // // The job runs as `pnpm ops capture-posts` runs it: the AppView says which // posts are live and where their images are, a headless Chromium with no login // shoots each live post cropped to it, the images come down beside the shot, // and the deleted post lands in posts-availability.json as deleted. import { test, expect, type APIRequestContext } from "@playwright/test"; import { mkdir, readFile, readdir, rm, writeFile } from "node:fs/promises"; import path from "node:path"; import { startFakeBluesky, BSKY_POSTS, ACCOUNTS } from "yt-dlp-transcript-common/testing/social/fakeBluesky.mjs"; import { writePosts } from "yt-dlp-transcript-common/lib/posts-server"; import { uploadDateFromCreatedAt, type Post } from "yt-dlp-transcript-common/lib/posts"; import { baseUrl } from "./baseUrl"; import { resetData, resolvePath, testPaths, writeSettings } from "./helpers"; import { blueskyOriginFile } from "./servers"; const AUTH = { authorization: "Bearer test-worker-token" }; const SLUG = "synth-bsky"; const HANDLE = ACCOUNTS[0].handle; async function waitForJob(request: APIRequestContext, jobId: string): Promise<{ status: string; tail: string[] }> { let last = { status: "", tail: [] as string[] }; await expect .poll( async () => { const res = await request.get(`${baseUrl}/api/ops/job/${jobId}?tail=40`, { headers: AUTH }); const body = (await res.json()) as { job: { status: string }; tail?: string[] }; last = { status: body.job.status, tail: body.tail ?? [] }; return last.status; }, { timeout: 90_000, intervals: [1_000] }, ) .toMatch(/^(done|failed|cancelled)$/); return last; } test("capture-posts on a Bluesky channel: shots cropped to the post, its images, and the deleted post recorded", async ({ request, }) => { test.setTimeout(150_000); await resetData("empty"); await writeSettings({ minFreeDiskGB: 0 }); const channelRoot = resolvePath(`${testPaths().transcriptsDir}/channels/${SLUG}`); await mkdir(channelRoot, { recursive: true }); await writeFile( path.join(channelRoot, "config.json"), JSON.stringify( { handling: "transcribe", sourceKind: "social", platform: "bluesky", postFetcher: "bluesky-atproto", socialHandle: HANDLE, name: "Synth Baker (Bluesky)", url: `https://bsky.app/profile/${HANDLE}`, }, null, 2, ), ); // The three invented posts, archived as a fetch would have left them. const posts: Post[] = BSKY_POSTS.map((p) => ({ id: p.rkey, slug: `${SLUG}/${p.rkey}`, channelSlug: SLUG, author: p.handle, createdAt: p.createdAt, uploadDate: uploadDateFromCreatedAt(p.createdAt), text: p.text, url: `https://bsky.app/profile/${p.handle}/post/${p.rkey}`, platform: "bluesky", isReply: false, isRepost: false, links: [], })); await writePosts(channelRoot, posts); const bsky = await startFakeBluesky(); const originFile = blueskyOriginFile(resolvePath(testPaths().transcriptsDir)); await writeFile(originFile, bsky.base); try { const ids = BSKY_POSTS.map((p) => p.rkey); const res = await request.post(`${baseUrl}/api/ops/capture-posts`, { headers: AUTH, data: { slug: SLUG, ids } }); const body = (await res.json()) as { ok?: boolean; jobId?: string; error?: string }; expect(res.status(), body.error).toBe(200); const job = await waitForJob(request, body.jobId!); expect(job.status, job.tail.join("\n")).toBe("done"); const media = path.join(channelRoot, "posts-media"); const [withImages, textOnly, deleted] = ids; expect((await readdir(path.join(media, withImages))).sort()).toEqual([ "capture.json", "media-1.png", "media-2.png", "shot.png", ]); expect((await readdir(path.join(media, textOnly))).sort()).toEqual(["capture.json", "shot.png"]); const gone = JSON.parse(await readFile(path.join(media, deleted, "capture.json"), "utf8")) as { state: string; shot?: unknown }; expect(gone.state).toBe("deleted"); expect(gone.shot).toBeUndefined(); // The shot is the post, not the page: the fake's thread item is 600 CSS // px wide in a 1280-px window, shot at 2x. const png = await readFile(path.join(media, withImages, "shot.png")); expect(png.readUInt32BE(16)).toBe(1200); const availability = JSON.parse(await readFile(path.join(channelRoot, "posts-availability.json"), "utf8")) as Record< string, { availability: string } >; expect(availability[deleted]?.availability).toBe("deleted"); expect(availability[withImages]?.availability).toBe("available"); // The page was opened for the live posts only; the deleted one never. const pages = bsky.requests.filter((r) => r.startsWith("/profile/")); expect(pages).toEqual([ `/profile/${HANDLE}/post/${withImages}`, `/profile/${HANDLE}/post/${textOnly}`, ]); // Avatars are X's: a Bluesky capture keeps none, and an avatars-only // run here is refused by name before any job. expect(await readdir(media)).not.toContain("_avatars"); const av = await request.post(`${baseUrl}/api/ops/capture-posts`, { headers: AUTH, data: { slug: SLUG, ids, avatarsOnly: true }, }); expect(av.status()).toBe(400); expect(((await av.json()) as { error?: string }).error).toMatch(/^Avatars are X's only; .* keeps none\.$/); // So is a post's context (the quoted post, the parent): a Bluesky // record carries none, and a context-only run is refused by name. const textRecord = JSON.parse(await readFile(path.join(media, textOnly, "capture.json"), "utf8")) as { context?: unknown }; expect(textRecord.context).toBeUndefined(); const ctx = await request.post(`${baseUrl}/api/ops/capture-posts`, { headers: AUTH, data: { slug: SLUG, ids, contextOnly: true }, }); expect(ctx.status()).toBe(400); expect(((await ctx.json()) as { error?: string }).error).toMatch(/^Post context is X's only; .* reads none\.$/); } finally { await rm(originFile, { force: true }); await bsky.close(); } });