Something went wrong. Try again.
Monorepo for Tangled
Something went wrong. Try again.
TypeScript
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768import { describe, expect, it, vi } from "vitest";import worker, { type Env } from "../src/index";
const store = (entries: Record<string, string> = {}) => ({ get: vi.fn(async (key: string) => entries[key] ?? null),});
const call = (query: string, { token = "s3cret", auth = "Bearer s3cret" as string | null, entries = {} } = {}) => { const kv = store(entries); const env = { EMAIL_DID: kv, EMAIL_DID_TOKEN: token } as unknown as Env; const request = new Request(`https://email-did.example.workers.dev/${query}`, { headers: auth === null ? {} : { authorization: auth }, }) as Parameters<typeof worker.fetch>[0]; return { res: worker.fetch(request, env), kv };};
describe("email-did", () => { it.each([ ["no authorization", { auth: null }], ["a wrong token", { auth: "Bearer nope" }], ["a token of another length", { auth: "Bearer s3cret-and-more" }], ["the token without the bearer scheme", { auth: "s3cret" }], ])("rejects %s without touching the store", async (_, opts) => { const { res, kv } = call("?emails=alice%40example.com", { ...opts, entries: { "alice@example.com": "did:plc:alice" } }); expect((await res).status).toBe(401); expect(kv.get).not.toHaveBeenCalled(); });
it("rejects everything when no token is configured, even a guess at the placeholder", async () => { const kv = store({ "alice@example.com": "did:plc:alice" }); const request = new Request("https://x.test/?emails=alice%40example.com", { headers: { authorization: "Bearer undefined" }, }) as Parameters<typeof worker.fetch>[0]; const res = await worker.fetch(request, { EMAIL_DID: kv } as unknown as Env); expect(res.status).toBe(401); expect(kv.get).not.toHaveBeenCalled(); });
it.each([ ["lowercases keys and answers in the caller's case", "?emails=Alice%40Example.com", { "alice@example.com": "did:plc:alice" }, { "Alice@Example.com": "did:plc:alice" }], ["maps missing keys to null", "?emails=nobody%40example.com", {}, { "nobody@example.com": null }], ["keeps a plus-address through query decoding", "?emails=a%2Bb%40example.com", { "a+b@example.com": "did:plc:plus" }, { "a+b@example.com": "did:plc:plus" }], ["answers an empty map for empty params", "", {}, {}], ])("%s", async (_, query, entries, expected) => { const res = await call(query, { entries }).res; expect(res.status).toBe(200); await expect(res.json()).resolves.toEqual(expected); });
it("reads a mixed-case duplicate once and answers every case", async () => { const { res, kv } = call("?emails=a%40b.com&emails=A%40B.com", { entries: { "a@b.com": "did:plc:a" } }); await expect((await res).json()).resolves.toEqual({ "a@b.com": "did:plc:a", "A@B.com": "did:plc:a" }); expect(kv.get).toHaveBeenCalledTimes(1); });
it("answers null for a store error instead of failing the batch", async () => { const env = { EMAIL_DID: { get: async () => { throw new Error("kv down"); } }, EMAIL_DID_TOKEN: "s3cret", } as unknown as Env; const request = new Request("https://x.test/?emails=a%40b.com&emails=c%40b.com", { headers: { authorization: "Bearer s3cret" }, }) as Parameters<typeof worker.fetch>[0]; const res = await worker.fetch(request, env); await expect(res.json()).resolves.toEqual({ "a@b.com": null, "c@b.com": null }); });});