95 lines
8.3 KiB
JavaScript
95 lines
8.3 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { randomUUID, createHash } from 'node:crypto';
|
|
import { fixture } from './helpers.mjs';
|
|
import { DesktopBridge } from '../src/core/desktop/desktop-bridge.mjs';
|
|
import { desktopInput } from '../src/shared/contracts/desktop-bridge.mjs';
|
|
import { readText, listFiles } from '../src/server/files.mjs';
|
|
import { HostAuthority } from '../src/core/desktop/host-authority.mjs';
|
|
import { desktopLaunchOptions } from '../src/core/desktop/launch-options.mjs';
|
|
|
|
test('actual Electron debug argument ordering retains exactly the compiled entry and rejects extra or repeated actions', () => {
|
|
const mainFile = path.resolve('dist-desktop/main.cjs'), argv = ['electron.exe', '--inspect=0', '--remote-debugging-port=0', mainFile, '--port', '4319', '--hidden-window'];
|
|
assert.deepEqual(desktopLaunchOptions(argv, { defaultApp: true, mainFile }), { '--port': '4319', hidden: true });
|
|
assert.deepEqual(desktopLaunchOptions(['product.exe', '--port', '4319'], { defaultApp: false }), { '--port': '4319' });
|
|
assert.throws(() => desktopLaunchOptions(argv, { defaultApp: true, mainFile: path.resolve('different.cjs') }));
|
|
assert.throws(() => desktopLaunchOptions([...argv, '--port', '4320'], { defaultApp: true, mainFile }));
|
|
assert.throws(() => desktopLaunchOptions([...argv, '--command', 'arbitrary'], { defaultApp: true, mainFile }));
|
|
});
|
|
|
|
function setup(t) {
|
|
const f = fixture(t), frame = { url: 'http://127.0.0.1:4319/' }, contents = { mainFrame: frame };
|
|
const window = { webContents: contents, isDestroyed: () => false }, event = { sender: contents, senderFrame: frame };
|
|
const scope = { projectId: f.project.id, sessionId: f.session.id, taskId: randomUUID() }, calls = [];
|
|
const saved = { id: f.session.id, projectId: f.project.id, execution: { task: { id: scope.taskId } }, eventSeq: 19 };
|
|
const identity = { instanceId: randomUUID(), runtimeIdentity: 'original' };
|
|
const controller = { url: 'http://127.0.0.1:4319', connect: async () => ({ identity }), status: async () => ({ identity }),
|
|
api: async (route, options) => { calls.push({ route, options }); if (route === '/bootstrap') return { projects: [f.project] }; if (route.startsWith('/sessions/')) return saved; return f.project; },
|
|
stop: async () => { calls.push({ stop: true }); return { processExited: true }; } };
|
|
const shell = { openPath: async value => { calls.push({ folder: value }); return ''; }, showItemInFolder: value => calls.push({ reveal: value }) };
|
|
const dialog = { showOpenDialog: async () => ({ canceled: true, filePaths: [] }), showMessageBox: async () => ({ response: 0 }) };
|
|
const bridge = new DesktopBridge(controller, { shell, dialog, getWindow: () => window });
|
|
return { ...f, frame, contents, window, event, scope, calls, saved, identity, controller, shell, dialog, bridge };
|
|
}
|
|
|
|
test('native bridge refuses other origins, subframes, other windows and unsupported inputs before connecting', async t => {
|
|
const f = setup(t);
|
|
for (const event of [{ ...f.event, sender: {} }, { ...f.event, senderFrame: { url: f.frame.url } }]) await assert.rejects(f.bridge.execute(event, 'pickProject'));
|
|
f.frame.url = 'https://example.invalid/'; await assert.rejects(f.bridge.execute(f.event, 'status')); assert.equal(f.calls.length, 0);
|
|
for (const input of [{ projectId: f.project.id, command: 'arbitrary' }, { projectId: '../project' }, ['project']]) assert.throws(() => desktopInput('projectFolder', input));
|
|
assert.throws(() => desktopInput('arbitrary', {}));
|
|
});
|
|
|
|
test('file reveal binds the current project, task, path, UTF-8 bytes and original SHA without executing the file', async t => {
|
|
const f = setup(t), file = readText(f.project, 'README.md'), input = { ...f.scope, path: file.path, sha256: file.sha256 };
|
|
await f.bridge.execute(f.event, 'revealFile', input); assert.equal(f.calls.filter(value => value.reveal).length, 1); assert.equal(f.calls.filter(value => value.folder).length, 0);
|
|
for (const changed of [{ taskId: randomUUID() }, { projectId: randomUUID() }, { sha256: '0'.repeat(64) }, { path: '../outside' }, { path: 'C:\\outside' }]) await assert.rejects(f.bridge.execute(f.event, 'revealFile', { ...input, ...changed }));
|
|
assert.equal(f.calls.filter(value => value.reveal).length, 1);
|
|
const invalid = Buffer.from([255, 254, 255]); fs.writeFileSync(path.join(f.projectRoot, 'bad.txt'), invalid);
|
|
await assert.rejects(f.bridge.execute(f.event, 'revealFile', { ...input, path: 'bad.txt', sha256: createHash('sha256').update(invalid).digest('hex') }));
|
|
});
|
|
|
|
test('file changed during asynchronous task recheck is not revealed as the old bytes', async t => {
|
|
const f = setup(t), file = readText(f.project, 'README.md'), original = f.controller.api; let requests = 0;
|
|
f.controller.api = async (...args) => { if (args[0].startsWith('/sessions/') && ++requests === 2) fs.appendFileSync(path.join(f.projectRoot, file.path), 'changed during original check'); return original(...args); };
|
|
await assert.rejects(f.bridge.execute(f.event, 'revealFile', { ...f.scope, path: file.path, sha256: file.sha256 }), /变化/);
|
|
assert.equal(f.calls.filter(value => value.reveal).length, 0);
|
|
});
|
|
|
|
test('desktop browser profile and aliases stay outside project inventory and file access', async t => {
|
|
const f = setup(t), profile = path.join(f.projectRoot, '.desktop-profile'); fs.mkdirSync(profile); fs.writeFileSync(path.join(profile, 'Cookies'), 'owned private profile');
|
|
fs.symlinkSync(profile, path.join(f.projectRoot, 'profile-alias'), 'junction');
|
|
assert.throws(() => readText(f.project, '.desktop-profile/Cookies')); assert.throws(() => readText(f.project, 'profile-alias/Cookies'));
|
|
assert.equal(listFiles(f.project).files.some(value => /profile|Cookies/.test(value.path)), false);
|
|
});
|
|
|
|
test('native picker cancel and origin change after dialog publish no project registration', async t => {
|
|
const f = setup(t); assert.equal(await f.bridge.execute(f.event, 'pickProject'), null);
|
|
f.dialog.showOpenDialog = async () => { f.frame.url = 'http://127.0.0.1:4320/'; return { canceled: false, filePaths: [f.projectRoot] }; };
|
|
await assert.rejects(f.bridge.execute(f.event, 'pickProject')); assert.equal(f.calls.some(value => value.route === '/projects'), false);
|
|
});
|
|
|
|
test('pending native action is not dispatched twice, and current task review returns its original seq', async t => {
|
|
const f = setup(t); let finish; f.dialog.showOpenDialog = () => new Promise(resolve => { finish = resolve; });
|
|
const first = f.bridge.execute(f.event, 'pickProject'); await new Promise(resolve => setImmediate(resolve));
|
|
await assert.rejects(f.bridge.execute(f.event, 'pickProject'), /正在处理/); finish({ canceled: true, filePaths: [] }); await first;
|
|
assert.deepEqual(await f.bridge.execute(f.event, 'reviewTask', f.scope), { ...f.scope, eventSeq: 19 });
|
|
});
|
|
|
|
test('native stop cancellation and changed original host never stop a replacement process', async t => {
|
|
const f = setup(t); assert.deepEqual(await f.bridge.execute(f.event, 'stopHost'), { cancelled: true }); assert.equal(f.calls.some(value => value.stop), false);
|
|
f.dialog.showMessageBox = async () => ({ response: 1 }); let connects = 0;
|
|
f.controller.connect = async () => ({ identity: ++connects === 1 ? f.identity : { ...f.identity, runtimeIdentity: 'replacement' } });
|
|
await assert.rejects(f.bridge.execute(f.event, 'stopHost'), /已变化/); assert.equal(f.calls.some(value => value.stop), false);
|
|
f.controller.connect = async () => ({ identity: f.identity }); assert.equal((await f.bridge.execute(f.event, 'stopHost')).processExited, true); assert.equal(f.calls.filter(value => value.stop).length, 1);
|
|
});
|
|
|
|
test('window register rejects host PID, stale installation, extra fields and missing creation identity', () => {
|
|
const authority = new HostAuthority({}), identity = { pid: process.pid, instanceId: randomUUID(), runtimeIdentity: 'original' }; authority.identity = () => identity;
|
|
const input = { windowId: randomUUID(), pid: 1234, createdAt: '2026-10-31T00:00:00.000Z', instanceId: identity.instanceId, runtimeIdentity: identity.runtimeIdentity };
|
|
assert.equal(authority.assertWindowInput(input), identity);
|
|
for (const changed of [{ pid: process.pid }, { createdAt: '' }, { runtimeIdentity: 'replacement' }, { program: 'foreign' }]) assert.throws(() => authority.assertWindowInput({ ...input, ...changed }));
|
|
});
|