forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceipt-utils.test.ts
More file actions
315 lines (238 loc) · 8.36 KB
/
Copy pathreceipt-utils.test.ts
File metadata and controls
315 lines (238 loc) · 8.36 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
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { buildReceiptData } from '@/lib/receipt-utils'
import type { StreamData } from '@/types/stream'
const TOKEN = { address: 'TADDR', symbol: 'USDC', decimals: 7 }
function makeStream(overrides?: Partial<StreamData>): StreamData {
return {
id: 'stream-123',
sender: 'GSENDER123',
recipient: 'GRECIPIENT456',
token: TOKEN,
depositedAmount: 1_000_000_000n, // 100 USDC at 7 decimals
withdrawnAmount: 0n,
startTime: 1000n,
endTime: 2000n,
cliffTime: 1000n,
cliffAmount: 0n,
amountPerSecond: 1_000_000n, // 0.1 USDC/s
linearAmount: 1_000_000_000n,
duration: 1000n,
cancelled: false,
...overrides,
}
}
describe('buildReceiptData', () => {
beforeEach(() => {
// Mock Date.now() to return a fixed timestamp for consistent testing
vi.useFakeTimers()
vi.setSystemTime(new Date(1500 * 1000)) // Set to midway (1500s)
})
afterEach(() => {
vi.useRealTimers()
})
describe('completed stream', () => {
it('derives correct status for completed stream', () => {
vi.setSystemTime(new Date(2500 * 1000)) // Set to after end time
const stream = makeStream({
startTime: 1000n,
endTime: 2000n,
cancelled: false,
})
const receipt = buildReceiptData(stream)
expect(receipt.status).toBe('Completed')
})
it('calculates correct duration for completed stream', () => {
const stream = makeStream({
startTime: 1000n,
endTime: 2000n, // 1000 seconds duration
})
const receipt = buildReceiptData(stream)
// 1000 seconds = 16 minutes and 40 seconds
expect(receipt.duration).toBe('16m')
})
it('includes all transaction hashes', () => {
const stream = makeStream()
const receipt = buildReceiptData(
stream,
'creation-tx-hash',
['withdraw-tx-1', 'withdraw-tx-2'],
undefined,
)
expect(receipt.creationTx).toBe('creation-tx-hash')
expect(receipt.withdrawalTxs).toEqual(['withdraw-tx-1', 'withdraw-tx-2'])
expect(receipt.cancellationTx).toBeUndefined()
})
})
describe('in-progress stream', () => {
it('derives correct status for active stream', () => {
vi.setSystemTime(new Date(1500 * 1000)) // Midway between start and end
const stream = makeStream({
startTime: 1000n,
endTime: 2000n,
cancelled: false,
})
const receipt = buildReceiptData(stream)
expect(receipt.status).toBe('Active')
})
it('calculates remaining amount correctly', () => {
const stream = makeStream({
depositedAmount: 1_000_000_000n, // 100 USDC
withdrawnAmount: 300_000_000n, // 30 USDC withdrawn
})
const receipt = buildReceiptData(stream)
expect(receipt.remainingAmountRaw).toBe(700_000_000n)
expect(receipt.remainingAmount).toBe('70') // 70 USDC
})
it('includes withdrawn amount', () => {
const stream = makeStream({
depositedAmount: 1_000_000_000n,
withdrawnAmount: 500_000_000n,
})
const receipt = buildReceiptData(stream)
expect(receipt.withdrawnAmountRaw).toBe(500_000_000n)
expect(receipt.withdrawnAmount).toBe('50')
})
})
describe('cancelled stream', () => {
it('derives correct status for cancelled stream', () => {
vi.setSystemTime(new Date(1200 * 1000))
const stream = makeStream({
cancelled: true,
})
const receipt = buildReceiptData(stream)
expect(receipt.status).toBe('Cancelled')
})
it('includes cancellation transaction hash', () => {
const stream = makeStream({ cancelled: true })
const receipt = buildReceiptData(stream, 'creation-tx', [], 'cancellation-tx-hash')
expect(receipt.cancellationTx).toBe('cancellation-tx-hash')
})
it('status is Cancelled even if current time is past end time', () => {
vi.setSystemTime(new Date(3000 * 1000)) // Past end time
const stream = makeStream({
startTime: 1000n,
endTime: 2000n,
cancelled: true,
})
const receipt = buildReceiptData(stream)
expect(receipt.status).toBe('Cancelled')
})
})
describe('duration formatting', () => {
it('formats duration with days, hours, and minutes', () => {
const stream = makeStream({
startTime: 0n,
endTime: 90061n, // 1 day, 1 hour, 1 minute, 1 second
})
const receipt = buildReceiptData(stream)
expect(receipt.duration).toBe('1d 1h 1m')
})
it('formats duration with only hours and minutes', () => {
const stream = makeStream({
startTime: 0n,
endTime: 3661n, // 1 hour, 1 minute, 1 second
})
const receipt = buildReceiptData(stream)
expect(receipt.duration).toBe('1h 1m')
})
it('formats duration with only minutes', () => {
const stream = makeStream({
startTime: 0n,
endTime: 300n, // 5 minutes
})
const receipt = buildReceiptData(stream)
expect(receipt.duration).toBe('5m')
})
it('formats zero duration', () => {
const stream = makeStream({
startTime: 1000n,
endTime: 1000n, // Same start and end
})
const receipt = buildReceiptData(stream)
expect(receipt.duration).toBe('0m')
})
})
describe('basic fields mapping', () => {
it('includes all basic stream information', () => {
const stream = makeStream()
const receipt = buildReceiptData(stream)
expect(receipt.streamId).toBe('stream-123')
expect(receipt.sender).toBe('GSENDER123')
expect(receipt.recipient).toBe('GRECIPIENT456')
expect(receipt.tokenSymbol).toBe('USDC')
expect(receipt.tokenAddress).toBe('TADDR')
})
it('includes total amount formatted correctly', () => {
const stream = makeStream({
depositedAmount: 2_500_000_000n, // 250 USDC
})
const receipt = buildReceiptData(stream)
expect(receipt.totalAmountRaw).toBe(2_500_000_000n)
expect(receipt.totalAmount).toBe('250')
})
it('includes cliff amount when present', () => {
const stream = makeStream({
cliffAmount: 100_000_000n, // 10 USDC
})
const receipt = buildReceiptData(stream)
expect(receipt.cliffAmount).toBe('10')
})
it('includes amount per second when present', () => {
const stream = makeStream({
amountPerSecond: 1_000_000n, // 0.1 USDC/s
})
const receipt = buildReceiptData(stream)
expect(receipt.amountPerSecond).toBe('0.1')
})
it('excludes cliff amount when zero', () => {
const stream = makeStream({
cliffAmount: 0n,
})
const receipt = buildReceiptData(stream)
expect(receipt.cliffAmount).toBeUndefined()
})
it('excludes amount per second when zero', () => {
const stream = makeStream({
amountPerSecond: 0n,
})
const receipt = buildReceiptData(stream)
expect(receipt.amountPerSecond).toBeUndefined()
})
})
describe('date formatting', () => {
it('formats dates for all time fields', () => {
const stream = makeStream({
startTime: 1000n,
endTime: 2000n,
cliffTime: 1500n,
})
const receipt = buildReceiptData(stream)
expect(receipt.startDate).toBeTruthy()
expect(receipt.endDate).toBeTruthy()
expect(receipt.cliffDate).toBeTruthy()
expect(typeof receipt.startDate).toBe('string')
expect(typeof receipt.endDate).toBe('string')
expect(typeof receipt.cliffDate).toBe('string')
})
it('includes generation timestamp', () => {
const stream = makeStream()
const receipt = buildReceiptData(stream)
expect(receipt.generatedDate).toBeTruthy()
expect(typeof receipt.generatedDate).toBe('string')
})
})
describe('withdrawal transaction handling', () => {
it('includes empty withdrawal list when none provided', () => {
const stream = makeStream()
const receipt = buildReceiptData(stream)
expect(receipt.withdrawalTxs).toEqual([])
})
it('includes multiple withdrawal transactions', () => {
const stream = makeStream()
const withdrawals = ['tx1', 'tx2', 'tx3']
const receipt = buildReceiptData(stream, undefined, withdrawals)
expect(receipt.withdrawalTxs).toEqual(withdrawals)
expect(receipt.withdrawalTxs).toHaveLength(3)
})
})
})