Swadhin Biswas Feature/ai code reviewer (#80) · 8/17/2026
9740d3d
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import {
BranchProtectionSchema,
CreateIssueSchema,
CreatePullRequestSchema,
CreateRepositorySchema,
GeneralConfigSchema,
RegisterUserSchema,
StorageConfigSchema,
WebhookConfigSchema,
} from "@/lib/validation";
import { describe, expect, it } from "vitest";
describe("RegisterUserSchema", () => {
it("accepts a valid registration", () => {
const result = RegisterUserSchema.safeParse({
username: "testuser",
email: "[email protected]",
password: "StrongP@ss123",
displayName: "Test User",
});
expect(result.success).toBe(true);
});
it("rejects missing username", () => {
const result = RegisterUserSchema.safeParse({
email: "[email protected]",
password: "StrongP@ss123",
});
expect(result.success).toBe(false);
});
it("rejects invalid email", () => {
const result = RegisterUserSchema.safeParse({
username: "testuser",
email: "notanemail",
password: "StrongP@ss123",
});
expect(result.success).toBe(false);
});
it("rejects short password", () => {
const result = RegisterUserSchema.safeParse({
username: "testuser",
email: "[email protected]",
password: "abc",
});
expect(result.success).toBe(false);
});
it("rejects empty username", () => {
const result = RegisterUserSchema.safeParse({
username: "",
email: "[email protected]",
password: "StrongP@ss123",
});
expect(result.success).toBe(false);
});
});
describe("CreateRepositorySchema", () => {
it("accepts minimal valid repo", () => {
const result = CreateRepositorySchema.safeParse({
name: "my-repo",
});
expect(result.success).toBe(true);
});
it("accepts full valid repo", () => {
const result = CreateRepositorySchema.safeParse({
name: "my-repo",
description: "A test repository",
visibility: "public",
defaultBranch: "main",
});
expect(result.success).toBe(true);
});
it("rejects missing name", () => {
const result = CreateRepositorySchema.safeParse({
description: "A test repository",
});
expect(result.success).toBe(false);
});
it("rejects empty name", () => {
const result = CreateRepositorySchema.safeParse({ name: "" });
expect(result.success).toBe(false);
});
it("rejects invalid visibility", () => {
const result = CreateRepositorySchema.safeParse({
name: "my-repo",
visibility: "invalid-mode",
});
expect(result.success).toBe(false);
});
});
describe("CreateIssueSchema", () => {
it("accepts a valid issue", () => {
const result = CreateIssueSchema.safeParse({
title: "Bug report",
body: "Something is broken",
});
expect(result.success).toBe(true);
});
it("rejects empty title", () => {
const result = CreateIssueSchema.safeParse({
title: "",
body: "Something is broken",
});
expect(result.success).toBe(false);
});
it("rejects missing title", () => {
const result = CreateIssueSchema.safeParse({
body: "Something is broken",
});
expect(result.success).toBe(false);
});
it("sanitizes HTML in body", () => {
const result = CreateIssueSchema.safeParse({
title: "XSS test",
body: '<script>alert("xss")</script>normal text',
});
expect(result.success).toBe(true);
if (result.success) {
expect(result.data.body).not.toContain("<script");
}
});
});
describe("CreatePullRequestSchema", () => {
it("accepts a valid pull request", () => {
const result = CreatePullRequestSchema.safeParse({
title: "New feature",
body: "Adds cool stuff",
head: "feature-branch",
base: "main",
});
expect(result.success).toBe(true);
});
it("rejects missing head branch", () => {
const result = CreatePullRequestSchema.safeParse({
title: "New feature",
base: "main",
});
expect(result.success).toBe(false);
});
it("rejects missing base branch", () => {
const result = CreatePullRequestSchema.safeParse({
title: "New feature",
head: "feature-branch",
});
expect(result.success).toBe(false);
});
it("rejects missing title", () => {
const result = CreatePullRequestSchema.safeParse({
head: "feature",
base: "main",
});
expect(result.success).toBe(false);
});
});
describe("BranchProtectionSchema", () => {
it("accepts valid protection rules", () => {
const result = BranchProtectionSchema.safeParse({
pattern: "main",
active: true,
requiresPr: true,
requiredApprovals: 2,
});
expect(result.success).toBe(true);
});
it("accepts minimal pattern", () => {
const result = BranchProtectionSchema.safeParse({
pattern: "main",
});
expect(result.success).toBe(true);
});
it("rejects missing pattern", () => {
const result = BranchProtectionSchema.safeParse({
active: true,
});
expect(result.success).toBe(false);
});
it("rejects negative approvals", () => {
const result = BranchProtectionSchema.safeParse({
pattern: "main",
requiredApprovals: -1,
});
expect(result.success).toBe(false);
});
});
describe("WebhookConfigSchema", () => {
it("accepts valid webhook config", async () => {
const result = await WebhookConfigSchema.safeParseAsync({
url: "https://example.com/webhook",
events: ["push", "pull_request"],
secret: "mysecret",
active: true,
});
expect(result.success).toBe(true);
});
it("rejects missing url", async () => {
const result = await WebhookConfigSchema.safeParseAsync({
events: ["push"],
});
expect(result.success).toBe(false);
});
it("rejects private IP urls (SSRF protection)", async () => {
const result = await WebhookConfigSchema.safeParseAsync({
url: "http://127.0.0.1:8080/hook",
events: ["push"],
});
// Should fail SSRF refine
expect(result.success).toBe(false);
});
it("rejects localhost urls", async () => {
const result = await WebhookConfigSchema.safeParseAsync({
url: "http://localhost:3000/hook",
events: ["push"],
});
expect(result.success).toBe(false);
});
});
describe("StorageConfigSchema", () => {
it("accepts local storage config", () => {
const result = StorageConfigSchema.safeParse({
type: "local",
basePath: "/data/storage",
});
expect(result.success).toBe(true);
});
it("accepts s3 storage config", () => {
const result = StorageConfigSchema.safeParse({
type: "s3",
bucket: "my-bucket",
region: "us-east-1",
});
expect(result.success).toBe(true);
});
});
describe("GeneralConfigSchema", () => {
it("accepts valid general config", () => {
const result = GeneralConfigSchema.safeParse({
siteName: "My CodeHub",
allowSignups: true,
});
expect(result.success).toBe(true);
});
});