-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsync-readme-versions.test.ts
More file actions
245 lines (213 loc) · 7 KB
/
sync-readme-versions.test.ts
File metadata and controls
245 lines (213 loc) · 7 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
import {afterEach, beforeEach, describe, expect, test} from 'vitest'
import type {
LatestReleaseQuery,
LatestReleaseQueryVariables
} from '@/gql/graphql.js'
import type {MockApi} from '@/tests/helpers/api.js'
import {setMockApi} from '@/tests/helpers/api.js'
import {
getLatestRelease,
QueryLatestRelease,
replaceVersionReferences
} from '../../bin/sync-readme-versions.js'
const SHA = 'abc123def456abc123def456abc123def456abc1'
const VERSION = '3.4.0'
const VARIABLES = {
owner: 'andykenward',
repo: 'github-actions-cloudflare-pages'
} as const satisfies LatestReleaseQueryVariables
describe(replaceVersionReferences, () => {
describe('main action', () => {
test('replaces tag-only version', () => {
expect(
replaceVersionReferences(
'uses: andykenward/github-actions-cloudflare-pages@v3.0.0',
SHA,
VERSION
)
).toBe(
`uses: andykenward/github-actions-cloudflare-pages@${SHA} #v${VERSION}`
)
})
test('replaces existing SHA+tag version', () => {
expect(
replaceVersionReferences(
`uses: andykenward/github-actions-cloudflare-pages@oldsha123 #v2.0.0`,
SHA,
VERSION
)
).toBe(
`uses: andykenward/github-actions-cloudflare-pages@${SHA} #v${VERSION}`
)
})
test('replaces multiple occurrences', () => {
const line = 'uses: andykenward/github-actions-cloudflare-pages@v3.0.0'
expect(
replaceVersionReferences([line, line].join('\n'), SHA, VERSION)
).toBe(
[
`uses: andykenward/github-actions-cloudflare-pages@${SHA} #v${VERSION}`,
`uses: andykenward/github-actions-cloudflare-pages@${SHA} #v${VERSION}`
].join('\n')
)
})
})
describe('delete sub-action', () => {
test('replaces tag-only version', () => {
expect(
replaceVersionReferences(
'uses: andykenward/github-actions-cloudflare-pages/delete@v2.3.2',
SHA,
VERSION
)
).toBe(
`uses: andykenward/github-actions-cloudflare-pages/delete@${SHA} #v${VERSION}`
)
})
test('replaces existing SHA+tag version', () => {
expect(
replaceVersionReferences(
`uses: andykenward/github-actions-cloudflare-pages/delete@oldsha123 #v2.0.0`,
SHA,
VERSION
)
).toBe(
`uses: andykenward/github-actions-cloudflare-pages/delete@${SHA} #v${VERSION}`
)
})
})
test('replaces both main and delete references in the same content', () => {
const content = [
'uses: andykenward/github-actions-cloudflare-pages@v3.0.0',
'uses: andykenward/github-actions-cloudflare-pages/delete@v2.3.2'
].join('\n')
expect(replaceVersionReferences(content, SHA, VERSION)).toBe(
[
`uses: andykenward/github-actions-cloudflare-pages@${SHA} #v${VERSION}`,
`uses: andykenward/github-actions-cloudflare-pages/delete@${SHA} #v${VERSION}`
].join('\n')
)
})
test('delete reference is not partially matched by main pattern', () => {
const result = replaceVersionReferences(
'uses: andykenward/github-actions-cloudflare-pages/delete@v2.3.2',
SHA,
VERSION
)
expect(result).toContain('/delete@')
expect(result).not.toMatch(/github-actions-cloudflare-pages@.*\/delete@/)
})
test('leaves content without action references unchanged', () => {
const content = '# Just a README\nSome text without action references.'
expect(replaceVersionReferences(content, SHA, VERSION)).toBe(content)
})
})
describe(getLatestRelease, () => {
let mockApi: MockApi
beforeEach(() => {
mockApi = setMockApi()
})
afterEach(async () => {
mockApi.mockAgent.assertNoPendingInterceptors()
await mockApi.mockAgent.close()
})
test('returns sha and version from GraphQL response', async () => {
mockApi.interceptGithub<LatestReleaseQuery, LatestReleaseQueryVariables>(
{query: QueryLatestRelease, variables: VARIABLES},
{
data: {
repository: {
latestRelease: {tagName: `v${VERSION}`, tagCommit: {oid: SHA}}
}
}
}
)
await expect(getLatestRelease()).resolves.toStrictEqual({
sha: SHA,
version: VERSION
})
})
test('strips v prefix from tagName', async () => {
mockApi.interceptGithub<LatestReleaseQuery, LatestReleaseQueryVariables>(
{query: QueryLatestRelease, variables: VARIABLES},
{
data: {
repository: {
latestRelease: {tagName: 'v1.2.3', tagCommit: {oid: SHA}}
}
}
}
)
const {version} = await getLatestRelease()
expect(version).toBe('1.2.3')
})
test('throws on non-ok HTTP response', async () => {
mockApi.interceptGithub<LatestReleaseQuery, LatestReleaseQueryVariables>(
{query: QueryLatestRelease, variables: VARIABLES},
{data: {} as LatestReleaseQuery},
401
)
await expect(getLatestRelease()).rejects.toThrow(
'GitHub API request failed: 401'
)
})
test('throws when GraphQL errors are present', async () => {
mockApi.interceptGithub<LatestReleaseQuery, LatestReleaseQueryVariables>(
{query: QueryLatestRelease, variables: VARIABLES},
{
data: {} as LatestReleaseQuery,
errors: [{type: 'NOT_FOUND', message: 'Not found'}]
}
)
await expect(getLatestRelease()).rejects.toThrow('GitHub API errors')
})
test('throws when repository is missing from response', async () => {
mockApi.interceptGithub<LatestReleaseQuery, LatestReleaseQueryVariables>(
{query: QueryLatestRelease, variables: VARIABLES},
{data: {repository: null}}
)
await expect(getLatestRelease()).rejects.toThrow(
'No repository in response'
)
})
test('throws when latestRelease is missing from response', async () => {
mockApi.interceptGithub<LatestReleaseQuery, LatestReleaseQueryVariables>(
{query: QueryLatestRelease, variables: VARIABLES},
{data: {repository: {latestRelease: null}}}
)
await expect(getLatestRelease()).rejects.toThrow(
'No latestRelease in response'
)
})
test('throws when tagCommit is missing from response', async () => {
mockApi.interceptGithub<LatestReleaseQuery, LatestReleaseQueryVariables>(
{query: QueryLatestRelease, variables: VARIABLES},
{
data: {
repository: {latestRelease: {tagName: `v${VERSION}`, tagCommit: null}}
}
}
)
await expect(getLatestRelease()).rejects.toThrow(
'No tagCommit in latest release'
)
})
test('throws when SHA is not 40 characters', async () => {
mockApi.interceptGithub<LatestReleaseQuery, LatestReleaseQueryVariables>(
{query: QueryLatestRelease, variables: VARIABLES},
{
data: {
repository: {
latestRelease: {
tagName: `v${VERSION}`,
tagCommit: {oid: 'tooshort'}
}
}
}
}
)
await expect(getLatestRelease()).rejects.toThrow(
'Expected full 40-char SHA'
)
})
})