-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathaccount.test.ts
More file actions
93 lines (79 loc) · 2.72 KB
/
Copy pathaccount.test.ts
File metadata and controls
93 lines (79 loc) · 2.72 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
import { AssetType } from '@polymarket/bindings/clob';
import { describe, expect, it } from 'vitest';
import type { SecureClient } from '../clients';
import { publicClient, safeWalletAddress, walletClient } from '../testing';
import { authenticateWith } from '../viem';
import {
dropNotifications,
fetchBalanceAllowance,
fetchClosedOnlyMode,
fetchNotifications,
listAccountTrades,
listOpenOrders,
} from './account';
describe('Account', () => {
describe('authenticated reads', () => {
it('fetches authenticated account state', async () => {
const secureClient = await publicClient
.beginAuthentication({ wallet: safeWalletAddress })
.then(authenticateWith(walletClient));
const [closedOnly, openOrders, trades, notifications, balanceAllowance] =
await Promise.all([
fetchClosedOnlyMode(secureClient),
listOpenOrders(secureClient).first(),
listAccountTrades(secureClient).first(),
fetchNotifications(secureClient),
fetchBalanceAllowance(secureClient, {
assetType: AssetType.COLLATERAL,
}),
]);
expect(typeof closedOnly).toBe('boolean');
expect(Array.isArray(openOrders.items)).toBe(true);
expect(Array.isArray(trades.items)).toBe(true);
expect(Array.isArray(notifications)).toBe(true);
expect(balanceAllowance).toEqual(
expect.objectContaining({
allowances: expect.any(Object),
balance: expect.any(String),
}),
);
});
});
describe('dropNotifications', () => {
it('marks notifications as read by id', async () => {
const secureClient = await publicClient
.beginAuthentication({ wallet: safeWalletAddress })
.then(authenticateWith(walletClient));
const notifications = await fetchNotifications(secureClient);
if (notifications.length === 0) {
return;
}
const ids = notifications.slice(0, 1).map(({ id }) => `${id}`);
await expect(
dropNotifications(secureClient, {
ids,
}),
).resolves.toBeUndefined();
const remainingNotifications = await waitForNotificationsToClear(
secureClient,
ids,
);
expect(
remainingNotifications.some(({ id }) => ids.includes(`${id}`)),
).toBe(false);
});
});
});
async function waitForNotificationsToClear(
secureClient: SecureClient,
ids: string[],
) {
for (let attempt = 0; attempt < 10; attempt += 1) {
const notifications = await fetchNotifications(secureClient);
if (!notifications.some(({ id }) => ids.includes(`${id}`))) {
return notifications;
}
await new Promise((resolve) => setTimeout(resolve, 250));
}
return fetchNotifications(secureClient);
}