swadhinbiswas Prevent duplicate custom PR state names per repository · 2/19/2026
12bea2e
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
import { beforeEach, describe, expect, it, vi } from "vitest";
const { getRepoPermissionMock, fakeSchema } = vi.hoisted(() => ({
getRepoPermissionMock: vi.fn(async () => "admin"),
fakeSchema: {
prStateDefinitions: { repositoryId: {}, name: {}, id: {} },
} as any,
}));
let mockDb: any;
vi.mock("@/db", () => ({
getDatabase: () => mockDb,
schema: fakeSchema,
}));
vi.mock("@/lib/permissions", () => ({
getRepoPermission: getRepoPermissionMock,
}));
import { POST as createState } from "@/pages/api/repos/[owner]/[repo]/settings/states";
import { PUT as updateState } from "@/pages/api/repos/[owner]/[repo]/settings/states/[id]";
function makeDb() {
return {
query: {
prStateDefinitions: {
findFirst: vi.fn(async (_args?: any) => ({ id: "existing-state", name: "in_review" })),
},
},
insert: vi.fn(() => ({
values: vi.fn(async () => ({ returning: vi.fn(async () => []) })),
})),
update: vi.fn(() => ({
set: vi.fn(() => ({
where: vi.fn(async () => []),
})),
})),
};
}
async function readJson(response: Response): Promise<any> {
return response.json();
}
describe("settings states duplicate protection", () => {
beforeEach(() => {
mockDb = makeDb();
getRepoPermissionMock.mockResolvedValue("admin");
});
it("rejects creating duplicate state name in repository", async () => {
const response = await createState({
locals: { repo: { id: "repo-1" }, user: { id: "user-1" } },
request: new Request("http://localhost/api/repos/owner/demo/settings/states", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ name: "In Review", displayName: "In Review" }),
}),
} as any);
const body = await readJson(response);
expect(response.status).toBe(400);
expect(body?.error?.code).toBe("BAD_REQUEST");
});
it("rejects updating state to duplicate name in repository", async () => {
mockDb.query.prStateDefinitions.findFirst
.mockResolvedValueOnce({ id: "state-2", name: "todo" })
.mockResolvedValueOnce({ id: "state-1", name: "in_review" });
const response = await updateState({
params: { id: "state-2" },
locals: { repo: { id: "repo-1" }, user: { id: "user-1" } },
request: new Request("http://localhost/api/repos/owner/demo/settings/states/state-2", {
method: "PUT",
headers: { "content-type": "application/json" },
body: JSON.stringify({ name: "In Review" }),
}),
} as any);
const body = await readJson(response);
expect(response.status).toBe(400);
expect(body?.error?.code).toBe("BAD_REQUEST");
});
});