import fs from 'node:fs'; import path from 'node:path'; import http from 'node:http'; import assert from 'node:assert/strict'; import { randomUUID, createHash } from 'node:crypto'; import { chromium, expect } from 'playwright/test'; import { fixture, launchWorkbench, call, completion, until } from '../tests/helpers.mjs'; import { Provider } from '../src/server/provider.mjs'; if (process.argv.includes('--help')) { console.log('node scripts/verify-task-control-ui.mjs [--app-root DIR] - isolated fixed HTTP task/review/diagnostic/protocol flows at desktop and 390px; no user project or real model.'); process.exit(0); } const args = process.argv.slice(2); assert.ok(!args.length || args.length === 2 && args[0] === '--app-root' && path.isAbsolute(args[1])); const appRoot = fs.realpathSync.native(args[1] ?? process.cwd()); const directory = path.resolve('.qa', 'task-control-ui-' + randomUUID()); fs.mkdirSync(directory, { recursive: true }); const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t); fs.writeFileSync(path.join(f.projectRoot, 'package.json'), JSON.stringify({ name: 'owned-task-control', type: 'module', scripts: { test: 'node --test wrong.test.mjs' } })); fs.writeFileSync(path.join(f.projectRoot, 'wrong.test.mjs'), "import test from 'node:test'; import assert from 'node:assert/strict'; test('owned failing check',()=>assert.equal(1,2));\n"); const originalSource = fs.readFileSync(path.join(f.projectRoot, 'wrong.test.mjs')); let mode = 'html', stage = 0, sessionId = '', page, browser; const requests = [], passed = [], screenshots = [], pageErrors = []; const model = http.createServer(async (request, response) => { try { const chunks = []; for await (const chunk of request) chunks.push(chunk); const input = JSON.parse(Buffer.concat(chunks)); requests.push({ mode, input }); let tools = [], content = '当前结果已说明。', usage = { prompt_tokens: 20, completion_tokens: 10, total_tokens: 30 }; if (mode === 'html' && stage === 0) tools = [call(randomUUID(), 'inspect_project', ''), call(randomUUID(), 'propose_file', { path: 'bike.html', content: 'SVG 骑行', reason: '按照原要求创建文件' })]; if (mode === 'checks') { const plan = input.messages.filter(value => value.role === 'tool').map(value => { try { return JSON.parse(value.content); } catch { return {}; } }).findLast(value => value.checks && value.id); tools = stage % 2 === 0 ? [call(randomUUID(), 'plan_checks', { names: ['test'] })] : [call(randomUUID(), 'run_checks', { plan_id: plan.id }), call(randomUUID(), 'read_file', { path: 'wrong.test.mjs' })]; } if (mode === 'diagnostic') { assert.ok(!input.tools.some(value => ['propose_file', 'run_command', 'run_checks'].includes(value.function.name))); if (stage === 0) tools = [call(randomUUID(), 'inspect_session', '')]; content = '这是原任务的工程检查失败;诊断没有修改源码或重跑检查,原目标仍然保留。'; } if (mode === 'invalid') { tools = [call(randomUUID(), 'inspect_project', '{}'), call('required-read-empty', 'read_file', '')]; usage = { prompt_tokens: 307, completion_tokens: 19, total_tokens: 326 }; } stage++; response.writeHead(200, { 'Content-Type': 'application/json' }); response.end(JSON.stringify({ ...completion(tools, tools.length ? '' : content), usage })); } catch (error) { response.writeHead(500, { 'Content-Type': 'application/json' }); response.end(JSON.stringify({ error: { message: error.message } })); } }); await new Promise(resolve => model.listen(0, '127.0.0.1', resolve)); t.after(() => new Promise(resolve => { model.closeAllConnections(); model.close(resolve); })); const provider = new Provider(), hub = provider.modelHub(f.store); const connection = hub.upsert({ name: '本地协议验收连接', model: 'owned-actual-model', baseUrl: 'http://127.0.0.1:' + model.address().port + '/v1', requiresKey: false }); hub.select({ type: 'connection', id: connection.id }); f.store.close(); const state = () => host.api('/sessions/' + sessionId); const panel = () => page.getByLabel('任务记录', { exact: true }); const waitStatus = status => expect.poll(async () => (await state()).status, { timeout: 20000 }).toBe(status); async function newTask(content) { if (!await page.getByRole('button', { name: '新建任务', exact: true }).isVisible()) await page.getByRole('button', { name: '展开项目栏' }).click(); await page.getByRole('button', { name: '新建任务', exact: true }).click(); await page.getByLabel('任务内容').fill(content); await page.getByRole('button', { name: '发送任务', exact: true }).click(); await until(async () => { sessionId = (await host.api('/sessions')).find(value => value.title === content)?.id; return !!sessionId; }); } async function capture(name) { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, fullPage: true }); fs.writeFileSync(path.join(directory, name + '.html'), await page.content()); screenshots.push({ path: file, sha256: createHash('sha256').update(fs.readFileSync(file)).digest('hex') }); } const mark = value => { passed.push(value); console.log('PASS ' + value); }; const host = await launchWorkbench(t, f.data, {}, { appRoot, nodePath: fs.existsSync(path.join(appRoot, 'runtime/node.exe')) ? path.join(appRoot, 'runtime/node.exe') : process.execPath }); try { browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage({ viewport: { width: 1440, height: 1000 } }); page.on('pageerror', error => pageErrors.push(error.message)); await page.goto(host.url); await page.waitForLoadState('networkidle'); await page.getByLabel('当前项目').selectOption(f.project.id); await newTask('创建 HTML 和 SVG,无需测试'); await waitStatus('needs_review'); await page.locator('.inspector-tabs').getByRole('button', { name: /改动/ }).click(); await page.getByRole('button', { name: '应用这项改动', exact: true }).click(); await waitStatus('completed'); await expect(page.getByRole('button', { name: '继续原任务', exact: true })).toHaveCount(0); const delivered = await state(); assert.equal(delivered.approvals.length, 0); assert.equal(delivered.execution.checks.length, 0); await page.getByRole('button', { name: '查看任务记录', exact: true }).click(); await expect(panel()).toContainText('文件已应用'); await expect(panel()).toContainText('未运行测试'); await expect(panel()).toContainText('owned-actual-model'); await capture('html-desktop'); mark('HTML applies once, ends without tests or continuation prompt, and shows actual model'); await page.setViewportSize({ width: 390, height: 844 }); await expect(page.getByRole('button', { name: '查看文件', exact: true })).toBeVisible(); await page.getByRole('button', { name: '查看任务记录', exact: true }).click(); await expect(panel()).toBeVisible(); await expect(panel()).toContainText('文件已应用'); await capture('html-390'); assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)); mark('390px result and task panel remain usable without horizontal overflow'); await page.setViewportSize({ width: 1440, height: 1000 }); mode = 'checks'; stage = 0; await newTask('修复失败测试并交付工程'); await waitStatus('awaiting_approval'); await page.getByRole('button', { name: '确认执行', exact: true }).click(); await waitStatus('paused'); const paused = await state(); assert.equal(paused.execution.run.stopReason, 'no_progress'); assert.equal(paused.approvals.length, 1); assert.equal(paused.execution.checks.length, 1); assert.equal(paused.execution.checks[0].status, 'failed'); assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'wrong.test.mjs')), originalSource); const taskId = paused.taskId, checkId = paused.execution.checks[0].id; await page.getByRole('button', { name: '查看任务记录', exact: true }).click(); await expect(panel()).toContainText('原工程检查有失败记录'); await capture('checks-desktop'); mark('one approved real failing check is reused across new plans and the no-progress task pauses'); mode = 'diagnostic'; stage = 0; await page.getByLabel('任务内容').fill('前面的错误是怎么回事?'); await page.getByRole('button', { name: '发送任务', exact: true }).click(); await until(async () => { const value = await state(); return value.execution.run.purpose === 'diagnostic' && !!value.execution.run.finishedAt; }, 15000); const diagnosed = await state(); assert.equal(diagnosed.taskId, taskId); assert.equal(diagnosed.status, 'paused'); assert.equal(diagnosed.execution.run.status, 'completed'); assert.equal(diagnosed.execution.checks[0].id, checkId); assert.equal(diagnosed.execution.task.instructions.length, 0); await expect(panel()).toContainText('只读诊断'); await expect(panel()).toContainText('修复失败测试并交付工程'); await capture('diagnostic-desktop'); await page.setViewportSize({ width: 390, height: 844 }); await expect(page.getByRole('button', { name: '查看文件', exact: true })).toBeVisible(); await page.getByRole('button', { name: '查看任务记录', exact: true }).click(); await expect(panel()).toBeVisible(); await expect(panel()).toContainText('只读诊断'); await capture('diagnostic-390'); mark('diagnostic answer completes separately and preserves original unfinished task at both widths'); await page.setViewportSize({ width: 1440, height: 1000 }); mode = 'invalid'; stage = 0; await newTask('协议失败用量核对'); await waitStatus('failed'); const failed = await state(); assert.equal(failed.execution.run.toolCalls, 0); assert.equal(failed.execution.run.usage.totalTokens, 326); assert.equal(failed.execution.run.usage.requests, 1); await expect(page.locator('.failure-card').last()).toContainText('read_file'); await page.getByRole('button', { name: '查看任务记录', exact: true }).click(); await expect(panel()).toContainText('326 tokens'); await expect(panel()).toContainText('1 次请求尝试'); await capture('protocol-desktop'); await page.reload(); await page.waitForLoadState('networkidle'); assert.equal((await state()).execution.run.usage.totalTokens, 326); mark('required empty arguments reject the entire batch and reported usage remains once after reload'); assert.deepEqual(pageErrors, []); fs.writeFileSync(path.join(directory, 'sessions.json'), JSON.stringify({ delivered, paused, diagnosed, failed }, null, 2)); const result = { success: true, version: host.bootstrap.version, appRoot, directory, passed, screenshots, pageErrors, requestCount: requests.length, realModelRequests: 0 }; fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2)); fs.writeFileSync('.qa/task-control-ui-validation.json', JSON.stringify(result, null, 2)); console.log(JSON.stringify(result)); } catch (error) { if (page) await capture('failure').catch(() => {}); fs.writeFileSync(path.join(directory, 'failure.json'), JSON.stringify({ success: false, mode, stage, passed, screenshots, error: error.stack, pageErrors }, null, 2)); throw error; } finally { if (browser) await browser.close(); for (const dispose of disposers.reverse()) await dispose(); }