This repository was archived by the owner on Aug 7, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworkflow.test.js
More file actions
254 lines (212 loc) · 7.6 KB
/
Copy pathworkflow.test.js
File metadata and controls
254 lines (212 loc) · 7.6 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
/**
* Unit tests for the GitHub Workflow module.
*/
import {jest} from '@jest/globals'
import fs from 'fs'
import path from 'path'
// Load fixtures
import commonOptions from 'fixtures/common-options.json'
import workflowConfig from 'fixtures/github/workflow-config.json'
const sampleWorkflow = fs.readFileSync(path.join(process.cwd(), 'test/github/__fixtures__/sample-workflow.yml'), 'utf8')
// Import the module under test
import Workflow from '../../src/github/workflow.js'
describe('workflow', () => {
let workflow
beforeEach(() => {
// Create instance with mocks using fixtures
const config = {
...workflowConfig,
object: {
...workflowConfig.object,
text: sampleWorkflow,
},
}
workflow = new Workflow(config, commonOptions.workflow)
})
afterEach(() => {
jest.resetAllMocks()
})
/**
* Test that Workflow class can be instantiated.
*/
test('should instantiate with valid token', () => {
expect(workflow).toBeInstanceOf(Workflow)
})
/**
* Test property getters and setters
*/
describe('property getters and setters', () => {
test('should handle id property correctly', () => {
expect(workflow.id).toBeUndefined()
workflow.id = 123
expect(workflow.id).toBe(123)
})
test('should handle node_id property correctly', () => {
expect(workflow.node_id).toBeUndefined()
workflow.node_id = 'node_123'
expect(workflow.node_id).toBe('node_123')
})
test('should handle name property correctly', () => {
expect(workflow.name).toBe(workflowConfig.name)
workflow.name = 'new-workflow'
expect(workflow.name).toBe('new-workflow')
})
test('should handle path property correctly', () => {
expect(workflow.path).toBe(workflowConfig.path)
workflow.path = '.github/workflows/new.yml'
expect(workflow.path).toBe('.github/workflows/new.yml')
})
test('should handle language property correctly', () => {
expect(workflow.language).toBe(workflowConfig.language?.name)
workflow.language = 'YAML'
expect(workflow.language).toBe('YAML')
})
test('should handle language property as undefined when not set', () => {
const configWithoutLanguage = {}
const testWorkflow = new Workflow(configWithoutLanguage, commonOptions.workflow)
expect(testWorkflow.language).toBeUndefined()
})
test('should handle text property correctly', () => {
expect(workflow.text).toBe(sampleWorkflow)
workflow.text = 'new content'
expect(workflow.text).toBe('new content')
})
test('should handle isTruncated property correctly', () => {
expect(workflow.isTruncated).toBe(false)
workflow.isTruncated = true
expect(workflow.isTruncated).toBe(true)
})
test('should handle state property correctly', () => {
expect(workflow.state).toBeUndefined()
workflow.state = 'active'
expect(workflow.state).toBe('active')
})
test('should handle created_at property correctly', () => {
expect(workflow.created_at).toBeUndefined()
const date = '2023-01-01T00:00:00Z'
workflow.created_at = date
expect(workflow.created_at).toBe(date)
})
test('should handle updated_at property correctly', () => {
expect(workflow.updated_at).toBeUndefined()
const date = '2023-01-02T00:00:00Z'
workflow.updated_at = date
expect(workflow.updated_at).toBe(date)
})
test('should handle last_run_at property correctly', () => {
expect(workflow.last_run_at).toBeUndefined()
const date = '2023-01-03T00:00:00Z'
workflow.last_run_at = date
expect(workflow.last_run_at).toBe(date)
})
})
/**
* Test workflow processing
*/
describe('workflow processing', () => {
test('should parse workflow content correctly with getYaml', async () => {
const yaml = await workflow.getYaml()
expect(yaml).toBeDefined()
expect(typeof yaml).toBe('object')
expect(yaml.name).toBeDefined()
})
test('should handle truncated workflow text gracefully', async () => {
workflow.isTruncated = true
const consoleSpy = jest.spyOn(workflow.logger, 'warn').mockImplementation()
const yaml = await workflow.getYaml()
expect(yaml).toBeNull()
expect(consoleSpy).toHaveBeenCalledWith('Workflow text is truncated. Skipping YAML parsing.')
consoleSpy.mockRestore()
})
test('should handle malformed YAML gracefully', async () => {
workflow.text = 'invalid: yaml: content: ['
const consoleSpy = jest.spyOn(workflow.logger, 'error').mockImplementation()
const yaml = await workflow.getYaml()
expect(yaml).toBeNull()
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('Malformed YAML:'))
consoleSpy.mockRestore()
})
})
/**
* Test async operations
*/
describe('async operations', () => {
beforeEach(() => {
// Mock the octokit requests
workflow.octokit.request = jest.fn()
})
test('should fetch workflow details from API', async () => {
const mockWorkflowData = {
id: 123,
node_id: 'W_123',
state: 'active',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z',
}
const mockRunsData = {
workflow_runs: [
{
updated_at: '2023-01-03T00:00:00Z',
},
],
}
workflow.octokit.request
.mockResolvedValueOnce({data: mockWorkflowData})
.mockResolvedValueOnce({data: mockRunsData})
const result = await workflow.getWorkflow('owner', 'repo', 'workflow.yml')
expect(result).toBeDefined()
expect(result.id).toBe(123)
expect(result.node_id).toBe('W_123')
expect(result.state).toBe('active')
expect(result.created_at).toBe('2023-01-01T00:00:00.000Z')
expect(result.updated_at).toBe('2023-01-02T00:00:00.000Z')
expect(result.last_run_at).toBe('2023-01-03T00:00:00.000Z')
expect(result.yaml).not.toBeInstanceOf(Promise)
expect(typeof result.yaml).toBe('object')
})
test('should handle API errors gracefully', async () => {
const error = new Error('API Error')
workflow.octokit.request.mockRejectedValueOnce(error)
await expect(workflow.getWorkflow('owner', 'repo', 'workflow.yml')).rejects.toThrow('API Error')
})
test('should handle empty workflow runs', async () => {
const mockWorkflowData = {
id: 123,
node_id: 'W_123',
state: 'active',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-02T00:00:00Z',
}
const mockRunsData = {
workflow_runs: [],
}
workflow.octokit.request
.mockResolvedValueOnce({data: mockWorkflowData})
.mockResolvedValueOnce({data: mockRunsData})
const result = await workflow.getWorkflow('owner', 'repo', 'workflow.yml')
expect(result.last_run_at).toBeNull()
})
})
/**
* Regression test: ensure getWorkflow returns a parsed YAML object (not a Promise)
*/
test('should resolve yaml object directly (regression)', async () => {
workflow.octokit.request = jest
.fn()
// workflow metadata
.mockResolvedValueOnce({
data: {
id: 42,
node_id: 'W_42',
state: 'active',
created_at: '2023-01-01T00:00:00Z',
updated_at: '2023-01-01T00:00:00Z',
},
})
// runs
.mockResolvedValueOnce({data: {workflow_runs: []}})
const result = await workflow.getWorkflow('o', 'r', workflow.path)
expect(result.yaml).not.toBeInstanceOf(Promise)
expect(result.yaml && typeof result.yaml).toBe('object')
})
})