Files
apes-sidebar/tests/open-external-windows.spec.ts
T
Menghuan1918 f6f7629b23 fix(open-external): Windows「在应用中打开」改为 cmd start → rundll32 候选链,全失败不再静默成功(#412)
现象(#412):Windows 上右键「在应用中打开」经常没反应。核实:`urlCommand` 的 win32 分支
只有 `rundll32.exe url.dll,FileProtocolHandler <url>` 一条候选,而 rundll32.exe 在任何
Windows 上都存在、spawn 必然成功 → 路由回 `{started:true}`,UI 拿不到任何失败信息。

修法:
① `urlCommand` → `urlCommands(...): ExternalCommand[]`(有序候选链):win32 为
   `cmd.exe /d /c start "" <url>` → `rundll32.exe url.dll,FileProtocolHandler <url>`
   (空首参是 start 的窗口标题,缺了它带引号的 URL 会被当成标题、只弹空控制台);
   darwin / linux / android / WSL 仍是单元素,语义逐字不变。
② URL 含 cmd 元字符(& % ^ ! | < > ")或控制字符时**跳过 cmd 分支**直接走 rundll32:
   Node 的 argv 引用拦不住 `cmd /c` 的第二次解析(%VAR% 展开、&/| 截断、^ 吞字符、"
   打乱引用),而 rundll32 以单个 argv 收到 URL、什么都不解释。
③ `launchExternal` 顺序尝试候选:只有 Node 发出 `spawn` 才算成功,同步抛错与 `error`
   事件都推进到下一候选;**全部候选失败**时抛 `SidebarError('internal', …)`,消息里逐个
   列出命令与底层报错(走既有 `{ok:false,error:{code,message}}` 信封)。
④ 调用方一并改形状:`EditorHost.openWith` 返回「是否被接受」(失败仍 console.error 留痕),
   `FileTree` 据此把失败渲染进既有 `openInAppFailed` 错误条——复用词条,未新增词典 key。

测试:
- tests/open-external.spec.ts:候选链顺序、元字符跳过规则、空格 URL 仍是单个 argv、
  失败回退顺序、全失败错误形状(含 `&` URL 只尝试 rundll32、reveal 分支的 path 措辞)。
- tests/open-external-windows.spec.ts(Windows lane,pnpm test:windows):只注入两个
  「不可能存在」的可执行名(`WindowsOpenerOptions` 测试缝),走**真实 spawn** 断言两条
  候选都失败时错误被回报而不是静默成功;**绝不拉起任何应用**(detached:true 会开真窗口,
  所以生产命令名一个都不在测试里出现)。
- tests/file-tree-open-with.spec.tsx / tests/editor-host.spec.tsx:被拒(返回 false)与
  抛错的打开都必须落进错误条,console-only 即红。

判别性(临时还原源码改动后跑新增用例,随后逐字恢复、md5 校验一致):
- 候选链退回单 rundll32(`if (false && …)`)→ 9 red(含 Windows lane 两条)
- spawn error 仍回 `{started:true}`(旧的「从不失败」契约)→ 6 red
- 去掉元字符跳过 → 11 red
- FileTree 不再上报失败 → 3 red
- EditorHost 恢复吞错 → 1 red

限制(写入设计文档):`start` 与 rundll32 在 handler 忽略 URL 时都退出 0,
「已启动但没打开」仍不可从退出码探测。
2026-10-05 22:58:01 +08:00

78 lines
3.7 KiB
TypeScript

/**
* Windows-lane evidence for #412 ("right-click → open in app does nothing").
*
* The reported bug was a SILENT SUCCESS: the single `rundll32.exe` branch
* spawned fine on every Windows box, so the route answered `{ started: true }`
* while nothing opened. This file drives the REAL `spawn` — the only injection
* is the two Windows opener NAMES (a test-only seam, `WindowsOpenerOptions`),
* pointed at executables that cannot exist on any machine. On `ci-windows`
* (`pnpm test:windows`) that exercises the real Windows runtime: Node's own
* ENOENT for the first branch, the fallback to the second branch, and the
* aggregated failure the UI now surfaces instead of silence.
*
* NOTHING IS EVER LAUNCHED — not a browser, not an editor, not a console
* window. That is exactly why the seam exists: spawning the production
* `cmd.exe` / `rundll32.exe` here would start a real handler (`detached: true`
* gives the child its own console window on Windows), which would pop windows
* on the runner or hang the lane. An executable that does not exist cannot
* start anything.
*
* The file is deliberately NOT platform-gated: the same code runs on the
* ubuntu lane and on a dev machine (real spawn, real ENOENT), and on the
* Windows lane it is that same path on the real OS.
*/
import { describe, expect, it } from 'vitest'
import { launchExternal, urlCommands } from '../src/open-external.ts'
import { SidebarError } from '../src/wire.ts'
/** Opener names that cannot exist: nothing on PATH matches, so spawn must fail. */
const MISSING_CMD = 'dsh-missing-opener-412-cmd.exe'
const MISSING_RUNDLL32 = 'dsh-missing-opener-412-rundll32.exe'
/** The seam bag both cases below inject (platform is win32, WSL is off). */
const missingOpeners = { cmdExecutable: MISSING_CMD, rundll32Executable: MISSING_RUNDLL32 }
describe('win32 URL opener chain on the real spawn path', () => {
it('pins the production chain these cases run against: cmd.exe first, rundll32 second', () => {
// The injected names only replace the executables; the SHAPE under test is
// the production one (an unskipped URL goes to `start` first).
expect(urlCommands('vscode://file/C:/a.ts', 'win32')).toEqual([
{ command: 'cmd.exe', args: ['/d', '/c', 'start', '', 'vscode://file/C:/a.ts'] },
{ command: 'rundll32.exe', args: ['url.dll,FileProtocolHandler', 'vscode://file/C:/a.ts'] },
])
})
it('reports both branches failing instead of a silent success', async () => {
const pending = launchExternal('url', 'vscode://file/C:/work/a.ts', {
platform: 'win32',
wsl: false,
commandOptions: missingOpeners,
})
const outcome = await pending.then(() => null, (reason: unknown) => reason)
expect(outcome).toBeInstanceOf(SidebarError)
const failure = outcome as SidebarError
expect(failure.code).toBe('internal')
expect(failure.status).toBe(500)
// Each branch is named WITH the OS's own message — two distinct ENOENTs
// prove both were attempted and both failures survived into the report.
expect(failure.message).toContain(MISSING_CMD)
expect(failure.message).toContain(MISSING_RUNDLL32)
expect(failure.message.match(/ENOENT/g)).toHaveLength(2)
})
it('reports only the rundll32 branch when a cmd metacharacter skips `start`', async () => {
const pending = launchExternal('url', 'myapp://file/a&b.ts', {
platform: 'win32',
wsl: false,
commandOptions: missingOpeners,
})
const outcome = await pending.then(() => null, (reason: unknown) => reason)
expect(outcome).toBeInstanceOf(SidebarError)
const failure = outcome as SidebarError
expect(failure.message).toContain(MISSING_RUNDLL32)
expect(failure.message).not.toContain(MISSING_CMD)
})
})