// The avatar store and the URL rule (xAvatars.ts). Temp dirs only. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test social/xAvatars.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readdir, readFile, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { AVATARS_DIRNAME, AVATARS_INDEX_FILENAME, isXAvatarHandle, lookupXAvatar, readXAvatarIndex, upgradeXAvatarUrl, writeXAvatar, xAvatarExtension, xAvatarFile, xAvatarsDir, } from "./xAvatars"; import { postCaptureDir, postsMediaDir } from "./postCapture"; const JPG = new Uint8Array([0xff, 0xd8, 0xff, 0xe0, 1, 2, 3]); const PB = "https://pbs.twimg.com/profile_images/1712345678901234567/AbC-dEf_"; test("url: every size X serves maps to _400x400, the extension and query kept", () => { for (const suffix of ["normal", "bigger", "mini", "reasonably_small", "x96", "200x200", "400x400"]) { assert.equal(upgradeXAvatarUrl(`${PB}${suffix}.jpg`), `${PB}400x400.jpg`, suffix); } // No suffix: the original upload, any size — asked for at 400 too. assert.equal( upgradeXAvatarUrl("https://pbs.twimg.com/profile_images/123/abc.png"), "https://pbs.twimg.com/profile_images/123/abc_400x400.png", ); // A name with an underscore of its own, and no extension at all. assert.equal( upgradeXAvatarUrl("https://pbs.twimg.com/profile_images/123/my_pic_normal.jpeg"), "https://pbs.twimg.com/profile_images/123/my_pic_400x400.jpeg", ); assert.equal( upgradeXAvatarUrl("https://pbs.twimg.com/profile_images/123/abc_normal"), "https://pbs.twimg.com/profile_images/123/abc_400x400", ); assert.equal( upgradeXAvatarUrl("https://pbs.twimg.com/profile_images/123/abc_normal.webp?v=2"), "https://pbs.twimg.com/profile_images/123/abc_400x400.webp?v=2", ); }); test("url: X's default egg, another host, another path or no URL is no avatar", () => { assert.equal(upgradeXAvatarUrl("https://abs.twimg.com/sticky/default_profile_images/default_profile_normal.png"), null); assert.equal(upgradeXAvatarUrl("https://pbs.twimg.com/sticky/default_profile_images/default_profile_400x400.png"), null); assert.equal(upgradeXAvatarUrl("https://evil.example/profile_images/123/abc_normal.jpg"), null); assert.equal(upgradeXAvatarUrl("https://pbs.twimg.com/media/Gabc123?format=jpg&name=small"), null); assert.equal(upgradeXAvatarUrl("data:image/png;base64,AAAA"), null); assert.equal(upgradeXAvatarUrl("not a url"), null); // Loopback is allowed: a test's fixture server. assert.equal( upgradeXAvatarUrl("http://127.0.0.1:4321/profile_images/123/abc_normal.jpg"), "http://127.0.0.1:4321/profile_images/123/abc_400x400.jpg", ); }); test("extension: the response's type, else the URL's, else jpg", () => { assert.equal(xAvatarExtension(`${PB}400x400.jpg`, "image/png"), "png"); assert.equal(xAvatarExtension(`${PB}400x400.jpg`, "image/jpeg; charset=binary"), "jpg"); assert.equal(xAvatarExtension(`${PB}400x400.jpeg`), "jpg"); assert.equal(xAvatarExtension(`${PB}400x400.webp`, "application/octet-stream"), "webp"); assert.equal(xAvatarExtension(`${PB}400x400.gif`), "gif"); assert.equal(xAvatarExtension(`${PB}400x400`), "jpg"); assert.equal(xAvatarExtension(`${PB}400x400.bmp`), "jpg"); }); test("handles: X's own shape only", () => { for (const h of ["ObviousRises", "a", "under_score_1", "x".repeat(30)]) assert.equal(isXAvatarHandle(h), true, h); for (const h of ["", "x".repeat(31), "../etc", "a.b", "a-b", "a b", "avatars.json"]) assert.equal(isXAvatarHandle(h), false, h); }); test("layout: posts-media/_avatars, and _avatars is never taken for a post id", async () => { const root = await mkdtemp(path.join(os.tmpdir(), "xavatars-layout-")); assert.equal(xAvatarsDir(postsMediaDir(root)), path.join(root, "posts-media", AVATARS_DIRNAME)); assert.throws(() => postCaptureDir(postsMediaDir(root), AVATARS_DIRNAME), /not a post id/); assert.doesNotThrow(() => postCaptureDir(postsMediaDir(root), "_avatars2")); }); test("store: the picture and its entry, in the exact shape umtool reads", async () => { const dir = path.join(await mkdtemp(path.join(os.tmpdir(), "xavatars-")), AVATARS_DIRNAME); const got = await writeXAvatar(dir, { handle: "ObviousRises", bytes: JPG, url: `${PB}400x400.jpg`, contentType: "image/jpeg", postId: "1850000000000000001", capturedAt: "2026-11-02T20:00:00.000Z", }); assert.equal(got.handle, "ObviousRises"); assert.deepEqual(await readdir(dir).then((n) => n.sort()), ["ObviousRises.jpg", AVATARS_INDEX_FILENAME]); assert.deepEqual(new Uint8Array(await readFile(path.join(dir, "ObviousRises.jpg"))), JPG); assert.deepEqual(JSON.parse(await readFile(path.join(dir, AVATARS_INDEX_FILENAME), "utf8")), { ObviousRises: { file: "ObviousRises.jpg", url: `${PB}400x400.jpg`, capturedAt: "2026-11-02T20:00:00.000Z", postId: "1850000000000000001", }, }); assert.equal(await xAvatarFile(dir, "obviousrises"), path.join(dir, "ObviousRises.jpg")); assert.equal(await xAvatarFile(dir, "nobody"), null); }); test("store: merged with what is there; another case replaces the entry and its file", async () => { const dir = path.join(await mkdtemp(path.join(os.tmpdir(), "xavatars-")), AVATARS_DIRNAME); const w = (handle: string, ext = "jpg", postId = "1") => writeXAvatar(dir, { handle, bytes: JPG, url: `${PB}400x400.${ext}`, postId, capturedAt: "2026-11-02T20:00:00.000Z" }); await w("alice"); await w("Bob", "png"); // Two at once: the read-modify-writes do not drop each other. await Promise.all([w("carol"), w("dave"), w("erin")]); let index = await readXAvatarIndex(dir); assert.deepEqual(Object.keys(index), ["alice", "Bob", "carol", "dave", "erin"]); // A re-capture as X prints it now: one entry, the old file gone. await w("BOB", "jpg", "2"); index = await readXAvatarIndex(dir); assert.deepEqual(Object.keys(index), ["alice", "BOB", "carol", "dave", "erin"]); assert.equal(index.BOB.postId, "2"); assert.equal(lookupXAvatar(index, "@bob")?.handle, "BOB"); assert.equal(lookupXAvatar(index, "nobody"), null); const files = (await readdir(dir)).sort(); assert.ok(!files.includes("Bob.png"), files.join(",")); assert.ok(files.includes("BOB.jpg")); assert.ok(!files.some((f) => f.includes(".tmp-")), "no temp file left"); }); test("store: a bad handle, a bad post id or no bytes is refused; an unreadable index is never overwritten", async () => { const dir = path.join(await mkdtemp(path.join(os.tmpdir(), "xavatars-")), AVATARS_DIRNAME); const ok = { handle: "alice", bytes: JPG, url: `${PB}400x400.jpg`, postId: "1" }; await assert.rejects(writeXAvatar(dir, { ...ok, handle: "../x" }), /not an X handle/); await assert.rejects(writeXAvatar(dir, { ...ok, handle: "avatars.json" }), /not an X handle/); await assert.rejects(writeXAvatar(dir, { ...ok, postId: "../1" }), /not a post id/); await assert.rejects(writeXAvatar(dir, { ...ok, bytes: new Uint8Array() }), /empty/); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, AVATARS_INDEX_FILENAME), "{ truncated"); await assert.rejects(writeXAvatar(dir, ok), /unparseable; not overwriting/); assert.equal(await readFile(path.join(dir, AVATARS_INDEX_FILENAME), "utf8"), "{ truncated"); // A reader just finds nothing. assert.deepEqual(await readXAvatarIndex(dir), {}); }); test("store: entries not of the shape, or under a key that is no handle, are left out on read", async () => { const dir = path.join(await mkdtemp(path.join(os.tmpdir(), "xavatars-")), AVATARS_DIRNAME); await mkdir(dir, { recursive: true }); const good = { file: "a.jpg", url: "u", capturedAt: "t", postId: "1" }; await writeFile( path.join(dir, AVATARS_INDEX_FILENAME), JSON.stringify({ a: good, "b/c": good, d: { file: 3 }, e: null }), ); assert.deepEqual(await readXAvatarIndex(dir), { a: good }); });