-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Expand file tree
/
Copy pathvela-wallet.test.ts
More file actions
108 lines (97 loc) · 3.38 KB
/
Copy pathvela-wallet.test.ts
File metadata and controls
108 lines (97 loc) · 3.38 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
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import path from 'node:path';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { createVelaWalletSnapshotReader } from '../../src/integrations/vela-wallet.js';
let originalHome: string | undefined;
let originalProfile: string | undefined;
let testHome: string;
function seedWalletLogin(): void {
const configFile = path.join(testHome, '.amr', 'config.json');
mkdirSync(path.dirname(configFile), { recursive: true });
writeFileSync(
configFile,
JSON.stringify({
profiles: {
local: {
apiUrl: 'https://wallet.example.test',
controlKey: 'ck-wallet-unit',
runtimeKey: 'rt-wallet-unit',
user: {
id: 'wallet-unit-user',
email: 'wallet-unit@example.com',
plan: 'plus',
},
},
},
}),
'utf8',
);
}
beforeEach(() => {
originalHome = process.env.HOME;
originalProfile = process.env.OPEN_DESIGN_AMR_PROFILE;
testHome = mkdtempSync(path.join(tmpdir(), 'od-vela-wallet-'));
process.env.HOME = testHome;
process.env.OPEN_DESIGN_AMR_PROFILE = 'local';
seedWalletLogin();
});
afterEach(() => {
if (originalHome === undefined) delete process.env.HOME;
else process.env.HOME = originalHome;
if (originalProfile === undefined) delete process.env.OPEN_DESIGN_AMR_PROFILE;
else process.env.OPEN_DESIGN_AMR_PROFILE = originalProfile;
rmSync(testHome, { recursive: true, force: true });
});
describe('createVelaWalletSnapshotReader balance validation', () => {
it.each([
{ label: 'missing', balanceUsd: undefined },
{ label: 'numeric', balanceUsd: 20 },
{ label: 'negative', balanceUsd: '-1.00' },
{ label: 'NaN', balanceUsd: 'NaN' },
{ label: 'infinite', balanceUsd: 'Infinity' },
{ label: 'exponent', balanceUsd: '1e2' },
])('rejects a $label balance when there is no valid cached snapshot', async ({ balanceUsd }) => {
const fetchMock = vi.fn(async () =>
new Response(JSON.stringify(balanceUsd === undefined ? {} : { balanceUsd }), {
status: 200,
headers: { 'content-type': 'application/json' },
}),
);
const reader = createVelaWalletSnapshotReader({ fetch: fetchMock as typeof fetch });
await expect(reader.read()).resolves.toMatchObject({
status: 'unavailable',
balanceUsd: null,
source: 'unavailable',
stale: false,
error: { code: 'upstream' },
});
});
it('serves the last valid snapshot as stale when a refresh returns an invalid balance', async () => {
let responseBody: unknown = { balanceUsd: '20.00' };
const fetchMock = vi.fn(async () =>
new Response(JSON.stringify(responseBody), {
status: 200,
headers: { 'content-type': 'application/json' },
}),
);
const reader = createVelaWalletSnapshotReader({
fetch: fetchMock as typeof fetch,
ttlMs: 60_000,
});
await expect(reader.read()).resolves.toMatchObject({
status: 'available',
balanceUsd: '20.00',
source: 'vela_api',
stale: false,
});
responseBody = { balanceUsd: '-1.00' };
await expect(reader.read({ refresh: true })).resolves.toMatchObject({
status: 'available',
balanceUsd: '20.00',
source: 'daemon_cache',
stale: true,
error: { code: 'upstream' },
});
});
});