import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { createHash } from 'node:crypto'; import { fixture } from './helpers.mjs'; import { desktopLaunchCommand } from '../scripts/desktop-launch.mjs'; import { desktopLaunchOptions } from '../src/core/desktop/launch-options.mjs'; for (const relative of ['node_modules/electron/dist/electron.exe', 'runtime/electron/electron.exe', 'desktop/AgentWorkbench.exe']) { test('desktop acceptance launch parses correctly with ' + relative, t => { const f = fixture(t), root = path.join(f.root, '验收 程序'); const write = file => { const absolute = path.join(root, file); fs.mkdirSync(path.dirname(absolute), { recursive: true }); fs.writeFileSync(absolute, 'static executable fixture'); }; write(relative); write('dist-desktop/main.cjs'); const packaged = relative === 'desktop/AgentWorkbench.exe'; if (packaged) { write('runtime/electron/electron.exe'); write('node_modules/electron/dist/electron.exe'); } const flags = ['--app-root', root, '--data-root', f.data, '--node', process.execPath, '--port', '4319', '--hidden-window']; const original = flags.slice(), command = desktopLaunchCommand(root, flags), mainFile = path.join(root, 'dist-desktop/main.cjs'); assert.equal(command.executablePath, fs.realpathSync.native(path.join(root, relative))); assert.deepEqual(flags, original); const debugArgs = ['--inspect=0', '--remote-debugging-port=0']; assert.deepEqual(desktopLaunchOptions([command.executablePath, ...debugArgs, ...command.args], { defaultApp: !packaged, mainFile }), { '--app-root': root, '--data-root': f.data, '--node': process.execPath, '--port': '4319', hidden: true }); if (packaged) assert.throws(() => desktopLaunchOptions([command.executablePath, ...debugArgs, mainFile, ...flags], { defaultApp: false, mainFile }), /Unsupported desktop option/); else assert.throws(() => desktopLaunchOptions([command.executablePath, ...debugArgs, ...command.args], { defaultApp: true, mainFile: path.join(root, 'other.cjs') }), /Desktop entry does not match/); assert.throws(() => desktopLaunchOptions([command.executablePath, ...debugArgs, ...command.args, '--port', '4320'], { defaultApp: !packaged, mainFile }), /Unsupported desktop option/); }); } test('desktop acceptance launch still verifies the executable against its original manifest', t => { const f = fixture(t), root = path.join(f.root, 'portable'), relative = 'desktop/AgentWorkbench.exe', executable = path.join(root, relative); fs.mkdirSync(path.dirname(executable), { recursive: true }); fs.writeFileSync(executable, 'original executable fixture'); const bytes = fs.readFileSync(executable); fs.writeFileSync(path.join(root, 'MANIFEST.json'), JSON.stringify({ files: [{ path: relative, bytes: bytes.length, sha256: createHash('sha256').update(bytes).digest('hex') }] })); assert.equal(desktopLaunchCommand(root, ['--hidden-window']).executablePath, executable); fs.appendFileSync(executable, 'changed'); assert.throws(() => desktopLaunchCommand(root, ['--hidden-window']), /桌面程序与原发行清单不一致/); });