forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-notifications.test.ts
More file actions
296 lines (248 loc) · 10.1 KB
/
Copy pathuse-notifications.test.ts
File metadata and controls
296 lines (248 loc) · 10.1 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
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { renderHook, waitFor, act } from '@testing-library/react'
import type { StreamData } from '@/types/stream'
// ── Mocks ──────────────────────────────────────────────────────────────────
vi.mock('@/lib/contract', () => ({
fetchStreamsForAddress: vi.fn(),
}))
vi.mock('@/components/providers/network-provider', () => ({
useNetwork: vi.fn(() => ({
network: 'testnet',
config: { rpcUrl: 'https://rpc.test', streamContractId: 'CONTRACT123' },
})),
}))
// The real SDK parses genuine XDR; tests only need to prove that
// `decodeEventStreamId` -> `scValToNative(xdr.ScVal.fromXDR(...))` is fed
// into the wallet-scoping filter, so the mock treats `value.xdr` as a
// pre-serialized JSON payload instead of real base64 XDR.
vi.mock('@stellar/stellar-sdk', () => ({
xdr: { ScVal: { fromXDR: (raw: string) => raw } },
scValToNative: (raw: string) => JSON.parse(raw),
}))
import { fetchStreamsForAddress } from '@/lib/contract'
import { useNotifications } from '@/hooks/use-notifications'
const WALLET = 'GWALLET_SELF'
const OTHER = 'GWALLET_OTHER'
const myStreams: StreamData[] = [
{
id: '1', // sent by the connected wallet
sender: WALLET,
recipient: OTHER,
token: { address: 'TOKEN', symbol: 'USDC', decimals: 7 },
depositedAmount: 1000n,
withdrawnAmount: 0n,
startTime: 0n,
endTime: 9999999999n,
cliffTime: 0n,
cliffAmount: 0n,
amountPerSecond: 1n,
linearAmount: 1000n,
duration: 9999999999n,
cancelled: false,
},
{
id: '2', // received by the connected wallet
sender: OTHER,
recipient: WALLET,
token: { address: 'TOKEN', symbol: 'USDC', decimals: 7 },
depositedAmount: 1000n,
withdrawnAmount: 0n,
startTime: 0n,
endTime: 9999999999n,
cliffTime: 0n,
cliffAmount: 0n,
amountPerSecond: 1n,
linearAmount: 1000n,
duration: 9999999999n,
cancelled: false,
},
]
function makeEvent(topic: string, streamId: string) {
return {
type: 'contract',
ledger: 501,
topic: [topic],
value: { xdr: JSON.stringify({ stream_id: streamId }) },
}
}
const NOTIF_STORAGE_KEY = 'flowstar:notifications'
function seedNotifications() {
const now = Date.now()
return [
{
id: 'n1',
type: 'stream_created' as const,
title: 'New stream received',
body: 'A new payment stream has been created for you.',
timestamp: now,
read: false,
},
{
id: 'n2',
type: 'withdrawal' as const,
title: 'Withdrawal from your stream',
body: 'A withdrawal has been made from a stream you sent.',
timestamp: now - 1000,
read: false,
},
{
id: 'n3',
type: 'stream_cancelled' as const,
title: 'Stream cancelled',
body: 'A stream you are receiving has been cancelled.',
timestamp: now - 2000,
read: true,
},
]
}
describe('useNotifications', () => {
beforeEach(() => {
vi.clearAllMocks()
localStorage.clear()
// Seed a non-zero last-seen ledger so the first poll fetches events
// immediately instead of only bootstrapping the ledger cursor.
localStorage.setItem('flowstar:last-seen-ledger', '500')
vi.mocked(fetchStreamsForAddress).mockResolvedValue(myStreams)
})
afterEach(() => {
vi.useRealTimers()
})
it('only surfaces notifications for streams the wallet actually sent or received', async () => {
const events = [
makeEvent('StreamCreatedEvent', '2'), // received by wallet -> notify
makeEvent('StreamCreatedEvent', '99'), // unrelated stream -> no notify
makeEvent('WithdrawEvent', '1'), // wallet is sender -> notify
makeEvent('WithdrawEvent', '2'), // wallet is recipient, not sender -> no notify
makeEvent('CancelEvent', '2'), // received by wallet -> notify
makeEvent('CancelEvent', '99'), // unrelated stream -> no notify
]
global.fetch = vi.fn(async (_url, init) => {
const body = JSON.parse((init as RequestInit).body as string)
if (body.method === 'getEvents') {
return {
json: async () => ({ result: { events, latestLedger: 505 } }),
} as Response
}
return { json: async () => ({ result: {} }) } as Response
})
const { result } = renderHook(() => useNotifications(WALLET))
await waitFor(() => expect(result.current.notifications).toHaveLength(3))
expect(fetchStreamsForAddress).toHaveBeenCalledWith('testnet', WALLET)
const bodies = result.current.notifications.map((n) => n.body)
expect(bodies).toContain('A new payment stream has been created for you.')
expect(bodies).toContain('A withdrawal has been made from a stream you sent.')
expect(bodies).toContain('A stream you are receiving has been cancelled.')
})
it('does not notify at all when none of the events belong to the wallet', async () => {
const events = [
makeEvent('StreamCreatedEvent', '99'),
makeEvent('WithdrawEvent', '99'),
makeEvent('CancelEvent', '99'),
]
global.fetch = vi.fn(async (_url, init) => {
const body = JSON.parse((init as RequestInit).body as string)
if (body.method === 'getEvents') {
return {
json: async () => ({ result: { events, latestLedger: 505 } }),
} as Response
}
return { json: async () => ({ result: {} }) } as Response
})
const { result } = renderHook(() => useNotifications(WALLET))
await waitFor(() => expect(fetchStreamsForAddress).toHaveBeenCalled())
// Give the poll a tick to process events before asserting nothing landed.
await new Promise((r) => setTimeout(r, 0))
expect(result.current.notifications).toHaveLength(0)
})
it('accumulates notifications from a single poll and persists them', async () => {
const events = [
makeEvent('StreamCreatedEvent', '2'), // received by wallet -> notify
makeEvent('WithdrawEvent', '1'), // wallet is sender -> notify
makeEvent('CancelEvent', '2'), // received by wallet -> notify
]
global.fetch = vi.fn(async (_url, init) => {
const body = JSON.parse((init as RequestInit).body as string)
if (body.method === 'getEvents') {
return {
json: async () => ({ result: { events, latestLedger: 505 } }),
} as Response
}
return { json: async () => ({ result: {} }) } as Response
})
const { result } = renderHook(() => useNotifications(WALLET))
await waitFor(() => expect(result.current.notifications).toHaveLength(3))
expect(result.current.unreadCount).toBe(3)
// New notifications are prepended, newest first.
const bodies = result.current.notifications.map((n) => n.body)
expect(bodies[0]).toBe('A stream you are receiving has been cancelled.')
expect(bodies[1]).toBe('A withdrawal has been made from a stream you sent.')
expect(bodies[2]).toBe('A new payment stream has been created for you.')
const persisted = JSON.parse(localStorage.getItem(NOTIF_STORAGE_KEY) ?? '[]')
expect(persisted).toHaveLength(3)
})
it('accumulates notifications across polls without duplicating', async () => {
vi.useFakeTimers()
const batches = [[makeEvent('StreamCreatedEvent', '2')], [makeEvent('WithdrawEvent', '1')]]
let getEventsCalls = 0
global.fetch = vi.fn(async (_url, init) => {
const body = JSON.parse((init as RequestInit).body as string)
if (body.method === 'getEvents') {
const events = batches[getEventsCalls] ?? []
getEventsCalls += 1
return {
json: async () => ({
result: { events, latestLedger: 505 + getEventsCalls },
}),
} as Response
}
return { json: async () => ({ result: {} }) } as Response
})
const { result } = renderHook(() => useNotifications(WALLET))
// Flush the initial poll (it runs on mount, not on a timer), which is
// purely microtask-driven once the fetch mock resolves immediately.
// waitFor can't be used here: it hangs under vitest fake timers.
await act(async () => {
for (let i = 0; i < 10; i += 1) await Promise.resolve()
})
expect(result.current.notifications).toHaveLength(1)
// Advance past POLL_INTERVAL (30s) so the second poll runs.
await act(async () => {
await vi.advanceTimersByTimeAsync(30_000)
})
expect(result.current.notifications).toHaveLength(2)
expect(getEventsCalls).toBe(2)
const persisted = JSON.parse(localStorage.getItem(NOTIF_STORAGE_KEY) ?? '[]')
expect(persisted).toHaveLength(2)
})
it('restores persisted notifications and computes unreadCount on mount', () => {
localStorage.setItem(NOTIF_STORAGE_KEY, JSON.stringify(seedNotifications()))
const { result } = renderHook(() => useNotifications(null))
expect(result.current.notifications).toHaveLength(3)
expect(result.current.unreadCount).toBe(2)
expect(result.current.notifications[0].id).toBe('n1')
})
it('markAllRead marks every notification as read and updates unreadCount', () => {
localStorage.setItem(NOTIF_STORAGE_KEY, JSON.stringify(seedNotifications()))
const { result } = renderHook(() => useNotifications(null))
expect(result.current.unreadCount).toBe(2)
act(() => {
result.current.markAllRead()
})
expect(result.current.unreadCount).toBe(0)
expect(result.current.notifications.every((n) => n.read)).toBe(true)
const persisted = JSON.parse(localStorage.getItem(NOTIF_STORAGE_KEY) ?? '[]')
expect(persisted).toHaveLength(3)
expect(persisted.every((n: { read: boolean }) => n.read)).toBe(true)
})
it('clearAll dismisses all notifications and clears persisted state', () => {
localStorage.setItem(NOTIF_STORAGE_KEY, JSON.stringify(seedNotifications()))
const { result } = renderHook(() => useNotifications(null))
expect(result.current.notifications).toHaveLength(3)
act(() => {
result.current.clearAll()
})
expect(result.current.notifications).toHaveLength(0)
expect(result.current.unreadCount).toBe(0)
expect(localStorage.getItem(NOTIF_STORAGE_KEY)).toBe('[]')
})
})