Swadhin Biswas feat: production readiness — fix critical bugs, achieve 100% test pass rate, complete documentation · 4/30/2026
ac8f8cc
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
/**
* Integration tests for GET /api/search
*/
import { beforeEach, describe, expect, it, vi } from "vitest";
/* ─── hoisted mocks ─── */
const mocks = vi.hoisted(() => ({
fakeSchema: {
repositories: {
name: "name",
description: "description",
starCount: "starCount",
},
users: { username: "username", displayName: "displayName" },
issues: { title: "title", body: "body" },
pullRequests: { title: "title", body: "body" },
workflows: { name: "name" },
},
}));
vi.mock("@/db", () => ({
getDatabase: () => mockDb,
schema: mocks.fakeSchema,
}));
vi.mock("@/lib/errors", () => ({
withErrorHandler: (fn: any) => fn,
}));
vi.mock("drizzle-orm", () => ({
like: vi.fn(),
or: vi.fn((...args: any[]) => args),
eq: vi.fn(),
desc: vi.fn(),
}));
/* ─── helpers ─── */
function makeDb() {
return {
query: {
repositories: {
findMany: vi.fn().mockResolvedValue([
{
name: "my-project",
description: "A project",
owner: { username: "alice" },
starCount: 10,
},
]),
},
users: {
findMany: vi
.fn()
.mockResolvedValue([{ username: "alice", displayName: "Alice" }]),
},
issues: {
findMany: vi.fn().mockResolvedValue([
{
title: "Bug fix",
number: 1,
repository: { name: "my-project", owner: { username: "alice" } },
},
]),
},
pullRequests: {
findMany: vi.fn().mockResolvedValue([
{
title: "Feature PR",
number: 2,
repository: { name: "my-project", owner: { username: "alice" } },
},
]),
},
workflows: {
findMany: vi.fn().mockResolvedValue([
{
name: "CI Build",
repository: { name: "my-project", owner: { username: "alice" } },
},
]),
},
},
};
}
let mockDb: ReturnType<typeof makeDb>;
const readJson = (r: Response) => r.json();
function ctx(query: string) {
return {
request: new Request(
`http://localhost/api/search?q=${encodeURIComponent(query)}`,
),
} as any;
}
/* ─── import route ─── */
import { GET } from "@/pages/api/search";
/* ─── tests ─── */
describe("GET /api/search", () => {
beforeEach(() => {
vi.clearAllMocks();
mockDb = makeDb();
});
it("returns results for valid query", async () => {
const res = await GET(ctx("project"));
expect(res.status).toBe(200);
const json = await readJson(res);
expect(json.data.results.length).toBeGreaterThan(0);
expect(json.data.results.some((r: any) => r.type === "repository")).toBe(true);
expect(json.data.results.some((r: any) => r.type === "user")).toBe(true);
expect(json.data.results.some((r: any) => r.type === "issue")).toBe(true);
expect(json.data.results.some((r: any) => r.type === "pr")).toBe(true);
expect(json.data.results.some((r: any) => r.type === "workflow")).toBe(true);
});
it("returns empty results for short query", async () => {
const res = await GET(ctx("a"));
expect(res.status).toBe(200);
const json = await readJson(res);
expect(json.data.results).toEqual([]);
});
it("returns empty results when no query", async () => {
const res = await GET({
request: new Request("http://localhost/api/search"),
} as any);
expect(res.status).toBe(200);
const json = await readJson(res);
expect(json.data.results).toEqual([]);
});
it("formats repository results correctly", async () => {
mockDb.query.users.findMany.mockResolvedValue([]);
mockDb.query.issues.findMany.mockResolvedValue([]);
mockDb.query.pullRequests.findMany.mockResolvedValue([]);
mockDb.query.workflows.findMany.mockResolvedValue([]);
const res = await GET(ctx("project"));
expect(res.status).toBe(200);
const json = await readJson(res);
const repoResult = json.data.results.find((r: any) => r.type === "repository");
expect(repoResult).toBeDefined();
expect(repoResult.title).toBe("alice/my-project");
expect(repoResult.url).toBe("/alice/my-project");
expect(repoResult.icon).toBe("repo");
});
it("formats user results correctly", async () => {
mockDb.query.repositories.findMany.mockResolvedValue([]);
mockDb.query.issues.findMany.mockResolvedValue([]);
mockDb.query.pullRequests.findMany.mockResolvedValue([]);
mockDb.query.workflows.findMany.mockResolvedValue([]);
const res = await GET(ctx("alice"));
expect(res.status).toBe(200);
const json = await readJson(res);
const userResult = json.data.results.find((r: any) => r.type === "user");
expect(userResult).toBeDefined();
expect(userResult.subtitle).toBe("@alice");
expect(userResult.url).toBe("/alice");
});
it("handles empty DB results gracefully", async () => {
mockDb.query.repositories.findMany.mockResolvedValue([]);
mockDb.query.users.findMany.mockResolvedValue([]);
mockDb.query.issues.findMany.mockResolvedValue([]);
mockDb.query.pullRequests.findMany.mockResolvedValue([]);
mockDb.query.workflows.findMany.mockResolvedValue([]);
const res = await GET(ctx("nonexistent"));
expect(res.status).toBe(200);
const json = await readJson(res);
expect(json.data.results).toEqual([]);
});
});