forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecurring.test.ts
More file actions
254 lines (217 loc) · 9.35 KB
/
Copy pathrecurring.test.ts
File metadata and controls
254 lines (217 loc) · 9.35 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
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import {
buildNextRunAt,
createRenewalPreset,
getRecurringRules,
getUpcomingRenewals,
removeRecurringRule,
saveRecurringRule,
type RecurringRule,
} from '@/lib/recurring'
// ─── Helpers ──────────────────────────────────────────────────────────────────
function makeRule(overrides: Partial<RecurringRule> = {}): RecurringRule {
return {
cadence: 'monthly',
nextRunAt: Date.now() + 1000,
lastCreatedAt: Date.now(),
streamId: 'stream-1',
recipient: 'GRECIPIENT',
tokenSymbol: 'USDC',
amount: '1000',
...overrides,
}
}
beforeEach(() => {
window.localStorage.clear()
})
afterEach(() => {
vi.unstubAllGlobals()
vi.restoreAllMocks()
})
// ─── buildNextRunAt ───────────────────────────────────────────────────────────
describe('buildNextRunAt', () => {
it('adds exactly 7 days for weekly cadence', () => {
const start = new Date('2026-01-10T12:00:00Z').getTime()
const next = buildNextRunAt(start, 'weekly')
expect(next - start).toBe(7 * 24 * 60 * 60 * 1000)
})
it('adds a real calendar month for monthly cadence', () => {
const start = new Date('2026-01-15T00:00:00').getTime()
const next = new Date(buildNextRunAt(start, 'monthly'))
expect(next.getMonth()).toBe(1) // February
expect(next.getDate()).toBe(15)
})
it('adds a real calendar quarter (3 months) for quarterly cadence', () => {
const start = new Date('2026-01-15T00:00:00').getTime()
const next = new Date(buildNextRunAt(start, 'quarterly'))
expect(next.getMonth()).toBe(3) // April
expect(next.getDate()).toBe(15)
})
it('does not drift to a fixed 30 days for monthly cadence', () => {
// Jan has 31 days; a fixed +30d would land on Feb 10 instead of Feb 15.
const start = new Date('2026-01-15T00:00:00').getTime()
const next = buildNextRunAt(start, 'monthly')
const thirtyDaysLater = start + 30 * 24 * 60 * 60 * 1000
expect(next).not.toBe(thirtyDaysLater)
})
it('clamps Jan 31 + 1 month to the last day of February (non-leap year)', () => {
const start = new Date('2025-01-31T00:00:00').getTime()
const next = new Date(buildNextRunAt(start, 'monthly'))
expect(next.getMonth()).toBe(1) // February
expect(next.getDate()).toBe(28) // 2025 is not a leap year
})
it('clamps Jan 31 + 1 month to Feb 29 on a leap year', () => {
const start = new Date('2024-01-31T00:00:00').getTime()
const next = new Date(buildNextRunAt(start, 'monthly'))
expect(next.getMonth()).toBe(1) // February
expect(next.getDate()).toBe(29) // 2024 is a leap year
})
it('clamps Nov 30 + 1 quarter to Feb 28/29 when landing in February', () => {
const start = new Date('2025-11-30T00:00:00').getTime()
const next = new Date(buildNextRunAt(start, 'quarterly'))
expect(next.getMonth()).toBe(1) // February
expect(next.getDate()).toBe(28)
})
it('preserves time-of-day across the month rollover', () => {
const start = new Date('2026-03-15T09:30:00').getTime()
const next = new Date(buildNextRunAt(start, 'monthly'))
expect(next.getHours()).toBe(9)
expect(next.getMinutes()).toBe(30)
})
it('rolls monthly across the year boundary', () => {
const start = new Date('2026-12-15T00:00:00').getTime()
const next = new Date(buildNextRunAt(start, 'monthly'))
expect(next.getFullYear()).toBe(2027)
expect(next.getMonth()).toBe(0) // January
expect(next.getDate()).toBe(15)
})
it('rolls quarterly across the year boundary', () => {
const start = new Date('2026-11-15T00:00:00').getTime()
const next = new Date(buildNextRunAt(start, 'quarterly'))
expect(next.getFullYear()).toBe(2027)
expect(next.getMonth()).toBe(1) // February
expect(next.getDate()).toBe(15)
})
it('clamps Aug 31 + 1 quarter to Nov 30', () => {
const start = new Date('2026-08-31T00:00:00').getTime()
const next = new Date(buildNextRunAt(start, 'quarterly'))
expect(next.getMonth()).toBe(10) // November
expect(next.getDate()).toBe(30)
})
})
// ─── saveRecurringRule / getRecurringRules / removeRecurringRule ─────────────
describe('saveRecurringRule / getRecurringRules', () => {
it('persists a rule and returns it from getRecurringRules', () => {
saveRecurringRule(makeRule({ streamId: 'a' }))
const rules = getRecurringRules()
expect(rules).toHaveLength(1)
expect(rules[0].streamId).toBe('a')
})
it('returns an empty array when nothing is stored', () => {
expect(getRecurringRules()).toEqual([])
})
it('replaces an existing rule for the same streamId instead of duplicating', () => {
saveRecurringRule(makeRule({ streamId: 'a', amount: '100' }))
saveRecurringRule(makeRule({ streamId: 'a', amount: '200' }))
const rules = getRecurringRules()
expect(rules).toHaveLength(1)
expect(rules[0].amount).toBe('200')
})
it('caps stored rules at 25 entries', () => {
for (let i = 0; i < 30; i += 1) {
saveRecurringRule(makeRule({ streamId: `stream-${i}` }))
}
expect(getRecurringRules()).toHaveLength(25)
})
it('returns [] when stored JSON is malformed', () => {
window.localStorage.setItem('flowstar:recurring-streams', '{not valid json')
expect(getRecurringRules()).toEqual([])
})
it('returns [] when stored JSON is valid but not an array', () => {
window.localStorage.setItem(
'flowstar:recurring-streams',
JSON.stringify({ streamId: 'not-an-array' }),
)
expect(getRecurringRules()).toEqual([])
})
})
describe('removeRecurringRule', () => {
it('removes only the matching rule', () => {
saveRecurringRule(makeRule({ streamId: 'a' }))
saveRecurringRule(makeRule({ streamId: 'b' }))
removeRecurringRule('a')
const rules = getRecurringRules()
expect(rules).toHaveLength(1)
expect(rules[0].streamId).toBe('b')
})
})
// ─── getUpcomingRenewals ──────────────────────────────────────────────────────
describe('getUpcomingRenewals', () => {
it('returns [] when no rules are stored', () => {
expect(getUpcomingRenewals()).toEqual([])
})
it('excludes rules whose nextRunAt is in the past', () => {
saveRecurringRule(makeRule({ streamId: 'past', nextRunAt: Date.now() - 1000 }))
saveRecurringRule(makeRule({ streamId: 'future', nextRunAt: Date.now() + 1000 }))
const upcoming = getUpcomingRenewals()
expect(upcoming.map((r) => r.streamId)).toEqual(['future'])
})
it('sorts remaining rules by nextRunAt ascending', () => {
saveRecurringRule(makeRule({ streamId: 'later', nextRunAt: Date.now() + 5000 }))
saveRecurringRule(makeRule({ streamId: 'sooner', nextRunAt: Date.now() + 1000 }))
const upcoming = getUpcomingRenewals()
expect(upcoming.map((r) => r.streamId)).toEqual(['sooner', 'later'])
})
it('excludes a rule scheduled exactly at now (strict > comparison)', () => {
const now = 1_700_000_000_000
vi.spyOn(Date, 'now').mockReturnValue(now)
saveRecurringRule(makeRule({ streamId: 'due-now', nextRunAt: now }))
saveRecurringRule(makeRule({ streamId: 'future', nextRunAt: now + 1 }))
expect(getUpcomingRenewals().map((r) => r.streamId)).toEqual(['future'])
})
})
// ─── Server-side safety (window undefined) ────────────────────────────────────
describe('server-side safety (no window)', () => {
it('getRecurringRules returns [] when window is undefined', () => {
vi.stubGlobal('window', undefined)
expect(getRecurringRules()).toEqual([])
})
it('saveRecurringRule is a no-op when window is undefined', () => {
vi.stubGlobal('window', undefined)
expect(() => saveRecurringRule(makeRule({ streamId: 'ssr' }))).not.toThrow()
})
it('removeRecurringRule is a no-op when window is undefined', () => {
vi.stubGlobal('window', undefined)
expect(() => removeRecurringRule('ssr')).not.toThrow()
})
})
// ─── createRenewalPreset ──────────────────────────────────────────────────────
describe('createRenewalPreset', () => {
it('builds and persists a preset from a stream', () => {
const stream = {
id: 'stream-9',
recipient: 'GRECIPIENT9',
token: { symbol: 'XLM' },
depositedAmount: 500n,
}
const preset = createRenewalPreset(stream, 'weekly')
expect(preset.streamId).toBe('stream-9')
expect(preset.recipient).toBe('GRECIPIENT9')
expect(preset.tokenSymbol).toBe('XLM')
expect(preset.amount).toBe('500')
expect(preset.cadence).toBe('weekly')
const stored = getRecurringRules()
expect(stored).toHaveLength(1)
expect(stored[0].streamId).toBe('stream-9')
})
it('sets nextRunAt in the future relative to lastCreatedAt', () => {
const stream = {
id: 'stream-10',
recipient: 'GRECIPIENT10',
token: { symbol: 'XLM' },
depositedAmount: 1n,
}
const preset = createRenewalPreset(stream, 'monthly')
expect(preset.nextRunAt).toBeGreaterThan(preset.lastCreatedAt)
})
})