Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990import { createHmac } from "node:crypto";import { afterEach, describe, expect, it, vi } from "vitest";import worker from "./index.js";import { avatarWidths } from "../../web/src/lib/avatarWidths.ts";import { widthFor } from "./size.js";
const env = { AVATAR_SHARED_SECRET: "s3cret", BOBBIN_URL: "https://bobbin.test",};const did = "did:plc:alice";const signed = (query) => `https://avatar.test/${createHmac("sha256", "s3cret").update(did).digest("hex")}/${did}${query}`;
const upstream = ( blobType, blob = () => new Response("img", { headers: { "Content-Type": blobType } }),) => vi.fn(async (url) => { if (url.includes("resolveMiniDoc")) return Response.json({ did, pds: "https://pds.test" }); if (url.includes("getRecord")) return Response.json({ value: { avatar: { ref: { $link: "bafyavatar" } } }, }); return blob(); });
const serve = async (query, fetch, put = async () => {}) => { vi.stubGlobal("fetch", fetch); vi.stubGlobal("caches", { default: { match: async () => undefined, put } }); return worker.fetch(new Request(signed(query)), env);};
afterEach(() => vi.unstubAllGlobals());
describe("widthFor", () => { it.each(avatarWidths)("resizes to %i", (width) => { expect(widthFor(String(width))).toBe(width); });
it.each([null, "", "100", "abc"])( "leaves %s at the original size", (size) => { expect(widthFor(size)).toBeNull(); }, );});
describe("fetch", () => { it("asks cloudflare to resize every width the web app uses", async () => { const fetch = upstream("image/jpeg"); const res = await serve("?size=192", fetch); expect(fetch.mock.calls.at(-1)[1]).toEqual({ cf: { image: { format: "webp", width: 192, height: 192, fit: "cover" } }, }); expect(res.headers.get("Content-Type")).toBe("image/webp"); });
it("keeps the blob's type when it serves the original", async () => { const fetch = upstream("image/jpeg"); const res = await serve("", fetch); expect(fetch.mock.calls.at(-1)[1]).toEqual({}); expect(res.headers.get("Content-Type")).toBe("image/jpeg"); });
describe("when the blob can't be fetched", () => { it.each([ ["is gone", () => new Response("", { status: 400 }), 400, 3600], ["is not found", () => new Response("", { status: 404 }), 404, 3600], ["is rate limited", () => new Response("", { status: 429 }), 429, 120], ["has a broken host", () => new Response("", { status: 502 }), 502, 120], [ "has an unreachable host", () => { throw new TypeError("network lost"); }, 502, 120, ], ])("caches the error when the blob %s", async (_, blob, status, ttl) => { const put = vi.fn(async () => {}); const res = await serve("?size=64", upstream("image/png", blob), put); expect(res.status).toBe(status); expect(res.headers.get("Cache-Control")).toBe(`public, max-age=${ttl}`); expect(put).toHaveBeenCalledOnce(); }); });});