import fs from 'node:fs' import path from 'node:path' import {createRequire} from 'node:module' import assert from 'node:assert/strict' const require=createRequire(import.meta.url) const {chromium}=require(process.env.PLAYWRIGHT_MODULE||'C:/Users/didi/.cache/codex-runtimes/codex-primary-runtime/dependencies/node/node_modules/playwright') const base=process.env.PULSE_TEST_URL||'http://127.0.0.1:7103',stamp=Date.now().toString(),out=path.resolve('../tmp/pulse-ui-20261015/flow-'+stamp) fs.mkdirSync(out,{recursive:true});console.log('Flow directory:',out) const browser=await chromium.launch({headless:true,executablePath:process.env.CHROME_EXECUTABLE||'C:/Program Files/Google/Chrome/Application/chrome.exe'}) const saved=path.resolve('.local/pulse-ui-browser-state.json'),context=await browser.newContext({viewport:{width:1480,height:1020},...(process.env.PULSE_FRESH!=='1'&&fs.existsSync(saved)?{storageState:saved}:{})}),page=await context.newPage() page.setDefaultTimeout(20000) const errors=[],report={at:new Date().toISOString(),base,modelMode:'real Kimi API + configured imajev',hardware:'browser media adapter and explicit command simulator',steps:[],errors} page.on('pageerror',e=>errors.push(e.message)) const stage=async(name,fn)=>{const start=Date.now();const result=await fn();report.steps.push({name,ms:Date.now()-start});console.log('Passed:',name);return result} const responseFor=route=>page.waitForResponse(r=>r.url().includes('/api/v1'+route)&&r.request().method()==='POST',{timeout:240000}) const shot=name=>page.screenshot({path:path.join(out,name+'.png')}) try{ await page.goto(base+'/app/agent');await page.getByRole('button',{name:/小狗围栏守护/}).click() await stage('Select recorded dog scene and ask Agent',async()=>{ assert.match(await page.locator('select[aria-label="观察来源"] option:checked').innerText(),/幼犬翻越围栏/) await page.waitForFunction(()=>{const b=document.querySelector('button[aria-label="一起确认"]');return b&&!b.disabled}) const pending=responseFor('/setups/draft');await page.getByRole('button',{name:'一起确认',exact:true}).click();const response=await pending;const body=await response.json();if(response.status()!==200)fs.writeFileSync(path.join(out,'api-error.json'),JSON.stringify(body,null,2));assert.equal(response.status(),200,body.error?.message);return body.data }).then(async draft=>{ fs.writeFileSync(path.join(out,'draft.json'),JSON.stringify(draft,null,2));await shot('01-grounding');console.log('Grounding confirmation needed:',JSON.stringify(draft.draft.questions)) const input=path.join(out,'confirmation.json'),deadline=Date.now()+480000 if(process.env.PULSE_CONFIRM_RECORDED_DOG==='1'){ assert.equal(draft.draft.taskKind,'dog_boundary');assert.match(draft.draft.targetName,/狗/) const fixture=JSON.parse(fs.readFileSync('.local/app-api-fixture.json','utf8')) const answers=Object.fromEntries(draft.draft.questions.map(q=>[q.id,/几次|次数|连续几/.test(q.question)?'连续两次有效画面都没看到再提醒。':/就是围栏|围栏.*吗|围栏在/.test(q.question)?'是,已圈出的客厅黑色金属围栏就是关注范围。':/抱|人/.test(q.question)?'有人抱出来不提醒。暂未看到与明确在外,分别按约定提醒。':/几|只|对象/.test(q.question)?'只关注当前画面里的这只白色小狗。':'明确看见整个身体在围栏外时提醒;围栏清晰但暂未看到时,等两次有效观察确认。'])) fs.writeFileSync(input,JSON.stringify({targetBox:fixture.binding.targetBox,regionBox:fixture.binding.regionBox,answers})) } while(!fs.existsSync(input)){if(Date.now()>deadline)throw new Error('Visual confirmation checkpoint timed out');await new Promise(r=>setTimeout(r,500))} const confirmation=JSON.parse(fs.readFileSync(input,'utf8')) if(confirmation.targetBox){for(const [key,label] of [['x','左侧'],['y','顶部'],['w','宽度'],['h','高度']])await page.getByRole('spinbutton',{name:'目标'+label+'百分比'}).fill(String(Math.round(confirmation.targetBox[key]*100)))} if(confirmation.regionBox){await page.getByRole('button',{name:'圈出观察范围',exact:true}).click();for(const [key,label] of [['x','左侧'],['y','顶部'],['w','宽度'],['h','高度']])await page.getByRole('spinbutton',{name:'范围'+label+'百分比'}).fill(String(Math.round(confirmation.regionBox[key]*100)))} for(const q of draft.draft.questions)await page.getByRole('textbox',{name:q.question,exact:true}).fill(confirmation.answers[q.id]||'') assert.equal(draft.draft.recommendation.capturePolicy.mode,'event');assert.ok(draft.draft.recommendation.reasonZh);assert.ok(draft.draft.recommendation.locationAlerts);report.recommendation=draft.draft.recommendation; await page.getByRole('checkbox',{name:'围栏内暂时没看到',exact:true}).check();await page.getByRole('checkbox',{name:'明确看到已经在围栏外',exact:true}).check();await page.getByRole('combobox',{name:'围栏内暂时没看到确认方式',exact:true}).selectOption('2');await page.getByRole('combobox',{name:'明确看到已经在围栏外确认方式',exact:true}).selectOption('1');await page.locator('.pulse-policy-proposal').scrollIntoViewIfNeeded();await shot('01-policy-confirmation');await page.locator('.pulse-policy-proposal').screenshot({path:path.join(out,'01-policy-card.png')}); await page.getByRole('checkbox',{name:'对象、范围和条件都确认好了',exact:true}).check() }) const setup=await stage('Confirm target and prepare the rule',async()=>{const pending=responseFor('/setups/prepare');await page.getByRole('button',{name:'生成守护方式',exact:true}).click();const response=await pending,body=await response.json();if(response.status()!==200)fs.writeFileSync(path.join(out,'api-error.json'),JSON.stringify(body,null,2));assert.equal(response.status(),200,body.error?.message);return body.data}) report.setupId=setup.id;await shot('02-rule') await stage('Run a real single-snapshot trial',async()=>{const pending=responseFor('/setups/'+setup.id+'/trial');await page.getByRole('button',{name:'先试着观察',exact:true}).click();const response=await pending,body=await response.json();assert.equal(response.status(),200,body.error?.message);report.trial=body.data;assert.equal(body.data.passed,true,'Visual trial did not pass')}) await shot('03-trial') await page.getByRole('checkbox',{name:'记住这个对象,下次可以直接选用',exact:true}).check();await page.getByRole('checkbox',{name:'按这份约定开始守护',exact:true}).check() await stage('Activate and open the watch detail',async()=>{await page.getByRole('button',{name:'开始守护',exact:true}).click();await page.waitForURL('**/app/watch/*',{timeout:30000});await page.getByRole('button',{name:'检查这次事件',exact:true}).waitFor()}) report.watchId=page.url().split('/').at(-1) let captureRequests=0;page.on('request',r=>{if(r.method()==='POST'&&r.url().endsWith('/observe'))captureRequests++}) await stage('Playback and elapsed time do not start camera inference',async()=>{await page.waitForFunction(()=>document.querySelector('video')?.readyState>=2);await page.locator('video').evaluate(v=>v.play());await page.waitForTimeout(6500);await page.locator('video').evaluate(v=>v.pause());assert.equal(captureRequests,0)}) await stage('A device event checks one isolated frame and confirms the outside state',async()=>{ await page.locator('video').evaluate(async v=>{v.pause();await new Promise(resolve=>{v.addEventListener('seeked',resolve,{once:true});v.currentTime=Math.min(35.18,v.duration-.02)})}) await shot('04-event-image');const pending=responseFor('/watches/'+report.watchId+'/observe');await page.getByRole('button',{name:'检查这次事件',exact:true}).click();const response=await pending,body=await response.json();assert.equal(response.status(),200,body.error?.message);report.eventObservation=body.data;assert.equal(body.data.answers.location.choice,'outside');assert.equal(body.data.notices.length,1);assert.equal(body.data.notices[0].ruleId,'outside');assert.equal(captureRequests,1) }) await shot('04-observation') const modelError=await page.locator('.pulse-error').allTextContents();assert.deepEqual(modelError,[],'Observation error: '+modelError.join(';')) await stage('Handle the generated state event',async()=>{ const eventLink=page.locator('a[href*="/app/event/"]').first();await eventLink.waitFor({timeout:20000});await eventLink.click();await page.getByRole('button',{name:'我来处理',exact:true}).click();await page.getByRole('button',{name:'已经处理',exact:true}).click();await page.getByText('用户已处理',{exact:true}).waitFor();await shot('05-event-completed') }) await stage('Inspect confirmed object memory',async()=>{await page.goto(base+'/app/memories');await page.getByRole('link',{name:'用它新建关注',exact:false}).first().waitFor();assert.ok(await page.locator('.pulse-memory-image img').first().evaluate(im=>im.complete&&im.naturalWidth>0));await shot('06-memory')}) await stage('Complete a confirmed simulated device action through the API',async()=>{ await page.goto(base+'/app/devices');await page.getByText('设备操作联调',{exact:false}).click();await page.getByRole('button',{name:'连接模拟设备',exact:true}).click();await page.getByRole('link',{name:/联调摄像头/}).click();await page.getByRole('button',{name:'切换模式',exact:true}).click();await page.getByRole('button',{name:'确认操作',exact:true}).click();await page.getByText('设备状态已确认',{exact:true}).first().waitFor();await shot('07-command-verified') }) await page.goto(base+'/app');await page.waitForTimeout(1200);await shot('08-app-with-data');await page.goto(base+'/workspace');await page.waitForTimeout(1200);await shot('09-desktop-with-data') assert.deepEqual(errors,[]);report.status='passed' }catch(e){report.status='failed';report.failure=e.message;await shot('failure').catch(()=>{});console.log('Flow stopped:',e.message);process.exitCode=1} finally{await context.storageState({path:saved});fs.writeFileSync(path.join(out,'flow-report.json'),JSON.stringify(report,null,2));fs.writeFileSync(path.resolve('../tmp/pulse-ui-20261015/latest-flow.json'),JSON.stringify({directory:out,status:report.status}));await browser.close()}