Files
dxdesigner-part-replacement/tools/verify_dsh_lifecycle_ui.mjs

85 lines
6.4 KiB
JavaScript

/** Natural-language lifecycle intents through the real embedded official UI. */
import assert from 'node:assert/strict';
import {writeFile} from 'node:fs/promises';
import path from 'node:path';
import {connect} from './dsh_cdp.mjs';
const output=path.resolve(process.argv[2]);
const c=await connect(Number(process.argv[3]||9235));
const report={checks:[],prompts:[],explicitToolNamesInPrompts:false,executor:'controlled_no_match'};
const context=()=>c.evaluate(`fetch('/dx/context').then(r=>r.json())`);
async function wait(test,timeout=110000){
const end=Date.now()+timeout;
while(Date.now()<end){if(await test())return;await new Promise(r=>setTimeout(r,160));}
throw Error('Lifecycle UI condition timed out');
}
async function idle(){await wait(()=>c.evaluate(`![...document.querySelectorAll('button[aria-label="停止生成"]')].some(e=>e.getBoundingClientRect().width>0)`));}
async function send(prompt){
report.prompts.push(prompt);
await writeFile(path.join(output,'progress.json'),JSON.stringify(report,null,2));
// A newly mounted Lexical composer can replace its first DOM node while the
// new-session transition finishes. Wait for real typed text before sending.
for(let attempt=0;attempt<5;attempt++){
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'&&document.querySelector('[contenteditable="true"]')?.textContent.includes(${JSON.stringify(prompt)});})()`));
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)})`));
}
async function state(status){await wait(async()=>{const d=await context();return d.lifecycle?.result?.status===status;});return (await context()).lifecycle.result;}
try{
await wait(()=>c.evaluate(`!!document.querySelector('[contenteditable="true"]')`),45000);
await send('先列出任务,找到工程目录 Board-A 对应的任务并切换到它。将这个任务改名为“DSH管理验收任务”,备注设为“自然语言管理验收”,已有旧料号 OLD123 对应的新料号改成 NEW789,其余配置保留。先不要执行。');
const changed=await state('updated');await idle();
assert.equal(changed.task_ids.length,2);
report.taskId=changed.task_id;
report.checks.push('natural-language lookup, select and shared-rule edit committed after native clicks');
await send('检查 DSH管理验收任务 现在能否运行,列出执行前的阻断原因。只检查,不要开始运行。');
const checked=await state('preflight');await idle();
assert.equal(checked.ready,true);assert.equal(checked.cad_connection_checked,false);
report.checks.push('preflight returned real readiness without claiming CAD connectivity');
await send('现在执行 DSH管理验收任务,只运行这一个任务。请通过主工具确认范围,结束后告诉我实际保存、失败和未命中的数量。');
await wait(async()=>{const d=await context();return d.lifecycle?.status==='executing';});
await c.click(`document.querySelectorAll('button[aria-label="新建会话"]')[1]`);
await wait(()=>c.evaluate(`!!document.querySelector('svg[class*="fish"]')&&!document.querySelector('[data-chat-group-part="response"]')&&!!document.querySelector('[contenteditable="true"]')`));
await new Promise(r=>setTimeout(r,600));
await send('请安全暂停当前正在执行的任务,等待当前项收尾,不要强制结束。');
const paused=await state('paused');await idle();
assert.equal(paused.tasks[0].no_match_items,1);assert.equal(paused.tasks[0].saved_items,0);
report.checks.push('second live conversation requested real StopControl pause; first execution returned actual paused evidence');
await send('继续执行 DSH管理验收任务 尚未完成的部分,保留已经完成的结果。通过主工具确认后执行,并告诉我真实数量。');
const completed=await state('completed');await idle();
assert.equal(completed.tasks[0].no_match_items,2);assert.equal(completed.tasks[0].saved_items,0);
assert.equal(completed.tasks[0].failed_items,0);
report.checks.push('continue executed remaining item once and reported no-match rather than replacement success');
await c.screenshot(path.join(output,'dsh-execution-results.png'));
await send('复制 DSH管理验收任务 的规则、备注和 Value 配置,创建一个副本任务。新工程目录我来选,不复制执行结果,也不要开始替换。');
const copied=await state('created');await idle();
assert.equal(copied.created_count,1);assert.ok(!copied.task_ids.includes(report.taskId));
report.copyId=copied.task_ids[0];
report.checks.push('copy used actual native project selection and returned new committed task ID');
await send('把刚才复制出来的副本任务移入回收站,保留证据,不要归档原来的 DSH管理验收任务。');
const archived=await state('archived');await idle();
assert.equal(archived.task_id,report.copyId);
report.checks.push('archive targeted only copied task and preserved source task');
await send('将原来的 DSH管理验收任务 名称改为 CANCEL_ME,我会在确认窗口里决定。不要执行工程。');
await state('cancelled');await idle();
report.checks.push('real Qt cancellation reached model without applying task mutation');
await c.screenshot(path.join(output,'dsh-lifecycle-complete.png'));
await c.click(`document.querySelector('[contenteditable="true"]')`);
await c.call('Input.insertText',{text:'请分析当前任务接下来如何配置。'});
report.status='passed';
}catch(error){
report.status='failed';report.error=error.message;
await c.screenshot(path.join(output,'failure.png')).catch(()=>{});
throw error;
}finally{
await writeFile(path.join(output,'report.json'),JSON.stringify(report,null,2));
c.close();
}