test(desktop): make the dev launcher tests install-independent

The resolveElectronExecutable case asserted against the real
node_modules/electron/dist, which bun never downloads without the
electron postinstall, so it failed on macOS/Linux dev machines.
Point the tests at a self-built fixture desktop root instead and
extend coverage to the darwin/linux candidates and the missing-
executable error path.

Follow-up to #1096.
This commit is contained in:
程序员阿江(Relakkes) 2026-07-25 16:23:39 +08:00
parent 6b7e90aec7
commit 441979aa2d

View File

@ -1,7 +1,23 @@
import { mkdirSync, mkdtempSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import path from 'node:path' import path from 'node:path'
import { describe, expect, it } from 'vitest' import { describe, expect, it } from 'vitest'
import { createElectronDevEnv, DEFAULT_RENDERER_URL, mergeNoProxy, resolveElectronExecutable } from './electron-dev' import { createElectronDevEnv, DEFAULT_RENDERER_URL, mergeNoProxy, resolveElectronExecutable } from './electron-dev'
// resolveElectronExecutable 依赖 node_modules/electron/dist 真实存在,
// 但 bun 默认不执行 electron 的 postinstall 下载,dist 在本机/CI 经常缺席。
// 测试用自带夹具 desktopRoot,不依赖环境安装状态。
function createDesktopRootFixture(withExecutables = true) {
const root = mkdtempSync(path.join(tmpdir(), 'cc-haha-dev-launcher-'))
if (!withExecutables) return root
const dist = path.join(root, 'node_modules', 'electron', 'dist')
mkdirSync(path.join(dist, 'Electron.app', 'Contents', 'MacOS'), { recursive: true })
writeFileSync(path.join(dist, 'electron.exe'), '')
writeFileSync(path.join(dist, 'electron'), '')
writeFileSync(path.join(dist, 'Electron.app', 'Contents', 'MacOS', 'Electron'), '')
return root
}
describe('desktop dev launcher environment', () => { describe('desktop dev launcher environment', () => {
it('uses localhost and bypasses proxies for local renderer startup', () => { it('uses localhost and bypasses proxies for local renderer startup', () => {
const env = createElectronDevEnv({ const env = createElectronDevEnv({
@ -29,7 +45,15 @@ describe('desktop dev launcher environment', () => {
expect(mergeNoProxy('localhost,127.0.0.1')).toBe('localhost,127.0.0.1,::1') expect(mergeNoProxy('localhost,127.0.0.1')).toBe('localhost,127.0.0.1,::1')
}) })
it('resolves Electron directly instead of shelling through bunx', () => { it.each([
expect(resolveElectronExecutable(path.resolve(import.meta.dirname, '..'), 'win32')).toMatch(/electron\.exe$/) ['win32', /electron\.exe$/],
['darwin', /Electron\.app[\\/]Contents[\\/]MacOS[\\/]Electron$/],
['linux', /dist[\\/]electron$/],
] as const)('resolves the %s Electron executable directly instead of shelling through bunx', (platform, pattern) => {
expect(resolveElectronExecutable(createDesktopRootFixture(), platform)).toMatch(pattern)
})
it('throws an actionable error when the Electron executable is missing', () => {
expect(() => resolveElectronExecutable(createDesktopRootFixture(false), 'linux')).toThrow(/Electron executable not found/)
}) })
}) })