/** 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 nativeScheduleStep(test,label){ report.scheduleClicks??=[]; for(let attempt=1;attempt<=3;attempt++){ await wait(async()=>!(await context()).operations?.busy,'native operations settled'); await click('dx-schedule-new'); try{ await wait(test,label,5000);report.scheduleClicks.push({label,attempt,accepted:true});return; }catch(error){ if(!error.message.startsWith('Timed out:'))throw error; await wait(async()=>!(await context()).operations?.busy,'schedule request settled'); if(await test()){report.scheduleClicks.push({label,attempt,accepted:true});return;} const notice=await c.evaluate(`document.querySelector('[data-testid="dx-operations-notice"]')?.textContent||''`); report.scheduleClicks.push({label,attempt,accepted:false,notice}); } } throw Error('Native schedule clicks were not admitted: '+label); } async function pauseLiveRun(){ const original=(await context()).operations.live_run; report.pauseClicks=[]; for(let attempt=0;attempt<3;attempt++){ const live=(await context()).operations.live_run; assert.equal(live.run_id,original.run_id,'pause may not retarget a different execution'); assert.equal(live.active,true,'the original execution must still be active'); await tab('running');await click('dx-running-pause'); try{ await wait(async()=>{const ops=(await context()).operations;return (ops.live_run?.run_id===original.run_id&&ops.live_run.pause_requested)|| (ops.last_execution?.run_id===original.run_id&&ops.last_execution.status==='paused');},'safe pause admission',5000); report.pauseClicks.push({attempt:attempt+1,accepted:true});return; }catch(error){ if(!error.message.startsWith('Timed out:'))throw error; const notice=await c.evaluate(`document.querySelector('[data-testid="dx-operations-notice"]')?.textContent||''`); report.pauseClicks.push({attempt:attempt+1,accepted:false,notice}); } } throw Error('Actual pause clicks were not admitted: '+JSON.stringify(report.pauseClicks)); } 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); const auditFault=await readFile(path.join(output,'audit-fault.json'),'utf8').then(JSON.parse).catch(()=>null); if(auditFault){ await tab('overview'); await click('dx-health-refresh'); await wait(async()=>{const d=await context();return d.operations?.decision_audit?.status==='corrupt'&&d.operations?.health?.checks?.length>0&&!d.operations?.busy;},'independent health survives audit corruption'); await click('dx-decisions-refresh'); await wait(()=>c.evaluate(`!!${element('dx-decision-audit-error')}&&${element('dx-decision-audit-error')}.textContent.includes('原文件已保留')`),'explicit audit error banner'); await c.screenshot(path.join(output,phase+'-audit-isolation.png')); report.checks.push('corrupt audit remains explicit while native health and independent operations refresh successfully'); } if(phase==='initial'){ await tab('schedules');await click('dx-schedules-refresh'); const orphanId=manifest.orphanSchedule; await click('dx-schedule-reconcile-'+orphanId); await wait(async()=>(await context()).operations?.activity?.status==='cancelled','orphan reconciliation native cancel'); assert.equal((await context()).operations.schedules.find(row=>row.schedule_id===orphanId).status,'claimed'); await click('dx-schedule-reconcile-'+orphanId); await wait(async()=>(await context()).operations?.schedules?.find(row=>row.schedule_id===orphanId)?.status==='interrupted','orphan reconciliation native confirmation'); await wait(async()=>(await context()).operations?.schedule_events?.some(row=>row.schedule_id===orphanId&&row.payload?.reconciled),'orphan reconciliation audit visible'); await click('dx-schedules-refresh'); await wait(async()=>((await context()).operations?.schedule_audit?.archived_events||0)>0,'lossless archive status'); await wait(()=>c.evaluate(`document.body.innerText.includes('已归档')&&document.body.innerText.includes('旧记录完整保留')`),'archive status rendered'); report.scheduleAudit=(await context()).operations.schedule_audit; await c.screenshot(path.join(output,'orphan-reconciled-and-archive.png')); report.checks.push('actual native cancel preserves claimed orphan; native confirm reconciles without execution; archive counts visible'); 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'); const beforeExecute=(await context()).lifecycle?.request_id; await send('现在执行 Board-A 任务,只运行这一个工程,按原生确认的范围处理。结束后告诉我真实保存、失败和未命中数量。'); let clarified=false; await wait(async()=>{ const data=await context(); if(data.operations?.live_run?.active===true)return true; const generating=await c.evaluate(`[...document.querySelectorAll('button[aria-label="停止生成"]')].some(e=>e.getBoundingClientRect().width>0)`); if(!clarified&&!generating&&data.lifecycle?.status==='preflight'&&data.lifecycle.request_id!==beforeExecute){ clarified=true; // Simulate a human answering the model's optional-baseline question. // The actual task still has to pass native preflight/confirmation. await send('继续立即执行 Board-A,仅此一个工程,并在原生窗口中确认。可选探测基线缺失请如实报告;按任务中心实际门禁处理,结束后只报告真实计数。'); } return false; },'live controlled run after natural-language clarification',120000); assert.equal((await context()).operations.health.checks.find(row=>row.name==='execution')?.code,'execution_running'); await pauseLiveRun(); 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'); report.resumeClicks=[]; for(let attempt=0;attempt<3;attempt++){ await click('dx-resume-'+prior.run_id); try{ await wait(async()=>{const d=await context();return (d.lifecycle?.request_id&&['awaiting_confirmation','executing','completed'].includes(d.lifecycle.status))|| d.operations?.last_execution?.run_id===prior.run_id;},'original queue resume admission',4000); report.resumeClicks.push({attempt:attempt+1,accepted:true});break; }catch(error){ if(!error.message.startsWith('Timed out:'))throw error; const notice=await c.evaluate(`document.querySelector('[data-testid="dx-operations-notice"]')?.textContent||''`); report.resumeClicks.push({attempt:attempt+1,accepted:false,notice}); if(attempt===2)throw Error('Actual resume clicks were not admitted: '+JSON.stringify(report.resumeClicks)); } } 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); await tab('running');await click('dx-history-refresh'); await wait(async()=>(await context()).operations?.runs?.find(row=>row.run_id===prior.run_id)?.counters?.no_match_items===2,'terminal history persisted total'); assert.equal((await context()).operations.runs.find(row=>row.run_id===prior.run_id).counters.saved_items,0); report.checks.push('terminal history displays persisted totals instead of unknown'); report.checks.push('second actual application process resumed original frozen queue and skipped completed item'); await select('Board-C');await tab('schedules'); await nativeScheduleStep(async()=>(await context()).operations?.activity?.status==='cancelled','native schedule cancellation'); assert.equal((await context()).operations.schedules.filter(row=>row.schedule_id!==manifest.orphanSchedule).length,0); const ownSchedule=async()=>(await context()).operations?.schedules?.find(row=>row.schedule_id!==manifest.orphanSchedule); await nativeScheduleStep(async()=>!!(await ownSchedule()),'natively authorized schedule saved'); await wait(async()=>(await ownSchedule())?.status==='completed','automatic due schedule completed',70000); const schedule=await ownSchedule();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 ownSchedule())?.status==='completed','schedule remains terminal'); await wait(async()=>{const events=(await context()).operations?.schedule_events||[]; return events.some(row=>row.event==='dispatch')&&events.some(row=>row.event==='result');},'persisted schedule audit events'); const scheduleEvents=(await context()).operations.schedule_events; assert.equal(scheduleEvents.find(row=>row.event==='dispatch'&&row.schedule_id===schedule.schedule_id).payload.authority,'native_future_authorization'); assert.equal(scheduleEvents.find(row=>row.event==='result'&&row.schedule_id===schedule.schedule_id).payload.receipt.status,'completed'); await wait(()=>c.evaluate(`document.body.innerText.includes('排期派发记录')&&document.body.innerText.includes('排期结果记录')`),'schedule audit cards rendered'); report.scheduleEvents=scheduleEvents; await c.screenshot(path.join(output,'schedule-audit-events.png')); report.checks.push('native future-run dispatch and actual receipt persisted and rendered in schedule audit cards'); 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();}