-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
71 lines (59 loc) · 2.94 KB
/
Copy pathtest.js
File metadata and controls
71 lines (59 loc) · 2.94 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
/*---------------------------------------------------------------------------------------------
* Round-trip test for the reference update-feed server — run: node test.js
*--------------------------------------------------------------------------------------------*/
// @ts-check
'use strict';
const assert = require('assert');
const http = require('http');
const fs = require('fs');
const os = require('os');
const path = require('path');
const RELEASES_FILE = path.join(fs.mkdtempSync(path.join(os.tmpdir(), 'levelcode-upd-')), 'releases.json');
fs.writeFileSync(RELEASES_FILE, JSON.stringify({
'darwin-arm64': { stable: { commit: 'NEWSHA', productVersion: '0.2.0', url: 'https://cdn/LevelCode-0.2.0-arm64.zip', sha256hash: 'abc', timestamp: 123 } }
}));
process.env.RELEASES_FILE = RELEASES_FILE;
const { server } = require('./server');
function req(p) {
return new Promise((resolve, reject) => {
http.get({ host: '127.0.0.1', port: server.address().port, path: p }, (res) => {
let body = '';
res.on('data', (c) => { body += c; });
res.on('end', () => resolve({ status: res.statusCode, body }));
}).on('error', reject);
});
}
async function main() {
await new Promise((res) => server.listen(0, res));
let pass = 0;
const ok = (name) => { pass++; console.log(' ok - ' + name); };
// old commit → 200 with the release
let r = await req('/api/update/darwin-arm64/stable/OLDSHA');
assert.strictEqual(r.status, 200);
const feed = JSON.parse(r.body);
assert.strictEqual(feed.version, 'NEWSHA');
assert.strictEqual(feed.productVersion, '0.2.0');
assert.strictEqual(feed.url, 'https://cdn/LevelCode-0.2.0-arm64.zip'); ok('old commit → 200 with the latest release');
// already-latest commit → 204
r = await req('/api/update/darwin-arm64/stable/NEWSHA');
assert.strictEqual(r.status, 204); ok('latest commit → 204 (up to date)');
// unknown target → 204 (nothing published)
r = await req('/api/update/win32-x64/stable/OLDSHA');
assert.strictEqual(r.status, 204); ok('unpublished target → 204');
// unknown quality → 204
r = await req('/api/update/darwin-arm64/insider/OLDSHA');
assert.strictEqual(r.status, 204); ok('unpublished quality → 204');
// health
r = await req('/health');
assert.strictEqual(r.status, 200); ok('health endpoint ok');
// fallback: with no releases.json, the server uses the committed releases.example.json
const savedRF = process.env.RELEASES_FILE;
process.env.RELEASES_FILE = path.join(os.tmpdir(), 'levelcode-missing-' + Date.now() + '.json');
r = await req('/api/update/darwin-arm64/stable/ffffffffdifferentcommit');
assert.strictEqual(r.status, 200); ok('falls back to releases.example.json when releases.json is absent');
process.env.RELEASES_FILE = savedRF;
server.close();
fs.rmSync(path.dirname(RELEASES_FILE), { recursive: true, force: true });
console.log('\nupdate-server: ' + pass + ' tests passed.');
}
main().catch((e) => { console.error(e); try { server.close(); } catch { } process.exit(1); });