-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Expand file tree
/
Copy pathvela-wallet.test.ts
More file actions
145 lines (130 loc) · 4.53 KB
/
Copy pathvela-wallet.test.ts
File metadata and controls
145 lines (130 loc) · 4.53 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
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 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;
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;
delete process.env.OPEN_DESIGN_AMR_PROFILE;
rmSync(testHome, { recursive: true, force: true });
});
describe('createVelaWalletSnapshotReader', () => {
it('honors its TTL and lets an explicit refresh bypass a fresh cache entry', async () => {
let nowMs = Date.parse('2026-07-31T02:00:00.000Z');
let balanceUsd = '20.00';
const fetchMock = vi.fn(async () =>
new Response(JSON.stringify({ balanceUsd }), {
status: 200,
headers: { 'content-type': 'application/json' },
}),
);
const reader = createVelaWalletSnapshotReader({
fetch: fetchMock as typeof fetch,
now: () => new Date(nowMs),
ttlMs: 100,
});
const initial = await reader.read();
balanceUsd = '30.00';
nowMs += 99;
const cached = await reader.read();
nowMs += 2;
const expired = await reader.read();
balanceUsd = '40.00';
const forced = await reader.read({ refresh: true });
expect(initial).toMatchObject({ balanceUsd: '20.00', source: 'vela_api' });
expect(cached).toMatchObject({ balanceUsd: '20.00', source: 'daemon_cache' });
expect(expired).toMatchObject({ balanceUsd: '30.00', source: 'vela_api' });
expect(forced).toMatchObject({ balanceUsd: '40.00', source: 'vela_api' });
expect(fetchMock).toHaveBeenCalledTimes(3);
});
it.each([
{ label: 'missing', body: {} },
{ label: 'numeric', body: { balanceUsd: 20 } },
{ label: 'negative', body: { balanceUsd: '-1.00' } },
{ label: 'NaN', body: { balanceUsd: 'NaN' } },
{ label: 'infinite', body: { balanceUsd: 'Infinity' } },
])('rejects a $label balance without overwriting the last valid snapshot', async ({ body }) => {
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,
});
const initial = await reader.read();
responseBody = body;
const rejected = await reader.read({ refresh: true });
const cached = await reader.read();
expect(initial).toMatchObject({ balanceUsd: '20.00', source: 'vela_api' });
expect(rejected).toMatchObject({
balanceUsd: '20.00',
source: 'daemon_cache',
stale: true,
error: { code: 'upstream' },
});
expect(cached).toMatchObject({
balanceUsd: '20.00',
source: 'daemon_cache',
stale: false,
});
expect(cached.error).toBeUndefined();
});
it('keeps the last valid snapshot when the upstream returns malformed JSON', async () => {
let malformed = false;
const fetchMock = vi.fn(async () =>
new Response(malformed ? '{"balanceUsd":' : JSON.stringify({ balanceUsd: '20.00' }), {
status: 200,
headers: { 'content-type': 'application/json' },
}),
);
const reader = createVelaWalletSnapshotReader({
fetch: fetchMock as typeof fetch,
ttlMs: 60_000,
});
await reader.read();
malformed = true;
const degraded = await reader.read({ refresh: true });
expect(degraded).toMatchObject({
balanceUsd: '20.00',
source: 'daemon_cache',
stale: true,
error: { code: 'network' },
});
});
});