import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import {randomUUID} from 'node:crypto'; import {chromium,expect} from 'playwright/test'; import {fixture,compatibleFixture,launchWorkbench,until} from '../tests/helpers.mjs'; import {observedTaskModels} from './live-model-observation.mjs'; const directory=path.resolve('.qa','model-budget-ui-'+randomUUID());fs.mkdirSync(directory,{recursive:true}); const disposers=[],t={after:fn=>disposers.push(fn)},f=fixture(t),screenshots=[],cases=[],errors=[]; let host,browser,report,state; try{ const model=await compatibleFixture(t,()=>({status:503,body:{error:{message:'Owned UI budget failure'}, usage:{prompt_tokens:55000,completion_tokens:17,total_tokens:55017}}})); f.store.close();host=await launchWorkbench(t,f.data);assert.notEqual(new URL(host.url).port,'4318'); await host.api('/config',{...host.bootstrap.config,contextLimit:16000,maxOutputTokens:512,maxRunTokens:35000,maxTaskTokens:1000000}); const connection=await host.api('/model-center/connections',{name:'自有预算验收模型',model:'owned-budget-ui',baseUrl:model.baseUrl,requiresKey:false}); await host.api('/model-center/select',{type:'connection',id:connection.id}); const route='/sessions/'+f.session.id; await host.api(route+'/messages',{content:'读取README,不修改文件,不运行检查。'}); await until(async()=>{state=await host.api(route);return !['running','awaiting_approval','stopping'].includes(state.status);},15000); assert.equal(state.status,'paused',JSON.stringify({status:state.status,failure:state.failure?.message,stopReason:state.execution.run?.stopReason,usage:state.execution.task?.usage,budget:state.execution.run?.budget})); assert.equal(state.execution.run.stopReason,'token_limit');assert.equal(model.requests.length,1); assert.equal(observedTaskModels(state).model,'owned-budget-ui'); browser=await chromium.launch({channel:'msedge',headless:true}); for(const width of [1440,390]){ const page=await browser.newPage({viewport:{width,height:1000}});page.on('pageerror',error=>errors.push(error.message)); await page.goto(host.url);await page.waitForLoadState('networkidle'); const project=page.getByLabel('当前项目',{exact:true}); if(!await project.isVisible())await page.getByRole('button',{name:'展开项目栏',exact:true}).click(); await project.selectOption(f.project.id); const panel=page.getByLabel('任务预算诊断',{exact:true});await expect(panel).toBeVisible(); for(const text of ['本次运行上游报告用量','55,017','35,000','20,017','用量未知请求 / 本地预留','0 / 0','单个在途响应可能超过预估'])await expect(panel).toContainText(text); await panel.getByRole('button',{name:'核对预算并续接原任务',exact:true}).click(); const dialog=page.getByRole('dialog',{name:'续接原任务',exact:true}); await dialog.getByLabel('新Run累计tokens',{exact:true}).fill('100000'); await dialog.getByRole('button',{name:'准备续接预览',exact:true}).click();await expect(dialog.getByRole('status')).toContainText('将创建原Task的新Run'); assert.equal(model.requests.length,1,'A budget preview cannot dispatch another model request.'); const box=await dialog.boundingBox();assert.ok(box.x>=0&&box.x+box.width<=width+1); await dialog.getByRole('button',{name:'关闭窗口',exact:true}).click();await page.reload();await expect(panel).toContainText('20,017'); const current=await host.api(route);assert.equal(current.execution.task.id,state.execution.task.id);assert.equal(current.execution.task.usage.requests,1); assert.equal(current.execution.run.consumption.length,1);assert.equal(model.requests.length,1); assert.ok(await page.evaluate(()=>document.documentElement.scrollWidth<=innerWidth+1),'No viewport overflow'); await panel.scrollIntoViewIfNeeded();const screenshot=path.join(directory,'reported-overrun-'+width+'.png'); await page.screenshot({path:screenshot,fullPage:true});screenshots.push(screenshot); fs.writeFileSync(path.join(directory,'reported-overrun-'+width+'.html'),await page.content()); cases.push(width+': actual reported overrun persists, zero unknown use stays distinct, and preview does not dispatch or replace the original Task');await page.close(); } assert.deepEqual(errors,[]);fs.writeFileSync(path.join(directory,'session.json'),JSON.stringify(await host.api(route),null,2)); report={passed:true,cases,screenshots,errors,realModelRequests:0,ownedHttpRequests:model.requests.length, modelObservation:observedTaskModels(state),scope:'Actual compiled host and owned HTTP reported overrun, persistence and preview UI; real-model autonomy/Purge benefit remain separate.'}; }catch(error){if(state)fs.writeFileSync(path.join(directory,'failed-state.json'),JSON.stringify(state,null,2));report={passed:false,cases,screenshots,errors,error:error.stack,realModelRequests:0};process.exitCode=1;} finally{ await browser?.close();await host?.close();for(const dispose of disposers.reverse())await dispose(); fs.writeFileSync(path.join(directory,'result.json'),JSON.stringify(report,null,2));console.log(JSON.stringify({...report,directory})); }