179 lines
6.9 KiB
JavaScript
179 lines
6.9 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { PassThrough } from 'node:stream';
|
|
import { test } from 'node:test';
|
|
import { startDaemon } from '../src/desktop-daemon.mjs';
|
|
import { StdioCanvas } from '../src/stdio-canvas.mjs';
|
|
|
|
const runId = '9a1ba294-f2c4-4e5f-91e0-ef6002f50e22';
|
|
const tick = () => new Promise(resolve => setImmediate(resolve));
|
|
|
|
function daemonFixture(t, { runSession, parentAlive = () => true } = {}) {
|
|
const input = new PassThrough();
|
|
const output = new PassThrough();
|
|
const frames = [];
|
|
const exits = [];
|
|
output.on('data', bytes => frames.push(JSON.parse(bytes.toString())));
|
|
const daemon = startDaemon({
|
|
input,
|
|
output,
|
|
parentAlive,
|
|
runSession,
|
|
brokerFactory: () => ({ close: async () => {} }),
|
|
exit: code => exits.push(code),
|
|
});
|
|
t.after(() => daemon.stop());
|
|
const send = value => input.write(JSON.stringify(value) + '\n');
|
|
return { input, frames, exits, send };
|
|
}
|
|
|
|
test('a heartbeat gap before dispatch keeps the original turn alive', async t => {
|
|
t.mock.timers.enable({ apis: ['Date', 'setInterval', 'setTimeout'] });
|
|
let resumeStartup;
|
|
const startup = new Promise(resolve => { resumeStartup = resolve; });
|
|
let dispatches = 0;
|
|
const { frames, exits, send } = daemonFixture(t, {
|
|
runSession: async () => {
|
|
await startup;
|
|
dispatches++;
|
|
return { text: '首屏候选已完成', unknown: false };
|
|
},
|
|
});
|
|
|
|
send({ type: 'heartbeat', id: 1 });
|
|
send({ type: 'run', runId, config: {} });
|
|
await tick();
|
|
t.mock.timers.tick(3000);
|
|
t.mock.timers.tick(30);
|
|
await tick();
|
|
assert.deepEqual(exits, [], 'the owner and the pipe are still alive');
|
|
assert.equal(dispatches, 0);
|
|
|
|
send({ type: 'heartbeat', id: 2 });
|
|
resumeStartup();
|
|
await tick();
|
|
assert.equal(dispatches, 1);
|
|
assert.deepEqual(frames.filter(frame => frame.type === 'heartbeat_ack').map(frame => frame.id), [1, 2]);
|
|
assert.equal(frames.filter(frame => frame.type === 'complete').length, 1);
|
|
assert.deepEqual(frames.at(-1), {
|
|
type: 'complete', runId, value: { text: '首屏候选已完成', unknown: false },
|
|
});
|
|
});
|
|
|
|
test('the daemon closes when its owner goes away', async t => {
|
|
t.mock.timers.enable({ apis: ['Date', 'setInterval', 'setTimeout'] });
|
|
let alive = true;
|
|
const { exits } = daemonFixture(t, { parentAlive: () => alive });
|
|
alive = false;
|
|
t.mock.timers.tick(200);
|
|
t.mock.timers.tick(30);
|
|
await tick();
|
|
assert.deepEqual(exits, [0]);
|
|
});
|
|
|
|
test('the daemon closes on EOF even while its owner is alive', async t => {
|
|
const { input, exits } = daemonFixture(t);
|
|
input.end();
|
|
await tick();
|
|
assert.deepEqual(exits, [0]);
|
|
});
|
|
|
|
|
|
test('a production deadline aborts a waiting canvas call and retains original requests', async t => {
|
|
t.mock.timers.enable({ apis: ['Date', 'setTimeout', 'setInterval'], now: 10_000 });
|
|
let entered = 0;
|
|
const { frames, send } = daemonFixture(t, {
|
|
runSession: async (config, call, _broker, _recovery, signal) => {
|
|
entered++;
|
|
await assert.rejects(call('pi-detail-images', { original: 'original-1' }), /达到时限/);
|
|
assert.equal(signal.aborted, true);
|
|
await assert.rejects(call('pi-detail-images', { original: 'original-1' }), /达到时限/);
|
|
return { pending: ['original-1'], unknown: true, reposted: false };
|
|
},
|
|
});
|
|
send({ type: 'run', runId, config: { productProduction: true, productionDeadline: 11 } });
|
|
await tick();
|
|
t.mock.timers.tick(999);
|
|
assert.equal(frames.filter(frame => frame.type === 'complete').length, 0);
|
|
t.mock.timers.tick(1);
|
|
await tick();
|
|
assert.equal(entered, 1);
|
|
assert.equal(frames.filter(frame => frame.type === 'canvas').length, 1);
|
|
assert.deepEqual(frames.at(-1), {
|
|
type: 'complete', runId, value: { pending: ['original-1'], unknown: true, reposted: false },
|
|
});
|
|
});
|
|
|
|
test('a pause on the bound production stops further tools and leaves the original request for recovery', async t => {
|
|
let cancelled = false;
|
|
const config = { productProduction: true, documentId: 27, project: 'desk', scope: 'account-a' };
|
|
const { frames, send } = daemonFixture(t, {
|
|
runSession: async (_, call, _broker, _recovery, signal) => {
|
|
await assert.rejects(call('pi-detail-images', { original: 'original-1' }), {category:'cancelled',dispatched:true});
|
|
cancelled = signal.aborted;
|
|
await assert.rejects(call('pi-detail-images', { original: 'original-1' }), {category:'cancelled',dispatched:false});
|
|
return { pending: ['original-1'], unknown: true, reposted: false };
|
|
},
|
|
});
|
|
send({ type: 'run', runId, config });
|
|
await tick();
|
|
send({ ...config, type: 'production_pause', scope: 'account-b' });
|
|
await tick();
|
|
assert.equal(cancelled, false);
|
|
send({ ...config, type: 'production_pause' });
|
|
await tick();
|
|
assert.equal(cancelled, true);
|
|
assert.equal(frames.filter(frame => frame.type === 'canvas').length, 1);
|
|
assert.deepEqual(frames.at(-1), {
|
|
type: 'complete', runId, value: { pending: ['original-1'], unknown: true, reposted: false },
|
|
});
|
|
});
|
|
|
|
test('presentation notifications do not wait for replies and the terminal command still does', async t => {
|
|
const {frames,send}=daemonFixture(t,{
|
|
runSession:async(_config,call)=>{
|
|
await call('pi-delta',{text:'正在写'});
|
|
await call('pi-activity',{text:'读取画布'});
|
|
await call('pi-end',{text:'写完'});
|
|
return {text:'写完'};
|
|
},
|
|
});
|
|
send({type:'run',runId,config:{}});
|
|
await tick();
|
|
assert.deepEqual(frames.slice(1).map(({type,action})=>[type,action]),[
|
|
['presentation','pi-delta'],['presentation','pi-activity'],['canvas','pi-end'],
|
|
]);
|
|
const {id}=frames.at(-1);
|
|
send({type:'canvas_result',runId,id,value:null});
|
|
await tick();
|
|
assert.equal(frames.at(-1).type,'complete');
|
|
});
|
|
|
|
test('the stdio canvas settles save, cancel and state replies ahead of a pending render', async t => {
|
|
const observed=[];
|
|
const {frames,send}=daemonFixture(t,{
|
|
runSession:async(_config,call)=>{
|
|
const canvas=new StdioCanvas(call,27);
|
|
const screen=canvas.detailRender({page:0}).then(receipt=>observed.push(receipt));
|
|
await Promise.all([
|
|
canvas.nativeCall({method:'engine.execute',revision:7,params:{command:'file.save',params:{}}}),
|
|
canvas.uiCancel('pending-interaction'),
|
|
canvas.read(),
|
|
].map(promise=>promise.then(receipt=>observed.push(receipt))));
|
|
await screen;
|
|
return {};
|
|
},
|
|
});
|
|
send({type:'run',runId,config:{}});
|
|
await tick();
|
|
const requests=frames.filter(frame=>frame.type==='canvas');
|
|
assert.deepEqual(requests.map(frame=>frame.action),['pi-product-screen','pi-native-call','pi-ui-cancel','pi-state']);
|
|
for(const {id,action} of requests.slice(1))send({type:'canvas_result',runId,id,value:action});
|
|
await tick();
|
|
assert.deepEqual(observed,['pi-native-call','pi-ui-cancel','pi-state']);
|
|
assert.equal(frames.some(frame=>frame.type==='complete'),false);
|
|
send({type:'canvas_result',runId,id:requests[0].id,value:{revision:7}});
|
|
await tick();
|
|
assert.deepEqual(observed.at(-1),{revision:7});
|
|
assert.equal(frames.at(-1).type,'complete');
|
|
});
|