// A fake Bluesky on loopback (release 29 S0): the public AppView the fetcher // reads, a bsky.app-shaped post page a headless browser can screenshot, and the // CDN's images. No test reaches the real Bluesky. // // const bsky = await startFakeBluesky(); // bsky.fetch a fetch that sends https://public.api.bsky.app/…, // https://bsky.app/… and https://cdn.bsky.app/… here // bsky.base the loopback origin (a browser goes here directly) // bsky.pageUrl(h, rkey) the post page on the fake (what bsky.app/profile//post/ is) // bsky.requests every request path, in order // await bsky.close() // // Routes: // GET /xrpc/com.atproto.identity.resolveHandle?handle= { did }, 400 for an unknown handle // GET /xrpc/app.bsky.feed.getPosts?uris=… the live posts among the uris // (a deleted post is simply absent) // GET /profile//post/ the post page: a thread item // `[data-testid="postThreadItem-by-"]` // holding the text and its images, inside // page chrome (a header, a side bar); a // deleted post renders "Post not found" // GET /img// a small PNG // opts.busyTimes: the first N AppView requests answer 429 (Retry-After: 1). import http from "node:http"; // A 2×2 PNG (opaque grey), enough for an to load and a file to be written. export const PNG = Buffer.from( "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAADklEQVR4nGNoAAMGCAUAKg4GARWeQtcAAAAASUVORK5CYII=", "base64", ); export const ACCOUNTS = [{ handle: "synthbaker.bsky.social", did: "did:plc:synthbaker0000000000000", displayName: "Synth Baker" }]; // Invented posts. `deleted` posts exist for the page (it says so) and never in getPosts. export const BSKY_POSTS = [ { handle: "synthbaker.bsky.social", rkey: "3lsynthpost0001", text: "The second shop opens in the spring. Small place, big oven.", createdAt: "2026-03-02T15:00:00.000Z", images: [ { name: "oven.png", alt: "The new oven" }, { name: "shop.png", alt: "The shop front" }, ], }, { handle: "synthbaker.bsky.social", rkey: "3lsynthpost0002", text: "No, I did not enter the pie contest. The judges are my friends.", createdAt: "2026-02-11T20:00:00.000Z", images: [], }, { handle: "synthbaker.bsky.social", rkey: "3lsynthpost0003", text: "This one was deleted.", createdAt: "2026-02-12T09:00:00.000Z", images: [], deleted: true, }, ]; const esc = (s) => String(s).replace(/&/g, "&").replace(//g, ">").replace(/"/g, """); export async function startFakeBluesky(opts = {}) { const accounts = opts.accounts ?? ACCOUNTS; const posts = opts.posts ?? BSKY_POSTS; const requests = []; let busy = opts.busyTimes ?? 0; let base = ""; const didOf = (handle) => accounts.find((a) => a.handle === handle)?.did ?? null; const accountOf = (actor) => accounts.find((a) => a.handle === actor || a.did === actor) ?? null; const imgUrl = (did, name) => `https://cdn.bsky.app/img/${did}/${name}`; const view = (p) => { const a = accountOf(p.handle); const images = p.images.map((im) => ({ thumb: imgUrl(a.did, im.name), fullsize: imgUrl(a.did, im.name), alt: im.alt })); return { uri: `at://${a.did}/app.bsky.feed.post/${p.rkey}`, cid: `bafy${p.rkey}`, author: { did: a.did, handle: a.handle, displayName: a.displayName }, record: { $type: "app.bsky.feed.post", text: p.text, createdAt: p.createdAt, langs: ["en"], ...(images.length ? { embed: { $type: "app.bsky.embed.images", images: p.images.map((im) => ({ alt: im.alt })) } } : {}), }, ...(images.length ? { embed: { $type: "app.bsky.embed.images#view", images } } : {}), replyCount: 1, repostCount: 2, likeCount: 3, quoteCount: 0, indexedAt: p.createdAt, }; }; const page = (handle, rkey) => { const p = posts.find((x) => x.handle === handle && x.rkey === rkey); const a = accountOf(handle); const chrome = (inner) => `Bluesky
Bluesky
${inner}
`; if (!p || !a || p.deleted) return chrome(`

Post not found

`); const imgs = p.images.map((im) => `${esc(im.alt)}`).join(""); return chrome(`
${esc(a.displayName)} @${esc(handle)}

${esc(p.text)}

${imgs}
${esc(p.createdAt)}
a reply
`); }; const server = http.createServer((req, res) => { const u = new URL(req.url ?? "/", "http://fake"); requests.push(u.pathname + u.search); const send = (status, type, body, headers = {}) => { res.writeHead(status, { "content-type": type, ...headers }); res.end(body); }; const json = (status, body, headers) => send(status, "application/json", JSON.stringify(body), headers); if (u.pathname.startsWith("/xrpc/") && busy > 0) { busy--; return json(429, { error: "RateLimitExceeded" }, { "retry-after": "1" }); } if (u.pathname === "/xrpc/com.atproto.identity.resolveHandle") { const did = didOf(u.searchParams.get("handle") ?? ""); return did ? json(200, { did }) : json(400, { error: "InvalidRequest", message: "Unable to resolve handle" }); } if (u.pathname === "/xrpc/app.bsky.feed.getPosts") { const uris = u.searchParams.getAll("uris"); const live = posts.filter((p) => !p.deleted).map(view).filter((v) => uris.includes(v.uri)); return json(200, { posts: live }); } const m = /^\/profile\/([^/]+)\/post\/([^/]+)$/.exec(u.pathname); if (m) return send(200, "text/html; charset=utf-8", page(decodeURIComponent(m[1]), decodeURIComponent(m[2]))); if (/^\/img\/[^/]+\/[^/]+$/.test(u.pathname)) return send(200, "image/png", PNG); return json(404, { error: "NotFound" }); }); await new Promise((r) => server.listen(0, "127.0.0.1", r)); base = `http://127.0.0.1:${server.address().port}`; return { base, requests, pageUrl: (handle, rkey) => `${base}/profile/${encodeURIComponent(handle)}/post/${encodeURIComponent(rkey)}`, fetch: (url, init) => fetch(String(url).replace(/^https:\/\/(public\.api\.bsky\.app|bsky\.app|cdn\.bsky\.app)/, base), init), close: () => new Promise((r) => server.close(() => r())), }; }