Something went wrong. Try again.
Monorepo for Tangled
Something went wrong. Try again.
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273import { describe, it, expect } from "bun:test";import worker, { decodePayload } from "../index";import { formatAvatarUrl } from "../components/shared/avatar";
describe("worker fetch and GET payload decoding", () => { it("decodes JSON payload directly", () => { const obj = { type: "repository", repoName: "test" }; expect(decodePayload(JSON.stringify(obj))).toEqual(obj); });
it("decodes base64url payload", () => { const obj = { type: "repository", repoName: "test" }; const jsonStr = JSON.stringify(obj); const b64 = Buffer.from(jsonStr, "utf-8").toString("base64url"); expect(decodePayload(b64)).toEqual(obj); });
it("decodes multibyte UTF-8 payload with emoji and CJK characters", () => { const obj = { type: "issue", title: "バグ修正 🦀 & 🚀", repoName: "core" }; const jsonStr = JSON.stringify(obj); const b64 = Buffer.from(jsonStr, "utf-8").toString("base64url"); expect(decodePayload(b64)).toEqual(obj); });
it("rejects non-GET/POST methods", async () => { const req = new Request("http://localhost/card", { method: "DELETE" }); const res = await worker.fetch(req, { ENVIRONMENT: "test" }); expect(res.status).toBe(405); });
it("returns 400 for GET without payload", async () => { const req = new Request("http://localhost/card", { method: "GET" }); const res = await worker.fetch(req, { ENVIRONMENT: "test" }); expect(res.status).toBe(400); });
it("formats avatar URLs to request JPEG for Satori compatibility", () => { expect( formatAvatarUrl("https://next.tangled.org/avatar/did%3Aplc%3A123?size=256"), ).toBe("https://next.tangled.org/avatar/did%3Aplc%3A123?size=256&format=jpeg");
expect( formatAvatarUrl("https://avatar-dev.tangled.org/sig/did:plc:123?size=256"), ).toBe("https://avatar-dev.tangled.org/sig/did:plc:123?size=256&format=jpeg");
expect( formatAvatarUrl("https://avatar.tangled.sh/sig/did:plc:123?size=256"), ).toBe("https://avatar.tangled.sh/sig/did:plc:123?size=256&format=jpeg");
expect( formatAvatarUrl("https://example.com/pfp.jpg?format=webp"), ).toBe("https://example.com/pfp.jpg?format=jpeg");
expect( formatAvatarUrl("data:image/jpeg;base64,123"), ).toBe("data:image/jpeg;base64,123");
expect(formatAvatarUrl(undefined)).toBeUndefined(); });
it("accepts payloads without optional avatarUrl or createdAt", async () => { const payload = { type: "repository", repoName: "minimal", ownerHandle: "alice", }; const b64 = Buffer.from(JSON.stringify(payload), "utf-8").toString("base64url"); const req = new Request(`http://localhost/card?payload=${b64}`, { method: "GET" }); const res = await worker.fetch(req, { ENVIRONMENT: "test" }); expect(res.status).toBe(200); });});