import fs from 'node:fs/promises'; import path from 'node:path'; import assert from 'node:assert/strict'; import {chromium,expect} from '@playwright/test'; import {login,call} from './role-permissions-live.mjs'; import {scripts,scriptDefinition} from '../../unreal_tran_web/src/features/virtual-training-scripts/definition.js'; const dir=path.resolve('reports/'+(process.env.REPORT_NAME||'bridge-controls-review-20260915'));await fs.mkdir(dir,{recursive:true}); const resultFile=process.env.RESULT_FILE||'result.json'; const signed=await login('root'),checks=[],errors=[],writes=[]; const pending=new Map(),networkErrors=[]; const browser=await chromium.launch({channel:'msedge',headless:true,args:['--enable-unsafe-swiftshader']}); const page=await browser.newPage({viewport:{width:Number(process.env.WIDTH)||1366,height:Number(process.env.HEIGHT)||768}}); await page.addInitScript(s=>{sessionStorage.setItem('unreal-tran:web:access-token:v1',s.accessToken);sessionStorage.setItem('unreal-tran:web:refresh-token:v1',s.refreshToken);localStorage.setItem('unreal-tran:web:preferences:v1',JSON.stringify({themeMode:'military'}));},signed); // Read-only geometry preflight before publishing a migration. Never use this // mode to claim completion: server validation remains on the existing version. if(process.env.PROPOSED_MODELS==='1'){ assert.notEqual(process.env.EXECUTE,'1'); await page.route(/\/api\/tran\/v1\/teaching\/assignments\/\d+$/,async route=>{ const response=await route.fetch(),body=await response.json(),script=scripts.find(s=>s.code===body.data?.code); if(script?.version==='4')body.data.definitionSnapshot=scriptDefinition(script); await route.fulfill({response,json:body}); }); } await page.route(/\/src\/features\/virtual-training-legacy\/upstream\/views\/training\.js(?:\?|$)/,async route=>{ const response=await route.fetch();await route.fulfill({response,body:(await response.text()).replace("this.showFeedback('error', `编辑场景加载失败:", "globalThis.__bridgeLoadError=error.stack; this.showFeedback('error', `编辑场景加载失败:").replace('const partId = hit.object.userData.partId;', 'globalThis.__lastPointerPick={id:hit.object.userData.partId,x:event.clientX,y:event.clientY,active:this.getActiveTargetId()}; const partId = hit.object.userData.partId;')+` globalThis.__inspectionSnapshot=(options={})=>{ const e=activeTraining;if(!e?.sceneReady)return null; const id=e.getActiveTargetId(),object=e.parts.get(id),box=object?physicalBounds(object):null; const screen=object?e.worldToScreen(box.getCenter(new THREE.Vector3())):null; const materials=[];e.publishedContentRoot.traverse(o=>{if(o.isMesh&&!o.userData.trainingPickProxy)for(const m of Array.isArray(o.material)?o.material:[o.material])materials.push({opacity:m.opacity,baseline:e.viewerMaterialBaseline.get(m)?.opacity,side:m.side});}); const labels=[...e.viewport.querySelectorAll('.training-part-label.is-visible')].map(n=>{const r=n.getBoundingClientRect();return {text:n.textContent.trim(),x:r.x,y:r.y,w:r.width,h:r.height,id:n.dataset.labelTarget||id};}); const op=e.getActiveMechanismOperation()?.operation; const lid=e.parts.get('front-pump-aux-control-box-cover'); const projected=object?e.inspection.projectBounds(object):null; const canvas=e.canvas.getBoundingClientRect(); const pickPoints=[]; if(projected){const ray=new THREE.Raycaster();for(const fy of [0.5,0.35,0.65,0.2,0.8,0.05,0.95])for(const fx of [0.5,0.35,0.65,0.2,0.8,0.05,0.95]){ const x=projected.x+projected.w*fx,y=projected.y+projected.h*fy; if(document.elementFromPoint(x+canvas.left,y+canvas.top)!==e.canvas)continue; ray.setFromCamera(new THREE.Vector2(x/canvas.width*2-1,1-y/canvas.height*2),e.camera); const hit=ray.intersectObjects(e.pickables,false).find(h=>!h.object.userData.trainingPickProxy&&effectivelyVisible(h.object)&&!(Array.isArray(h.object.material)?h.object.material:[h.object.material]).some(m=>m.transparent&&m.opacity!h.object.userData.trainingPickProxy&&effectivelyVisible(h.object)); if(hit?.object.userData.partId==='scene:front-pump-rescue-vehicle')freePoints.push({x:canvas.left+x,y:canvas.top+y}); }} let grounding=null; if(options.newModel){ const vehicle=e.viewerPrimaryObject,terrain=e.publishedContentRoot.children.filter(o=>o!==vehicle),bounds=physicalBounds(vehicle),ray=new THREE.Raycaster(),gaps=[]; vehicle.traverse(mesh=>{if(!mesh.isMesh||mesh.userData.trainingPickProxy)return;const a=mesh.geometry.attributes.position,p=new THREE.Vector3();for(let i=0;ibounds.min.y+.002)continue;ray.set(new THREE.Vector3(p.x,p.y+2,p.z),new THREE.Vector3(0,-1,0));const hit=ray.intersectObjects(terrain,true)[0];if(hit)gaps.push(p.y-hit.point.y);}}); grounding={samples:gaps.length,minGap:Math.min(...gaps),maxGap:Math.max(...gaps)}; } return {id,step:e.session.stepIndex,operation:e.getActiveMechanismOperation()?.state.index,errors:e.errorCount,complete:e.session.isComplete(),pending:!!e.bridgeOperationPending||!!e.serverCommitPending, drag:e.bridgeDrag?.operation.targetId,cameraTween:!!e.viewerCameraTween,lastPointerPick:globalThis.__lastPointerPick, lid:lid?{position:lid.position.toArray(),quaternion:lid.quaternion.toArray()}:null,evidenceCount:e.mechanismState?.evidence.length, grounding,modelSources:e.scenario.sceneResource.snapshot.objects.filter(o=>o.type==='imported').map(o=>({name:o.sourceName,url:o.resourceUrl})), hdr:e.scene.environment?.name,distance:e.camera.position.distanceTo(e.controls.target),zoom:e.camera.zoom,exposure:e.renderer.toneMappingExposure,light:[e.ambient.intensity,e.sun.intensity], screen,bounds:box?box.getSize(new THREE.Vector3()).toArray():null,projected,pivot,pickPoints,freePoints, tip:object?.userData.trainingHandleTipLocal ? object.localToWorld(object.userData.trainingHandleTipLocal.clone()).toArray():null, front:object?.userData.trainingInspectionFront?.toArray(), workflowCoverage:e.inspection.workflowTargets().filter(effectivelyVisible).map(o=>{const meshes=[];o.traverse(n=>{if(n.isMesh&&!n.userData.trainingPickProxy&&effectivelyVisible(n))meshes.push(n);});return {name:o.userData.partId,glowing:meshes.every(m=>e.inspection.outline.selectedObjects.includes(m)||e.inspection.workflowOutline.selectedObjects.includes(m))};}), camera:e.camera.position.toArray(),target:e.controls.target.toArray(),outline:e.inspection.outline.selectedObjects.length,cutaway:e.inspection.cutaway?.size||0,materials,labels, narration:e.steps.map(s=>({video:e.context.videoUrls?.includes(s.audio?.narrationUrl)===true,fallback:!!s.audio?.narrationFallbackUrl})), operationData:op,position:object?.position.toArray(),quaternion:(object?.userData.bridgeRotatingPart||object)?.quaternion.toArray()}; };`}); }); page.on('pageerror',error=>errors.push(error.message)); page.on('request',r=>pending.set(r,new URL(r.url()).pathname)); page.on('requestfinished',r=>pending.delete(r)); page.on('requestfailed',r=>{networkErrors.push({path:new URL(r.url()).pathname,error:r.failure()?.errorText});pending.delete(r);}); page.on('request',request=>{if(request.url().includes('/api/tran/')&&!['GET','HEAD'].includes(request.method()))writes.push(request.url());}); const snapshot=()=>page.evaluate(()=>globalThis.__inspectionSnapshot?.()); const ready=async()=>{await expect(page.locator('[data-role="loading"]')).toHaveClass(/is-hidden/,{timeout:120000});const error=await page.evaluate(()=>globalThis.__bridgeLoadError);if(error)throw new Error(error);await expect.poll(snapshot,{timeout:30000}).not.toBeNull();await page.waitForTimeout(1800);}; try { for(const id of (process.env.TASK_IDS||'86,87,88,89').split(',')){ console.log('OPEN',id); await page.goto((process.env.BASE_URL||'http://127.0.0.1:6180')+'/teaching/virtual-training/preview/'+id);await ready(); const introSkip=page.locator('.training-introduction [data-intro-command="skip"]'); if(await introSkip.isVisible())await introSkip.click(); const state=await snapshot(); await page.screenshot({path:path.join(dir,`task-${id}-initial.png`)}); assert.equal(state.hdr,'venice_sunset_1k');assert.ok(state.outline>0); assert.ok(state.materials.every(m=>m.side===2),'DoubleSide physical materials'); assert.ok(state.materials.every(m=>m.baseline==null||m.opacity===m.baseline),'original opacity'); assert.equal(state.zoom,1,'initial overview is not magnified');assert.ok(state.workflowCoverage.every(t=>t.glowing),'all visible workflow parts glow on entry'); assert.ok(state.labels.some(l=>l.id===state.id),'active target label visible'); checks.push({name:'task '+id+' first view',state});console.log('VIEW',id,JSON.stringify({projected:state.projected,labels:state.labels.map(l=>l.text),cutaway:state.cutaway})); if(process.env.CHECK_NEW_MODEL==='1'){ const inspected=await page.evaluate(()=>globalThis.__inspectionSnapshot({newModel:true})); assert.ok(inspected.modelSources.some(m=>m.name==='0916架桥车.glb'&&m.url.includes('bridge-erector-20260916.glb'))); assert.ok(inspected.grounding.samples>0);assert.ok(inspected.grounding.minGap>-.003&&inspected.grounding.maxGap<.003,'new vehicle contacts authored terrain'); checks.push({name:'new model source and ground contacts',pass:true,grounding:inspected.grounding,modelSources:inspected.modelSources}); } if(process.env.REQUIRE_VIDEO==='1'){ assert.ok(state.narration.length>0);assert.ok(state.narration.every(s=>s.video&&s.fallback),'all steps have video and voice fallback'); await page.locator('[data-action="teacher-speak"]').click(); const video=page.locator('.training-server-video'); await expect(video).toBeVisible(); await expect.poll(()=>video.evaluate(v=>v.readyState>=2&&v.videoWidth>0&&v.currentTime>0),{timeout:45000}).toBe(true); checks.push({name:'task '+id+' digital-human video playback',pass:true,media:await video.evaluate(v=>({width:v.videoWidth,height:v.videoHeight,duration:v.duration,currentTime:v.currentTime}))}); } if(process.env.EXECUTE==='1'||process.env.EXTRA_CONTROLS==='1'){ for(const action of ['steps','tools']){ const button=page.locator(`[data-viewer-action="${action}"]`); if(await button.getAttribute('aria-expanded')==='true')await button.click(); } await page.waitForTimeout(400); } if(process.env.EXTRA_CONTROLS==='1'){ await page.locator('[data-viewer-action="tools"]').click(); await page.locator('[data-action="viewer-reset-view"]').click();await page.waitForTimeout(1000); const free=await page.evaluate(()=>globalThis.__inspectionSnapshot({free:true}));assert.ok(free.freePoints.length,'vehicle body can be selected'); const point=free.freePoints[0],beforeWrites=writes.length; await page.mouse.click(point.x,point.y);await page.waitForTimeout(900); const selected=await snapshot();assert.equal(selected.errors,free.errors);assert.equal(selected.step,free.step);assert.equal(selected.operation,free.operation);assert.equal(writes.length,beforeWrites); for(let i=0;i<4;i++)await page.locator('[data-action="toggle-issue-highlight"]').click(); assert.ok((await snapshot()).materials.every(m=>m.baseline==null||m.opacity===m.baseline)); await page.locator('[data-action="viewer-reset-view"]').click();await page.waitForTimeout(1000); const orbit=await page.evaluate(()=>globalThis.__inspectionSnapshot({free:true}));assert.ok(orbit.freePoints.length); const dragPoint=orbit.freePoints[0];await page.mouse.move(dragPoint.x,dragPoint.y);await page.mouse.down();await page.mouse.move(dragPoint.x+45,dragPoint.y+20,{steps:8});await page.mouse.up();await page.waitForTimeout(900); const moved=await snapshot();assert.equal(moved.errors,0);assert.notDeepEqual(moved.camera,orbit.camera);assert.equal(writes.length,beforeWrites); await page.locator('[data-viewer-action="tools"]').click(); await page.locator('[data-viewer-action="fullscreen"]').click();await page.waitForTimeout(400);await page.keyboard.press('Escape');await page.waitForTimeout(500); await expect(page.locator('[data-viewer-action="fullscreen"]')).toHaveAttribute('title','全屏操作');await expect(page.locator('[data-role="modal"]')).toBeHidden(); assert.ok(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth),'no page horizontal overflow'); await page.screenshot({path:path.join(dir,'free-inspection-controls.png')});checks.push({name:'non-workflow body click/orbit, selection-highlight restoration, Escape fullscreen exit',pass:true}); } if(process.env.EXECUTE==='1'){ // Labels only focus: no operation, no validation request, no score/error change. const beforeWrites=writes.length; await page.locator('[data-role="part-label"]').click();await page.waitForTimeout(900); const enlarged=await snapshot();assert.equal(enlarged.step,state.step);assert.equal(enlarged.operation,state.operation);assert.equal(writes.length,beforeWrites);assert.ok(enlarged.projected.w+enlarged.projected.h>state.projected.w+state.projected.h,'label enlarges the target from overview'); const standOff=enlarged.distance; // Native fullscreen and both collapsible panels remain reversible on laptops. await page.locator('[data-viewer-action="steps"]').click();await expect(page.locator('.training-steps-panel')).toBeVisible(); await page.locator('[data-viewer-action="steps"]').click();await expect(page.locator('.training-steps-panel')).toBeHidden(); await page.locator('[data-viewer-action="tools"]').click();await expect(page.locator('[data-role="model-commandbar"]')).toBeVisible(); assert.equal(await page.locator('.training-bottom-bar [data-viewer-action]').count(),3,'display controls are in the bottom bar'); assert.equal(await page.locator('.training-viewer-actions').innerText(),'','icon-only controls'); for(const button of await page.locator('[data-viewer-action]').all())assert.ok(await button.getAttribute('title'),'icons have hover titles'); await page.locator('[data-action="toggle-translucent-others"]').click();await page.waitForTimeout(250); assert.ok((await snapshot()).materials.some(m=>m.baseline>0 && m.opacitym.baseline>0 && m.opacitym.baseline==null||m.opacity===m.baseline),'opacity restored after separate translucency is off'); await page.locator('[data-action="toggle-part-labels"]').click(); for(let n=0;n<2;n++){await page.locator('[data-action="toggle-issue-highlight"]').click();await page.waitForTimeout(100);} assert.ok((await snapshot()).materials.every(m=>m.baseline==null||m.opacity===m.baseline)); await page.locator('[data-action="toggle-hide-others"]').click();await page.waitForTimeout(300); const isolated=await snapshot(); assert.ok(isolated.labels.length>0 && isolated.labels.every(l=>l.id===enlarged.id),'hide others keeps the focused label and hides all others'); await page.locator('[data-action="toggle-hide-others"]').click(); await page.locator('[data-viewer-action="tools"]').click(); await page.locator('[data-viewer-action="fullscreen"]').click();await page.waitForTimeout(500); await expect(page.locator('[data-viewer-action="fullscreen"]')).toHaveAttribute('title','退出全屏'); await page.screenshot({path:path.join(dir,`task-${id}-fullscreen.png`)}); await page.locator('[data-viewer-action="fullscreen"]').click();await page.waitForTimeout(500); await expect(page.locator('[data-viewer-action="fullscreen"]')).toHaveAttribute('title','全屏操作'); const assignment=await call('tran/v1/teaching/assignments/'+id,signed.accessToken); const total=assignment.data.definitionSnapshot.steps.length; const prepared=new Set(); let gestures=0; while(true){ await expect.poll(async()=>!!(await snapshot())&&!(await snapshot()).cameraTween,{timeout:10000}).toBe(true); let start=await snapshot(); if(process.env.PREPARE_SAFETY==='1'&&!prepared.has(start.step)&&!(process.env.LATE_PREPARATION==='1'&&start.step===4)){ await page.locator('[data-open-preparation]').click(); for(const box of await page.locator('[data-safety]').all())await box.check(); if(assignment.data.definitionSnapshot.steps[start.step].mode==='measure')await page.locator('[data-measurement]').fill('本次教员预览按脚本演示连接管路;管路处理方向与依据需由教员核对,此记录仅用于验证交互,不作实装质量判定。'); await page.locator('[data-save-preparation]').click(); await page.locator('[data-close-preparation]').click();prepared.add(start.step); } if(process.env.MANUAL_OCCLUSION_TOOLS==='1'&&!start.pickPoints.length){ const toolsButton=page.locator('[data-viewer-action="tools"]'); if(await toolsButton.getAttribute('aria-expanded')!=='true')await toolsButton.click(); const transparent=page.locator('[data-action="toggle-translucent-others"]'); if(await transparent.getAttribute('aria-pressed')!=='true')await transparent.click(); await toolsButton.click();await page.waitForTimeout(500);start=await snapshot(); if(!start.pickPoints.length){ await page.locator('[data-role="part-label"]').click();await page.waitForTimeout(900); await toolsButton.click();await page.locator('[data-action="toggle-hide-others"]').click();await toolsButton.click(); await page.waitForTimeout(400);start=await snapshot(); checks.push({name:'task '+id+' explicit isolate for overlapping workflow targets',targetId:start.id,pass:start.pickPoints.length>0}); } checks.push({name:'task '+id+' internal target inspected with explicit translucent tool',targetId:start.id,pass:start.pickPoints.length>0}); } if(assignment.data.definitionSnapshot.steps[start.step].mode==='measure' && !prepared.has(start.step)){ await page.locator('[data-open-preparation]').click(); await page.locator('[data-measurement]').fill('本次教员预览按脚本演示连接管路;管路处理方向与依据需由教员核对,此记录仅用于验证交互,不作实装质量判定。'); await page.locator('[data-save-preparation]').click();await expect(page.locator('[data-save-status]')).toContainText('已保留'); await page.locator('[data-close-preparation]').click();prepared.add(start.step); checks.push({name:'task '+id+' step '+(start.step+1)+' observation retained for instructor review',pass:true}); } if(start.step===0 && start.zoom===1){await page.locator('[data-role=part-label]').click();await page.waitForTimeout(900);continue;} assert.ok(start.pickPoints.length,'visible target has a real mesh hit: '+start.id); assert.ok(Math.abs(start.distance-standOff)<0.015,'consistent stand-off '+start.id); if(start.front)assert.ok(start.front.reduce((n,v,i)=>n+v*(start.camera[i]-start.target[i])/start.distance,0)>.99,'camera faces the authored control front'); assert.equal(start.exposure,state.exposure);assert.deepEqual(start.light,state.light); for(let i=0;if.offset||f.scale)); if(draggable){ await page.mouse.move(point.x-68,point.y,{steps:6});await page.waitForTimeout(100); const mid=await snapshot(); if(operation.kind==='rotate'){ if(start.tip){assert.ok(mid.tip[1]>start.tip[1]+0.0001,'rescue handle moves upwards toward panel open arrow');await page.screenshot({path:path.join(dir,`task-${id}-rescue-moving.png`)});} assert.notDeepEqual(mid.quaternion,start.quaternion,'rotation follows held pointer '+start.id); if(!operation.effects?.some(effect=>effect.offset))assert.ok(Math.hypot(...mid.pivot.map((v,i)=>v-start.pivot[i]))<0.0001,'hinge remains anchored '+start.id); } await page.mouse.move(point.x-175,point.y,{steps:8}); } await page.mouse.up(); if(process.env.LATE_PREPARATION==='1'&&start.step===4&&start.operation===1&&!prepared.has(start.step)){ await expect(page.locator('.training-server-preparation')).toHaveAttribute('open','',{timeout:20000}); await expect.poll(async()=>!(await snapshot()).pending,{timeout:20000}).toBe(true); const rejected=await snapshot(); assert.deepEqual(rejected.lid,start.lid,'preparation rejection must keep the already opened lid in place'); assert.equal(rejected.operation,1);assert.equal(rejected.evidenceCount,1);assert.equal(rejected.complete,false); assert.deepEqual(rejected.quaternion,start.quaternion,'unaccepted rescue rotation is rolled back'); for(const box of await page.locator('[data-safety]').all())await box.check(); for(let save=0;save<2;save++){ await page.locator('[data-save-preparation]').click(); await expect(page.locator('[data-save-status]')).toContainText('已保留'); assert.deepEqual((await snapshot()).lid,start.lid,'saving preparation preserves the open lid'); } await page.locator('[data-close-preparation]').click();prepared.add(start.step); await page.screenshot({path:path.join(dir,`task-${id}-late-preparation-lid-open.png`)}); checks.push({name:'late preparation: rejected switch resets only its own motion; open lid survives repeated saves',pass:true,lid:start.lid}); continue; } await expect.poll(async()=>{const s=await snapshot();return !s.pending&&(s.complete||s.operation!==start.operation);},{timeout:20000}).toBe(true); await page.waitForTimeout(850);const done=await snapshot();if(start.tip)assert.ok(done.tip[1]>start.tip[1]+0.0001,'completed rescue switch stays upwards');assert.equal(done.errors,0,'no false operation error'); if(process.env.MANUAL_OCCLUSION_TOOLS==='1'&&await page.locator('[data-action="toggle-hide-others"]').getAttribute('aria-pressed')==='true'){ await page.locator('[data-viewer-action="tools"]').click();await page.locator('[data-action="toggle-hide-others"]').click();await page.locator('[data-viewer-action="tools"]').click(); } checks.push({name:'task '+id+' step '+(start.step+1)+' operation '+(start.operation+1),target:start.id,drag:!!draggable,distance:start.distance,projected:start.projected,labels:start.labels.length}); console.log('ACTION',id,start.step+1,start.operation+1,start.id); if(++gestures>120)throw new Error('unexpected operation loop'); if(done.complete){ await page.screenshot({path:path.join(dir,`task-${id}-step-${start.step+1}.png`)}); if(start.step+1>=total)break; await page.locator('[data-action="next"]').click();await page.waitForTimeout(1200); } } const after=await call('tran/v1/teaching/assignments/'+id,signed.accessToken); assert.deepEqual(after.data.definitionSnapshot,assignment.data.definitionSnapshot);assert.equal(after.data.version,assignment.data.version); } } assert.deepEqual(errors,[]); await fs.writeFile(path.join(dir,resultFile),JSON.stringify({pass:true,checks,errors,writes},null,2)); console.log('PASS',checks.length); }catch(error){await page.screenshot({path:path.join(dir,'failure.png')}).catch(()=>{});await fs.writeFile(path.join(dir,resultFile),JSON.stringify({pass:false,error:error.message,checks,state:await snapshot(),errors,writes,networkErrors,pending:[...pending.values()]},null,2));throw error;} finally{await browser.close();}