dsh-ris-ide 1.7.3 → 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.
Files changed (72) hide show
  1. package/README.md +19 -7
  2. package/bin/proxy-janitor.mjs +106 -11
  3. package/bin/risd.mjs +287 -0
  4. package/bin/run-with-log.ps1 +43 -5
  5. package/lib/client.js +515 -1152
  6. package/lib/index.js +1124 -55
  7. package/lib/janitor.js +106 -15
  8. package/lib/proxy.js +86 -7
  9. package/lib/proxybridge-bundle.js +798 -0
  10. package/lib/redirector.js +484 -35
  11. package/lib/risd/build.js +91 -0
  12. package/lib/risd/debug.js +374 -0
  13. package/lib/risd/git.js +207 -0
  14. package/lib/risd/leases.js +124 -0
  15. package/lib/risd/lifecycle.js +148 -0
  16. package/lib/risd/log.js +59 -0
  17. package/lib/risd/ping.js +94 -0
  18. package/lib/risd/programs.js +348 -0
  19. package/lib/risd/server.js +1478 -0
  20. package/lib/risd/state.js +137 -0
  21. package/lib/risd/ui.js +1705 -0
  22. package/lib/risd/view.js +79 -0
  23. package/lib/risd-client.js +282 -0
  24. package/lib/standalone/window.html +14 -16
  25. package/lib/startup.js +114 -0
  26. package/package.json +4 -1
  27. package/test/client-build.test.mjs +113 -0
  28. package/test/client-viewer.test.mjs +166 -200
  29. package/test/dsh-origin.test.mjs +140 -0
  30. package/test/dsh-window-refresh.test.mjs +100 -0
  31. package/test/harness.test.mjs +9 -0
  32. package/test/host-noloop.test.mjs +74 -0
  33. package/test/host-tail.test.mjs +21 -4
  34. package/test/host-window.test.mjs +4 -4
  35. package/test/janitor.test.mjs +29 -1
  36. package/test/lifecycle.test.mjs +26 -5
  37. package/test/merge-owners.test.mjs +155 -0
  38. package/test/proxy-restart.test.mjs +37 -0
  39. package/test/proxy-window.test.mjs +45 -0
  40. package/test/proxy.test.mjs +41 -0
  41. package/test/proxybridge-bundle.test.mjs +687 -0
  42. package/test/redirector-bundle.test.mjs +391 -0
  43. package/test/redirector.test.mjs +59 -9
  44. package/test/risd-api.test.mjs +470 -0
  45. package/test/risd-bridge-degrade.test.mjs +101 -0
  46. package/test/risd-build.test.mjs +20 -0
  47. package/test/risd-cli-home.test.mjs +50 -0
  48. package/test/risd-client.test.mjs +130 -0
  49. package/test/risd-debug.test.mjs +261 -0
  50. package/test/risd-election.test.mjs +104 -0
  51. package/test/risd-engine.test.mjs +132 -0
  52. package/test/risd-entry.test.mjs +118 -0
  53. package/test/risd-git.test.mjs +109 -0
  54. package/test/risd-handoff.test.mjs +116 -0
  55. package/test/risd-heal.test.mjs +50 -0
  56. package/test/risd-helpers.mjs +108 -0
  57. package/test/risd-host-p2.test.mjs +147 -0
  58. package/test/risd-janitor-guard.test.mjs +46 -0
  59. package/test/risd-lease.test.mjs +83 -0
  60. package/test/risd-lifecycle.test.mjs +210 -0
  61. package/test/risd-log-follow.test.mjs +227 -0
  62. package/test/risd-mode.test.mjs +161 -0
  63. package/test/risd-ping.test.mjs +99 -0
  64. package/test/risd-programs.test.mjs +169 -0
  65. package/test/risd-review-p2.test.mjs +217 -0
  66. package/test/risd-shutdown.test.mjs +97 -0
  67. package/test/risd-teardown.test.mjs +87 -0
  68. package/test/risd-tools.test.mjs +211 -0
  69. package/test/risd-ui.test.mjs +445 -0
  70. package/test/risd-write.test.mjs +170 -0
  71. package/test/startup-reconcile.test.mjs +100 -0
  72. package/test/windows.test.mjs +37 -2
