dsh-ris-ide 1.7.2 → 1.8.0
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 +43 -4
- package/bin/check-listing.mjs +247 -0
- package/bin/proxy-janitor.mjs +183 -0
- package/bin/risd.mjs +287 -0
- package/bin/run-with-log.ps1 +43 -5
- package/lib/client-codemirror.js +92 -66
- package/lib/client.js +774 -89
- package/lib/index.js +2256 -21
- package/lib/janitor.js +377 -0
- package/lib/proxy.js +1577 -0
- package/lib/proxybridge-bundle.js +798 -0
- package/lib/redirector.js +2558 -0
- package/lib/risd/build.js +91 -0
- package/lib/risd/debug.js +374 -0
- package/lib/risd/git.js +207 -0
- package/lib/risd/leases.js +124 -0
- package/lib/risd/lifecycle.js +148 -0
- package/lib/risd/log.js +59 -0
- package/lib/risd/ping.js +94 -0
- package/lib/risd/programs.js +348 -0
- package/lib/risd/server.js +1478 -0
- package/lib/risd/state.js +137 -0
- package/lib/risd/ui.js +1705 -0
- package/lib/risd/view.js +79 -0
- package/lib/risd-client.js +282 -0
- package/lib/standalone/window.html +14 -16
- package/lib/startup.js +114 -0
- package/package.json +7 -2
- package/skills/dsh-ris-ide-debug/SKILL.md +37 -2
- package/test/client-build.test.mjs +113 -0
- package/test/client-viewer.test.mjs +314 -20
- package/test/companion.test.mjs +65 -0
- package/test/dsh-origin.test.mjs +140 -0
- package/test/dsh-window-refresh.test.mjs +100 -0
- package/test/harness.test.mjs +9 -0
- package/test/host-noloop.test.mjs +74 -0
- package/test/host-tail.test.mjs +54 -5
- package/test/host-window.test.mjs +4 -4
- package/test/inherit-engine.test.mjs +175 -0
- package/test/install-phase.test.mjs +36 -0
- package/test/janitor.test.mjs +172 -0
- package/test/lifecycle.test.mjs +109 -0
- package/test/merge-owners.test.mjs +155 -0
- package/test/proxy-restart.test.mjs +37 -0
- package/test/proxy-route.test.mjs +599 -0
- package/test/proxy-window.test.mjs +45 -0
- package/test/proxy.test.mjs +914 -0
- package/test/proxybridge-bundle.test.mjs +687 -0
- package/test/redirector-bundle.test.mjs +391 -0
- package/test/redirector.test.mjs +571 -0
- package/test/risd-api.test.mjs +470 -0
- package/test/risd-bridge-degrade.test.mjs +101 -0
- package/test/risd-build.test.mjs +20 -0
- package/test/risd-cli-home.test.mjs +50 -0
- package/test/risd-client.test.mjs +130 -0
- package/test/risd-debug.test.mjs +261 -0
- package/test/risd-election.test.mjs +104 -0
- package/test/risd-engine.test.mjs +132 -0
- package/test/risd-entry.test.mjs +118 -0
- package/test/risd-git.test.mjs +109 -0
- package/test/risd-handoff.test.mjs +116 -0
- package/test/risd-heal.test.mjs +50 -0
- package/test/risd-helpers.mjs +108 -0
- package/test/risd-host-p2.test.mjs +147 -0
- package/test/risd-janitor-guard.test.mjs +46 -0
- package/test/risd-lease.test.mjs +83 -0
- package/test/risd-lifecycle.test.mjs +210 -0
- package/test/risd-log-follow.test.mjs +227 -0
- package/test/risd-mode.test.mjs +161 -0
- package/test/risd-ping.test.mjs +99 -0
- package/test/risd-programs.test.mjs +169 -0
- package/test/risd-review-p2.test.mjs +217 -0
- package/test/risd-shutdown.test.mjs +97 -0
- package/test/risd-teardown.test.mjs +87 -0
- package/test/risd-tools.test.mjs +211 -0
- package/test/risd-ui.test.mjs +445 -0
- package/test/risd-write.test.mjs +170 -0
- package/test/startup-reconcile.test.mjs +100 -0
- package/test/windows.test.mjs +37 -2
|
@@ -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,172 @@
|
|
|
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, writeFileSync } 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
|
+
removeOwnJanitorState,
|
|
26
|
+
} = await import('../lib/janitor.js')
|
|
27
|
+
|
|
28
|
+
/** A process that stays alive until we kill it (stands in for dsh). */
|
|
29
|
+
function fakeOwner() {
|
|
30
|
+
const child = spawn(process.execPath, ['-e', 'setInterval(() => {}, 1000)'], { stdio: 'ignore' })
|
|
31
|
+
return child
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function waitFor(predicate, { attempts = 80, interval = 100 } = {}) {
|
|
35
|
+
return new Promise((resolve) => {
|
|
36
|
+
let tries = 0
|
|
37
|
+
const timer = setInterval(() => {
|
|
38
|
+
tries += 1
|
|
39
|
+
if (predicate() || tries >= attempts) {
|
|
40
|
+
clearInterval(timer)
|
|
41
|
+
resolve(predicate())
|
|
42
|
+
}
|
|
43
|
+
}, interval)
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
test('nothing is armed until a watcher is started', () => {
|
|
48
|
+
assert.equal(janitorStatus().running, false)
|
|
49
|
+
assert.equal(existsSync(janitorStatePath()), false)
|
|
50
|
+
// With no pid files there is nothing to clean: cleanup must be a cheap no-op.
|
|
51
|
+
assert.deepEqual(pendingSideEffects(), { bridge: false, engine: false, service: false, git: false })
|
|
52
|
+
const done = cleanupSideEffects()
|
|
53
|
+
assert.equal(done.engine, false)
|
|
54
|
+
assert.equal(done.bridge, false)
|
|
55
|
+
assert.equal(done.service, false)
|
|
56
|
+
assert.equal(done.git, false)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('the bridge probe is detection-based, not "our pid file exists" (B5)', () => {
|
|
60
|
+
// A bridge the user started by hand has no pid file of ours; the old pending
|
|
61
|
+
// list said "nothing to do" and left it intercepting after dsh died.
|
|
62
|
+
assert.equal(pendingSideEffects({ detect: () => true }).bridge, true)
|
|
63
|
+
assert.equal(pendingSideEffects({ detect: () => false }).bridge, false)
|
|
64
|
+
const done = cleanupSideEffects({
|
|
65
|
+
detect: () => true,
|
|
66
|
+
stopBridge: () => ({ stopped: true, pid: 111 }),
|
|
67
|
+
restore: () => ({ restored: false, reason: '未停止过服务' }),
|
|
68
|
+
})
|
|
69
|
+
assert.equal(done.bridge, true)
|
|
70
|
+
assert.match(done.log.join('\n'), /bridge: 已停止 pid=111/)
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
test('a failed bridge stop says why instead of looking clean (B5)', () => {
|
|
74
|
+
const done = cleanupSideEffects({
|
|
75
|
+
detect: () => true,
|
|
76
|
+
stopBridge: () => ({ stopped: false, reason: '需要 sudo kill 42' }),
|
|
77
|
+
restore: () => ({ restored: false, reason: '未停止过服务' }),
|
|
78
|
+
})
|
|
79
|
+
assert.equal(done.bridge, false)
|
|
80
|
+
assert.match(done.log.join('\n'), /未停止(需要 sudo kill 42)/)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
test('the parked service is recognised at the canonical path and restored (B2)', () => {
|
|
84
|
+
// The writer used <proxy home>/proxybridge/, janitor read <proxy home>/: the two
|
|
85
|
+
// never met, so ProxyBridgeSvc stayed parked on Manual after every crash.
|
|
86
|
+
writeWindowsServiceState({ stoppedByUs: true, originalStartType: 'Automatic', parkedOnManual: true })
|
|
87
|
+
try {
|
|
88
|
+
assert.equal(existsSync(serviceStatePath()), true)
|
|
89
|
+
assert.equal(pendingSideEffects().service, true, 'janitor must see the same file the writer wrote')
|
|
90
|
+
const done = cleanupSideEffects({
|
|
91
|
+
detect: () => false,
|
|
92
|
+
stopBridge: () => ({ stopped: false, reason: '未运行' }),
|
|
93
|
+
// restoreWindowsService answers {restored:true}; reading `.ok` made a real
|
|
94
|
+
// recovery look like a failure (and undefined look like success).
|
|
95
|
+
restore: () => ({ restored: true }),
|
|
96
|
+
})
|
|
97
|
+
assert.equal(done.service, true)
|
|
98
|
+
assert.match(done.log.join('\n'), /service: 已恢复启动方式/)
|
|
99
|
+
} finally {
|
|
100
|
+
rmSync(serviceStatePath(), { force: true })
|
|
101
|
+
rmSync(join(home, 'proxybridge-service.json'), { force: true })
|
|
102
|
+
}
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
test('arming spawns a detached watcher that records its owner, and disarming stops it', async () => {
|
|
106
|
+
const owner = fakeOwner()
|
|
107
|
+
try {
|
|
108
|
+
const armed = armJanitor({ ownerPid: owner.pid })
|
|
109
|
+
assert.equal(armed.armed, true, armed.reason)
|
|
110
|
+
assert.notEqual(armed.pid, owner.pid, 'the watcher must be its own process')
|
|
111
|
+
|
|
112
|
+
const status = janitorStatus()
|
|
113
|
+
assert.equal(status.running, true)
|
|
114
|
+
assert.equal(status.owner, owner.pid)
|
|
115
|
+
|
|
116
|
+
// Arming twice for the same owner is idempotent (a reload must not stack them).
|
|
117
|
+
const again = armJanitor({ ownerPid: owner.pid })
|
|
118
|
+
assert.equal(again.pid, armed.pid)
|
|
119
|
+
|
|
120
|
+
const stopped = disarmJanitor()
|
|
121
|
+
assert.equal(stopped.stopped, true)
|
|
122
|
+
assert.equal(janitorStatus().running, false)
|
|
123
|
+
assert.equal(existsSync(janitorStatePath()), false)
|
|
124
|
+
} finally {
|
|
125
|
+
owner.kill('SIGKILL')
|
|
126
|
+
disarmJanitor()
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
test('the watcher cleans up and exits once its owner is gone', async () => {
|
|
131
|
+
const owner = fakeOwner()
|
|
132
|
+
const armed = armJanitor({ ownerPid: owner.pid })
|
|
133
|
+
assert.equal(armed.armed, true, armed.reason)
|
|
134
|
+
|
|
135
|
+
owner.kill('SIGKILL')
|
|
136
|
+
const cleaned = await waitFor(() => existsSync(janitorStatePath()) === false, { attempts: 120, interval: 100 })
|
|
137
|
+
assert.equal(cleaned, true, 'the state file must be removed by the exiting watcher')
|
|
138
|
+
|
|
139
|
+
const logPath = join(home, 'janitor.log')
|
|
140
|
+
assert.equal(existsSync(logPath), true)
|
|
141
|
+
const log = readFileSync(logPath, 'utf8')
|
|
142
|
+
assert.match(log, /监视 dsh \d+/)
|
|
143
|
+
assert.match(log, /已退出,开始清理/)
|
|
144
|
+
assert.match(log, /清理完成/)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
test('disarming never signals a stranger that inherited the watcher pid (B-P2-9)', async () => {
|
|
148
|
+
const stranger = fakeOwner()
|
|
149
|
+
try {
|
|
150
|
+
writeFileSync(janitorStatePath(), `${JSON.stringify({ pid: stranger.pid, owner: 12345, log: '' })}\n`, { mode: 0o600 })
|
|
151
|
+
const result = disarmJanitor()
|
|
152
|
+
assert.equal(result.stopped, false, 'a pid that is not a proxy-janitor is left alone')
|
|
153
|
+
await new Promise((resolve) => setTimeout(resolve, 300))
|
|
154
|
+
assert.equal(stranger.exitCode, null, 'the unrelated process is still running')
|
|
155
|
+
assert.equal(existsSync(janitorStatePath()), false, 'the stale state file is still removed')
|
|
156
|
+
} finally {
|
|
157
|
+
stranger.kill('SIGKILL')
|
|
158
|
+
rmSync(janitorStatePath(), { force: true })
|
|
159
|
+
}
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
test('the watcher only removes a state file it wrote itself (B-P2-9)', () => {
|
|
163
|
+
writeFileSync(janitorStatePath(), `${JSON.stringify({ pid: 424242, owner: 1, log: '' })}\n`, { mode: 0o600 })
|
|
164
|
+
try {
|
|
165
|
+
assert.equal(removeOwnJanitorState(999), false)
|
|
166
|
+
assert.equal(existsSync(janitorStatePath()), true, 'a successor watcher file is not deleted')
|
|
167
|
+
assert.equal(removeOwnJanitorState(424242), true)
|
|
168
|
+
assert.equal(existsSync(janitorStatePath()), false)
|
|
169
|
+
} finally {
|
|
170
|
+
rmSync(janitorStatePath(), { force: true })
|
|
171
|
+
}
|
|
172
|
+
})
|
|
@@ -0,0 +1,109 @@
|
|
|
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, writeFileSync } 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, __test } = 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, { daemonMode: 'off' })
|
|
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, { daemonMode: 'off' })
|
|
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, { daemonMode: 'off' })
|
|
85
|
+
ctx.dispose()
|
|
86
|
+
}
|
|
87
|
+
assert.equal(process.listenerCount('exit'), before)
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('a failed heartbeat write warns once instead of failing silently', () => {
|
|
91
|
+
const warns = []
|
|
92
|
+
const saved = process.env.DSH_RIS_IDE_PROXY_HOME
|
|
93
|
+
// A regular file where a directory is expected makes mkdir() fail fast (ENOTDIR),
|
|
94
|
+
// so writeHeartbeat() reports false — the same code path as a read-only home.
|
|
95
|
+
const blocker = join(home, 'heartbeat-blocker')
|
|
96
|
+
writeFileSync(blocker, 'not a directory\n')
|
|
97
|
+
process.env.DSH_RIS_IDE_PROXY_HOME = join(blocker, 'home')
|
|
98
|
+
let beat = null
|
|
99
|
+
try {
|
|
100
|
+
beat = __test.startHeartbeat({ warn: (message) => warns.push(message) })
|
|
101
|
+
} finally {
|
|
102
|
+
if (saved === undefined) delete process.env.DSH_RIS_IDE_PROXY_HOME
|
|
103
|
+
else process.env.DSH_RIS_IDE_PROXY_HOME = saved
|
|
104
|
+
}
|
|
105
|
+
if (beat !== null) clearInterval(beat)
|
|
106
|
+
assert.equal(warns.length, 1, 'the first failed write warns exactly once')
|
|
107
|
+
assert.match(warns[0], /心跳写入失败/)
|
|
108
|
+
assert.match(warns[0], /20s/)
|
|
109
|
+
})
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `mergeConfigs` must preserve a rule's per-workspace `owners` map.
|
|
3
|
+
*
|
|
4
|
+
* `yds` writes the map and later withdraws only its own endpoints from it. The
|
|
5
|
+
* read path (`normalizeConfig`) already kept it; the daemon write path rebuilt each
|
|
6
|
+
* row from a fixed field list and dropped it, so one config round trip broke
|
|
7
|
+
* `yds stop` (review B-P0-2 / D-P0-1). These tests pin the write path.
|
|
8
|
+
*/
|
|
9
|
+
import assert from 'node:assert/strict'
|
|
10
|
+
import test from 'node:test'
|
|
11
|
+
|
|
12
|
+
import { mergeConfigs, normalizeConfig, normalizeOwners } from '../lib/proxy.js'
|
|
13
|
+
|
|
14
|
+
// The canonical (normalized) shape: hosts/ports are comma strings.
|
|
15
|
+
const ydsOwners = () => ({
|
|
16
|
+
'/ws/a': { program: 'java', hosts: '10.0.0.1', ports: '3306', enabled: true, shadow: false, direct: false },
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
/** A stored table: one driven (`yds`) row with owners, one user row. */
|
|
20
|
+
function storedConfig() {
|
|
21
|
+
return normalizeConfig({
|
|
22
|
+
socks: { host: '127.0.0.1', port: 1080 },
|
|
23
|
+
listenPort: 7890,
|
|
24
|
+
rules: [
|
|
25
|
+
{
|
|
26
|
+
id: 'yds-java',
|
|
27
|
+
label: 'YonCode java',
|
|
28
|
+
program: 'java',
|
|
29
|
+
hosts: '10.0.0.1',
|
|
30
|
+
ports: '3306',
|
|
31
|
+
enabled: true,
|
|
32
|
+
source: 'yds',
|
|
33
|
+
workspace: '/ws/a',
|
|
34
|
+
owners: ydsOwners(),
|
|
35
|
+
},
|
|
36
|
+
{ id: 'user-curl', program: 'curl', hosts: '10.0.0.2', ports: '80', enabled: true, source: 'user', workspace: '/ws/b' },
|
|
37
|
+
],
|
|
38
|
+
shadows: [],
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const row = (config, id) => config.rules.find((item) => item.id === id)
|
|
43
|
+
|
|
44
|
+
test('a driven row keeps its owners across the write path when the patch restates them', () => {
|
|
45
|
+
const merged = mergeConfigs(storedConfig(), {
|
|
46
|
+
replaceRulesFrom: 'yds',
|
|
47
|
+
rules: [{
|
|
48
|
+
id: 'yds-java',
|
|
49
|
+
program: 'java',
|
|
50
|
+
hosts: '10.0.0.7',
|
|
51
|
+
ports: '3306',
|
|
52
|
+
enabled: true,
|
|
53
|
+
source: 'yds',
|
|
54
|
+
owners: {
|
|
55
|
+
'/ws/a': { program: 'java', hosts: ['10.0.0.7'], ports: ['3306'], enabled: true },
|
|
56
|
+
'/ws/c': { program: 'java', hosts: ['10.0.0.7'], ports: ['3306'], enabled: true },
|
|
57
|
+
},
|
|
58
|
+
}],
|
|
59
|
+
})
|
|
60
|
+
const owners = row(merged, 'yds-java').owners
|
|
61
|
+
assert.deepEqual(Object.keys(owners).sort(), ['/ws/a', '/ws/c'])
|
|
62
|
+
assert.equal(owners['/ws/a'].hosts, '10.0.0.7', 'arrays are stored as the canonical comma string')
|
|
63
|
+
assert.equal(owners['/ws/a'].program, 'java')
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
test('a whole-source replacement does not inherit owners the patch does not restate', () => {
|
|
67
|
+
const merged = mergeConfigs(storedConfig(), {
|
|
68
|
+
replaceRulesFrom: 'yds',
|
|
69
|
+
rules: [{ id: 'yds-java', program: 'java', hosts: '10.0.0.3', ports: '3306', enabled: true, source: 'yds' }],
|
|
70
|
+
})
|
|
71
|
+
assert.equal(row(merged, 'yds-java').owners, undefined, 'the replacement is a delete-and-recreate, not an edit')
|
|
72
|
+
})
|
|
73
|
+
|
|
74
|
+
test('editing a different row never loses the driven row owners', () => {
|
|
75
|
+
const merged = mergeConfigs(storedConfig(), {
|
|
76
|
+
rules: [{ id: 'user-curl', program: 'curl', hosts: '10.0.0.2', ports: '81', enabled: true, source: 'user' }],
|
|
77
|
+
})
|
|
78
|
+
assert.equal(row(merged, 'user-curl').ports, '81')
|
|
79
|
+
assert.deepEqual(row(merged, 'yds-java').owners, ydsOwners())
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test('editing the driven row without restating owners inherits the stored map', () => {
|
|
83
|
+
const merged = mergeConfigs(storedConfig(), {
|
|
84
|
+
rules: [{ id: 'yds-java', program: 'java', hosts: '10.0.0.9', ports: '3306', enabled: true, source: 'yds' }],
|
|
85
|
+
})
|
|
86
|
+
assert.equal(row(merged, 'yds-java').hosts, '10.0.0.9')
|
|
87
|
+
assert.deepEqual(row(merged, 'yds-java').owners, ydsOwners(), 'a plain edit is not a withdrawal')
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
test('normalizeOwners whitelists the known fields and drops unusable entries', () => {
|
|
91
|
+
assert.equal(normalizeOwners(null), undefined)
|
|
92
|
+
assert.equal(normalizeOwners([]), undefined)
|
|
93
|
+
assert.equal(normalizeOwners({}), undefined)
|
|
94
|
+
assert.equal(normalizeOwners({ '/ws/x': {} }), undefined, 'an entry without a program is dropped')
|
|
95
|
+
assert.equal(normalizeOwners({ '/ws/x': { program: ' ' } }), undefined)
|
|
96
|
+
assert.equal(normalizeOwners({ '/ws/x': 'nope' }), undefined)
|
|
97
|
+
assert.deepEqual(
|
|
98
|
+
normalizeOwners({ '/ws/x': { program: ' java ', hosts: '1.2.3.4', ports: ['80', '443'], enabled: 1, extra: 'dropped' } }),
|
|
99
|
+
{ '/ws/x': { program: 'java', hosts: '1.2.3.4', ports: '80,443', enabled: false, shadow: false, direct: false } },
|
|
100
|
+
)
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
// --- yds upgrade contract (D 段已提交 d305fd5) -----------------------------
|
|
104
|
+
// 1. Rows migrated from before owners existed are filed under the reserved
|
|
105
|
+
// `<legacy>` bucket: it is not a workspace path, so nothing may normalise or
|
|
106
|
+
// drop it, and a workspace stop must never withdraw it.
|
|
107
|
+
test('the reserved <legacy> owner bucket survives both write paths untouched', () => {
|
|
108
|
+
const legacy = { program: 'java', hosts: ['10.0.0.1'], ports: ['3306'], enabled: true }
|
|
109
|
+
const stored = normalizeConfig({
|
|
110
|
+
socks: { host: '127.0.0.1', port: 1080 },
|
|
111
|
+
listenPort: 7890,
|
|
112
|
+
rules: [{ id: 'yds-java', program: 'java', hosts: '10.0.0.1', ports: '3306', enabled: true, source: 'yds', workspace: '/ws/a', owners: { '<legacy>': legacy } }],
|
|
113
|
+
shadows: [],
|
|
114
|
+
})
|
|
115
|
+
assert.deepEqual(Object.keys(row(stored, 'yds-java').owners), ['<legacy>'])
|
|
116
|
+
|
|
117
|
+
const edited = mergeConfigs(stored, {
|
|
118
|
+
rules: [{ id: 'yds-java', program: 'java', hosts: '10.0.0.2', ports: '3306', enabled: true, source: 'yds' }],
|
|
119
|
+
})
|
|
120
|
+
assert.deepEqual(row(edited, 'yds-java').owners, {
|
|
121
|
+
'<legacy>': { program: 'java', hosts: '10.0.0.1', ports: '3306', enabled: true, shadow: false, direct: false },
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
const replaced = mergeConfigs(stored, {
|
|
125
|
+
replaceRulesFrom: 'yds',
|
|
126
|
+
rules: [{ id: 'yds-java', program: 'java', hosts: '10.0.0.2', ports: '3306', enabled: true, source: 'yds', owners: { '<legacy>': legacy } }],
|
|
127
|
+
})
|
|
128
|
+
assert.deepEqual(Object.keys(row(replaced, 'yds-java').owners), ['<legacy>'])
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
// 2. A slug conflict makes the driver suffix the id with a short hash; the id is
|
|
132
|
+
// the dedupe key (`yds\0id`) and must be stored verbatim.
|
|
133
|
+
test('a hashed driven id is preserved verbatim (dedupe key is the id itself)', () => {
|
|
134
|
+
const merged = mergeConfigs(storedConfig(), {
|
|
135
|
+
replaceRulesFrom: 'yds',
|
|
136
|
+
rules: [{ id: 'yds-java-1a2b3c', program: 'java', hosts: '10.0.0.1', ports: '3306', enabled: true, source: 'yds' }],
|
|
137
|
+
})
|
|
138
|
+
const driven = merged.rules.filter((item) => item.source === 'yds')
|
|
139
|
+
assert.deepEqual(driven.map((item) => item.id), ['yds-java-1a2b3c'])
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
// 3. The driver's POST /v1/proxy no longer sends `shadows`; a missing field must
|
|
143
|
+
// keep the stored fallback listeners rather than silently dropping them.
|
|
144
|
+
test('a patch without shadows keeps the stored fallback listeners', () => {
|
|
145
|
+
const stored = normalizeConfig({
|
|
146
|
+
socks: { host: '127.0.0.1', port: 1080 },
|
|
147
|
+
listenPort: 7890,
|
|
148
|
+
rules: [{ id: 'user-curl', program: 'curl', hosts: '10.0.0.2', ports: '80', enabled: true, source: 'user' }],
|
|
149
|
+
shadows: [{ host: '10.0.0.5', port: 3306 }],
|
|
150
|
+
})
|
|
151
|
+
const merged = mergeConfigs(stored, {
|
|
152
|
+
rules: [{ id: 'user-curl', program: 'curl', hosts: '10.0.0.2', ports: '80', enabled: true, source: 'user' }],
|
|
153
|
+
})
|
|
154
|
+
assert.deepEqual(merged.shadows, [{ host: '10.0.0.5', port: 3306, localPort: 3306 }])
|
|
155
|
+
})
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// The bridge parses its profile when it starts, so a rule-table save can leave old
|
|
2
|
+
// rules intercepting. The daemon page therefore offers "restart bridge" right next
|
|
3
|
+
// to that warning, and both the daemon and the plugin must understand the action.
|
|
4
|
+
import { test } from 'node:test'
|
|
5
|
+
import assert from 'node:assert/strict'
|
|
6
|
+
import { readFileSync } from 'node:fs'
|
|
7
|
+
|
|
8
|
+
const read = (file) => readFileSync(new URL(`../${file}`, import.meta.url), 'utf8')
|
|
9
|
+
|
|
10
|
+
test('the daemon page offers a restart button next to a stale-bridge note', () => {
|
|
11
|
+
const ui = read('lib/risd/ui.js')
|
|
12
|
+
assert.match(ui, /id="bridge-restart"/)
|
|
13
|
+
// The button only appears for notes that say the bridge did not take the new rules.
|
|
14
|
+
assert.match(ui, /未重载\|旧规则/)
|
|
15
|
+
assert.match(ui, /'为什么要重启:'/)
|
|
16
|
+
assert.match(ui, /redirector-restart/)
|
|
17
|
+
})
|
|
18
|
+
|
|
19
|
+
test('the daemon restarts the bridge: stop, settle, start from the new profile', () => {
|
|
20
|
+
const server = read('lib/risd/server.js')
|
|
21
|
+
const at = server.indexOf("action === 'redirector-restart'")
|
|
22
|
+
assert.ok(at > 0, 'daemon must handle redirector-restart')
|
|
23
|
+
const block = server.slice(at, at + 600)
|
|
24
|
+
assert.match(block, /await stopRedirector\(\)/)
|
|
25
|
+
assert.match(block, /setTimeout\(resolve, 300\)/)
|
|
26
|
+
assert.match(block, /await startRedirectors\(\)/)
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test('the plugin forwards the restart action to the daemon and can run it itself', () => {
|
|
30
|
+
const index = read('lib/index.js')
|
|
31
|
+
assert.match(index, /'redirector-restart'/)
|
|
32
|
+
const at = index.indexOf("action === 'redirector-restart'")
|
|
33
|
+
assert.ok(at > 0, 'plugin must handle redirector-restart locally')
|
|
34
|
+
const block = index.slice(at, at + 400)
|
|
35
|
+
assert.match(block, /await stopRedirector\(\)/)
|
|
36
|
+
assert.match(block, /await startRedirectors\(\)/)
|
|
37
|
+
})
|