-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathcheck-for-updates.test.ts
More file actions
112 lines (94 loc) · 3.66 KB
/
check-for-updates.test.ts
File metadata and controls
112 lines (94 loc) · 3.66 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
/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'node:assert';
import child_process from 'node:child_process';
import fs from 'node:fs/promises';
import os from 'node:os';
import {afterEach, describe, it} from 'node:test';
import sinon from 'sinon';
import {checkForUpdates} from '../src/utils/check-for-updates.js';
import {VERSION} from '../src/version.js';
describe('checkForUpdates', () => {
afterEach(() => {
sinon.restore();
});
it('notifies if cache exists and version is different', async () => {
sinon.stub(os, 'homedir').returns('/home/user');
sinon.stub(fs, 'readFile').callsFake(async filePath => {
if (filePath.toString().includes('latest.json')) {
return JSON.stringify({
version: '99.9.9',
timestamp: Date.now(),
});
}
throw new Error(`File not found: ${filePath}`);
});
const warnStub = sinon.stub(console, 'warn');
const spawnStub = sinon.stub(child_process, 'spawn');
await checkForUpdates('Run `npm update` to update.');
assert.ok(
warnStub.calledWith(
sinon.match('Update available: ' + VERSION + ' -> 99.9.9'),
),
);
assert.ok(spawnStub.notCalled);
});
it('does not spawn fetch process if cache is fresh', async () => {
sinon.stub(os, 'homedir').returns('/home/user');
sinon.stub(fs, 'readFile').callsFake(async filePath => {
if (filePath.toString().includes('latest.json')) {
return JSON.stringify({
version: VERSION,
timestamp: Date.now(),
});
}
throw new Error(`File not found: ${filePath}`);
});
const spawnStub = sinon.stub(child_process, 'spawn');
await checkForUpdates('Run `npm update` to update.');
assert.ok(spawnStub.notCalled);
});
it('spawns detached process if cache is stale', async () => {
sinon.stub(os, 'homedir').returns('/home/user');
sinon.stub(fs, 'readFile').callsFake(async filePath => {
if (filePath.toString().includes('latest.json')) {
return JSON.stringify({
version: VERSION,
timestamp: Date.now() - 25 * 60 * 60 * 1000, // 25 hours ago
});
}
throw new Error(`File not found: ${filePath}`);
});
const unrefSpy = sinon.spy();
const spawnStub = sinon.stub(child_process, 'spawn').returns({
unref: unrefSpy,
} as unknown as child_process.ChildProcess);
await checkForUpdates('Run `npm update` to update.');
assert.ok(spawnStub.calledOnce);
assert.strictEqual(spawnStub.firstCall.args[0], process.execPath);
assert.ok(spawnStub.firstCall.args[1][0]?.includes('check-latest-version.js'));
assert.ok(spawnStub.firstCall.args[1][1]?.includes('latest.json'));
assert.strictEqual(spawnStub.firstCall.args[2]?.detached, true);
assert.ok(unrefSpy.calledOnce);
});
it('spawns detached process if cache is missing', async () => {
sinon.stub(os, 'homedir').returns('/home/user');
sinon.stub(fs, 'readFile').callsFake(async filePath => {
throw new Error(`File not found: ${filePath}`);
});
const unrefSpy = sinon.spy();
const spawnStub = sinon.stub(child_process, 'spawn').returns({
unref: unrefSpy,
} as unknown as child_process.ChildProcess);
await checkForUpdates('Run `npm update` to update.');
assert.ok(spawnStub.calledOnce);
assert.strictEqual(spawnStub.firstCall.args[0], process.execPath);
assert.ok(spawnStub.firstCall.args[1][0]?.includes('check-latest-version.js'));
assert.ok(spawnStub.firstCall.args[1][1]?.includes('latest.json'));
assert.strictEqual(spawnStub.firstCall.args[2]?.detached, true);
assert.ok(unrefSpy.calledOnce);
});
});