/** Actual embedded Qt Chromium clicks, including restart recovery. */ import assert from 'node:assert/strict'; import {readFile,writeFile} from 'node:fs/promises'; import path from 'node:path'; import {connect} from './dsh_cdp.mjs'; const output=path.resolve(process.argv[2]),phase=process.argv[4]||'initial'; const c=await connect(Number(process.argv[3]||9237)); const manifest=JSON.parse(await readFile(path.join(output,'manifest.json'),'utf8')); const report={phase,checks:[],prompts:[],explicitToolNamesInPrompts:false}; const context=()=>c.evaluate(`fetch('/dx/context').then(r=>r.json())`); async function wait(test,label,timeout=90000){ const end=Date.now()+timeout; while(Date.now()setTimeout(r,160)); } throw Error('Timed out: '+label); } const element=id=>`document.querySelector('[data-testid="${id}"]')`; async function click(id){await wait(()=>c.evaluate(`!!${element(id)}&&!${element(id)}.disabled`),id);await c.click(element(id));} async function tab(id){await click('dx-operations-tab-'+id);} async function idle(){await wait(()=>c.evaluate(`![...document.querySelectorAll('button[aria-label="停止生成"]')].some(e=>e.getBoundingClientRect().width>0)`),'generation idle');} async function send(prompt){ report.prompts.push(prompt); for(let i=0;i<5;i++){ await c.click(`document.querySelector('[contenteditable="true"]')`); await c.call('Input.dispatchKeyEvent',{type:'keyDown',key:'a',code:'KeyA',modifiers:2}); await c.call('Input.dispatchKeyEvent',{type:'keyUp',key:'a',code:'KeyA',modifiers:2}); await c.call('Input.insertText',{text:prompt});await new Promise(r=>setTimeout(r,350)); if(await c.evaluate(`document.querySelector('[contenteditable="true"]')?.textContent.includes(${JSON.stringify(prompt)})`))break; } await wait(()=>c.evaluate(`(()=>{const e=document.querySelector('button[aria-label="发送消息"]');return e&&!e.disabled&&e.getAttribute('aria-disabled')!=='true';})()`),'send enabled'); await c.click(`document.querySelector('button[aria-label="发送消息"]')`); await wait(()=>c.evaluate(`!document.querySelector('[contenteditable="true"]')?.textContent.includes(${JSON.stringify(prompt)})&&document.body.innerText.includes(${JSON.stringify(prompt)})`),'prompt submitted'); } async function select(name){ const nonce=String(Date.now()); await writeFile(path.join(output,'native-selection.json'),JSON.stringify({nonce,task_id:manifest.tasks[name]})); await wait(async()=>{try{return JSON.parse(await readFile(path.join(output,'native-selection-done.json'),'utf8')).nonce===nonce;}catch{return false;}},'native select '+name); await wait(async()=>(await context()).task_id===manifest.tasks[name],'selected context '+name); await tab('overview'); await wait(()=>c.evaluate(`document.querySelector('[data-testid="dx-task-overview"] > section:first-child > div')?.textContent===${JSON.stringify(name)}`),'dock reflects native selection '+name); } async function input(id,text){ await click(id); await c.evaluate(`${element(id)}.focus()`); assert.equal(await c.evaluate(`document.activeElement?.getAttribute('data-testid')`),id,'number field has keyboard focus'); await c.call('Input.dispatchKeyEvent',{type:'rawKeyDown',key:'a',code:'KeyA',windowsVirtualKeyCode:65,nativeVirtualKeyCode:65,modifiers:2,commands:['selectAll']}); await c.call('Input.dispatchKeyEvent',{type:'keyUp',key:'a',code:'KeyA',windowsVirtualKeyCode:65,nativeVirtualKeyCode:65,modifiers:2}); await c.call('Input.insertText',{text}); assert.equal(await c.evaluate(`${element(id)}.value`),text,'actual keyboard input replaced field contents'); } try{ await wait(()=>c.evaluate(`!!document.querySelector('[contenteditable="true"]')&&!!${element('dx-operations-tab-running')}`),'composer and operations dock',180000); if(phase==='initial'){ await send('只读检查当前 Board-A 任务的执行影响、已有执行历史和本地异常经验。说明匹配器件数量是否已知,暂时不要执行或修改任务。'); await wait(async()=>(await context()).operations?.impact?.task_ids?.includes(manifest.tasks['Board-A'])||(await context()).operations?.impact?.coverage==='mapping_only','real-model impact response');await idle(); await tab('impact');await click('dx-impact-refresh'); await wait(async()=>(await context()).operations?.impact?.coverage==='mapping_only','mapping impact'); assert.equal((await context()).operations.impact.component_count,null); await c.screenshot(path.join(output,'mapping-impact.png'));report.checks.push('natural-language read + actual mapping impact tab preserves unknown component counts'); await select('Board-B');await tab('impact');await click('dx-impact-refresh'); await wait(async()=>(await context()).operations?.impact?.coverage==='exact','exact impact'); const exact=(await context()).operations.impact;assert.equal(exact.component_count,2);assert.equal(exact.rows[0].uid,'accept-uid-1'); await c.screenshot(path.join(output,'exact-impact.png'));report.checks.push('exact saved targets display RefDes, UID and Value/Cell/NC policies'); await tab('experiences');await click('dx-experience-search'); await wait(()=>c.evaluate(`[...document.querySelectorAll('[data-testid="dx-experience-verification"]')].some(x=>x.textContent==='已验证')&&[...document.querySelectorAll('[data-testid="dx-experience-verification"]')].some(x=>x.textContent==='未验证')`),'verified/unverified experience badges'); await c.screenshot(path.join(output,'experience-badges.png'));report.checks.push('persisted verified evidence and evidence-less suggestion retain distinct badges'); await tab('tickets');await click('dx-tickets-refresh'); await new Promise(r=>setTimeout(r,1800)); const tickets=(await context()).operations?.tickets||[]; if(tickets.length){await click('dx-ticket-sync-'+tickets[0].incident_id);await wait(async()=>!!(await context()).operations?.tickets?.find(x=>x.incident_id===tickets[0].incident_id)?.last_attempt_at,'read-only real ticket refresh',60000);report.ticket=(await context()).operations.tickets[0];} else report.ticket={status:'unavailable',reason:'No existing ticket binding available; no remote issue created'}; await c.screenshot(path.join(output,'tickets.png')); await tab('usage');await input('dx-response-timeout','120');await input('dx-context-limit','12000');await click('dx-usage-configure'); await wait(()=>c.evaluate(`fetch('/dx/runtime').then(r=>r.json()).then(x=>x.settings.response_timeout_seconds===120&&x.settings.model_context_limit===12000)`),'usage settings persisted'); report.checks.push('actual numeric input and save configure response timeout and context limit'); await select('Board-A'); await send('现在执行 Board-A 任务,只运行这一个工程,按原生确认的范围处理。结束后告诉我真实保存、失败和未命中数量。'); await wait(async()=>(await context()).operations?.live_run?.active===true,'live controlled run'); await tab('running');await click('dx-running-pause'); await wait(async()=>{const d=await context();return d.operations?.last_execution?.status==='paused'&&!d.operations?.live_run?.active;},'safe pause receipt'); const receipt=(await context()).operations.last_execution;assert.equal(receipt.tasks[0].no_match_items,1);assert.equal(receipt.tasks[0].saved_items,0); report.runId=receipt.run_id;await idle();await c.screenshot(path.join(output,'paused-before-restart.png')); report.checks.push('actual running-tab pause waited for controlled first item and retained original queue'); }else{ const prior=JSON.parse(await readFile(path.join(output,'paused-run.json'),'utf8')); await tab('running');await click('dx-history-refresh'); await wait(async()=>{const d=await context();return d.operations?.activity?.status==='read'&&!d.operations?.busy;},'history read settled'); await click('dx-resume-'+prior.run_id); await wait(async()=>(await context()).operations?.last_execution?.status==='completed','restarted original queue completed'); const result=(await context()).operations.last_execution;assert.equal(result.run_id,prior.run_id);assert.equal(result.queue_id,prior.queue_id);assert.equal(result.tasks[0].no_match_items,2); report.checks.push('second actual application process resumed original frozen queue and skipped completed item'); await select('Board-C');await tab('schedules');await click('dx-schedule-new'); await wait(async()=>(await context()).operations?.activity?.status==='cancelled','native schedule cancellation'); assert.equal((await context()).operations.schedules.length,0); await click('dx-schedule-new');await wait(async()=>(await context()).operations?.schedules?.length===1,'natively authorized schedule saved'); await wait(async()=>(await context()).operations?.schedules?.[0]?.status==='completed','automatic due schedule completed',70000); const schedule=(await context()).operations.schedules[0];assert.equal(schedule.auto_start,true);report.schedule=schedule; await new Promise(r=>setTimeout(r,17000));await click('dx-schedules-refresh'); await wait(async()=>(await context()).operations?.schedules?.[0]?.status==='completed','schedule remains terminal'); report.checks.push('cancel saved nothing; explicit future-run authorization executed due mapping scope exactly once'); await tab('usage'); assert.deepEqual(await c.evaluate(`fetch('/dx/runtime').then(r=>r.json()).then(x=>x.settings)`),{response_timeout_seconds:120,model_context_limit:12000}); report.checks.push('usage configuration survived complete application restart'); await tab('tickets');await click('dx-tickets-refresh'); await wait(async()=>(await context()).operations?.activity?.status==='read','restarted ticket list'); const tickets=(await context()).operations?.tickets||[]; if(tickets.length){ await click('dx-ticket-sync-'+tickets[0].incident_id); await wait(async()=>(await context()).operations?.tickets?.some(x=>x.incident_id===tickets[0].incident_id&&x.last_attempt_at),'existing real ticket GET',60000); report.ticket=(await context()).operations.tickets.find(x=>x.incident_id===tickets[0].incident_id); assert.equal(report.ticket.status,'synced'); report.checks.push('existing bound real ticket synchronized through read-only GET; no issue or comment created'); }else report.ticket={status:'unavailable',reason:'No existing bound ticket available'}; await c.screenshot(path.join(output,'after-restart-operations.png')); } report.status='passed'; }catch(error){report.status='failed';report.error=error.message;await c.screenshot(path.join(output,phase+'-failure.png')).catch(()=>{});throw error;} finally{await writeFile(path.join(output,phase+'-web.json'),JSON.stringify(report,null,2));c.close();}