import fs from 'node:fs'; import path from 'node:path'; import http from 'node:http'; import assert from 'node:assert/strict'; import { createHash, randomUUID } from 'node:crypto'; import { chromium, expect } from 'playwright/test'; import { fixture, launchWorkbench, until } from '../tests/helpers.mjs'; import { Provider } from '../src/server/provider.mjs'; if (process.argv.includes('--help')) { console.log('node scripts/verify-model-selection-ui.mjs [--app-root ABS] — isolated compiled UI, two owned HTTP routes, add/switch/budget/reload/demo/locking at desktop and 390px; no configured-model calls'); 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 = args[1] ?? process.cwd(); const directory = path.resolve('.qa/model-selection-k-' + randomUUID()); fs.mkdirSync(directory, { recursive: true }); const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t), requests = []; let release, block = false; const upstream = http.createServer(async (req, res) => { const chunks = []; for await (const chunk of req) chunks.push(chunk); const body = JSON.parse(Buffer.concat(chunks)); requests.push({ url: req.url, body }); if (block) await new Promise(resolve => { release = resolve; }); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ model: body.model, choices: [{ message: { role: 'assistant', content: 'Owned route receipt.' } }], usage: { prompt_tokens: 13, completion_tokens: 7, total_tokens: 20 } })); }); await new Promise(resolve => upstream.listen(0, '127.0.0.1', resolve)); const endpoint = 'http://127.0.0.1:' + upstream.address().port; f.store.saveConfig({ mode: 'compatible', baseUrl: endpoint + '/legacy/v1', model: 'owned-legacy-gpt', maxSteps: 1 }); const provider = new Provider(), hub = provider.modelHub(f.store); const selected = hub.upsert({ name: 'Claude · 自有接口', model: 'owned-claude', baseUrl: endpoint + '/selected/v1', requiresKey: false, modelIdentityPolicy: 'require-match' }); const noKey = hub.upsert({ name: '待填凭据', model: 'no-credential', baseUrl: endpoint + '/no-key/v1' }); const pool = hub.pool({ name: '工程模型池', members: [{ connectionId: selected.id, weight: 1 }], strategy: 'round-robin', sticky: true, maxAttempts: 1, queueTimeoutMs: 1000 }); hub.select({ type: 'pool', id: pool.id }); f.store.close(); const nodePath = fs.existsSync(path.join(appRoot, 'runtime/node.exe')) ? path.join(appRoot, 'runtime/node.exe') : process.execPath; const host = await launchWorkbench(t, f.data, {}, { appRoot, nodePath }); assert.notEqual(new URL(host.url).port, '4318'); let browser, page, result; const checks = [], screenshots = [], errors = []; const mark = name => { checks.push(name); console.log('PASS ' + name); }; const capture = async name => { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file }); screenshots.push({ path: file, sha256: createHash('sha256').update(fs.readFileSync(file)).digest('hex') }); }; const dialog = () => page.getByRole('dialog', { name: '模型中心', exact: true }); const select = () => dialog().getByLabel('新任务默认模型', { exact: true }); const close = () => dialog().getByRole('button', { name: '关闭窗口', exact: true }).click(); try { browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage({ viewport: { width: 1440, height: 1000 } }); page.on('pageerror', error => errors.push(error.message)); await page.goto(host.url); await page.waitForLoadState('networkidle'); await page.getByLabel('当前项目').selectOption(f.project.id); await page.getByRole('button', { name: '选择与管理模型', exact: true }).click(); await expect(dialog()).toBeVisible(); await expect(select()).toHaveValue('pool:' + pool.id); // This Playwright build reports disabled