dsh-ris-ide 1.7.1 → 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.
@@ -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
+ })