Files
CADmind/frontend/tests/studio-tools-browser.cjs
2026-09-24 10:40:57 +08:00

14 lines
4.6 KiB
JavaScript

// Real STL inspection and mocked modification transport; no model API calls.
const {chromium}=require(process.env.PLAYWRIGHT_PATH||'playwright');const assert=require('node:assert/strict');
(async()=>{const b=await chromium.launch({channel:'chrome',headless:true}),p=await b.newPage({viewport:{width:1600,height:1000}}),errors=[];p.on('pageerror',e=>errors.push(e.message));await p.goto(process.env.CADMIND_URL||'http://127.0.0.1:8093/',{waitUntil:'networkidle'});assert.equal(await p.locator('[data-tool="hole"]').isDisabled(),true);assert.equal(await p.locator('.view-gizmo').isVisible(),false);await p.locator('.part-row').filter({hasText:'Z型折弯支架'}).click();await p.waitForFunction(()=>studioState().viewerMode==='solid');const part=await p.evaluate(()=>studioState().activePart);await p.waitForTimeout(300);await p.screenshot({path:'/tmp/lingxin-tools.png'});
await p.locator('[data-tool="measure"]').click();const rect=await p.locator('#viewport').boundingBox();let count=0;
for(const dy of [.47,.53,.59,.65]){for(const dx of [.36,.42,.48]){await p.mouse.click(rect.x+rect.width*dx,rect.y+rect.height*dy);count=await p.evaluate(()=>studioState().measurementPoints.length);if(count===2)break;}if(count===2)break;}
assert.equal(count,2,'Two surface picks');let state=await p.evaluate(()=>studioState());let expected=Math.hypot(...state.measurementPoints[0].map((v,i)=>v-state.measurementPoints[1][i]))/state.modelScale;assert.ok(expected>0);assert.ok(Math.abs(expected-state.measurement)<1e-8);await p.screenshot({path:'/tmp/lingxin-measure.png'});
await p.locator('[data-tool="section"]').click();assert.equal(await p.evaluate(()=>studioState().sectionEnabled),true);await p.getByLabel('剖切方向',{exact:true}).selectOption('z');await p.getByLabel('剖切位置',{exact:true}).fill('30');await p.getByRole('button',{name:'反向剖切',exact:true}).click();await p.screenshot({path:'/tmp/lingxin-section.png'});await p.getByRole('button',{name:'关闭零件工具',exact:true}).click();assert.equal(await p.evaluate(()=>studioState().sectionEnabled),false);
const response=await (await p.request.get(new URL('/api/parts/'+part,p.url()).href)).json(),sent=[];
await p.route('**/api/tasks',async r=>{sent.push(r.request().postDataJSON());await r.fulfill({json:{ok:true,task_id:'tools-test'}})});await p.route('**/api/tasks/tools-test',r=>r.fulfill({json:{done:true,response}}));
await p.locator('[data-tool="size"]').click();await p.getByRole('button',{name:'应用修改并生成',exact:true}).click();assert.equal(sent.length,0);await p.getByLabel('X 总尺寸 (mm)',{exact:true}).fill('85');await p.getByRole('button',{name:'应用修改并生成',exact:true}).click();await p.waitForFunction(()=>!studioState().busy&&studioState().viewerMode==='solid');assert.equal(sent[0].action,'modify');assert.equal(sent[0].part_name,part);assert.match(sent[0].text,/X 方向总包络尺寸改为 85 mm/);
await p.locator('[data-tool="hole"]').click();await p.getByLabel('孔径 (mm)',{exact:true}).fill('4');await p.getByRole('button',{name:'在零件表面选取位置',exact:true}).click();await p.mouse.click(rect.x+rect.width*.42,rect.y+rect.height*.53);assert.ok(await p.evaluate(()=>studioState().selectedPoint));await p.getByLabel('孔类型',{exact:true}).selectOption('blind');await p.getByLabel('孔深 (mm)',{exact:true}).fill('2');await p.screenshot({path:'/tmp/lingxin-hole.png'});await p.getByRole('button',{name:'应用修改并生成',exact:true}).click();await p.waitForFunction(()=>!studioState().busy&&studioState().viewerMode==='solid');assert.match(sent[1].text,/盲孔,深度 2 mm/);assert.match(sent[1].text,/原始 STL 坐标/);
await p.locator('[data-tool="fillet"]').click();await p.getByLabel('圆角半径 (mm)',{exact:true}).fill('1.5');await p.getByLabel('需要处理的边缘',{exact:true}).fill('底板四周外边缘,保留孔口');await p.getByRole('button',{name:'应用修改并生成',exact:true}).click();await p.waitForFunction(()=>!studioState().busy&&studioState().viewerMode==='solid');assert.match(sent[2].text,/半径 1.5 mm/);assert.equal(sent.length,3);
await p.getByRole('button',{name:'新建零件',exact:true}).click();assert.equal(await p.locator('[data-tool="measure"]').isDisabled(),true);await p.setViewportSize({width:390,height:844});assert.equal(await p.evaluate(()=>document.documentElement.scrollWidth>innerWidth),false);assert.deepEqual(errors,[]);console.log('PASS disabled empty state, real mesh measurement in mm, clipping/reversal/restoration, dimensions/hole/fillet sent to existing modify pipeline (mocked API, no model charges), mobile');await b.close()})().catch(e=>{console.error(e);process.exit(1)});