/ opencodehub / tests / unit / api-helpers.test.ts
Swadhin Biswas test(qa): add e2e, integration, unit, and load coverage · 3/6/2026
1c29e63
209 lines · 5.9 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import {
  badRequest,
  conflict,
  created,
  forbidden,
  getPagination,
  noContent,
  notFound,
  paginationMeta,
  parseQuery,
  rateLimited,
  serverError,
  success,
  unauthorized,
  validationError,
} from "@/lib/api";
import { describe, expect, it } from "vitest";
import { z } from "zod";

describe("success", () => {
  it("returns 200 JSON response", async () => {
    const res = success({ name: "test" });
    expect(res.status).toBe(200);
    const body = await res.json();
    expect(body.success).toBe(true);
    expect(body.data.name).toBe("test");
  });

  it("includes meta when provided", async () => {
    const meta = { page: 1, perPage: 10, total: 100 };
    const res = success([1, 2, 3], meta);
    const body = await res.json();
    expect(body.meta).toBeDefined();
    expect(body.meta.page).toBe(1);
  });
});

describe("created", () => {
  it("returns 201 JSON response", async () => {
    const res = created({ id: "abc123" });
    expect(res.status).toBe(201);
    const body = await res.json();
    expect(body.success).toBe(true);
  });
});

describe("noContent", () => {
  it("returns 204 with no body", () => {
    const res = noContent();
    expect(res.status).toBe(204);
  });
});

describe("error responses", () => {
  it("badRequest returns 400", async () => {
    const res = badRequest("Invalid input");
    expect(res.status).toBe(400);
    const body = await res.json();
    expect(body.success).toBe(false);
    expect(body.error.message).toBe("Invalid input");
  });

  it("unauthorized returns 401", async () => {
    const res = unauthorized();
    expect(res.status).toBe(401);
  });

  it("unauthorized with custom message", async () => {
    const res = unauthorized("Login required");
    expect(res.status).toBe(401);
    const body = await res.json();
    expect(body.error.message).toBe("Login required");
  });

  it("forbidden returns 403", async () => {
    const res = forbidden();
    expect(res.status).toBe(403);
  });

  it("notFound returns 404", async () => {
    const res = notFound("Repo not found");
    expect(res.status).toBe(404);
    const body = await res.json();
    expect(body.error.message).toBe("Repo not found");
  });

  it("conflict returns 409", async () => {
    const res = conflict("Already exists");
    expect(res.status).toBe(409);
    const body = await res.json();
    expect(body.error.message).toBe("Already exists");
  });

  it("serverError returns 500", async () => {
    const res = serverError();
    expect(res.status).toBe(500);
  });

  it("rateLimited returns 429", async () => {
    const res = rateLimited(60);
    expect(res.status).toBe(429);
    expect(res.headers.get("Retry-After")).toBe("60");
  });

  it("validationError returns 400 with zod errors", async () => {
    const schema = z.object({ name: z.string().min(1) });
    const result = schema.safeParse({ name: "" });
    if (!result.success) {
      const res = validationError(result.error);
      expect(res.status).toBe(400);
      const body = await res.json();
      expect(body.success).toBe(false);
    }
  });
});

describe("parseQuery", () => {
  it("parses valid query parameters", () => {
    const schema = z.object({
      page: z.coerce.number().default(1),
      search: z.string().optional(),
    });
    const url = new URL("http://localhost?page=3&search=hello");
    const result = parseQuery(url, schema);
    expect("data" in result).toBe(true);
    if ("data" in result) {
      expect(result.data.page).toBe(3);
      expect(result.data.search).toBe("hello");
    }
  });

  it("returns error for invalid query", () => {
    const schema = z.object({
      page: z.coerce.number().int().min(1),
    });
    const url = new URL("http://localhost?page=-5");
    const result = parseQuery(url, schema);
    expect("error" in result).toBe(true);
  });

  it("uses defaults when query params are missing", () => {
    const schema = z.object({
      page: z.coerce.number().default(1),
      perPage: z.coerce.number().default(20),
    });
    const url = new URL("http://localhost");
    const result = parseQuery(url, schema);
    expect("data" in result).toBe(true);
    if ("data" in result) {
      expect(result.data.page).toBe(1);
      expect(result.data.perPage).toBe(20);
    }
  });
});

describe("getPagination", () => {
  it("returns default pagination for empty URL", () => {
    const url = new URL("http://localhost");
    const result = getPagination(url);
    expect(result.page).toBe(1);
    expect(result.perPage).toBeGreaterThan(0);
    expect(result.offset).toBe(0);
  });

  it("parses page and perPage from query", () => {
    const url = new URL("http://localhost?page=3&per_page=50");
    const result = getPagination(url);
    expect(result.page).toBe(3);
    expect(result.perPage).toBe(50);
    expect(result.offset).toBe(100);
  });

  it("clamps perPage to max", () => {
    const url = new URL("http://localhost?per_page=9999");
    const result = getPagination(url, 20, 100);
    expect(result.perPage).toBeLessThanOrEqual(100);
  });

  it("defaults to page 1 for invalid page", () => {
    const url = new URL("http://localhost?page=abc");
    const result = getPagination(url);
    expect(result.page).toBe(1);
  });
});

describe("paginationMeta", () => {
  it("calculates correct pagination metadata", () => {
    const pagination = { page: 2, perPage: 10, offset: 10 };
    const meta = paginationMeta(95, pagination);
    expect(meta?.page).toBe(2);
    expect(meta?.perPage).toBe(10);
    expect(meta?.total).toBe(95);
    expect(meta?.totalPages).toBe(10);
  });

  it("handles 0 total items", () => {
    const pagination = { page: 1, perPage: 10, offset: 0 };
    const meta = paginationMeta(0, pagination);
    expect(meta?.total).toBe(0);
    expect(meta?.totalPages).toBe(0);
  });

  it("handles single page", () => {
    const pagination = { page: 1, perPage: 10, offset: 0 };
    const meta = paginationMeta(5, pagination);
    expect(meta?.totalPages).toBe(1);
  });
});