-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-packaged.mjs
More file actions
75 lines (68 loc) · 2.37 KB
/
Copy pathtest-packaged.mjs
File metadata and controls
75 lines (68 loc) · 2.37 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
/**
* Packaged-mode simulation: runs the EXACT first-run path a packaged app
* takes (ensureHarness with packaged=true extracting the snapshot into a
* userData-like dir, then booting the pre-built CLI), without electron-builder.
*
* Run: node test-packaged.mjs
* @module dcode/test-packaged
*/
import fs from 'node:fs'
import http from 'node:http'
import os from 'node:os'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { HARNESS_BUILT_ARGS, startServer } from './src/server.mjs'
import { ensureHarness } from './src/updater.mjs'
const root = path.join(path.dirname(fileURLToPath(import.meta.url)))
const snapshotPath = path.join(root, 'pack', 'harness.tgz')
const snapshotMetaPath = path.join(root, 'pack', 'harness.meta.json')
const userDataLike = fs.mkdtempSync(path.join(os.tmpdir(), 'dcode-packaged-'))
console.log('userData-like dir:', userDataLike)
try {
const ok = await ensureHarness({
onProgress: (line) => console.log(' >', line),
packaged: true,
harnessDir: path.join(userDataLike, 'harness'),
snapshotPath,
snapshotMetaPath,
version: '0.1.0',
})
if (!ok) throw new Error('ensureHarness (packaged) failed')
// Second call must be a no-op (version stamp matches).
const second = await ensureHarness({
onProgress: () => {},
packaged: true,
harnessDir: path.join(userDataLike, 'harness'),
snapshotPath,
snapshotMetaPath,
version: '0.1.0',
})
if (!second) throw new Error('second ensureHarness call failed')
console.log('Booting the BUILT harness from the extracted snapshot…')
const handle = await startServer({
log: (line) => console.log(' [server]', line),
cwd: path.join(userDataLike, 'harness'),
args: HARNESS_BUILT_ARGS,
})
console.log('READY', handle.url)
const answered = await new Promise((resolve) => {
const req = http.get(handle.url, (res) => {
res.resume()
resolve(res.statusCode !== undefined && res.statusCode < 500)
})
req.on('error', () => resolve(false))
req.setTimeout(5000, () => {
req.destroy()
resolve(false)
})
})
console.log('HTTP answers:', answered)
await handle.stop()
console.log('PACKAGED SIMULATION OK')
process.exit(answered ? 0 : 1)
} catch (error) {
console.error('PACKAGED SIMULATION FAILED:', error)
process.exit(1)
} finally {
fs.rmSync(userDataLike, { recursive: true, force: true })
}