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

46 lines
3.6 KiB
JavaScript

// Run with PLAYWRIGHT_PATH pointing at an installed Playwright package; mocks every generation request.
const {chromium}=require(process.env.PLAYWRIGHT_PATH || 'playwright');
const assert=require('node:assert/strict');
(async()=>{
const browser=await chromium.launch({channel:'chrome',headless:true});
const page=await browser.newPage({viewport:{width:1100,height:850}});
const requests=[];
await page.route('**/api/tasks', async route=>{
requests.push(route.request().postDataJSON());
await route.fulfill({json:{ok:true,task_id:'fixture'}});
});
await page.route('**/api/tasks/fixture',async route=>route.fulfill({json:{done:true,response:{
ok:false,status:'needs_clarification',message:'请选择影响设计的方案。',part_name:'quality_fixture',can_clarify:true,can_resume:false,
questions:['采用哪种装配方式?','两个尺寸要求以哪个为准?'],
question_items:[{question:'采用哪种装配方式?',reason:'你同时要求完全封闭和徒手拆装,需要明确连接方式。',options:['分体卡扣连接','分体螺钉连接']},
{question:'两个尺寸要求以哪个为准?',reason:'图纸和文字给出的整体宽度不同。',options:['以文字要求为准','以图纸标注为准']}]
}}}));
await page.goto(process.env.CADMIND_URL || 'http://127.0.0.1:8092/',{waitUntil:'domcontentloaded'});
await page.locator('#text').count().then(async n=>{});
const input=page.locator('textarea').last();await input.fill('测试分步设计选择');
await page.getByRole('button',{name:'发送',exact:true}).click();
const panel=page.locator('.decision-panel').first();
await panel.getByRole('heading',{name:'采用哪种装配方式?'}).waitFor();
assert.equal(await panel.getByRole('button',{name:'下一步',exact:true}).isDisabled(),true);
await panel.getByRole('button',{name:'分体卡扣连接',exact:true}).click();
assert.equal(requests.length,1); // choosing never starts CAD
await panel.getByRole('button',{name:'下一步',exact:true}).click();
await panel.getByRole('button',{name:'以图纸标注为准',exact:true}).click();
await panel.getByRole('textbox',{name:'自行输入要求'}).fill('以文字宽度为准,保留图纸的孔位布局');
await panel.getByRole('button',{name:'上一步',exact:true}).click();
assert.equal(await panel.getByRole('button',{name:'分体卡扣连接',exact:true}).getAttribute('aria-pressed'),'true');
await panel.getByRole('button',{name:'下一步',exact:true}).click();
assert.equal(await panel.getByRole('textbox',{name:'自行输入要求'}).inputValue(),'以文字宽度为准,保留图纸的孔位布局');
await page.screenshot({path:process.env.SCREENSHOT_PATH || '/tmp/cadmind-clarification.png',fullPage:true});
await panel.getByRole('button',{name:'提交并继续生成',exact:true}).click();
await page.waitForFunction(()=>document.querySelector('.decision-panel')?.textContent.includes('已提交'));
assert.equal(requests.length,2);assert.equal(requests[1].action,'resume');
assert.match(requests[1].clarification,/分体卡扣连接/);
assert.match(requests[1].clarification,/以文字宽度为准,保留图纸的孔位布局/);
assert.doesNotMatch(requests[1].clarification,/用户选择或补充:以图纸标注为准/);
await page.setViewportSize({width:390,height:844});
assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth<=window.innerWidth),true);
console.log('PASS: no preselection; steps; back preserves answers; free text overrides option; one resume request; mobile width');
await browser.close();
})().catch(e=>{console.error(e);process.exit(1);});