import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import { randomUUID } from 'node:crypto'; import { chromium } from 'playwright'; import { Store } from '../src/server/store.mjs'; import { launchWorkbench } from '../tests/helpers.mjs'; const root = path.resolve('.qa', 'reading-s-ui-' + randomUUID()), data = path.join(root, 'data'); fs.mkdirSync(root, { recursive: true }); const store = new Store(data), cases = [], checks = [], errors = [], disposers = []; const t = { after: fn => disposers.push(fn) }; const appRoot = path.resolve(process.env.AGENT_WORKBENCH_QA_APP_ROOT ?? '.'); const receipt = { schemaVersion: 1, reservedTokens: 29623, routes: [{ sources: [{ runId: 'owned-fixture', attemptId: 'owned-observation' }] }] }; for (const kind of ['run', 'task', 'context', 'historical']) { const directory = path.join(root, 'fixture-' + kind); fs.mkdirSync(directory); fs.writeFileSync(path.join(directory, 'README.md'), 'Owned UI fixture\n'); const project = store.addProject(directory), session = store.createSession(project.id); const goal = '自有界面验收样例:核对已保存的暂停原因。没有请求真实模型。'; const run = store.beginRun(session, goal, false, { ...store.config, contextLimit: 16000, maxOutputTokens: 2048 }); store.message(session, 'user', goal); session.providerMessages.push({ role: 'user', content: goal }); const reason = kind === 'task' ? 'task_token_limit' : kind === 'context' ? 'context_limit' : 'token_limit'; if (kind !== 'historical') run.budgetPause = { schema: 1, reason, taskId: run.taskId, runId: run.id, requiredInputTokens: kind === 'context' ? 13300 : 12352, inputBudget: 12352, contextLimit: 16000, outputReservation: 2048, safetyTokens: 1600, runRemainingTokens: kind === 'task' ? 50000 : 17429, taskRemainingTokens: kind === 'task' ? 17429 : 200000, ...(kind !== 'context' ? { pauseKind: 'request_reservation', remainingTokens: 17429, tokenReservation: receipt } : {}) }; session.status = 'paused'; store.message(session, 'system', '自有样例原暂停记录,历史未改写。'); store.finishRun(session, run, reason); cases.push({ kind, projectId: project.id, sessionId: session.id, taskId: run.taskId, runId: run.id }); } store.close(); let browser, host, report; const visible = async locator => { await locator.waitFor({ state: 'visible', timeout: 15000 }); assert.ok(await locator.isVisible()); }; try { host = await launchWorkbench(t, data, {}, { appRoot }); assert.notEqual(new URL(host.url).port, '4318'); 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, { waitUntil: 'networkidle' }); for (const item of cases) { const project = page.getByLabel('当前项目', { exact: true }); if (!await project.isVisible()) await page.getByRole('button', { name: '展开项目栏', exact: true }).click(); await project.selectOption(item.projectId); const panel = page.getByLabel('任务预算诊断', { exact: true }); await visible(panel); const title = item.kind === 'run' ? '下一次请求超出运行余额' : item.kind === 'task' ? '下一次请求超出任务余额' : item.kind === 'context' ? '上下文窗口不足' : '本次运行预算不足'; await visible(panel.getByRole('heading', { name: title, exact: true })); const text = await panel.innerText(); if (['run', 'task'].includes(item.kind)) { assert.match(text, /输入可容纳/); assert.match(text, /29,623/); assert.match(text, /17,429/); assert.match(text, /12,352/); } if (item.kind === 'historical') { assert.match(text, /旧记录没有的预算分解不补造/); assert.ok(!text.includes('输入可容纳')); } await panel.scrollIntoViewIfNeeded(); await page.screenshot({ path: path.join(root, item.kind + '-' + width + '.png'), fullPage: true }); fs.writeFileSync(path.join(root, item.kind + '-' + width + '.txt'), text); const overflow = await page.evaluate(() => document.documentElement.scrollWidth - innerWidth); assert.ok(overflow <= 1, String(overflow)); if (item.kind === 'run') { await panel.getByRole('button', { name: '核对预算并续接原任务', exact: true }).click(); const dialog = page.getByRole('dialog', { name: '续接原任务', exact: true }); await visible(dialog); await dialog.getByRole('button', { name: '准备续接预览', exact: true }).click(); await visible(dialog.getByRole('status')); assert.match(await dialog.getByRole('status').innerText(), /将创建原Task的新Run/); const box = await dialog.boundingBox(); assert.ok(box.x >= 0 && box.x + box.width <= width + 1); await page.screenshot({ path: path.join(root, 'preview-' + width + '.png'), fullPage: true }); await dialog.getByRole('button', { name: '关闭窗口', exact: true }).click(); const current = await host.api('/sessions/' + item.sessionId); assert.equal(current.taskId, item.taskId); assert.equal(current.runId, item.runId); assert.equal(current.execution.task.usage.requests, 0); await page.reload({ waitUntil: 'networkidle' }); await visible(panel.getByRole('heading', { name: title, exact: true })); checks.push(width + ': preview and cancel preserve original Task/Run and do not dispatch'); } checks.push(width + ': ' + item.kind + ' diagnosis and persistence'); } await page.close(); } assert.deepEqual(errors, []); report = { success: true, appRoot, checks, cases, errors, realModelRequests: 0, fixtureModelRequests: 0 }; } catch (error) { report = { success: false, appRoot, checks, cases, 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(root, 'result.json'), JSON.stringify(report, null, 2) + '\n'); console.log(JSON.stringify({ ...report, root })); }