import { execFileSync } from 'node:child_process'; import { mkdirSync } from 'node:fs'; import path from 'node:path'; const base = process.env.WF_WEB_URL ?? 'http://127.0.0.1:3000'; const session = process.env.COPILOT_E2E_SESSION ?? `wf-copilot-${process.pid}`; const artifacts = process.env.COPILOT_E2E_ARTIFACTS; const browser = process.env.AGENT_BROWSER_BIN ?? 'agent-browser'; if (artifacts) mkdirSync(artifacts, { recursive: true }); const cli = (args, timeout = 700_000) => execFileSync(browser, ['--session', session, ...args], { encoding: 'utf8', timeout, env: { ...process.env, AGENT_BROWSER_IDLE_TIMEOUT_MS: '1800000', AGENT_BROWSER_DEFAULT_TIMEOUT: process.env.AGENT_BROWSER_DEFAULT_TIMEOUT ?? '700000', }, }).trim(); const evaluate = expression => { const raw = cli(['eval', `JSON.stringify(${expression})`]); const once = JSON.parse(raw); return typeof once === 'string' ? JSON.parse(once) : once; }; const waitText = text => cli(['wait', '--fn', `document.body.innerText.includes(${JSON.stringify(text)})`]); const ask = question => { cli(['fill', '#copilot-question', question]); cli(['wait', '--fn', "document.querySelector('.copilot-composer button')?.disabled === false"]); cli(['click', '.copilot-composer button']); }; const capture = name => { if (artifacts) cli(['screenshot', path.join(artifacts, `${name}.png`)]); }; const results = []; try { cli(['open', `${base}/projects`]); cli(['wait', '--load', 'networkidle']); cli([ 'eval', "Object.keys(localStorage).filter(k => k.startsWith('wishforge.copilot.session.')).forEach(k => localStorage.removeItem(k)); localStorage.setItem('wishforge.copilot.open','true'); location.reload()", ]); cli(['wait', '--load', 'networkidle']); cli(['wait', '.copilot-sidebar.is-open']); if (process.env.COPILOT_E2E_PROJECT_ID) cli(['select', '.copilot-project select', process.env.COPILOT_E2E_PROJECT_ID]); results.push('右侧 Copilot 默认展开'); ask('如何提交 Wish?请根据当前项目 Wiki 回答。'); waitText('FAQ / 配置'); if (!evaluate("document.querySelector('.copilot-message.assistant:last-of-type .copilot-sources') !== null")) throw new Error('FAQ 没有 Wiki 来源'); if (evaluate("document.body.innerText.includes('查看真实 Wish 运行')")) throw new Error('FAQ 不应创建 Wish'); capture('faq'); results.push('FAQ 直接答复且带 Wiki 来源,未创建 Wish'); ask('我实际点击提交 Wish 后接口返回 500,页面没有创建记录。请记录复现信息并整理成 Bug Wish。'); waitText('疑似 Bug'); cli(['wait', '.copilot-message.assistant:last-of-type .copilot-wish-draft']); capture('bug-before-confirmation'); if (evaluate('document.querySelectorAll(\'a[href*="/runs"]\').length === 0')) throw new Error('页面应有既有运行导航'); if (evaluate("document.querySelectorAll('.copilot-wish-link').length !== 0")) throw new Error('确认前不应创建 Wish'); cli(['click', '.copilot-message.assistant:last-of-type .copilot-wish-draft button.button']); waitText('查看真实 Wish 运行'); capture('bug-created'); results.push('疑似 Bug 在用户确认后返回真实 Wish 运行链接'); ask( '我希望为 Wish 提交增加草稿自动保存。请根据 Wiki 中提交 Wish 的目标与验收标准约束,用 Grill-me 卡片澄清保存范围。', ); cli(['wait', '.copilot-message.assistant:last-of-type .grill-card']); const before = evaluate( "document.querySelector('.copilot-message.assistant:last-of-type .copilot-wish-draft textarea').value", ); cli(['click', '.copilot-message.assistant:last-of-type .grill-card button']); const after = evaluate( "document.querySelector('.copilot-message.assistant:last-of-type .copilot-wish-draft textarea').value", ); if (before === after) throw new Error('Grill-me 选择没有进入 Wish goal'); capture('grill-selected'); cli(['click', '.copilot-message.assistant:last-of-type .copilot-wish-draft button.button']); cli(['wait', '--fn', "document.querySelectorAll('.copilot-wish-link').length === 2"]); capture('grill-created'); results.push('Wiki 支撑的 Grill-me 卡片可交互,选择进入 Wish 并确认创建'); const links = evaluate( "Array.from(document.querySelectorAll('.copilot-wish-link')).map(a => a.getAttribute('href'))", ); if (new Set(links).size !== 2) throw new Error(`应有两个不同的真实 Wish 链接,实际 ${JSON.stringify(links)}`); const projectId = evaluate("document.querySelector('.copilot-project select').value"); const sessionId = evaluate(`localStorage.getItem('wishforge.copilot.session.${projectId}')`); cli(['open', `${base}/runs`]); cli(['wait', '.copilot-sidebar.is-open']); cli(['wait', '--load', 'networkidle']); waitText('FAQ / 配置'); capture('recovered-on-runs'); results.push('跨项目主页面保持展开,导航后持久会话仍可恢复'); cli(['click', '.copilot-header button[aria-label="收起 AI Copilot"]']); if (evaluate("document.querySelector('.console').classList.contains('copilot-open')")) throw new Error('侧栏未能收起'); cli(['open', `${base}/projects`]); if (evaluate("document.querySelector('.console').classList.contains('copilot-open')")) throw new Error('收起状态没有跨页面保持'); results.push('展开/收起状态跨页面保持'); console.log(JSON.stringify({ status: 'passed', session, projectId, sessionId, results, wishLinks: links }, null, 2)); } finally { cli(['close'], 15_000); }