swadhinbiswas Improve template repository discovery and filtering · 2/19/2026
f00b5bc
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
import { beforeEach, describe, expect, it, vi } from "vitest";
const {
getUserFromRequestMock,
fakeSchema,
} = vi.hoisted(() => ({
getUserFromRequestMock: vi.fn(),
fakeSchema: {
repositories: { isTemplate: {}, visibility: {}, ownerId: {}, id: {}, name: {}, description: {}, updatedAt: {} },
repositoryCollaborators: { userId: {}, repositoryId: {} },
users: { username: {}, id: {} },
} as any,
}));
let mockDb: any;
vi.mock("@/db", () => ({
getDatabase: () => mockDb,
schema: fakeSchema,
}));
vi.mock("@/db/schema", () => ({
repositories: fakeSchema.repositories,
repositoryCollaborators: fakeSchema.repositoryCollaborators,
users: fakeSchema.users,
}));
vi.mock("@/lib/auth", () => ({
getUserFromRequest: getUserFromRequestMock,
}));
import { GET as listTemplatesGet } from "@/pages/api/repos/templates";
describe("repo templates route", () => {
beforeEach(() => {
getUserFromRequestMock.mockReset();
mockDb = {
query: {
repositoryCollaborators: {
findMany: vi.fn(async () => []),
},
users: {
findFirst: vi.fn(async () => ({ id: "owner-1" })),
},
repositories: {
findMany: vi.fn(async () => [
{
id: "repo-1",
name: "starter-template",
description: "Template",
visibility: "private",
defaultBranch: "main",
language: "TypeScript",
updatedAt: new Date("2026-02-18T00:00:00Z"),
owner: {
id: "owner-1",
username: "acme",
displayName: "Acme",
avatarUrl: null,
},
},
]),
},
},
};
});
it("returns public templates for anonymous users", async () => {
getUserFromRequestMock.mockResolvedValue(null);
const response = await listTemplatesGet({
request: new Request("http://localhost/api/repos/templates"),
url: new URL("http://localhost/api/repos/templates"),
} as any);
const body = await response.json();
expect(response.status).toBe(200);
expect(body?.data?.templates?.length).toBe(1);
expect(mockDb.query.repositoryCollaborators.findMany).not.toHaveBeenCalled();
});
it("loads collaborator repository access for authenticated non-admin users", async () => {
getUserFromRequestMock.mockResolvedValue({ userId: "user-1", isAdmin: false });
await listTemplatesGet({
request: new Request("http://localhost/api/repos/templates"),
url: new URL("http://localhost/api/repos/templates"),
} as any);
expect(mockDb.query.repositoryCollaborators.findMany).toHaveBeenCalledTimes(1);
});
it("returns empty set when owner filter does not resolve", async () => {
getUserFromRequestMock.mockResolvedValue({ userId: "user-1", isAdmin: false });
mockDb.query.users.findFirst.mockResolvedValueOnce(null);
const response = await listTemplatesGet({
request: new Request("http://localhost/api/repos/templates?owner=missing"),
url: new URL("http://localhost/api/repos/templates?owner=missing"),
} as any);
const body = await response.json();
expect(response.status).toBe(200);
expect(body?.data?.templates).toEqual([]);
});
});