Skip to content

Commit a9d393e

Browse files
committed
Add unit tests for the contributors route
1 parent aa3cbba commit a9d393e

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

.sync-state.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
"lastProcessed": "2025-09-19T22:14:33.891Z"
1919
}
2020
}
21-
}
21+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { describe, it } from "node:test"
2+
import { strict as assert } from "node:assert"
3+
import { NextRequest } from "next/server"
4+
5+
import { GET } from "./route"
6+
7+
import contributorData from "../../../../scripts/sync-landing-schema/data.json"
8+
9+
describe("Contributors API", () => {
10+
it("returns 400 when project parameter is missing", async () => {
11+
const request = new NextRequest("http://localhost:3000/api/contributors")
12+
const response = await GET(request)
13+
14+
assert.strictEqual(response.status, 400)
15+
16+
const json = await response.json()
17+
assert.strictEqual(json.error, "Bad request")
18+
assert.strictEqual(json.message, "Repository parameter is required")
19+
})
20+
21+
it("returns contributors data for synced project", async () => {
22+
const request = new NextRequest(
23+
"http://localhost:3000/api/contributors?project=GraphQL",
24+
)
25+
const response = await GET(request)
26+
27+
assert.strictEqual(response.status, 200)
28+
29+
const json = await response.json()
30+
const expectedData = contributorData.GraphQL
31+
32+
// Should return all contributors when no pagination params
33+
assert.strictEqual(json.length, 5) // Default first=5
34+
35+
// Should be sorted by contributions (descending), then by id (ascending)
36+
const sortedExpected = expectedData
37+
.sort((a, b) => {
38+
if (b.contributions !== a.contributions) {
39+
return b.contributions - a.contributions
40+
}
41+
return a.id.localeCompare(b.id)
42+
})
43+
.slice(0, 5)
44+
45+
assert.deepStrictEqual(json, sortedExpected)
46+
})
47+
48+
it("returns empty array for a project we don't have data for", async () => {
49+
const request = new NextRequest(
50+
"http://localhost:3000/api/contributors?project=NonExistent",
51+
)
52+
const response = await GET(request)
53+
54+
assert.strictEqual(response.status, 200)
55+
56+
const json = await response.json()
57+
assert.ok(Array.isArray(json))
58+
assert.strictEqual(json.length, 0)
59+
})
60+
61+
it("respects first parameter for pagination", async () => {
62+
const request = new NextRequest(
63+
"http://localhost:3000/api/contributors?project=GraphQL&first=2",
64+
)
65+
const response = await GET(request)
66+
67+
assert.strictEqual(response.status, 200)
68+
69+
const json = await response.json()
70+
const expectedData = contributorData.GraphQL
71+
const sortedExpected = expectedData
72+
.sort((a, b) => {
73+
if (b.contributions !== a.contributions) {
74+
return b.contributions - a.contributions
75+
}
76+
return a.id.localeCompare(b.id)
77+
})
78+
.slice(0, 2)
79+
80+
assert.strictEqual(json.length, 2)
81+
assert.deepStrictEqual(json, sortedExpected)
82+
})
83+
84+
it("handles after parameter for cursor pagination", async () => {
85+
const expectedData = contributorData.GraphQL
86+
const sortedExpected = expectedData.sort((a, b) => {
87+
if (b.contributions !== a.contributions) {
88+
return b.contributions - a.contributions
89+
}
90+
return a.id.localeCompare(b.id)
91+
})
92+
93+
// Get first contributor's ID to use as cursor
94+
const firstContributorId = sortedExpected[0].id
95+
96+
const request = new NextRequest(
97+
`http://localhost:3000/api/contributors?project=GraphQL&first=2&after=${firstContributorId}`,
98+
)
99+
const response = await GET(request)
100+
101+
const json = await response.json()
102+
const expectedAfterFirst = sortedExpected.slice(1, 3)
103+
104+
assert.strictEqual(json.length, 2)
105+
assert.deepStrictEqual(json, expectedAfterFirst)
106+
})
107+
})

src/github-stats.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,4 +1239,4 @@
12391239
"lastRelease": "2025-09-01T19:49:28Z",
12401240
"formattedLastRelease": "1 week ago"
12411241
}
1242-
}
1242+
}

0 commit comments

Comments
 (0)