-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
77 lines (62 loc) · 2.21 KB
/
test.js
File metadata and controls
77 lines (62 loc) · 2.21 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
const fs = require('fs');
const { expect } = require('chai');
const sinon = require('sinon');
const { fetchLatestVersion } = require('./index.js');
describe('Tests:', () => {
let readFileStub;
before(() => {
// Stub fs.readFile used by readSubscribersFromFile
readFileStub = sinon
.stub(fs, 'readFile')
.resolves('subscriber1@example.com\nsubscriber2@example.com\n');
});
after(() => {
readFileStub.restore();
});
describe('fetchLatestVersion', () => {
it('should fetch and parse the latest Node.js version with date', async () => {
const fetchStub = sinon.stub(global, 'fetch').resolves({
ok: true,
text: () =>
Promise.resolve(`
<a href="node-v22.4.1.pkg">node-v22.4.1.pkg</a> 08-Jul-2024 14:39 82473530
`),
});
const [latestVersion, releaseDate] = await fetchLatestVersion();
expect(latestVersion).to.equal('22.4.1');
expect(releaseDate).to.equal('08-Jul-2024 14:39');
fetchStub.restore();
});
it('should fallback if release date is missing but version exists', async () => {
const fetchStub = sinon.stub(global, 'fetch').resolves({
ok: true,
text: () =>
Promise.resolve(`
<a href="node-v22.5.0.pkg">node-v22.5.0.pkg</a>
`),
});
const [latestVersion, releaseDate] = await fetchLatestVersion();
expect(latestVersion).to.equal('22.5.0');
expect(new Date(releaseDate).toString()).to.not.equal('Invalid Date'); // should be ISO fallback
fetchStub.restore();
});
it('should return null if fetch fails', async () => {
const fetchStub = sinon
.stub(global, 'fetch')
.rejects(new Error('Failed to fetch'));
const result = await fetchLatestVersion();
expect(result).to.be.null;
fetchStub.restore();
});
it('should return null if no version found', async () => {
const fetchStub = sinon.stub(global, 'fetch').resolves({
ok: true,
text: () =>
Promise.resolve('<html><body>No versions here</body></html>'),
});
const result = await fetchLatestVersion();
expect(result).to.be.null;
fetchStub.restore();
});
});
});