@@ -0,0 +1,391 @@
1
+ /**
2
+ * Tests for the *downloaded* ProxyBridge distribution used by the redirector.
3
+ *
4
+ * The plugin no longer ships binaries (2.9 MB in `lib/vendor/…` made every install
5
+ * heavy and pinned the upstream version): `lib/proxybridge-bundle.js` downloads or
6
+ * accepts an uploaded `.tgz` into `~/.dsh-ris-ide/proxybridge/bundle`, and this file
7
+ * pins how the lookup uses that cache.
8
+ *
9
+ * Fixtures are built in-process (a tiny ustar writer + gzip), so no test touches
10
+ * the network or the real `~/.dsh-ris-ide`.
11
+ */
12
+ import assert from 'node:assert/strict'
13
+ import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, utimesSync, writeFileSync } from 'node:fs'
14
+ import { tmpdir } from 'node:os'
15
+ import { join } from 'node:path'
16
+ import test from 'node:test'
17
+ import { gzipSync } from 'node:zlib'
18
+
19
+ import { DEFAULT_BUNDLE_URL, bundleLayout, installDir, installFromTgz, isInstalled } from '../lib/proxybridge-bundle.js'
20
+
21
+ const home = mkdtempSync(join(tmpdir(), 'ris-redirector-home-'))
22
+ process.env.DSH_RIS_IDE_PROXY_HOME = home
23
+ // These tests serve fixture archives, so the release pin (which guards real
24
+ // downloads) is switched off; the pin itself is covered in the bundle module tests.
25
+ process.env.DSH_RIS_IDE_PROXYBRIDGE_SHA256 = ''
26
+
27
+ const {
28
+ __test,
29
+ bundledBundleDir,
30
+ bundledRedirectorDir,
31
+ ensureBundleReady,
32
+ prepareWindowsBundle,
33
+ redirectorCandidates,
34
+ } = await import('../lib/redirector.js')
35
+
36
+ const BUNDLE = bundleLayout()
37
+ const WINDOWS_FILES = ['ProxyBridge_CLI.exe', 'ProxyBridgeCore.dll', 'WinDivert.dll', 'WinDivert64.sys']
38
+ const LICENCES = ['NOTICE.md', 'LICENSE-ProxyBridge.txt']
39
+ const fileContent = (rel) => `fixture bytes for ${rel}\n`
40
+
41
+ /** Octal field: width-1 digits + NUL, as ustar spells numbers. */
42
+ const octalField = (value, width) => `${value.toString(8).padStart(width - 1, '0')}\0`
43
+
44
+ function tarHeader(name, size, { mode = 0o644 } = {}) {
45
+ const header = Buffer.alloc(512, 0)
46
+ Buffer.from(name, 'utf8').copy(header, 0)
47
+ header.write(octalField(mode, 8), 100, 'ascii')
48
+ header.write(octalField(0, 8), 108, 'ascii')
49
+ header.write(octalField(0, 8), 116, 'ascii')
50
+ header.write(octalField(size, 12), 124, 'ascii')
51
+ header.write(octalField(0, 12), 136, 'ascii')
52
+ header.write(' ', 148, 'ascii')
53
+ header.write('0', 156, 'ascii')
54
+ header.write('ustar\0', 257, 'ascii')
55
+ header.write('00', 263, 'ascii')
56
+ let sum = 0
57
+ for (const byte of header) sum += byte
58
+ header.write(`${sum.toString(8).padStart(6, '0')}\0 `, 148, 'ascii')
59
+ return header
60
+ }
61
+
62
+ /** Build a real `.tgz` from `{name, content, mode}` entries. */
63
+ function makeTgz(entries) {
64
+ const chunks = []
65
+ for (const entry of entries) {
66
+ const content = Buffer.from(entry.content)
67
+ chunks.push(tarHeader(entry.name, content.length, entry))
68
+ chunks.push(content)
69
+ const pad = (512 - (content.length % 512)) % 512
70
+ if (pad > 0) chunks.push(Buffer.alloc(pad))
71
+ }
72
+ chunks.push(Buffer.alloc(1024))
73
+ return gzipSync(Buffer.concat(chunks))
74
+ }
75
+
76
+ const bundleTgz = () => makeTgz(BUNDLE.map((rel) => ({
77
+ name: rel,
78
+ content: fileContent(rel),
79
+ mode: /\.(exe|dll|sys|so)$|ProxyBridge$/.test(rel) === true ? 0o755 : 0o644,
80
+ })))
81
+
82
+ /** `fetch` stand-in serving one body; records every URL it was asked for. */
83
+ function fetchServing(body) {
84
+ const calls = { urls: [] }
85
+ const impl = async (url) => {
86
+ calls.urls.push(String(url))
87
+ return {
88
+ ok: true,
89
+ status: 200,
90
+ arrayBuffer: async () => body.buffer.slice(body.byteOffset, body.byteOffset + body.length),
91
+ }
92
+ }
93
+ return { impl, calls }
94
+ }
95
+
96
+ const installFixture = () => {
97
+ // A fresh machine starts with no cache; every test proves its own precondition.
98
+ rmSync(join(home, 'proxybridge'), { recursive: true, force: true })
99
+ __test.clearProbeCaches()
100
+ return installFromTgz({ home, bytes: bundleTgz() })
101
+ }
102
+
103
+ test('the plugin no longer ships ProxyBridge binaries', () => {
104
+ assert.deepEqual(bundleLayout(), [
105
+ 'LICENSE-ProxyBridge.txt',
106
+ 'NOTICE.md',
107
+ 'linux/ProxyBridge',
108
+ 'linux/libproxybridge.so',
109
+ 'windows/ProxyBridgeCore.dll',
110
+ 'windows/ProxyBridge_CLI.exe',
111
+ 'windows/WinDivert.dll',
112
+ 'windows/WinDivert64.sys',
113
+ ])
114
+ // The lookup points at the download cache in the proxy home, never into `lib/`.
115
+ assert.equal(bundledBundleDir(), installDir(home))
116
+ assert.equal(bundledRedirectorDir({ platform: 'linux' }), join(home, 'proxybridge', 'bundle', 'linux'))
117
+ assert.equal(bundledRedirectorDir({ platform: 'win32' }), join(home, 'proxybridge', 'bundle', 'windows'))
118
+ })
119
+
120
+ test('an uninstalled distribution is reported instead of silently skipped', () => {
121
+ rmSync(join(home, 'proxybridge'), { recursive: true, force: true })
122
+ delete process.env.DSH_RIS_IDE_PROXYBRIDGE
123
+ __test.clearProbeCaches()
124
+ const candidates = redirectorCandidates({ side: 'local' })
125
+ assert.equal(candidates.missingBundle, true, 'the candidates know the cache is empty')
126
+ assert.equal(candidates.bundleInstalled, false)
127
+ assert.equal(candidates.bundleDir, installDir(home))
128
+ assert.ok(candidates.includes(join(installDir(home), 'linux', 'ProxyBridge')), 'the expected path is still shown')
129
+
130
+ const found = __test.findRedirector({ side: 'local' })
131
+ assert.equal(found.found, false)
132
+ assert.equal(found.missingBundle, true, 'callers must be able to show "download/upload"')
133
+ assert.match(found.reason, /组件未安装/)
134
+ assert.match(found.reason, /维护页/)
135
+ assert.ok(found.reason.includes(DEFAULT_BUNDLE_URL), 'the reason carries the download address')
136
+ })
137
+
138
+ test('an installed cache is the first candidate on both sides', () => {
139
+ installFixture()
140
+ delete process.env.DSH_RIS_IDE_PROXYBRIDGE
141
+ assert.equal(isInstalled(installDir(home)), true)
142
+
143
+ const local = redirectorCandidates({ side: 'local' })
144
+ assert.equal(local[0], join(installDir(home), 'linux', 'ProxyBridge'))
145
+ assert.equal(local.missingBundle, false)
146
+ assert.equal(local.bundleInstalled, true)
147
+
148
+ const windows = redirectorCandidates({ side: 'windows', platform: 'linux' })
149
+ assert.equal(windows[0], join(installDir(home), 'windows', 'ProxyBridge_CLI.exe'))
150
+ assert.equal(windows.missingBundle, false)
151
+ })
152
+
153
+ test('findRedirector finds the cached CLI without any machine-wide install', () => {
154
+ installFixture()
155
+ delete process.env.DSH_RIS_IDE_PROXYBRIDGE
156
+ const found = __test.findRedirector({ side: 'local' })
157
+ assert.equal(found.found, true)
158
+ assert.equal(found.path, join(installDir(home), 'linux', 'ProxyBridge'))
159
+ assert.equal(found.kind, 'linux-cli')
160
+ assert.equal(found.missingBundle, false)
161
+ assert.equal(found.bundleDir, installDir(home))
162
+ })
163
+
164
+ test('prepareWindowsBundle refuses to point at a path that will never appear', () => {
165
+ rmSync(join(home, 'proxybridge'), { recursive: true, force: true })
166
+ __test.clearProbeCaches()
167
+ assert.equal(prepareWindowsBundle({ platform: 'win32' }), undefined, '未安装 ⇒ undefined(不是空字符串路径)')
168
+ })
169
+
170
+ test('prepareWindowsBundle materialises the cached Windows form plus licences', () => {
171
+ installFixture()
172
+ const target = mkdtempSync(join(tmpdir(), 'ris-ide-bundle-'))
173
+ try {
174
+ assert.equal(prepareWindowsBundle({ platform: 'win32', targetDir: target, force: true }), target)
175
+ for (const name of [...WINDOWS_FILES, ...LICENCES]) {
176
+ assert.ok(existsSync(join(target, name)), `${name} was materialised`)
177
+ assert.equal(statSync(join(target, name)).size, statSync(join(installDir(home), name === 'NOTICE.md' || name === 'LICENSE-ProxyBridge.txt' ? name : `windows/${name}`)).size)
178
+ }
179
+ assert.deepEqual(readdirSync(target).sort(), [...WINDOWS_FILES, ...LICENCES].sort())
180
+ // Second run is a no-op (same size and mtime) and still reports the folder.
181
+ assert.equal(prepareWindowsBundle({ platform: 'win32', targetDir: target, force: true }), target)
182
+ } finally {
183
+ rmSync(target, { recursive: true, force: true })
184
+ }
185
+ })
186
+
187
+ test('a copy with an old timestamp is refreshed even when the size matches', () => {
188
+ installFixture()
189
+ const target = mkdtempSync(join(tmpdir(), 'ris-ide-bundle-stale-'))
190
+ try {
191
+ assert.equal(prepareWindowsBundle({ platform: 'win32', targetDir: target, force: true }), target)
192
+ const copied = join(target, 'ProxyBridge_CLI.exe')
193
+ const source = join(installDir(home), 'windows', 'ProxyBridge_CLI.exe')
194
+ utimesSync(copied, new Date(0), new Date(0))
195
+ assert.equal(prepareWindowsBundle({ platform: 'win32', targetDir: target, force: true }), target)
196
+ const delta = Math.abs(statSync(copied).mtimeMs - statSync(source).mtimeMs)
197
+ assert.ok(delta <= 2000, `the refresh carries the source mtime (delta ${delta}ms)`)
198
+ } finally {
199
+ rmSync(target, { recursive: true, force: true })
200
+ }
201
+ })
202
+
203
+ test('findRedirector materialises the cached Windows CLI when no platform is pinned', () => {
204
+ installFixture()
205
+ const target = mkdtempSync(join(tmpdir(), 'ris-ide-bundle-auto-'))
206
+ try {
207
+ delete process.env.DSH_RIS_IDE_PROXYBRIDGE
208
+ __test.clearProbeCaches()
209
+ __test.setProbeHooks({ bundleTarget: () => target })
210
+ const found = __test.findRedirector({ side: 'windows' })
211
+ assert.equal(found.found, true, 'a Windows-addressable target must yield a usable CLI')
212
+ assert.equal(found.path, join(target, 'ProxyBridge_CLI.exe'))
213
+ assert.ok(existsSync(found.path), 'the executable really exists')
214
+ assert.equal(found.candidates[0], join(target, 'ProxyBridge_CLI.exe'))
215
+ assert.ok(existsSync(join(target, 'NOTICE.md')), 'the copy carries the licence text')
216
+ assert.equal(found.missingBundle, false)
217
+ } finally {
218
+ __test.resetProbeHooks()
219
+ __test.clearProbeCaches()
220
+ rmSync(target, { recursive: true, force: true })
221
+ }
222
+ })
223
+
224
+ test('a bundle installed while the daemon was down refreshes the Windows copy on the next start', () => {
225
+ installFixture()
226
+ const target = mkdtempSync(join(tmpdir(), 'ris-ide-bundle-restart-'))
227
+ try {
228
+ __test.clearProbeCaches()
229
+ __test.setProbeHooks({ bundleTarget: () => target })
230
+ assert.equal(prepareWindowsBundle(), target)
231
+ const copied = join(target, 'ProxyBridge_CLI.exe')
232
+ assert.equal(readFileSync(copied, 'utf8'), fileContent('windows/ProxyBridge_CLI.exe'))
233
+
234
+ // The daemon "restarts": the in-memory materialisation cache is gone, but the
235
+ // marker file next to the copy (and the new bundle on disk) remain.
236
+ __test.clearProbeCaches()
237
+ __test.setProbeHooks({ bundleTarget: () => target })
238
+ const replacement = makeTgz(BUNDLE.map((rel) => ({
239
+ name: rel,
240
+ content: rel === 'windows/ProxyBridge_CLI.exe'
241
+ ? fileContent(rel).replace('fixture', 'FIXTURE')
242
+ : fileContent(rel),
243
+ mode: /\.(exe|dll|sys|so)$|ProxyBridge$/.test(rel) === true ? 0o755 : 0o644,
244
+ })))
245
+ installFromTgz({ home, bytes: replacement })
246
+
247
+ assert.equal(prepareWindowsBundle(), target)
248
+ assert.equal(
249
+ readFileSync(copied, 'utf8'),
250
+ fileContent('windows/ProxyBridge_CLI.exe').replace('fixture', 'FIXTURE'),
251
+ 'the disk marker must trigger a refresh even with an empty in-memory cache',
252
+ )
253
+ } finally {
254
+ __test.resetProbeHooks()
255
+ __test.clearProbeCaches()
256
+ rmSync(target, { recursive: true, force: true })
257
+ }
258
+ })
259
+
260
+ test('a failed materialisation is not cached: the next call retries', () => {
261
+ installFixture()
262
+ const target = mkdtempSync(join(tmpdir(), 'ris-ide-bundle-retry-'))
263
+ let calls = 0
264
+ try {
265
+ __test.clearProbeCaches()
266
+ __test.setProbeHooks({ bundleTarget: () => (++calls === 1 ? '' : target) })
267
+ assert.equal(prepareWindowsBundle(), '', 'the first run has no Windows-addressable target')
268
+ assert.equal(prepareWindowsBundle(), target, 'the second run retries and succeeds')
269
+ assert.equal(calls, 2)
270
+ // A complete bundle IS cached: the third call must not need the hook again.
271
+ assert.equal(prepareWindowsBundle(), target)
272
+ assert.equal(calls, 2)
273
+ } finally {
274
+ __test.resetProbeHooks()
275
+ __test.clearProbeCaches()
276
+ rmSync(target, { recursive: true, force: true })
277
+ }
278
+ })
279
+
280
+ test('ensureBundleReady downloads into the proxy home and reuses the cache', async () => {
281
+ rmSync(join(home, 'proxybridge'), { recursive: true, force: true })
282
+ __test.clearProbeCaches()
283
+ delete process.env.DSH_RIS_IDE_PROXYBRIDGE_URL
284
+ const { impl, calls } = fetchServing(bundleTgz())
285
+ const first = await ensureBundleReady({ fetchImpl: impl })
286
+ assert.equal(first.ok, true)
287
+ assert.equal(first.fromCache, false)
288
+ assert.equal(first.dir, installDir(home))
289
+ assert.equal(first.url, DEFAULT_BUNDLE_URL)
290
+ assert.equal(isInstalled(installDir(home)), true)
291
+ const second = await ensureBundleReady({ fetchImpl: impl })
292
+ assert.equal(second.ok, true)
293
+ assert.equal(second.fromCache, true, 'the cache must be reused without another request')
294
+ assert.equal(calls.urls.length, 1)
295
+ })
296
+
297
+ test('ensureBundleReady never throws on a download failure', async () => {
298
+ rmSync(join(home, 'proxybridge'), { recursive: true, force: true })
299
+ __test.clearProbeCaches()
300
+ const failing = async () => ({ ok: false, status: 502, arrayBuffer: async () => new ArrayBuffer(0) })
301
+ const result = await ensureBundleReady({ fetchImpl: failing })
302
+ assert.equal(result.ok, false)
303
+ assert.match(String(result.error), /HTTP 502/)
304
+ assert.match(String(result.hint), /检查网络/)
305
+ })
306
+
307
+ test('the env override beats settings for ensureBundleReady (one resolver, same as the daemon)', async () => {
308
+ rmSync(join(home, 'proxybridge'), { recursive: true, force: true })
309
+ __test.clearProbeCaches()
310
+ const settingsPath = join(home, 'settings.json')
311
+ writeFileSync(settingsPath, JSON.stringify({ proxybridgeUrl: 'https://mirror.example/ris-proxy.tgz' }))
312
+ process.env.DSH_RIS_IDE_PROXYBRIDGE_URL = 'https://env.example/ris-proxy.tgz'
313
+ const { impl, calls } = fetchServing(bundleTgz())
314
+ try {
315
+ const result = await ensureBundleReady({ fetchImpl: impl })
316
+ assert.equal(result.ok, true)
317
+ // URL resolution lives only in `resolveBundleUrls` (env → settings → mirrors),
318
+ // so redirector-side and daemon-side installs agree (review A3-P2-1).
319
+ assert.deepEqual(calls.urls, ['https://env.example/ris-proxy.tgz'])
320
+ } finally {
321
+ delete process.env.DSH_RIS_IDE_PROXYBRIDGE_URL
322
+ rmSync(settingsPath, { force: true })
323
+ }
324
+ })
325
+
326
+ test('a new bundle refreshes the materialised Windows copy even at the same size', () => {
327
+ installFixture()
328
+ const target = mkdtempSync(join(tmpdir(), 'ris-ide-bundle-refresh-'))
329
+ try {
330
+ __test.clearProbeCaches()
331
+ __test.setProbeHooks({ bundleTarget: () => target })
332
+ assert.equal(prepareWindowsBundle(), target, 'the first bundle is materialised')
333
+ const copied = join(target, 'ProxyBridge_CLI.exe')
334
+ assert.equal(readFileSync(copied, 'utf8'), fileContent('windows/ProxyBridge_CLI.exe'))
335
+
336
+ // Same byte count, different content, freshly written source: the size+mtime
337
+ // shortcut alone would keep the old copy (review B-P1-2). Only the bundle
338
+ // fingerprint tells them apart.
339
+ const replacement = makeTgz(BUNDLE.map((rel) => ({
340
+ name: rel,
341
+ content: rel === 'windows/ProxyBridge_CLI.exe'
342
+ ? fileContent(rel).replace('fixture', 'FIXTURE')
343
+ : fileContent(rel),
344
+ mode: /\.(exe|dll|sys|so)$|ProxyBridge$/.test(rel) === true ? 0o755 : 0o644,
345
+ })))
346
+ installFromTgz({ home, bytes: replacement })
347
+
348
+ assert.equal(prepareWindowsBundle(), target)
349
+ assert.equal(
350
+ readFileSync(copied, 'utf8'),
351
+ fileContent('windows/ProxyBridge_CLI.exe').replace('fixture', 'FIXTURE'),
352
+ 'the Windows copy follows the newly installed bundle',
353
+ )
354
+ } finally {
355
+ __test.resetProbeHooks()
356
+ __test.clearProbeCaches()
357
+ rmSync(target, { recursive: true, force: true })
358
+ }
359
+ })
360
+
361
+ test('windowsArgsOf only translates slash arguments that are existing files', () => {
362
+ const dir = mkdtempSync(join(tmpdir(), 'ris-ide-args-'))
363
+ const profile = join(dir, 'dsh-ris-ide.pbprofile')
364
+ writeFileSync(profile, '{}')
365
+ try {
366
+ const { windowsArgsOf, windowsPathOf } = __test
367
+ const args = windowsArgsOf([
368
+ '--profile',
369
+ profile,
370
+ '--verbose',
371
+ '2',
372
+ '/d',
373
+ '/p:key=value',
374
+ '/mnt/definitely-missing/x',
375
+ '\\\\wsl.localhost\\Ubuntu\\home\\wangjun\\x',
376
+ ])
377
+ assert.equal(args[1], windowsPathOf(profile), 'an existing file is translated')
378
+ assert.equal(args[4], '/d')
379
+ assert.equal(args[5], '/p:key=value')
380
+ assert.equal(args[6], '/mnt/definitely-missing/x')
381
+ assert.equal(args[7], '\\\\wsl.localhost\\Ubuntu\\home\\wangjun\\x')
382
+ } finally {
383
+ rmSync(dir, { recursive: true, force: true })
384
+ }
385
+ })
386
+
387
+ process.on('exit', () => {
388
+ delete process.env.DSH_RIS_IDE_PROXY_HOME
389
+ delete process.env.DSH_RIS_IDE_PROXYBRIDGE_SHA256
390
+ rmSync(home, { recursive: true, force: true })
391
+ })
@@ -56,9 +56,15 @@ test('a Windows command line never carries a WSL-only /mnt path', async () => {
56
56
  'Start-Process cannot resolve /mnt paths: they are WSL-only')
57
57
  const already = 'D:\\apps\\ProxyBridge_CLI.exe'
58
58
  assert.equal(windowsPathOf(already), already, 'an already-Windows path is left alone')
59
+ // Only arguments that are existing files are treated as paths (a `/d` or
60
+ // `/p:key=value` switch is not), so the profile has to exist here — exactly the
61
+ // situation at runtime, where writeRedirectorProfile just wrote it.
62
+ const profileDir = mkdtempSync(join(tmpdir(), 'ris-ide-plan-'))
63
+ const profile = join(profileDir, 'dsh-ris-ide.pbprofile')
64
+ writeFileSync(profile, '{}')
59
65
  const plan = redirectorCommand({
60
66
  cliPath: '/mnt/x/Program Files/ProxyBridge/ProxyBridge_CLI.exe',
61
- profilePath: '/mnt/x/pb/dsh-ris-ide.pbprofile',
67
+ profilePath: profile,
62
68
  platform: 'win32',
63
69
  })
64
70
  assert.equal(plan.display.includes('/mnt/'), false, 'no mnt path may survive in the Windows command')
@@ -159,16 +165,21 @@ test('the start command carries the elevation requirement of each platform', ()
159
165
  })
