Files
pipelines/app/tool/chat_background_tasks_modal_probe.mjs
T

78 lines
5.1 KiB
JavaScript

// Real composer badge/modal interaction, using recorded loopback task fixtures.
import assert from 'node:assert/strict';
import { mkdir, mkdtemp, writeFile } from 'node:fs/promises';
import { once } from 'node:events';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { startFixture } from './runs_probe_server.mjs';
import { launchChrome, waitForPort, waitForPage, connect, sleep } from './pipelines_probe_cdp_lib.mjs';
import { axNodes, enableSemantics, waitFor, capture, click } from './pipelines_probe_cdp_input.mjs';
const platform = process.env.PROBE_PLATFORM ?? 'material';
const width = Number(process.env.PROBE_WIDTH ?? 1280);
const output = resolve(process.argv[3] ?? `build/background-task-modal-${platform}`);
await mkdir(output, { recursive: true });
const fixture = await startFixture({ root:resolve(process.argv[2]), chatMessages: [], chatShells: [
{ id:'a', status:'running', command:'tail -f fixture.log' },
{ id:'b', status:'running', command:'fixture build' },
{ id:'finished', status:'exited', command:'old finished command' },
], chatShellOutput: Array.from({length:200},(_,i)=>`Recorded output line ${i}`).join('\n') });
const profile = await mkdtemp(join(tmpdir(),'background-task-modal-'));
const chrome = launchChrome('/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',profile,'about:blank');
const log = {platform,width,checks:[],captures:[]};
let c;
const names = async () => (await axNodes(c)).flatMap(n=>[n.name?.value??'',n.value?.value??'']);
const node = async name => (await axNodes(c)).find(n=>n.name?.value===name&&n.backendDOMNodeId);
const emit = body => fetch(fixture.url+'/fixture/chat-event',{method:'POST',body:JSON.stringify(body)});
try {
c = await connect((await waitForPage(await waitForPort(join(profile,'DevToolsActivePort')))).webSocketDebuggerUrl);
await c.send('Page.enable');
if(platform==='material') await c.send('Emulation.setUserAgentOverride',{
userAgent:'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Safari/537.36',platform:'Win32',
});
await c.send('Emulation.setDeviceMetricsOverride',{width,height:844,deviceScaleFactor:1,mobile:false});
await c.send('Page.navigate',{url:fixture.url+'/chat/ses_probechat'});
assert.ok(await waitFor(()=>c.evaluate("Boolean(document.querySelector('flt-semantics-placeholder'))"),30000));
await enableSemantics(c);
const badge=await waitFor(()=>node('Background tasks, 2 tasks'),15000);
assert.ok(badge,'live count badge');
const send=await node('Send');assert.ok(send,'Send control');
const {model:b}=await c.send('DOM.getBoxModel',{backendNodeId:badge.backendDOMNodeId});
const {model:s}=await c.send('DOM.getBoxModel',{backendNodeId:send.backendDOMNodeId});
assert.ok(b.content[5]<=s.content[1]+1,'task badge above Send');
assert.ok(!(await names()).some(name=>name.includes('tail -f fixture.log')),'no tasks in the transcript');
await capture(c,output,log,'01-badge-above-send');
log.checks.push('running count above Send; task list absent from chat body');
await click(c,'Background tasks, 2 tasks');
assert.ok(await waitFor(async()=>(await names()).some(name=>name.includes('tail -f fixture.log')),10000));
assert.ok(!(await names()).some(name=>name.includes('old finished command')));
let state=await (await fetch(fixture.url+'/fixture/state')).json();
assert.equal(state.shellOutputRequests.length,0,'no eager output read');
await click(c,'tail -f fixture.log');
assert.ok(await waitFor(async()=>(await names()).some(name=>name.includes('Recorded output line 0')),10000));
state=await (await fetch(fixture.url+'/fixture/state')).json();
assert.deepEqual(state.shellOutputRequests[0],{session_id:'ses_probechat',shell_id:'a',cursor:0,limit:65536});
await capture(c,output,log,'02-task-output-in-modal');
log.checks.push('native modal exposes tasks and lazy bounded output');
await emit({kind:'shell',shell:{id:'a',status:'exited'}});
assert.ok(await waitFor(async()=>(await names()).includes('1 running'),10000));
assert.ok(!(await names()).some(name=>name.includes('Recorded output line 0')),'exited task output removed');
await capture(c,output,log,'03-live-task-exit');
await click(c,'Done');
assert.ok(await waitFor(()=>node('Background tasks, 1 task'),10000));
await emit({kind:'shell',shell:{id:'b',status:'exited'}});
assert.ok(await waitFor(()=>node('Background tasks, 0 tasks'),10000));
await click(c,'Background tasks, 0 tasks');
assert.ok(await waitFor(async()=>(await names()).includes('No background tasks running.'),10000));
await capture(c,output,log,'04-empty-modal');
await click(c,'Done');
log.checks.push('modal updates live on exits; dismissal restores chat; zero count opens honest empty state');
console.log(JSON.stringify(log,null,2));
} catch(error) {
if(c) {await capture(c,output,log,'failure');await writeFile(join(output,'failure-ax.json'),JSON.stringify(await axNodes(c),null,2));}
throw error;
} finally {
await writeFile(join(output,'result.json'),JSON.stringify(log,null,2));
c?.close();const exited=once(chrome,'exit');chrome.kill();await exited;await fixture.close();
}