-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathgithub.test.ts
More file actions
320 lines (256 loc) · 10 KB
/
github.test.ts
File metadata and controls
320 lines (256 loc) · 10 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import { describe, expect, test, mock, beforeEach } from "bun:test"
// ---------------------------------------------------------------------------
// Mock execSync to avoid real GitHub API calls
// ---------------------------------------------------------------------------
let mockExecOutput: string | null = null
let mockExecShouldThrow = false
let lastExecCmd: string | null = null
// Spread the real child_process module so `spawn`, `exec`, etc. still work,
// and only override `execSync` for our tests.
import * as realChildProcess from "child_process"
mock.module("child_process", () => ({
...realChildProcess,
execSync: (cmd: string, _opts?: any) => {
lastExecCmd = cmd
if (mockExecShouldThrow) throw new Error("exec failed")
return mockExecOutput ?? ""
},
}))
// Import after mocking
const { fetchReleases, getRelease, getReleaseTags, validateRelease } = await import(
"../../../../script/upstream/utils/github"
)
// ---------------------------------------------------------------------------
// Test data
// ---------------------------------------------------------------------------
const MOCK_RELEASES = [
{
tag_name: "v1.2.26",
name: "v1.2.26",
prerelease: false,
draft: false,
published_at: "2026-03-13T16:33:18Z",
html_url: "https://github.com/anomalyco/opencode/releases/tag/v1.2.26",
},
{
tag_name: "v1.2.25",
name: "v1.2.25",
prerelease: false,
draft: false,
published_at: "2026-03-12T23:34:33Z",
html_url: "https://github.com/anomalyco/opencode/releases/tag/v1.2.25",
},
{
tag_name: "v1.2.24-beta.1",
name: "v1.2.24-beta.1",
prerelease: true,
draft: false,
published_at: "2026-03-08T10:00:00Z",
html_url: "https://github.com/anomalyco/opencode/releases/tag/v1.2.24-beta.1",
},
{
tag_name: "v1.2.24",
name: "v1.2.24",
prerelease: false,
draft: false,
published_at: "2026-03-09T16:10:00Z",
html_url: "https://github.com/anomalyco/opencode/releases/tag/v1.2.24",
},
]
const MOCK_DRAFT_RELEASE = {
tag_name: "v1.3.0",
name: "v1.3.0",
prerelease: false,
draft: true,
published_at: "2026-03-15T00:00:00Z",
html_url: "https://github.com/anomalyco/opencode/releases/tag/v1.3.0",
}
// ---------------------------------------------------------------------------
// fetchReleases
// ---------------------------------------------------------------------------
describe("fetchReleases()", () => {
beforeEach(() => {
mockExecOutput = null
mockExecShouldThrow = false
lastExecCmd = null
})
test("returns stable releases, excluding drafts and pre-releases", async () => {
const stableOnly = MOCK_RELEASES.filter((r) => !r.prerelease && !r.draft)
mockExecOutput = JSON.stringify(stableOnly)
const releases = await fetchReleases("anomalyco/opencode")
expect(releases).toHaveLength(3)
expect(releases.every((r) => !r.prerelease && !r.draft)).toBe(true)
})
test("includes pre-releases when includePrerelease is true", async () => {
const nonDraft = MOCK_RELEASES.filter((r) => !r.draft)
mockExecOutput = JSON.stringify(nonDraft)
const releases = await fetchReleases("anomalyco/opencode", {
includePrerelease: true,
})
expect(releases).toHaveLength(4)
expect(releases.some((r) => r.prerelease)).toBe(true)
})
test("excludes draft releases even with includePrerelease", async () => {
const allWithDraft = [...MOCK_RELEASES, MOCK_DRAFT_RELEASE].filter((r) => !r.draft)
mockExecOutput = JSON.stringify(allWithDraft)
const releases = await fetchReleases("anomalyco/opencode", {
includePrerelease: true,
})
expect(releases.every((r) => !r.draft)).toBe(true)
})
test("returns empty array when no releases exist", async () => {
mockExecOutput = ""
const releases = await fetchReleases("anomalyco/opencode")
expect(releases).toEqual([])
})
test("throws on API failure", async () => {
mockExecShouldThrow = true
expect(fetchReleases("anomalyco/opencode")).rejects.toThrow(
"Failed to fetch releases",
)
})
test("calls gh API with correct repo", async () => {
mockExecOutput = "[]"
await fetchReleases("anomalyco/opencode")
expect(lastExecCmd).toContain("repos/anomalyco/opencode/releases")
})
test("respects limit parameter", async () => {
mockExecOutput = "[]"
await fetchReleases("anomalyco/opencode", { limit: 5 })
expect(lastExecCmd).toContain(".[0:5]")
})
test("pipes paginated output to external jq for slurping", async () => {
mockExecOutput = "[]"
await fetchReleases("anomalyco/opencode")
// Uses --jq '.[]' to unpack pages, then pipes to jq -s for slurping
expect(lastExecCmd).toContain("--jq '.[]'")
expect(lastExecCmd).toContain("| jq -s")
})
test("filters before slicing (filter then limit)", async () => {
mockExecOutput = "[]"
await fetchReleases("anomalyco/opencode", { limit: 10 })
expect(lastExecCmd).toContain("[.[] | select(")
expect(lastExecCmd).toMatch(/select\(.*\)\] \| \.\[0:10\]/)
})
})
// ---------------------------------------------------------------------------
// getRelease
// ---------------------------------------------------------------------------
describe("getRelease()", () => {
beforeEach(() => {
mockExecOutput = null
mockExecShouldThrow = false
lastExecCmd = null
})
test("returns release for a valid published tag", async () => {
mockExecOutput = JSON.stringify(MOCK_RELEASES[0])
const release = await getRelease("anomalyco/opencode", "v1.2.26")
expect(release).not.toBeNull()
expect(release!.tag_name).toBe("v1.2.26")
expect(release!.draft).toBe(false)
})
test("returns null for a draft release", async () => {
mockExecOutput = JSON.stringify(MOCK_DRAFT_RELEASE)
const release = await getRelease("anomalyco/opencode", "v1.3.0")
expect(release).toBeNull()
})
test("returns null when tag does not exist", async () => {
mockExecShouldThrow = true
const release = await getRelease("anomalyco/opencode", "v99.99.99")
expect(release).toBeNull()
})
test("returns null for empty response", async () => {
mockExecOutput = ""
const release = await getRelease("anomalyco/opencode", "v1.2.26")
expect(release).toBeNull()
})
test("queries the correct tag endpoint", async () => {
mockExecOutput = JSON.stringify(MOCK_RELEASES[0])
await getRelease("anomalyco/opencode", "v1.2.26")
expect(lastExecCmd).toContain("releases/tags/v1.2.26")
})
})
// ---------------------------------------------------------------------------
// getReleaseTags
// ---------------------------------------------------------------------------
describe("getReleaseTags()", () => {
beforeEach(() => {
mockExecOutput = null
mockExecShouldThrow = false
})
test("returns only tag names from releases", async () => {
const stableOnly = MOCK_RELEASES.filter((r) => !r.prerelease && !r.draft)
mockExecOutput = JSON.stringify(stableOnly)
const tags = await getReleaseTags("anomalyco/opencode")
expect(tags).toEqual(["v1.2.26", "v1.2.25", "v1.2.24"])
})
test("includes pre-release tags when requested", async () => {
const nonDraft = MOCK_RELEASES.filter((r) => !r.draft)
mockExecOutput = JSON.stringify(nonDraft)
const tags = await getReleaseTags("anomalyco/opencode", {
includePrerelease: true,
})
expect(tags).toContain("v1.2.24-beta.1")
})
})
// ---------------------------------------------------------------------------
// validateRelease
// ---------------------------------------------------------------------------
describe("validateRelease()", () => {
beforeEach(() => {
mockExecOutput = null
mockExecShouldThrow = false
})
test("valid: true for a published stable release", async () => {
mockExecOutput = JSON.stringify(MOCK_RELEASES[0])
const result = await validateRelease("anomalyco/opencode", "v1.2.26")
expect(result.valid).toBe(true)
expect(result.release).toBeDefined()
expect(result.release!.tag_name).toBe("v1.2.26")
})
test("valid: false for a non-existent tag", async () => {
mockExecShouldThrow = true
const result = await validateRelease("anomalyco/opencode", "v99.99.99")
expect(result.valid).toBe(false)
expect(result.reason).toContain("not a published GitHub release")
})
test("valid: false for a pre-release", async () => {
mockExecOutput = JSON.stringify(MOCK_RELEASES[2]) // beta
const result = await validateRelease("anomalyco/opencode", "v1.2.24-beta.1")
expect(result.valid).toBe(false)
expect(result.reason).toContain("pre-release")
})
test("valid: false for a draft release", async () => {
mockExecOutput = JSON.stringify(MOCK_DRAFT_RELEASE)
// getRelease returns null for drafts
const result = await validateRelease("anomalyco/opencode", "v1.3.0")
expect(result.valid).toBe(false)
})
test("reason includes --include-prerelease hint for pre-releases", async () => {
mockExecOutput = JSON.stringify(MOCK_RELEASES[2])
const result = await validateRelease("anomalyco/opencode", "v1.2.24-beta.1")
expect(result.reason).toContain("--include-prerelease")
})
test("reason mentions the repo name for non-existent tags", async () => {
mockExecShouldThrow = true
const result = await validateRelease("anomalyco/opencode", "vscode-v0.0.5")
expect(result.reason).toContain("anomalyco/opencode")
})
test("valid: true for a pre-release when includePrerelease is true", async () => {
mockExecOutput = JSON.stringify(MOCK_RELEASES[2]) // beta
const result = await validateRelease("anomalyco/opencode", "v1.2.24-beta.1", {
includePrerelease: true,
})
expect(result.valid).toBe(true)
expect(result.release).toBeDefined()
expect(result.release!.prerelease).toBe(true)
})
test("valid: false for a pre-release when includePrerelease is false", async () => {
mockExecOutput = JSON.stringify(MOCK_RELEASES[2])
const result = await validateRelease("anomalyco/opencode", "v1.2.24-beta.1", {
includePrerelease: false,
})
expect(result.valid).toBe(false)
expect(result.reason).toContain("pre-release")
})
})