forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv-header-alias.test.ts
More file actions
221 lines (191 loc) · 8.84 KB
/
Copy pathcsv-header-alias.test.ts
File metadata and controls
221 lines (191 loc) · 8.84 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
import { describe, it, expect } from 'vitest'
import { parseCsvBatch } from '@/lib/csv-parser'
/**
* Tests for CSV header alias detection in parseCsvBatch().
* Verifies that different supported alias spellings for the same logical column
* all map to the same internal field correctly.
*/
describe('parseCsvBatch - header alias detection', () => {
describe('recipient field aliases', () => {
it('maps "recipient" to recipient field', () => {
const csv = ['recipient,amount,start_time,end_time', 'GABC,1000,1000,2000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.errors).toHaveLength(0)
expect(result.headerMapping?.recipient).toBe(0)
expect(result.rows[0].recipient).toBe('GABC')
})
it('maps "recipient_address" to recipient field', () => {
const csv = ['recipient_address,amount,start_time,end_time', 'GXYZ,1000,1000,2000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.errors).toHaveLength(0)
expect(result.headerMapping?.recipient).toBe(0)
expect(result.rows[0].recipient).toBe('GXYZ')
})
it('maps "address" to recipient field', () => {
const csv = ['address,amount,start_time,end_time', 'GDEF,1000,1000,2000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.errors).toHaveLength(0)
expect(result.headerMapping?.recipient).toBe(0)
expect(result.rows[0].recipient).toBe('GDEF')
})
it('maps "to" to recipient field', () => {
const csv = ['to,amount,start_time,end_time', 'GHIJ,1000,1000,2000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.errors).toHaveLength(0)
expect(result.headerMapping?.recipient).toBe(0)
expect(result.rows[0].recipient).toBe('GHIJ')
})
})
describe('amount field aliases', () => {
it('maps "amount" to amount field', () => {
const csv = ['recipient,amount,start_time,end_time', 'GABC,1000,1000,2000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.headerMapping?.amount).toBe(1)
expect(result.rows[0].amount).toBe('1000')
})
it('maps "total_amount" to amount field', () => {
const csv = ['recipient,total_amount,start_time,end_time', 'GABC,2500,1000,2000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.headerMapping?.amount).toBe(1)
expect(result.rows[0].amount).toBe('2500')
})
it('maps "stream_amount" to amount field', () => {
const csv = ['recipient,stream_amount,start_time,end_time', 'GABC,5000,1000,2000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.headerMapping?.amount).toBe(1)
expect(result.rows[0].amount).toBe('5000')
})
})
describe('start_time field aliases', () => {
it('maps "start_time" to start_time field', () => {
const csv = ['recipient,amount,start_time,end_time', 'GABC,1000,1000,2000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.headerMapping?.start_time).toBe(2)
expect(result.rows[0].start_time).toBe('1000')
})
it('maps "start_date" to start_time field', () => {
const csv = ['recipient,amount,start_date,end_time', 'GABC,1000,1500,2000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.headerMapping?.start_time).toBe(2)
expect(result.rows[0].start_time).toBe('1500')
})
it('maps "start_timestamp" to start_time field', () => {
const csv = ['recipient,amount,start_timestamp,end_time', 'GABC,1000,1234,2000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.headerMapping?.start_time).toBe(2)
expect(result.rows[0].start_time).toBe('1234')
})
})
describe('end_time field aliases', () => {
it('maps "end_time" to end_time field', () => {
const csv = ['recipient,amount,start_time,end_time', 'GABC,1000,1000,2000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.headerMapping?.end_time).toBe(3)
expect(result.rows[0].end_time).toBe('2000')
})
it('maps "end_date" to end_time field', () => {
const csv = ['recipient,amount,start_time,end_date', 'GABC,1000,1000,3000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.headerMapping?.end_time).toBe(3)
expect(result.rows[0].end_time).toBe('3000')
})
it('maps "end_timestamp" to end_time field', () => {
const csv = ['recipient,amount,start_time,end_timestamp', 'GABC,1000,1000,4000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.headerMapping?.end_time).toBe(3)
expect(result.rows[0].end_time).toBe('4000')
})
})
describe('cliff_time field aliases', () => {
it('maps "cliff_time" to cliff_time field', () => {
const csv = ['recipient,amount,start_time,end_time,cliff_time', 'GABC,1000,1000,2000,1500'].join('\n')
const result = parseCsvBatch(csv)
expect(result.headerMapping?.cliff_time).toBe(4)
expect(result.rows[0].cliff_time).toBe('1500')
})
it('maps "cliff_date" to cliff_time field', () => {
const csv = ['recipient,amount,start_time,end_time,cliff_date', 'GABC,1000,1000,2000,1600'].join('\n')
const result = parseCsvBatch(csv)
expect(result.headerMapping?.cliff_time).toBe(4)
expect(result.rows[0].cliff_time).toBe('1600')
})
it('maps "cliff_timestamp" to cliff_time field', () => {
const csv = ['recipient,amount,start_time,end_time,cliff_timestamp', 'GABC,1000,1000,2000,1700'].join('\n')
const result = parseCsvBatch(csv)
expect(result.headerMapping?.cliff_time).toBe(4)
expect(result.rows[0].cliff_time).toBe('1700')
})
})
describe('cliff_duration field aliases', () => {
it('maps "cliff_duration" to cliff_duration field', () => {
const csv = ['recipient,amount,start_time,end_time,cliff_duration', 'GABC,1000,1000,2000,30d'].join('\n')
const result = parseCsvBatch(csv)
expect(result.headerMapping?.cliff_duration).toBe(4)
expect(result.rows[0].cliff_duration).toBe('30d')
})
it('maps "cliff_period" to cliff_duration field', () => {
const csv = ['recipient,amount,start_time,end_time,cliff_period', 'GABC,1000,1000,2000,7d'].join('\n')
const result = parseCsvBatch(csv)
expect(result.headerMapping?.cliff_duration).toBe(4)
expect(result.rows[0].cliff_duration).toBe('7d')
})
})
describe('mixed aliases in single CSV', () => {
it('correctly maps different aliases for each field', () => {
const csv = ['to,stream_amount,start_date,end_timestamp,cliff_period', 'GABC,5000,1000,3000,14d'].join('\n')
const result = parseCsvBatch(csv)
expect(result.errors).toHaveLength(0)
expect(result.headerMapping?.recipient).toBe(0)
expect(result.headerMapping?.amount).toBe(1)
expect(result.headerMapping?.start_time).toBe(2)
expect(result.headerMapping?.end_time).toBe(3)
expect(result.headerMapping?.cliff_duration).toBe(4)
expect(result.rows[0].recipient).toBe('GABC')
expect(result.rows[0].amount).toBe('5000')
expect(result.rows[0].start_time).toBe('1000')
expect(result.rows[0].end_time).toBe('3000')
expect(result.rows[0].cliff_duration).toBe('14d')
})
})
describe('case insensitivity', () => {
it('matches aliases regardless of case', () => {
const csv = ['RECIPIENT,AMOUNT,START_TIME,END_TIME', 'GABC,1000,1000,2000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.errors).toHaveLength(0)
expect(result.rows[0].recipient).toBe('GABC')
expect(result.rows[0].amount).toBe('1000')
})
it('matches mixed case aliases', () => {
const csv = ['Recipient_Address,Total_Amount,Start_Date,End_Date', 'GXYZ,2000,1000,3000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.errors).toHaveLength(0)
expect(result.rows[0].recipient).toBe('GXYZ')
expect(result.rows[0].amount).toBe('2000')
})
})
describe('whitespace handling in headers', () => {
it('trims whitespace from headers before matching', () => {
const csv = [' recipient , amount , start_time , end_time ', 'GABC,1000,1000,2000'].join('\n')
const result = parseCsvBatch(csv)
expect(result.errors).toHaveLength(0)
expect(result.rows[0].recipient).toBe('GABC')
})
})
describe('multiple rows with aliases', () => {
it('correctly parses multiple rows using aliases', () => {
const csv = [
'to,total_amount,start_date,end_date',
'GABC,1000,1000,2000',
'GXYZ,2000,1500,3000',
'GDEF,1500,2000,4000',
].join('\n')
const result = parseCsvBatch(csv)
expect(result.errors).toHaveLength(0)
expect(result.rows).toHaveLength(3)
expect(result.rows[0].recipient).toBe('GABC')
expect(result.rows[1].recipient).toBe('GXYZ')
expect(result.rows[2].recipient).toBe('GDEF')
expect(result.rows[1].amount).toBe('2000')
})
})
})