160
166
 
161
167
  test('detection reports the two sides independently', () => {
168
+ // The distribution is downloaded into ~/.dsh-ris-ide/proxybridge/bundle; when it
169
+ // is not installed yet a machine-wide (manually extracted / packaged) copy is
170
+ // still honoured, which is what this test pins.
162
171
  const vendor = __test.redirectorVendorDir({ platform: 'linux' })
163
172
  mkdirSync(vendor, { recursive: true })
164
173
  const fake = join(vendor, 'ProxyBridge')
165
174
  writeFileSync(fake, '#!/bin/sh\necho "ProxyBridge CLI V9.9.9"\n', { encoding: 'utf8' })
166
175
  chmodSync(fake, 0o755)
167
176
  try {
177
+ __test.clearProbeCaches()
168
178
  const status = __test.redirectorStatus()
169
- assert.equal(status.local.available, true, 'the vendored copy is what WSL uses')
179
+ assert.equal(status.local.available, true)
170
180
  assert.equal(status.local.kind, 'linux-cli')
171
- assert.equal(status.local.version, '9.9.9')
181
+ assert.equal(status.local.path, fake, 'the machine-wide fallback is used when no download exists')
182
+ assert.match(String(status.local.version), /^\d+\.\d+\.\d+$/)
172
183
  // Windows side depends on the machine, so only its shape is checked.
173
184
  assert.equal(typeof status.windows.available, 'boolean')
174
185
  assert.equal(typeof status.windows.path, 'string')
@@ -209,17 +220,25 @@ test('stopping a redirector that is not running is a no-op, not a throw', () =>
209
220
  assert.deepEqual(__test.stopRedirectorSync(), { stopped: false, reason: '未在运行' })
210
221
  })
211
222
 
212
- test('starting without a CLI reports why instead of throwing', async () => {
223
+ test('a missing override falls back to the machine-wide copy', () => {
224
+ // The explicit override is only a preference: a stale path must not hide a real
225
+ // installation (the downloaded cache replaces the old in-repo vendor folder).
226
+ const vendor = __test.redirectorVendorDir({ platform: 'linux' })
227
+ mkdirSync(vendor, { recursive: true })
228
+ const fake = join(vendor, 'ProxyBridge')
229
+ writeFileSync(fake, '#!/bin/sh\necho "x V1.0.0"\n', { encoding: 'utf8' })
230
+ chmodSync(fake, 0o755)
213
231
  const real = process.env.DSH_RIS_IDE_PROXYBRIDGE
214
- process.env.DSH_RIS_IDE_PROXYBRIDGE = join(home, 'proxybridge', 'vendor', 'linux', 'ProxyBridge')
215
- rmSync(join(home, 'proxybridge', 'vendor'), { recursive: true, force: true })
232
+ process.env.DSH_RIS_IDE_PROXYBRIDGE = join(home, 'proxybridge', 'vendor', 'linux', 'missing-ProxyBridge')
216
233
  try {
217
- const result = await __test.startRedirector({ side: 'local' })
218
- assert.equal(result.started, false)
219
- assert.match(String(result.reason), /未找到/)
234
+ __test.clearProbeCaches()
235
+ const found = __test.findRedirector({ side: 'local' })
236
+ assert.equal(found.found, true)
237
+ assert.equal(found.path, fake)
220
238
  } finally {
221
239
  if (real === undefined) delete process.env.DSH_RIS_IDE_PROXYBRIDGE
222
240
  else process.env.DSH_RIS_IDE_PROXYBRIDGE = real
241
+ rmSync(fake, { force: true })
223
242
  }
224
243
  })
225
244
 
@@ -466,6 +485,37 @@ test('the profile names a rule by its label so the ProxyBridge console stays rea
466
485
  assert.equal(plain.profile.ProxyRules[0].Name, 'yds-java')
467
486
  })
468
487
 
488
+ test('a stale stop marker never answers the next stop request (B-P2-7)', async () => {
489
+ process.env.DSH_RIS_IDE_PROXY_HOME = home
490
+ const mod = await import('../lib/redirector.js')
491
+ // The watchdog wrote "done" for an earlier stop; nothing has been asked for the
492
+ // new one yet, so a plain "does the file exist" read would report success while
493
+ // the CLI is still intercepting.
494
+ writeFileSync(mod.stoppedMarkerPath(), 'done\n')
495
+ assert.equal(mod.stopCompleted(), true)
496
+ assert.equal(mod.requestRedirectorStop(), true)
497
+ assert.equal(mod.stopCompleted(), false, 'the request cleared the previous answer')
498
+ assert.equal(existsSync(mod.stopRequestPath()), true)
499
+ mod.clearStopSignals()
500
+ })
501
+
502
+ test('stopping a hand-started bridge is recorded instead of silently killed (B-P2-10)', () => {
503
+ process.env.DSH_RIS_IDE_PROXY_HOME = home
504
+ __test.clearRedirectorPid()
505
+ useHooks({ windowsPids: () => [5555], windowsAlive: () => false, taskkill: () => 'SUCCESS' })
506
+ try {
507
+ const result = __test.stopRedirectorSync({ graceMs: 100 })
508
+ assert.equal(result.stopped, true)
509
+ assert.equal(result.foreign, true, 'the pid came from detection, not from our pid file')
510
+ const log = readFileSync(join(home, 'proxy-forward.log'), 'utf8')
511
+ assert.match(log, /非本工具记录的桥接 pid=5555/)
512
+ } finally {
513
+ useHooks({})
514
+ __test.clearRedirectorPid()
515
+ rmSync(join(home, 'proxy-forward.log'), { force: true })
516
+ }
517
+ })
518
+
469
519
  test('the heartbeat ages out and the stop signals round-trip', async () => {
470
520
  const tmpHome = mkdtempSync(join(tmpdir(), 'hb-'))
471
521
  process.env.DSH_RIS_IDE_PROXY_HOME = tmpHome