phoebus_os/installer/tests/ui/assistant-bridge.test.cjs
Phoebus 3b219fc851
Some checks failed
ci / test (push) Has been cancelled
fix(agents): harden both user-level agent paths
2026-10-12 07:15:31 -07:00

77 lines
4.1 KiB
Bash

// Run with node installer/tests/ui/assistant-bridge.test.cjs.
// Exercise the shipped QML JavaScript against a fake stdio process; no model,
// desktop settings or authentication are involved.
const fs = require('node:fs');
const path = require('node:path');
const vm = require('node:vm');
const assert = require('node:assert/strict');
const test = require('node:test');
const source = fs.readFileSync(path.join(__dirname, '../../live/desktop/phoebus-assistant/AssistantBridge.qml'), 'utf8');
// Separate scanner keeps nested scopes while ignoring string/comment braces.
function body(pattern) {
const m = pattern.exec(source); assert.ok(m);
const start = m.index + m[0].length;
let depth = 1, quote = '', escaped = false, comment = false;
for (let i = start; i < source.length; i++) {
const c = source[i];
if (comment) { if (c === '\n') comment = false; continue; }
if (quote) {
if (escaped) escaped = false;
else if (c === '\\') escaped = true;
else if (c === quote) quote = '';
continue;
}
if (c === '/' && source[i+1] === '/') { comment = true; i++; continue; }
if (c === '"' || c === "'") { quote = c; continue; }
if (c === '{') depth++;
if (c === '}' && --depth === 0) return source.slice(start, i);
}
throw new Error('unterminated handler');
}
function fixture() {
const writes = [], log = [], root = {
ready:false, crashed:false, lastError:'', handshake:null, pending:{},
nextId:1, stoppedByUser:false, restartAttempts:0, handshakeReady() {}
};
const proc = {running:true, write(line) { writes.push(JSON.parse(line)); }};
const restartTimer = {restarts:0, restart() {this.restarts++;}, stop() {}};
const context = vm.createContext({root, proc, restartTimer, console:{log(s) {log.push(s);}}});
for (const name of ['start','stop','call','handleLine','refreshHandshake']) {
const pattern = new RegExp('function '+name+'\\(([^)]*)\\)\\s*\\{');
const m = pattern.exec(source); assert.ok(m);
vm.runInContext('root.'+name+' = function('+m[1]+') {'+body(pattern)+'};', context);
context[name] = (...args) => root[name](...args);
}
vm.runInContext('root.started = function() {'+body(/onStarted:\s*\{/)+ '};',context);
vm.runInContext('root.exited = function(exitCode) {'+body(/onExited:\s*exitCode\s*=>\s*\{/)+ '};',context);
return {root, proc, writes, log, restartTimer};
}
test('ready requires a compatible handshake, and raw protocol errors are not logged', () => {
const f=fixture(); f.root.started(); assert.equal(f.root.ready,false);
f.root.handleLine(JSON.stringify({id:f.writes[0].id,ok:true,result:{protocol_version:999}}));
assert.equal(f.root.ready,false); assert.equal(f.root.crashed,true);
f.root.handleLine('synthetic-private-text'); assert.deepEqual(f.log,[]);
f.root.started();
f.root.handleLine(JSON.stringify({id:f.writes.at(-1).id,ok:true,result:{protocol_version:1}}));
assert.equal(f.root.ready,true);
});
test('progress retains the pending request, final response consumes it once', () => {
const f=fixture(); let progress=0, done=0;
f.root.call('ask',{},()=>done++,()=>progress++);
const id=f.writes[0].id;
f.root.handleLine(JSON.stringify({id,event:'progress',progress:{}}));
assert.equal(progress,1); assert.equal(done,0);
f.root.handleLine(JSON.stringify({id,ok:true,result:{}}));
f.root.handleLine(JSON.stringify({id,ok:true,result:{}}));
assert.equal(done,1);
});
test('exit fails pending work without replay; retries are bounded and stop is deliberate', () => {
const f=fixture(); let failure;
f.root.call('execute',{plan_id:'reviewed'},reply=>failure=reply);
f.root.exited(1); assert.equal(failure.ok,false); assert.match(failure.error,/History/);
assert.deepEqual(Object.keys(f.root.pending),[]); assert.equal(f.writes.length,1);
f.root.exited(1); f.root.exited(1); f.root.exited(1);
assert.equal(f.restartTimer.restarts,3);
f.root.stop(); f.root.exited(0); assert.equal(f.restartTimer.restarts,3);
});