/** Click the actual embedded Chromium UI and record native SVG/animation evidence. */ import assert from 'node:assert/strict'; import {mkdir,writeFile} from 'node:fs/promises'; import path from 'node:path'; import {connect} from './dsh_cdp.mjs'; const output=path.resolve(process.argv[2] || 'build/dsh-native-acceptance/ui'); await mkdir(output,{recursive:true}); const c=await connect(Number(process.argv[3] || 9226)); const report={driver:'Qt Chromium CDP Input events',checks:[]}; const liveModel=process.argv.includes('--live-model'); async function wait(expression,timeout=15000) { const end=Date.now()+timeout; while(Date.now()setTimeout(r,100));} throw Error('UI condition timed out: '+expression); } async function dismissOnboarding() { const end=Date.now()+12000; let quiet=Date.now(); while(Date.now()e.textContent.trim()==='继续'),start:document.body.innerText.includes('开始使用')})`); if(modal.preview) { await c.click(`[...document.querySelectorAll('button')].find(e=>e.textContent.trim()==='继续')`); quiet=Date.now(); }else if(modal.start) { await c.click(`[...document.querySelectorAll('button[aria-label="关闭"]')].at(-1)`); quiet=Date.now(); }else if(Date.now()-quiet>1000) return; await new Promise(resolve=>setTimeout(resolve,150)); } throw Error('The native onboarding dialog could not be dismissed'); } try { await wait(`!!document.querySelector('svg[class*="fish"]')`,45000); await dismissOnboarding(); for(let attempt=0;attempt<10 && !await c.evaluate(`document.body.innerText.includes('DSH助手')`);attempt++) { if(await c.evaluate(`!!document.querySelector('button[aria-label="打开侧边栏"]')`)) await c.click(`document.querySelector('button[aria-label="打开侧边栏"]')`); await new Promise(resolve=>setTimeout(resolve,150)); } await wait(`document.body.innerText.includes('DSH助手')`); await wait(`!!document.querySelector('[data-testid="dx-task-panel"]')`); report.checks.push('official frontend and native right dock loaded'); const context=await c.evaluate(`fetch('/dx/context').then(r=>r.json())`); assert.ok(context.task_id,'Select a task in the task center before running this test.'); report.checks.push('current task received from task center'); await c.click(`[...document.querySelectorAll('button')].find(e=>e.textContent.trim()==='刷新任务上下文')`); await wait(`fetch('/dx/context').then(r=>r.json()).then(d=>d.revision!==${JSON.stringify(context.revision)})`); report.checks.push('refresh clicked and confirmed through Qt task bridge'); await c.click(`document.querySelectorAll('button[aria-label="新建会话"]')[1]`); await wait(`!!document.querySelector('[data-testid="dx-task-panel"]')`); report.checks.push('native new conversation retains task dock'); if(!liveModel) { await c.click(`document.querySelector('button[aria-label="收起侧边栏"]')`); await wait(`!!document.querySelector('button[aria-label="打开侧边栏"]')`); await c.click(`document.querySelector('button[aria-label="打开侧边栏"]')`); await wait(`!!document.querySelector('button[aria-label="收起侧边栏"]')`); report.checks.push('native sidebar collapsed and expanded'); } const fish=await c.evaluate(`(()=>{const r=document.querySelector('svg[class*="fish"]').getBoundingClientRect();return {x:r.x+r.width/2,y:r.y+r.height/2};})()`); await c.call('Input.dispatchMouseEvent',{type:'mouseMoved',...fish}); await wait(`document.getAnimations().length>0 || document.querySelectorAll('svg animate').length>0`); report.nativeSvgCount=await c.evaluate(`document.querySelectorAll('svg').length`); report.nativeAnimations=await c.evaluate(`({css:document.getAnimations().length,svg:document.querySelectorAll('svg animate').length})`); assert.ok(report.nativeSvgCount>=15); for(let i=0;i<8;i++) { await c.screenshot(path.join(output,`hover-${i}.png`)); await new Promise(resolve=>setTimeout(resolve,120)); } report.checks.push('upstream whale SVG hover animation runs in Qt Chromium'); const transforms=await c.evaluate(`document.getAnimations().map(a=>({name:a.animationName,time:a.currentTime}))`); report.animationState=transforms; const modelButton=`[...document.querySelectorAll('button')].find(e=>e.getBoundingClientRect().width>0 && (e.getAttribute('aria-label')==='请选择模型' || /skyworth-deepseek/.test(e.title+' '+e.getAttribute('aria-label'))))`; await c.click(modelButton); await wait(`document.querySelector('[role="dialog"],[role="menu"],[role="listbox"]')!==null`); report.modelPickerEntries=await c.evaluate(`[...new Set((document.body.innerText.match(/skyworth-deepseek[\\w.-]*/g)||[]))]`); report.checks.push('native model picker opened'); await c.call('Input.dispatchKeyEvent',{type:'keyDown',key:'Escape',code:'Escape'}); await c.call('Input.dispatchKeyEvent',{type:'keyUp',key:'Escape',code:'Escape'}); await c.click(`document.querySelector('[contenteditable="true"]')`); await c.call('Input.insertText',{text:liveModel ? '请分析当前任务:先调用 dx_task_snapshot 获取真实任务快照,然后用中文告诉我任务名称、当前状态和允许的下一步。不要修改工程。' : '请分析当前任务的异常,并说明允许的处理方式。'}); await wait(`document.querySelector('[contenteditable="true"]').textContent.includes('请分析当前任务')`); report.checks.push('typed into official composer with native keyboard input'); report.liveModelReplyTested=false; if(liveModel) { await c.click(`document.querySelector('button[aria-label="发送消息"]')`); const replies=`[...document.querySelectorAll('[data-chat-group-part="response"]')].map(e=>e.innerText).join('\\n')`; await wait(`(${replies}).length>30 && /任务|状态/.test(${replies})`,100000); await wait(`![...document.querySelectorAll('button[aria-label="停止生成"]')].some(e=>e.getBoundingClientRect().width>0)`,100000); report.reply=await c.evaluate(replies); report.liveModelReplyTested=true; report.liveTurnCompleted=true; report.checks.push('real API reply rendered through the official Harness conversation'); await c.click(`document.querySelector('[contenteditable="true"]')`); await c.call('Input.insertText',{text:'请分析当前任务的下一步。'}); } else report.reason='No model credential configured in this isolated acceptance profile.'; await c.screenshot(path.join(output,'dsh-native-final.png')); if(process.argv.includes('--confirm-cancel')) { await c.click(`[...document.querySelectorAll('button')].find(e=>e.textContent.trim()==='准备失败项重试')`); await wait(`fetch('/dx/context').then(r=>r.json()).then(d=>d.action_result.includes('已取消'))`,20000); report.checks.push('sensitive action required Qt confirmation and was cancelled'); } report.status='passed'; await writeFile(path.join(output,'report.json'),JSON.stringify(report,null,2)); console.log(JSON.stringify(report)); } catch(error) { await c.screenshot(path.join(output,'failure.png')).catch(()=>{}); await writeFile(path.join(output,'report.json'),JSON.stringify({...report,status:'failed',error:error.message},null,2)); throw error; } finally {c.close();}