/ opencodehub / tests / integration / user-email-templates-route.test.ts
swadhinbiswas Complete email templates routing and analytics APIs · 2/20/2026
1c01425
40 lines · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { describe, expect, it } from "vitest";
import { GET as templatesGet, POST as templatesPost } from "@/pages/api/user/email/templates";

async function json(response: Response): Promise<any> {
  return response.json();
}

describe("user email templates route", () => {
  it("returns 401 when unauthenticated", async () => {
    const response = await templatesGet({ locals: {} } as any);
    expect(response.status).toBe(401);
  });

  it("lists templates and renders preview", async () => {
    const listResponse = await templatesGet({ locals: { user: { id: "user-1" } } } as any);
    const listBody = await json(listResponse);

    expect(listResponse.status).toBe(200);
    expect(Array.isArray(listBody?.data?.templates)).toBe(true);
    expect(listBody?.data?.templates?.length).toBeGreaterThan(0);

    const previewResponse = await templatesPost({
      locals: { user: { id: "user-1" } },
      request: new Request("http://localhost/api/user/email/templates", {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({
          templateId: "pr_opened",
          variables: { number: 42, title: "Add tests", author: "alice" },
        }),
      }),
    } as any);

    const previewBody = await json(previewResponse);
    expect(previewResponse.status).toBe(200);
    expect(previewBody?.data?.preview?.subject).toContain("PR #42 opened");
    expect(previewBody?.data?.preview?.missingVariables?.length).toBe(0);
  });
});