forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate-notifier.test.ts
More file actions
166 lines (128 loc) · 5.29 KB
/
Copy pathupdate-notifier.test.ts
File metadata and controls
166 lines (128 loc) · 5.29 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
import { type UpdateCheckResult, checkForUpdate, printUpdateNotification } from '../update-notifier.js';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
const { mockReadFile, mockWriteFile, mockMkdir } = vi.hoisted(() => ({
mockReadFile: vi.fn(),
mockWriteFile: vi.fn(),
mockMkdir: vi.fn(),
}));
vi.mock('fs/promises', () => ({
readFile: mockReadFile,
writeFile: mockWriteFile,
mkdir: mockMkdir,
}));
vi.mock('../constants.js', () => ({
PACKAGE_VERSION: '1.0.0',
}));
const { mockFetchLatestVersion, mockCompareVersions } = vi.hoisted(() => ({
mockFetchLatestVersion: vi.fn(),
mockCompareVersions: vi.fn(),
}));
vi.mock('../commands/update/action.js', () => ({
fetchLatestVersion: mockFetchLatestVersion,
compareVersions: mockCompareVersions,
}));
describe('checkForUpdate', () => {
beforeEach(() => {
vi.spyOn(Date, 'now').mockReturnValue(1708646400000);
mockWriteFile.mockResolvedValue(undefined);
mockMkdir.mockResolvedValue(undefined);
});
afterEach(() => {
vi.restoreAllMocks();
mockReadFile.mockReset();
mockWriteFile.mockReset();
mockMkdir.mockReset();
mockFetchLatestVersion.mockReset();
mockCompareVersions.mockReset();
});
it('fetches from registry when no cache exists', async () => {
mockReadFile.mockRejectedValue(new Error('ENOENT'));
mockFetchLatestVersion.mockResolvedValue('2.0.0');
mockCompareVersions.mockReturnValue(1);
const result = await checkForUpdate();
expect(result).toEqual({ updateAvailable: true, latestVersion: '2.0.0' });
expect(mockFetchLatestVersion).toHaveBeenCalled();
});
it('uses cache when last check was less than 24 hours ago', async () => {
const cache = JSON.stringify({
lastCheck: 1708646400000 - 1000, // 1 second ago
latestVersion: '2.0.0',
});
mockReadFile.mockResolvedValue(cache);
mockCompareVersions.mockReturnValue(1);
const result = await checkForUpdate();
expect(result).toEqual({ updateAvailable: true, latestVersion: '2.0.0' });
expect(mockFetchLatestVersion).not.toHaveBeenCalled();
});
it('fetches from registry when cache is expired', async () => {
const cache = JSON.stringify({
lastCheck: 1708646400000 - 25 * 60 * 60 * 1000, // 25 hours ago
latestVersion: '1.5.0',
});
mockReadFile.mockResolvedValue(cache);
mockFetchLatestVersion.mockResolvedValue('2.0.0');
mockCompareVersions.mockReturnValue(1);
const result = await checkForUpdate();
expect(result).toEqual({ updateAvailable: true, latestVersion: '2.0.0' });
expect(mockFetchLatestVersion).toHaveBeenCalled();
});
it('writes cache after fetching', async () => {
mockReadFile.mockRejectedValue(new Error('ENOENT'));
mockFetchLatestVersion.mockResolvedValue('2.0.0');
mockCompareVersions.mockReturnValue(1);
await checkForUpdate();
expect(mockMkdir).toHaveBeenCalled();
expect(mockWriteFile).toHaveBeenCalledWith(
expect.stringContaining('update-check.json'),
JSON.stringify({ lastCheck: 1708646400000, latestVersion: '2.0.0' }),
'utf-8'
);
});
it('returns updateAvailable: false when versions match', async () => {
mockReadFile.mockRejectedValue(new Error('ENOENT'));
mockFetchLatestVersion.mockResolvedValue('1.0.0');
mockCompareVersions.mockReturnValue(0);
const result = await checkForUpdate();
expect(result).toEqual({ updateAvailable: false, latestVersion: '1.0.0' });
});
it('returns updateAvailable: false when current is newer', async () => {
mockReadFile.mockRejectedValue(new Error('ENOENT'));
mockFetchLatestVersion.mockResolvedValue('0.9.0');
mockCompareVersions.mockReturnValue(-1);
const result = await checkForUpdate();
expect(result).toEqual({ updateAvailable: false, latestVersion: '0.9.0' });
});
it('returns null on fetch error', async () => {
mockReadFile.mockRejectedValue(new Error('ENOENT'));
mockFetchLatestVersion.mockRejectedValue(new Error('network error'));
const result = await checkForUpdate();
expect(result).toBeNull();
});
it('returns null on cache parse error and fetch error', async () => {
mockReadFile.mockResolvedValue('invalid json');
mockFetchLatestVersion.mockRejectedValue(new Error('network error'));
const result = await checkForUpdate();
expect(result).toBeNull();
});
it('succeeds even when cache write fails', async () => {
mockReadFile.mockRejectedValue(new Error('ENOENT'));
mockFetchLatestVersion.mockResolvedValue('2.0.0');
mockCompareVersions.mockReturnValue(1);
mockWriteFile.mockRejectedValue(new Error('EACCES'));
const result = await checkForUpdate();
expect(result).toEqual({ updateAvailable: true, latestVersion: '2.0.0' });
});
});
describe('printUpdateNotification', () => {
it('writes notification to stderr', () => {
const stderrSpy = vi.spyOn(process.stderr, 'write').mockReturnValue(true);
const result: UpdateCheckResult = { updateAvailable: true, latestVersion: '2.0.0' };
printUpdateNotification(result);
const output = stderrSpy.mock.calls.map(c => c[0]).join('');
expect(output).toContain('Update available:');
expect(output).toContain('1.0.0');
expect(output).toContain('2.0.0');
expect(output).toContain('npm install -g @aws/agentcore@latest');
stderrSpy.mockRestore();
});
});