import fs from 'node:fs'; import path from 'node:path'; import net from 'node:net'; import assert from 'node:assert/strict'; import { randomUUID, createHash } from 'node:crypto'; import { chromium, _electron } from 'playwright'; import { fixture, compatibleFixture, completion, call, until, launchWorkbench } from '../tests/helpers.mjs'; import { createDeliveryProject } from '../tests/fixtures/delivery-project.mjs'; import { ownerBytes } from '../tests/fixtures/workers-project.mjs'; import { HostController } from '../src/core/desktop/host-controller.mjs'; import { desktopExecutableIdentity } from '../src/core/desktop/app-installation.mjs'; import { projectEnvironment } from '../src/core/process-environment.mjs'; import { openWorkbenchTool } from './ui-navigation.mjs'; const desktop = process.argv.includes('--desktop'), appRoot = path.resolve(process.env.AGENT_WORKBENCH_QA_APP_ROOT || '.'); const directory = path.resolve(process.env.ROLE_ROUTING_QA_OUTPUT || '.qa/role-routing-f', (desktop ? 'electron-' : 'web-') + randomUUID()); fs.mkdirSync(directory, { recursive: true }); const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t, { modernDefaults: true }); createDeliveryProject(f, { ownerChanges: true }); f.session.title = 'F 自有角色协作验收'; f.store.saveConfig({ approvalMode: 'full' }); f.store.save(f.session); const owner = ownerBytes(f); f.store.close(); const local = await compatibleFixture(t, (_url, body) => ({ ...completion(body.messages.some(row => row.role === 'tool') ? [] : [call('owned-' + body.model, 'propose_file', { path: body.model === 'owned-a' ? 'src/a.mjs' : 'src/b.mjs', content: 'export const value = ' + (body.model === 'owned-a' ? '11' : '22') + ';\n', reason: '本机合成工程提案' })], '自称完成不认证要求。'), model: body.model })); const passed = [], screenshots = [], errors = []; let host, browser, application, controller, page, window, result; const save = (name, value) => fs.writeFileSync(path.join(directory, name), JSON.stringify(value, null, 2), { flag: 'wx' }); const mark = text => { passed.push(text); console.log('PASS ' + text); }; const panel = () => page.getByLabel('隔离协作控制台', { exact: true }); async function shot(name) { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file }); screenshots.push(file); } async function resize(width, height) { if (window) { await window.evaluate((win, value) => win.setContentSize(value.width, value.height), { width, height }); await until(() => page.evaluate(value => innerWidth === value, width)); } else await page.setViewportSize({ width, height }); } async function group(id) { return (await host.api('/sessions/' + f.session.id + '/collaborations')).groups.find(row => row.id === id); } try { if (desktop) { const socket = net.createServer(); await new Promise(resolve => socket.listen(0, '127.0.0.1', resolve)); const port = socket.address().port; await new Promise(resolve => socket.close(resolve)); assert.notEqual(port, 4318); controller = new HostController({ appRoot, dataRoot: f.data, port, nodePath: process.execPath, timeoutMs: 60000 }); await controller.start(); host = { url: controller.url, api: (route, input) => controller.api(route, input === undefined ? {} : { method: 'POST', input }) }; const executable = desktopExecutableIdentity(appRoot).path; application = await _electron.launch({ executablePath: executable, args: [...(executable.toLowerCase().endsWith('agentworkbench.exe') ? [] : [path.join(appRoot, 'dist-desktop/main.cjs')]), '--app-root', appRoot, '--data-root', f.data, '--node', process.execPath, '--port', String(port), '--hidden-window'], cwd: appRoot, env: projectEnvironment(), timeout: 60000 }); page = await application.firstWindow(); window = await application.browserWindow(page); await window.evaluate(win => { win.showInactive(); win.setContentSize(1440, 1080); }); } else { host = await launchWorkbench(t, f.data, {}, { appRoot }); assert.notEqual(new URL(host.url).port, '4318'); browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage({ viewport: { width: 1440, height: 1080 } }); await page.goto(host.url); } page.on('pageerror', error => errors.push(error.message)); await page.locator('.connection.connected').waitFor(); const a = await host.api('/model-center/connections', { name: 'F 工程模型', model: 'owned-a', baseUrl: local.baseUrl, requiresKey: false, nativeParameters: { reasoning_effort: 'high' } }); const b = await host.api('/model-center/connections', { name: 'F 测试模型', model: 'owned-b', baseUrl: local.baseUrl, requiresKey: false, nativeParameters: { reasoning_effort: 'low' } }); const messages = await host.api('/model-center/connections', { name: 'F Messages', model: 'owned-messages', protocol: 'anthropic-messages', baseUrl: local.baseUrl, requiresKey: false, maxOutputTokens: 98304 }); await host.api('/model-center/select', { type: 'connection', id: a.id }); await page.getByLabel('当前项目').selectOption(f.project.id); await page.locator('.session-list button').filter({ hasText: f.session.title }).click(); await openWorkbenchTool(page, '隔离协作'); await panel().getByLabel('父目标', { exact: true }).fill('F 本机合成:两个明确职责和模型'); for (let index = 0; index < 2; index++) { const lane = panel().locator('fieldset').nth(index); await lane.getByLabel('任务名称', { exact: true }).fill('F 子任务 ' + index); await lane.getByLabel('职责', { exact: true }).fill(index ? '测试完善' : '工程实现'); await lane.getByLabel('独立子目标', { exact: true }).fill('修改自己的模块 ' + index); await lane.getByLabel('允许修改的文件或目录', { exact: true }).fill(index ? 'src/b.mjs' : 'src/a.mjs'); await lane.getByLabel('交付要求(每行一项)', { exact: true }).fill('保持接口兼容\n验证实际修改'); if (index) await lane.getByLabel('执行模型', { exact: true }).selectOption('connection:' + b.id); } assert.equal(await panel().locator('.worker-budget-form').count(), 0); const fail = route => route.fulfill({ status: 503, contentType: 'application/json', body: JSON.stringify({ error: 'F 合成暂时保存失败' }) }); await page.route('**/api/sessions/*/collaborations', route => route.request().method() === 'POST' ? fail(route) : route.continue()); await panel().getByRole('button', { name: '封存分工计划', exact: true }).click(); await panel().getByRole('alert').waitFor(); assert.equal(await panel().locator('fieldset').nth(1).getByLabel('执行模型', { exact: true }).inputValue(), 'connection:' + b.id); await page.unroute('**/api/sessions/*/collaborations'); mark('failed plan submission keeps role, requirements, scope and independent model input; unlimited mode shows no quota editor'); for (const palette of ['green', 'blue', 'purple']) { await page.evaluate(value => { localStorage.setItem('orven-palette', value); document.documentElement.dataset.palette = value; }, palette); await resize(390, 844); assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= innerWidth)); await shot(palette + '-mobile-plan'); } await resize(1440, 1080); await shot('desktop-plan'); mark('the existing Orven form renders in three palettes and at 390px without page overflow'); await panel().getByRole('button', { name: '封存分工计划', exact: true }).click(); await until(async () => (await host.api('/sessions/' + f.session.id + '/collaborations')).groups.length === 1, 15000); const frozen = (await host.api('/sessions/' + f.session.id + '/collaborations')).groups[0], id = frozen.id; assert.equal(frozen.children[0].assignment.model.model, 'owned-a'); assert.equal(frozen.children[1].assignment.model.model, 'owned-b'); assert.equal(local.requests.length, 0); await host.api('/model-center/select', { type: 'connection', id: b.id }); await panel().getByRole('button', { name: '创建两个工作区', exact: true }).click(); await until(async () => (await group(id)).status === 'ready', 60000); await panel().getByRole('button', { name: '开始两个子任务', exact: true }).click(); await until(async () => (await group(id)).status === 'ready_to_collect', 90000); await panel().getByRole('button', { name: '刷新', exact: true }).click(); await panel().getByText('检查未验证', { exact: false }).first().waitFor(); assert.deepEqual([...new Set(local.requests.map(row => row.input.model))].sort(), ['owned-a', 'owned-b']); assert.ok(local.requests.filter(row => row.input.model === 'owned-a').every(row => row.input.reasoning_effort === 'high')); assert.ok(local.requests.filter(row => row.input.model === 'owned-b').every(row => row.input.reasoning_effort === 'low')); mark('full-access UI creates and runs both workspaces without approval; actual local HTTP retains each frozen route and reasoning after the default changes'); await panel().getByRole('button', { name: '核对成果', exact: true }).click(); await until(async () => (await group(id)).status === 'collected', 30000); await panel().getByRole('button', { name: '查看原成果记录', exact: true }).click(); const record = page.getByRole('dialog', { name: '原成果核对记录', exact: true }); await record.getByText('not_evaluated', { exact: false }).waitFor(); const collection = await host.api('/sessions/' + f.session.id + '/collaborations/' + id + '/collection'); save('collection.json', collection); assert.ok(collection.workers.every(row => row.quality.checks.status === 'not_verified' && row.quality.requests.length === 2)); await shot('collection'); await record.getByRole('button', { name: '关闭窗口', exact: true }).click(); await panel().getByRole('button', { name: '合并到父工程', exact: true }).click(); await until(async () => (await group(id)).merge?.status === 'applied', 30000); assert.deepEqual(ownerBytes(f), owner); mark('collection exposes real byte changes, original dispatch and unverified checks/requirements; parent auto-merge preserves owner Git/index/files'); await page.getByRole('dialog', { name: '隔离协作', exact: true }).getByRole('button', { name: '关闭窗口', exact: true }).click(); await openWorkbenchTool(page, '打开模型中心'); const card = page.locator('[data-connection-id="' + messages.id + '"]'); await card.getByText('max_tokens = 98304', { exact: false }).waitFor(); await card.getByRole('button', { name: '推理与能力', exact: true }).click(); await page.getByLabel('原生请求参数', { exact: true }).getByText('Messages 协议必须带 max_tokens', { exact: false }).waitFor(); await shot('messages-required'); assert.equal(local.requests.length, 4); assert.deepEqual(errors, []); mark('model center and native editor disclose Messages required max_tokens and declaration provenance without requesting any real provider'); result = { success: true, actualElectron: desktop, appRoot, passed, screenshots, errors, localRequests: local.requests.length, realModelRequests: 0, officialServiceTouched: false, serverSha256: createHash('sha256').update(fs.readFileSync(path.join(appRoot, 'dist-server/server.mjs'))).digest('hex') }; } catch (error) { if (page) try { await shot('failure'); fs.writeFileSync(path.join(directory, 'failure-dom.txt'), await page.locator('body').innerText()); } catch {} result = { success: false, appRoot, actualElectron: desktop, passed, screenshots, errors, error: error.stack, realModelRequests: 0 }; } finally { save('local-requests.json', local.requests); if (application) await application.close(); if (browser) await browser.close(); if (controller && await controller.health()) await controller.stop(); for (const dispose of disposers.reverse()) await dispose(); save('result.json', result); } console.log(JSON.stringify({ directory, ...result }, null, 2)); if (!result.success) process.exitCode = 1;