/** Render the registered native incident panel and exercise its cancel action offline. */ import assert from 'node:assert/strict'; import {readFile} from 'node:fs/promises'; import vm from 'node:vm'; let plugin,TaskPanel,data,stateIndex; const requests=[]; const React={ createElement:(type,props,...children)=>({type,props:props||{},children}), useState:()=>[[data,'','overview',false,null][stateIndex++],()=>{}], useEffect:()=>{} }; const sandbox={window:{__ModuleLoader__:{load:value=>{plugin=value;}}}, fetch:async(url,options)=>{ if(url==='/dx/context')return {ok:true,json:async()=>({...data,revision:'fresh-revision'})}; requests.push({url,body:JSON.parse(options.body)}); return {ok:true,status:200}; }}; vm.runInNewContext(await readFile(new URL('../integration/dsh/task-plugin/client.js',import.meta.url),'utf8'),sandbox); const exported=plugin.factory(name=>name==='react'?React:{}); exported.apply({effect:()=>{},slots:{inject:(_name,callback)=>callback(),register:(slot,component)=>{ if(slot.name==='sidebar.right.pane.tab')TaskPanel=component; }},sidebarRightTabs:{register:()=>{}},sidebarRight:{mounted:{}}}); function expand(node){ if(Array.isArray(node))return node.flatMap(part=>expand(part)); if(node===null||node===undefined||typeof node==='boolean')return []; if(typeof node!=='object')return [String(node)]; if(typeof node.type==='function')return expand(node.type({...node.props,children:node.children})); return [{...node,children:node.children.flatMap(part=>expand(part))}]; } const nodes=tree=>tree.flatMap(node=>typeof node==='object'?[node,...nodes(node.children)]:[]); const text=tree=>tree.map(node=>typeof node==='object'?text(node.children):node).join(''); function render(automation){ data={task_id:'selected-task',revision:'current-revision',progress:{},rules_count:0, automation:{incident_id:'incident',task_id:'',...automation}}; stateIndex=0; return expand(TaskPanel()); } const element=(tree,id)=>nodes(tree).find(node=>node.props['data-testid']===id); let tree=render({status:'resolved',cleanup_attempts:1,verification:{resources_released:true,remaining_resources:[]}}); assert.match(text([element(tree,'dx-auto-cleanup-attempts')]),/1/); assert.match(text([element(tree,'dx-auto-verification')]),/资源释放已独立核验通过/); assert.match(text([element(tree,'dx-auto-verification')]),/不代表 CAD 任务成功/); tree=render({status:'pending_issue',cleanup_attempts:1,verification:{resources_released:false,remaining_resources:[{}]}}); assert.match(text([element(tree,'dx-auto-verification')]),/剩余 1 项资源/); tree=render({status:'pending_issue',cleanup_attempts:0,verification:{resources_released:false,remaining_resources:null}}); assert.match(text([element(tree,'dx-auto-verification')]),/剩余资源数量未知/); tree=render({status:'pending_issue',cleanup_attempts:1,verification:{resources_released:false,remaining_resources:[{}, {__truncated__:true}]}}); assert.match(text([element(tree,'dx-auto-verification')]),/至少 1 项资源/); tree=render({status:'observed_cleared',cleanup_attempts:0,verification:{resources_released:true,remaining_resources:[]}}); assert.equal(text([element(tree,'dx-auto-status')]),'阻塞已自行解除,未执行清理'); assert.ok(!text(tree).includes('正在保留现场'),'a cleared incident must not still claim diagnosis is running'); assert.equal(element(tree,'dx-auto-cancel'),undefined); tree=render({status:'cancelled',cleanup_attempts:0,can_cancel:false}); assert.equal(text([element(tree,'dx-auto-status')]),'诊断已取消,证据已保留'); assert.ok(!text(tree).includes('正在保留现场'),'a cancelled incident must not still claim diagnosis is running'); assert.equal(element(tree,'dx-auto-cancel'),undefined); tree=render({status:'analyzing',task_id:'other-incident-task',can_cancel:true}); const cancel=element(tree,'dx-auto-cancel'); assert.ok(cancel,'an active diagnosis can be cancelled after selecting another task'); assert.equal(text([cancel]),'取消 AI 诊断'); await cancel.props.onClick(); assert.equal(requests.length,1); assert.deepEqual(requests[0],{url:'/dx/action',body:{action:'cancel_diagnosis',task_id:'selected-task',revision:'fresh-revision'}}); console.log(JSON.stringify({status:'passed',cleanupEvidenceVisible:true,terminalStatusesAccurate:true,cancelActionRouted:true}));