import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { setTimeout as delay } from 'node:timers/promises'; import { fixture, workspace } from './helpers.mjs'; import { HostController } from '../src/core/desktop/host-controller.mjs'; import { observeWindowsProcess } from '../src/core/desktop/process-identity.mjs'; function ownedStartup(t, timeoutMs = 10000) { const f = fixture(t), appRoot = path.join(f.root, 'owned-startup-app'), dataRoot = path.join(f.root, 'owned-startup-data'); for (const directory of ['dist-client', 'dist-server', 'dist-desktop']) fs.cpSync(path.join(workspace, directory), path.join(appRoot, directory), { recursive: true }); fs.copyFileSync(path.join(workspace, 'package.json'), path.join(appRoot, 'package.json')); const source = "import fs from 'node:fs';import path from 'node:path';const stop=path.join(process.env.AGENT_WORKBENCH_DATA_DIR,'exit-owned-startup');setInterval(()=>{if(fs.existsSync(stop))process.exit(0)},25);"; fs.writeFileSync(path.join(appRoot, 'dist-server/server.mjs'), source); const controller = new HostController({ appRoot, dataRoot, nodePath: process.execPath, port: 49997, timeoutMs }); const intent = () => JSON.parse(fs.readFileSync(path.join(dataRoot, 'desktop-launch-state.json'), 'utf8')); f.onCleanup(async () => { fs.writeFileSync(path.join(dataRoot, 'exit-owned-startup'), 'exit through own fixture'); const pid = intent().pid, deadline = Date.now() + 10000; while (observeWindowsProcess(pid)) { assert.ok(Date.now() < deadline, 'Owned fixture must exit through its own sentinel.'); await delay(50); } }); return { controller, intent }; } test('owned startup retries only read-only observation after a slow health response and attests the same original child', { timeout: 30000 }, async t => { const { controller, intent } = ownedStartup(t); let connects = 0, observations = 0; controller.connect = async () => ++connects === 1 ? null : { identity: { pid: intent().pid, instanceId: 'owned-attested-result' } }; controller.health = async () => { if (++observations === 1) throw Object.assign(new Error('controlled registration delay'), { code: 'HOST_PORT_UNVERIFIED' }); return { pid: intent().pid }; }; const result = await controller.start(); assert.equal(result.reused, false); assert.equal(connects, 2); assert.equal(observations, 2); assert.equal(result.identity.pid, intent().pid); assert.equal(intent().state, 'ready'); assert.equal(observeWindowsProcess(intent().pid).createdAt, intent().createdAt); }); test('unverified startup expires with its original live child and never treats timeout or an unrelated health PID as proof', { timeout: 30000 }, async t => { const { controller, intent } = ownedStartup(t, 1000); let connects = 0; controller.connect = async () => { connects++; return null; }; controller.health = async () => { throw Object.assign(new Error('controlled unknown response'), { code: 'HOST_PORT_UNVERIFIED' }); }; await assert.rejects(controller.start(), error => error.code === 'HOST_START_UNVERIFIED'); const original = intent(); assert.equal(connects, 1); assert.equal(original.state, 'starting'); assert.equal(observeWindowsProcess(original.pid).createdAt, original.createdAt); controller.health = async () => ({ pid: original.pid + 1 }); // The original unresolved launch prevents a second spawn, even after timeout. await assert.rejects(controller.start(), error => error.code === 'HOST_START_UNVERIFIED'); assert.equal(intent().pid, original.pid); });