dsh-ris-ide 1.7.2 → 1.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -2
- package/bin/check-listing.mjs +247 -0
- package/bin/proxy-janitor.mjs +88 -0
- package/lib/client-codemirror.js +92 -66
- package/lib/client.js +1347 -25
- package/lib/index.js +1174 -8
- package/lib/janitor.js +286 -0
- package/lib/proxy.js +1498 -0
- package/lib/redirector.js +2109 -0
- package/package.json +4 -2
- package/skills/dsh-ris-ide-debug/SKILL.md +37 -2
- package/test/client-viewer.test.mjs +332 -4
- package/test/companion.test.mjs +65 -0
- package/test/host-tail.test.mjs +33 -1
- package/test/inherit-engine.test.mjs +175 -0
- package/test/install-phase.test.mjs +36 -0
- package/test/janitor.test.mjs +144 -0
- package/test/lifecycle.test.mjs +88 -0
- package/test/proxy-route.test.mjs +599 -0
- package/test/proxy.test.mjs +873 -0
- package/test/redirector.test.mjs +521 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An engine left behind by a previous dsh process (the Ctrl+C leak under the old
|
|
3
|
+
* teardown) must be adopted and reported as inherited, not ignored or duplicated.
|
|
4
|
+
*
|
|
5
|
+
* A live janitor means the machine already has an owner for that engine (another
|
|
6
|
+
* dsh, or a watcher about to clean up after a crash) — that engine must NOT be
|
|
7
|
+
* claimed as ours.
|
|
8
|
+
*/
|
|
9
|
+
import assert from 'node:assert/strict'
|
|
10
|
+
import { spawn } from 'node:child_process'
|
|
11
|
+
import { existsSync, mkdtempSync, rmSync, unlinkSync, writeFileSync } from 'node:fs'
|
|
12
|
+
import { connect, createServer } from 'node:net'
|
|
13
|
+
import { tmpdir } from 'node:os'
|
|
14
|
+
import { join } from 'node:path'
|
|
15
|
+
import { fileURLToPath } from 'node:url'
|
|
16
|
+
import { after, test } from 'node:test'
|
|
17
|
+
|
|
18
|
+
const home = mkdtempSync(join(tmpdir(), 'ris-inherit-'))
|
|
19
|
+
const workspace = mkdtempSync(join(tmpdir(), 'ris-inherit-ws-'))
|
|
20
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
21
|
+
|
|
22
|
+
const { __test } = await import('../lib/index.js')
|
|
23
|
+
const { proxyPid, proxyPidPath } = await import('../lib/proxy.js')
|
|
24
|
+
const { janitorStatePath, disarmJanitor } = await import('../lib/janitor.js')
|
|
25
|
+
|
|
26
|
+
const PROXY = fileURLToPath(new URL('../lib/proxy.js', import.meta.url))
|
|
27
|
+
const MARKER = 'dsh-ris-ide-proxy'
|
|
28
|
+
/** @type {import('node:child_process').ChildProcess | null} */
|
|
29
|
+
let engine = null
|
|
30
|
+
|
|
31
|
+
/** @returns {Promise<number>} a port the OS says is free right now. */
|
|
32
|
+
function freePort() {
|
|
33
|
+
return new Promise((resolve) => {
|
|
34
|
+
const server = createServer()
|
|
35
|
+
server.listen(0, '127.0.0.1', () => {
|
|
36
|
+
const { port } = server.address()
|
|
37
|
+
server.close(() => resolve(port))
|
|
38
|
+
})
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Resolve once something accepts a TCP connection on the port.
|
|
44
|
+
* @param {number} port the port to probe.
|
|
45
|
+
* @param {{attempts?: number, interval?: number}} [options] budget.
|
|
46
|
+
* @returns {Promise<boolean>} whether the port accepted a connection.
|
|
47
|
+
*/
|
|
48
|
+
function waitForPort(port, { attempts = 60, interval = 100 } = {}) {
|
|
49
|
+
return new Promise((resolve) => {
|
|
50
|
+
let tries = 0
|
|
51
|
+
const attempt = () => {
|
|
52
|
+
const socket = connect({ host: '127.0.0.1', port })
|
|
53
|
+
socket.once('connect', () => { socket.destroy(); resolve(true) })
|
|
54
|
+
socket.once('error', () => {
|
|
55
|
+
socket.destroy()
|
|
56
|
+
tries += 1
|
|
57
|
+
if (tries >= attempts) resolve(false)
|
|
58
|
+
else setTimeout(attempt, interval)
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
attempt()
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Poll a predicate until it holds (or the attempts run out).
|
|
67
|
+
* @param {() => boolean} predicate the condition.
|
|
68
|
+
* @param {{attempts?: number, interval?: number}} [options] budget.
|
|
69
|
+
* @returns {Promise<boolean>} whether it held.
|
|
70
|
+
*/
|
|
71
|
+
function waitFor(predicate, { attempts = 120, interval = 100 } = {}) {
|
|
72
|
+
return new Promise((resolve) => {
|
|
73
|
+
let tries = 0
|
|
74
|
+
const timer = setInterval(() => {
|
|
75
|
+
tries += 1
|
|
76
|
+
if (predicate()) {
|
|
77
|
+
clearInterval(timer)
|
|
78
|
+
resolve(true)
|
|
79
|
+
} else if (tries >= attempts) {
|
|
80
|
+
clearInterval(timer)
|
|
81
|
+
resolve(false)
|
|
82
|
+
}
|
|
83
|
+
}, interval)
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Spawn a detached engine exactly the way startProxy does, and publish its pidfile.
|
|
89
|
+
* @returns {Promise<{child: import('node:child_process').ChildProcess, port: number}>} the child and its port.
|
|
90
|
+
*/
|
|
91
|
+
async function spawnOrphanEngine() {
|
|
92
|
+
const port = await freePort()
|
|
93
|
+
writeFileSync(join(home, 'proxy.json'), JSON.stringify({
|
|
94
|
+
listenPort: port,
|
|
95
|
+
socks: { host: '10.3.91.121', port: 1080 },
|
|
96
|
+
rules: [],
|
|
97
|
+
shadows: [],
|
|
98
|
+
}))
|
|
99
|
+
const child = spawn(process.execPath, [PROXY, '--cwd', workspace, 'serve', `--marker=${MARKER}`], {
|
|
100
|
+
detached: true, stdio: 'ignore', windowsHide: true,
|
|
101
|
+
})
|
|
102
|
+
child.unref()
|
|
103
|
+
writeFileSync(proxyPidPath(), String(child.pid))
|
|
104
|
+
return { child, port }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Kill a spawned engine (SIGTERM is enough; it exits within the poll budget).
|
|
109
|
+
* @param {import('node:child_process').ChildProcess} child the engine.
|
|
110
|
+
* @returns {Promise<void>} settles when it is gone.
|
|
111
|
+
*/
|
|
112
|
+
async function killEngine(child) {
|
|
113
|
+
if (child.exitCode === null) {
|
|
114
|
+
try { child.kill('SIGTERM') } catch { /* already gone */ }
|
|
115
|
+
await waitFor(() => child.exitCode !== null, { attempts: 50, interval: 100 })
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
after(async () => {
|
|
120
|
+
if (engine !== null) await killEngine(engine)
|
|
121
|
+
disarmJanitor()
|
|
122
|
+
rmSync(home, { recursive: true, force: true })
|
|
123
|
+
rmSync(workspace, { recursive: true, force: true })
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
test('an engine with no watcher is adopted and shown as inherited', async () => {
|
|
127
|
+
// The pre-fix crash path: process.exit() killed dsh, the detached engine kept
|
|
128
|
+
// running, and no janitor state survived to name its owner.
|
|
129
|
+
rmSync(janitorStatePath(), { force: true })
|
|
130
|
+
const spawned = await spawnOrphanEngine()
|
|
131
|
+
engine = spawned.child
|
|
132
|
+
const ready = await waitForPort(spawned.port)
|
|
133
|
+
assert.equal(ready, true, 'the engine should start listening in the temp home')
|
|
134
|
+
assert.equal(await waitFor(() => proxyPid() === engine.pid), true, 'the pidfile must identify the engine')
|
|
135
|
+
|
|
136
|
+
const adopted = __test.adoptLeftoverEngine()
|
|
137
|
+
assert.notEqual(adopted, null, 'an engine with no watcher must be adopted')
|
|
138
|
+
assert.equal(adopted.pid, engine.pid)
|
|
139
|
+
|
|
140
|
+
const view = await __test.proxyView(workspace)
|
|
141
|
+
assert.equal(view.running, true, 'the adopted engine is the running one')
|
|
142
|
+
assert.equal(view.inherited, true, 'the dialog must mark it as inherited')
|
|
143
|
+
assert.equal(view.inheritedPid, engine.pid)
|
|
144
|
+
assert.equal(view.inheritedFrom, 0, 'no watcher state means the old dsh pid is unknown')
|
|
145
|
+
assert.equal(view.owner, 0, 'nothing owns it before this process arms the watcher')
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
test('the banner disappears once the adopted engine is gone', async () => {
|
|
149
|
+
assert.notEqual(engine, null)
|
|
150
|
+
await killEngine(engine)
|
|
151
|
+
rmSync(proxyPidPath(), { force: true })
|
|
152
|
+
assert.equal(__test.adoptLeftoverEngine(), null, 'a dead engine is nothing to inherit')
|
|
153
|
+
const view = await __test.proxyView(workspace)
|
|
154
|
+
assert.equal(view.inherited, false)
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
test('a live watcher means the engine has an owner and must not be claimed', async () => {
|
|
158
|
+
const spawned = await spawnOrphanEngine()
|
|
159
|
+
engine = spawned.child
|
|
160
|
+
assert.equal(await waitForPort(spawned.port), true, 'the engine should start listening')
|
|
161
|
+
assert.equal(await waitFor(() => proxyPid() === engine.pid), true, 'the pidfile must identify the engine')
|
|
162
|
+
// A watcher exists and is alive, but it watches a dead dsh: this is the
|
|
163
|
+
// crash-in-progress case — the engine is about to be cleaned up, not ours.
|
|
164
|
+
writeFileSync(janitorStatePath(), JSON.stringify({
|
|
165
|
+
// The watcher pid must be a live process that is NOT us: disarmJanitor() in the
|
|
166
|
+
// teardown would SIGTERM its own test runner otherwise.
|
|
167
|
+
pid: engine.pid,
|
|
168
|
+
owner: 999999,
|
|
169
|
+
log: join(home, 'janitor.log'),
|
|
170
|
+
}))
|
|
171
|
+
assert.equal(__test.adoptLeftoverEngine(), null, 'a live watcher is not ours to take over')
|
|
172
|
+
const view = await __test.proxyView(workspace)
|
|
173
|
+
assert.equal(view.inherited, false, 'the dialog must not claim an engine another watcher owns')
|
|
174
|
+
rmSync(janitorStatePath(), { force: true })
|
|
175
|
+
})
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import test from 'node:test'
|
|
2
|
+
import assert from 'node:assert/strict'
|
|
3
|
+
import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
|
|
4
|
+
import { tmpdir } from 'node:os'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { installPlanFor } from '../lib/index.js'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A clone without node_modules is the common case for a fresh frontend checkout:
|
|
10
|
+
* the panel must install before it runs, and the log has to say so.
|
|
11
|
+
*/
|
|
12
|
+
test('installPlanFor installs only when dependencies are missing', () => {
|
|
13
|
+
const root = mkdtempSync(join(tmpdir(), 'dsh-install-'))
|
|
14
|
+
// No package.json: not a node project at all.
|
|
15
|
+
assert.equal(installPlanFor(root), null)
|
|
16
|
+
|
|
17
|
+
writeFileSync(join(root, 'package.json'), '{"name":"demo"}')
|
|
18
|
+
assert.deepEqual(installPlanFor(root), { pm: 'npm', args: ['install', '--no-audit', '--no-fund'], dir: root })
|
|
19
|
+
|
|
20
|
+
// An empty node_modules is what a killed install leaves behind.
|
|
21
|
+
mkdirSync(join(root, 'node_modules'))
|
|
22
|
+
assert.notEqual(installPlanFor(root), null)
|
|
23
|
+
|
|
24
|
+
writeFileSync(join(root, 'node_modules', 'leftover'), '')
|
|
25
|
+
assert.equal(installPlanFor(root), null)
|
|
26
|
+
|
|
27
|
+
// Lockfile decides the package manager.
|
|
28
|
+
rmSync(join(root, 'node_modules'), { recursive: true, force: true })
|
|
29
|
+
writeFileSync(join(root, 'pnpm-lock.yaml'), '')
|
|
30
|
+
assert.equal(installPlanFor(root)?.pm, 'pnpm')
|
|
31
|
+
rmSync(join(root, 'pnpm-lock.yaml'))
|
|
32
|
+
writeFileSync(join(root, 'yarn.lock'), '')
|
|
33
|
+
assert.equal(installPlanFor(root)?.pm, 'yarn')
|
|
34
|
+
|
|
35
|
+
rmSync(root, { recursive: true, force: true })
|
|
36
|
+
})
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The janitor is the only thing that stops the proxy layers when dsh itself is
|
|
3
|
+
* killed (no `exit` listener runs on SIGKILL). These tests drive it against a
|
|
4
|
+
* temp proxy home and a throwaway "owner" process, so nothing real is touched.
|
|
5
|
+
*/
|
|
6
|
+
import assert from 'node:assert/strict'
|
|
7
|
+
import { spawn } from 'node:child_process'
|
|
8
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'
|
|
9
|
+
import { tmpdir } from 'node:os'
|
|
10
|
+
import { join } from 'node:path'
|
|
11
|
+
import { test } from 'node:test'
|
|
12
|
+
|
|
13
|
+
import { serviceStatePath, writeWindowsServiceState } from '../lib/redirector.js'
|
|
14
|
+
|
|
15
|
+
const home = mkdtempSync(join(tmpdir(), 'janitor-'))
|
|
16
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
17
|
+
|
|
18
|
+
const {
|
|
19
|
+
armJanitor,
|
|
20
|
+
cleanupSideEffects,
|
|
21
|
+
disarmJanitor,
|
|
22
|
+
janitorStatePath,
|
|
23
|
+
janitorStatus,
|
|
24
|
+
pendingSideEffects,
|
|
25
|
+
} = await import('../lib/janitor.js')
|
|
26
|
+
|
|
27
|
+
/** A process that stays alive until we kill it (stands in for dsh). */
|
|
28
|
+
function fakeOwner() {
|
|
29
|
+
const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' })
|
|
30
|
+
return child
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function waitFor(predicate, { attempts = 80, interval = 100 } = {}) {
|
|
34
|
+
return new Promise((resolve) => {
|
|
35
|
+
let tries = 0
|
|
36
|
+
const timer = setInterval(() => {
|
|
37
|
+
tries += 1
|
|
38
|
+
if (predicate() || tries >= attempts) {
|
|
39
|
+
clearInterval(timer)
|
|
40
|
+
resolve(predicate())
|
|
41
|
+
}
|
|
42
|
+
}, interval)
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
test('nothing is armed until a watcher is started', () => {
|
|
47
|
+
assert.equal(janitorStatus().running, false)
|
|
48
|
+
assert.equal(existsSync(janitorStatePath()), false)
|
|
49
|
+
// With no pid files there is nothing to clean: cleanup must be a cheap no-op.
|
|
50
|
+
assert.deepEqual(pendingSideEffects(), { bridge: false, engine: false, service: false, git: false })
|
|
51
|
+
const done = cleanupSideEffects()
|
|
52
|
+
assert.equal(done.engine, false)
|
|
53
|
+
assert.equal(done.bridge, false)
|
|
54
|
+
assert.equal(done.service, false)
|
|
55
|
+
assert.equal(done.git, false)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('the bridge probe is detection-based, not "our pid file exists" (B5)', () => {
|
|
59
|
+
// A bridge the user started by hand has no pid file of ours; the old pending
|
|
60
|
+
// list said "nothing to do" and left it intercepting after dsh died.
|
|
61
|
+
assert.equal(pendingSideEffects({ detect: () => true }).bridge, true)
|
|
62
|
+
assert.equal(pendingSideEffects({ detect: () => false }).bridge, false)
|
|
63
|
+
const done = cleanupSideEffects({
|
|
64
|
+
detect: () => true,
|
|
65
|
+
stopBridge: () => ({ stopped: true, pid: 111 }),
|
|
66
|
+
restore: () => ({ restored: false, reason: '未停止过服务' }),
|
|
67
|
+
})
|
|
68
|
+
assert.equal(done.bridge, true)
|
|
69
|
+
assert.match(done.log.join('\n'), /bridge: 已停止 pid=111/)
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
test('a failed bridge stop says why instead of looking clean (B5)', () => {
|
|
73
|
+
const done = cleanupSideEffects({
|
|
74
|
+
detect: () => true,
|
|
75
|
+
stopBridge: () => ({ stopped: false, reason: '需要 sudo kill 42' }),
|
|
76
|
+
restore: () => ({ restored: false, reason: '未停止过服务' }),
|
|
77
|
+
})
|
|
78
|
+
assert.equal(done.bridge, false)
|
|
79
|
+
assert.match(done.log.join('\n'), /未停止(需要 sudo kill 42)/)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test('the parked service is recognised at the canonical path and restored (B2)', () => {
|
|
83
|
+
// The writer used <proxy home>/proxybridge/, janitor read <proxy home>/: the two
|
|
84
|
+
// never met, so ProxyBridgeSvc stayed parked on Manual after every crash.
|
|
85
|
+
writeWindowsServiceState({ stoppedByUs: true, originalStartType: 'Automatic', parkedOnManual: true })
|
|
86
|
+
try {
|
|
87
|
+
assert.equal(existsSync(serviceStatePath()), true)
|
|
88
|
+
assert.equal(pendingSideEffects().service, true, 'janitor must see the same file the writer wrote')
|
|
89
|
+
const done = cleanupSideEffects({
|
|
90
|
+
detect: () => false,
|
|
91
|
+
stopBridge: () => ({ stopped: false, reason: '未运行' }),
|
|
92
|
+
// restoreWindowsService answers {restored:true}; reading `.ok` made a real
|
|
93
|
+
// recovery look like a failure (and undefined look like success).
|
|
94
|
+
restore: () => ({ restored: true }),
|
|
95
|
+
})
|
|
96
|
+
assert.equal(done.service, true)
|
|
97
|
+
assert.match(done.log.join('\n'), /service: 已恢复启动方式/)
|
|
98
|
+
} finally {
|
|
99
|
+
rmSync(serviceStatePath(), { force: true })
|
|
100
|
+
rmSync(join(home, 'proxybridge-service.json'), { force: true })
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('arming spawns a detached watcher that records its owner, and disarming stops it', async () => {
|
|
105
|
+
const owner = fakeOwner()
|
|
106
|
+
try {
|
|
107
|
+
const armed = armJanitor({ ownerPid: owner.pid })
|
|
108
|
+
assert.equal(armed.armed, true, armed.reason)
|
|
109
|
+
assert.notEqual(armed.pid, owner.pid, 'the watcher must be its own process')
|
|
110
|
+
|
|
111
|
+
const status = janitorStatus()
|
|
112
|
+
assert.equal(status.running, true)
|
|
113
|
+
assert.equal(status.owner, owner.pid)
|
|
114
|
+
|
|
115
|
+
// Arming twice for the same owner is idempotent (a reload must not stack them).
|
|
116
|
+
const again = armJanitor({ ownerPid: owner.pid })
|
|
117
|
+
assert.equal(again.pid, armed.pid)
|
|
118
|
+
|
|
119
|
+
const stopped = disarmJanitor()
|
|
120
|
+
assert.equal(stopped.stopped, true)
|
|
121
|
+
assert.equal(janitorStatus().running, false)
|
|
122
|
+
assert.equal(existsSync(janitorStatePath()), false)
|
|
123
|
+
} finally {
|
|
124
|
+
owner.kill('SIGKILL')
|
|
125
|
+
disarmJanitor()
|
|
126
|
+
}
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
test('the watcher cleans up and exits once its owner is gone', async () => {
|
|
130
|
+
const owner = fakeOwner()
|
|
131
|
+
const armed = armJanitor({ ownerPid: owner.pid })
|
|
132
|
+
assert.equal(armed.armed, true, armed.reason)
|
|
133
|
+
|
|
134
|
+
owner.kill('SIGKILL')
|
|
135
|
+
const cleaned = await waitFor(() => existsSync(janitorStatePath()) === false, { attempts: 120, interval: 100 })
|
|
136
|
+
assert.equal(cleaned, true, 'the state file must be removed by the exiting watcher')
|
|
137
|
+
|
|
138
|
+
const logPath = join(home, 'janitor.log')
|
|
139
|
+
assert.equal(existsSync(logPath), true)
|
|
140
|
+
const log = readFileSync(logPath, 'utf8')
|
|
141
|
+
assert.match(log, /监视 dsh \d+/)
|
|
142
|
+
assert.match(log, /已退出,开始清理/)
|
|
143
|
+
assert.match(log, /清理完成/)
|
|
144
|
+
})
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Child services (the detached proxy engine, the janitor, the git proxy) belong to
|
|
3
|
+
* the PROCESS, not to one plugin effect.
|
|
4
|
+
*
|
|
5
|
+
* dsh disposes the fiber tree and only then calls `process.exit()`, so the teardown
|
|
6
|
+
* must not remove the exit listener or disarm the janitor: doing either was the
|
|
7
|
+
* Ctrl+C leak — the process exited with nobody left to stop the engine, and the
|
|
8
|
+
* detached engine kept routing traffic with no UI to turn it off. These tests pin
|
|
9
|
+
* the contract: a disposed plugin leaves both guards armed, and a profile hot reload
|
|
10
|
+
* neither stacks exit listeners nor leaves the heartbeat dead.
|
|
11
|
+
*/
|
|
12
|
+
import assert from 'node:assert/strict'
|
|
13
|
+
import { existsSync, mkdtempSync, rmSync } from 'node:fs'
|
|
14
|
+
import { tmpdir } from 'node:os'
|
|
15
|
+
import { join } from 'node:path'
|
|
16
|
+
import { after, test } from 'node:test'
|
|
17
|
+
|
|
18
|
+
const home = mkdtempSync(join(tmpdir(), 'ris-lifecycle-'))
|
|
19
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = home
|
|
20
|
+
|
|
21
|
+
const { apply } = await import('../lib/index.js')
|
|
22
|
+
const { janitorStatePath, janitorStatus, disarmJanitor } = await import('../lib/janitor.js')
|
|
23
|
+
const { heartbeatPath } = await import('../lib/redirector.js')
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The smallest host context `apply()` touches: an effect registry that can run the
|
|
27
|
+
* disposers in reverse (what dsh's fiber dispose does), plus no-op route/tool tables.
|
|
28
|
+
* @returns {object} the fake ctx.
|
|
29
|
+
*/
|
|
30
|
+
function fakeCtx() {
|
|
31
|
+
const disposers = []
|
|
32
|
+
return {
|
|
33
|
+
effect(fn) {
|
|
34
|
+
const dispose = fn()
|
|
35
|
+
if (typeof dispose === 'function') disposers.push(dispose)
|
|
36
|
+
},
|
|
37
|
+
webServer: { register: () => () => {} },
|
|
38
|
+
tools: { register: () => {} },
|
|
39
|
+
logger: { warn: () => {} },
|
|
40
|
+
dispose() {
|
|
41
|
+
while (disposers.length > 0) disposers.pop()()
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
after(() => {
|
|
47
|
+
// The plugin deliberately does NOT disarm on teardown, so the test must.
|
|
48
|
+
disarmJanitor()
|
|
49
|
+
rmSync(home, { recursive: true, force: true })
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
test('a disposed plugin keeps the exit hook and the watcher armed', () => {
|
|
53
|
+
const before = process.listenerCount('exit')
|
|
54
|
+
const ctx = fakeCtx()
|
|
55
|
+
apply(ctx, {})
|
|
56
|
+
assert.ok(process.listenerCount('exit') > before, 'apply must install the exit hook')
|
|
57
|
+
const armed = janitorStatus()
|
|
58
|
+
assert.equal(armed.running, true, 'apply must arm the janitor')
|
|
59
|
+
assert.equal(armed.owner, process.pid)
|
|
60
|
+
assert.equal(existsSync(heartbeatPath()), true, 'apply must write the watchdog heartbeat')
|
|
61
|
+
|
|
62
|
+
ctx.dispose()
|
|
63
|
+
// The bug: the teardown removed both guards, so process.exit() had no listener to
|
|
64
|
+
// run and the detached engine survived.
|
|
65
|
+
assert.ok(process.listenerCount('exit') > before, 'cleanup must NOT remove the exit hook')
|
|
66
|
+
assert.equal(existsSync(janitorStatePath()), true, 'cleanup must NOT disarm the watcher')
|
|
67
|
+
assert.equal(janitorStatus().running, true, 'the watcher itself keeps running')
|
|
68
|
+
|
|
69
|
+
const installed = process.listenerCount('exit')
|
|
70
|
+
// Re-applying (a profile hot reload runs every effect again) reuses the hook.
|
|
71
|
+
const reload = fakeCtx()
|
|
72
|
+
apply(reload, {})
|
|
73
|
+
assert.equal(process.listenerCount('exit'), installed, 'a hot reload must not stack exit hooks')
|
|
74
|
+
assert.equal(existsSync(heartbeatPath()), true, 'the heartbeat is rebuilt after the teardown stopped it')
|
|
75
|
+
reload.dispose()
|
|
76
|
+
assert.equal(process.listenerCount('exit'), installed, 'a reload teardown still leaves the hook')
|
|
77
|
+
assert.equal(existsSync(janitorStatePath()), true)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('three apply/dispose cycles add no exit listeners at all', () => {
|
|
81
|
+
const before = process.listenerCount('exit')
|
|
82
|
+
for (let index = 0; index < 3; index += 1) {
|
|
83
|
+
const ctx = fakeCtx()
|
|
84
|
+
apply(ctx, {})
|
|
85
|
+
ctx.dispose()
|
|
86
|
+
}
|
|
87
|
+
assert.equal(process.listenerCount('exit'), before)
|
|
88
|
+
})
|