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 pathcsv.test.js
More file actions
232 lines (198 loc) · 7.02 KB
/
Copy pathcsv.test.js
File metadata and controls
232 lines (198 loc) · 7.02 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
/**
* Unit tests for the CSV reporter module.
*/
import {jest} from '@jest/globals'
// Mock dependencies
jest.mock('node:fs/promises')
// Load fixtures
import commonOptions from 'fixtures/common-options.json'
import testDataRaw from 'fixtures/report/test-data.json'
// Import the module under test
import CsvReporter from '../../src/report/csv.js'
import Reporter from '../../src/report/reporter.js'
describe('CsvReporter', () => {
let csvReporter
let options
let testData
let testFilePath
beforeEach(() => {
testFilePath = '/test/path/report.csv'
options = commonOptions.csvReport
// Transform fixture data to match test requirements (Sets, etc.)
testData = testDataRaw.map(item => ({
...item,
listeners: Array.isArray(item.listeners) ? new Set(item.listeners) : item.listeners,
permissions: Array.isArray(item.permissions) ? new Set(item.permissions) : item.permissions,
runsOn: Array.isArray(item.runsOn) ? new Set(item.runsOn) : item.runsOn,
secrets: item.secrets ? (Array.isArray(item.secrets) ? new Set(item.secrets) : item.secrets) : null,
vars: item.vars && Array.isArray(item.vars) ? new Set(item.vars) : item.vars,
uses: Array.isArray(item.uses) ? new Set(item.uses) : item.uses,
updated_at: item.updated_at === '2025-06-10T00:00:00Z' ? new Date(item.updated_at) : item.updated_at,
}))
// Create a spy for the saveFile method
jest.spyOn(Reporter.prototype, 'saveFile').mockImplementation(() => Promise.resolve())
// Mock the createUniquePath method
jest.spyOn(Reporter.prototype, 'createUniquePath').mockReturnValue('/test/path/report.unique.csv')
// Create instance with test data
csvReporter = new CsvReporter(testFilePath, options, testData)
})
afterEach(() => {
jest.resetAllMocks()
})
/**
* Test basic functionality
*/
describe('basic functionality', () => {
/**
* Test that CsvReporter class can be instantiated.
*/
test('should instantiate with valid parameters', () => {
expect(csvReporter).toBeInstanceOf(CsvReporter)
expect(csvReporter.path).toBe(testFilePath)
expect(csvReporter.options).toEqual(options)
expect(csvReporter.data).toEqual(testData)
})
})
/**
* Test save operations
*/
describe('save operations', () => {
/**
* Test the save method
*/
test('should save data as CSV correctly', async () => {
await csvReporter.save()
// Verify that saveFile was called with correct arguments
expect(Reporter.prototype.saveFile).toHaveBeenCalledWith(
testFilePath,
expect.stringContaining(
'owner,repo,name,workflow,state,created_at,updated_at,last_run_at,listeners,permissions,runs-on,secrets,vars,uses',
),
)
})
/**
* Test the saveUnique method
*/
test('should save unique uses data as CSV correctly', async () => {
await csvReporter.saveUnique()
// Verify that saveFile was called with correct arguments
expect(Reporter.prototype.saveFile).toHaveBeenCalledWith(
'/test/path/report.unique.csv',
expect.stringContaining('uses'),
)
})
})
/**
* Test CSV formatting
*/
describe('CSV formatting', () => {
/**
* Test the createHeaders method
*/
test('should create correct headers based on options', () => {
// Test with all options enabled
const headers = csvReporter.createHeaders()
expect(headers).toEqual([
'owner',
'repo',
'name',
'workflow',
'state',
'created_at',
'updated_at',
'last_run_at',
'listeners',
'permissions',
'runs-on',
'secrets',
'vars',
'uses',
])
// Test with limited options
const limitedOptions = {listeners: true, uses: true}
const limitedCsv = new CsvReporter(testFilePath, limitedOptions, testData)
const limitedHeaders = limitedCsv.createHeaders()
expect(limitedHeaders).toEqual([
'owner',
'repo',
'name',
'workflow',
'state',
'created_at',
'updated_at',
'last_run_at',
'listeners',
'uses',
])
})
/**
* Test formatValue method with different types of values
*/
test('should format values correctly for CSV output', () => {
// Test with a string
expect(csvReporter.formatValue('test')).toBe('test')
// Test with a number
expect(csvReporter.formatValue(123)).toBe(123)
// Test with a Date object
const date = new Date('2025-06-01T00:00:00Z')
expect(csvReporter.formatValue(date)).toBe('2025-06-01T00:00:00.000Z')
// Test with a Set
const set = new Set(['item1', 'item2'])
expect(csvReporter.formatValue(set)).toBe('item1, item2')
// Test with an array
expect(csvReporter.formatValue(['item1', 'item2'])).toEqual(['item1', 'item2'])
// Test with null/undefined
expect(csvReporter.formatValue(null)).toBe('')
expect(csvReporter.formatValue(undefined)).toBe('')
// Test with a complex object
const obj = {key1: 'value1', key2: 'value2'}
expect(csvReporter.formatValue(obj)).toBe('{key1: value1, key2: value2}')
})
/**
* Test formatObjectForCsv method
*/
test('should format objects correctly for CSV', () => {
// Test with a plain object
const plainObj = {key1: 'value1', key2: 'value2'}
expect(csvReporter.formatObjectForCsv(plainObj)).toBe('{key1: value1, key2: value2}')
// Test with a nested object
const nestedObj = {key1: 'value1', key2: {nested: 'value'}}
expect(csvReporter.formatObjectForCsv(nestedObj)).toBe('{key1: value1, key2: {nested: value}}')
// Test with special types in object
const specialObj = {
str: 'string',
num: 123,
bool: true,
nil: null,
set: new Set(['item1', 'item2']),
arr: ['item1', 'item2'],
}
const result = csvReporter.formatObjectForCsv(specialObj)
expect(result).toContain('str: string')
expect(result).toContain('num: 123')
expect(result).toContain('bool: true')
expect(result).toContain('{str: string, num: 123, bool: true, nil: null, set: {}, arr: [item1, item2]}')
})
})
/**
* Test error handling
*/
describe('error handling', () => {
/**
* Test specific error handling for save method
*/
test('should throw specific error on save failure', async () => {
// Mock saveFile to throw an error
Reporter.prototype.saveFile.mockRejectedValueOnce(new Error('Test error'))
await expect(csvReporter.save()).rejects.toThrow('Failed to save CSV report')
})
/**
* Test specific error handling for saveUnique method
*/
test('should throw specific error on saveUnique failure', async () => {
// Mock saveFile to throw an error
Reporter.prototype.saveFile.mockRejectedValueOnce(new Error('Test error'))
await expect(csvReporter.saveUnique()).rejects.toThrow('Failed to save unique uses CSV report')
})
})
})