diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f2105f48a5..ccb5a40a53 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -78,6 +78,8 @@ jobs: env: JSRA_UI_DATA_DIR: ${{ runner.temp }}\ui-data - run: npm run check:plugins + env: + JSRA_UI_DATA_DIR: ${{ runner.temp }}\ui-data - uses: actions/upload-artifact@v4 if: failure() with: diff --git a/scripts/check-plugins.mjs b/scripts/check-plugins.mjs index fe6e95be67..aa6c14932e 100644 --- a/scripts/check-plugins.mjs +++ b/scripts/check-plugins.mjs @@ -6,11 +6,16 @@ import crypto from 'node:crypto'; import { spawn } from 'node:child_process'; import WebSocket from 'ws'; import { chromium } from '@zhizhuodemao/patchright'; +import { folderProof } from '../sidecar/grants.mjs'; const root = process.cwd(), parent = path.resolve('.tmp'); fs.mkdirSync(parent, { recursive: true }); -const data = fs.mkdtempSync(path.join(parent, 'plugins-ui-')), - token = crypto.randomBytes(20).toString('hex'), +// CI 在 JSRA_UI_DATA_DIR 下已经装好 CloakBrowser 内核;复用同一个目录,不要每次开新的临时目录。 +const data = process.env.JSRA_UI_DATA_DIR + ? path.join(path.resolve(process.env.JSRA_UI_DATA_DIR), 'WebDecryptHub') + : path.join(fs.mkdtempSync(path.join(parent, 'plugins-ui-')), 'WebDecryptHub'); +fs.mkdirSync(data, { recursive: true }); +const token = crypto.randomBytes(20).toString('hex'), pending = new Map(); let socket, sequence = 0, @@ -174,10 +179,12 @@ try { pageErrors = []; page.on('pageerror', (error) => pageErrors.push(error.message)); await page.goto(`http://127.0.0.1:${site.address().port}/?port=${port}&token=${token}`); + // 首次引导只在全新数据目录出现;复用已有目录时它不会弹,不能硬等。 const skip = page.getByRole('button', { name: '稍后', exact: true }); - await skip.waitFor({ state: 'visible', timeout: 15000 }); - await skip.click(); - await page.locator('.setup-dialog').waitFor({ state: 'detached' }); + if (await skip.count()) { + await skip.click(); + await page.locator('.setup-dialog').waitFor({ state: 'detached' }); + } const rail = page.getByRole('button', { name: '展开会话列表' }); if (await rail.count()) await rail.first().click(); await page.locator('.rail-nav').getByRole('button', { name: '插件', exact: true }).click(); @@ -243,8 +250,16 @@ try { headless: true, embedMode: 'screencast', }); - const session = (await invoke('session-list')).focused; - await invoke('workspace-set', { sessionId: session, managed: true }); + // 插件页本身不依赖会话,但验证已下载的 WDH 浏览器需要一个真实会话。 + // 项目必须挂已授权目录:用和 UI 同一套 folderProof 给自己签一个单次凭证。 + const grant = { sessionId: 'plugin-check', path: process.cwd(), expiresAt: Date.now() + 60000 }; + grant.signature = folderProof(token, grant); + const project = await invoke('project-save', { + scope: 'plugin-check', + project: { name: 'plugin-check-' + Date.now(), folders: [{ key: 'main', grant }], primaryKey: 'main' }, + }); + const created = await invoke('session-create', { projectId: project.id, title: 'plugin check' }); + const session = created.id; const opened = await invoke('browser-launch', { sessionId: session, url: 'http://127.0.0.1:' + site.address().port + '/browser-fixture', @@ -269,7 +284,7 @@ try { async () => (await invoke('plugins-list')).official.find((p) => p.id === 'wdh').enabled === false, 'disabled WDH', ); - const sid = (await invoke('session-list')).focused; + const sid = session; if ((await invoke('list-tools', { sessionId: sid })).some((t) => t.name === 'browser_open')) throw Error('Disabled WDH tools still registered'); if (pageErrors.length) throw Error(pageErrors.join('\n')); diff --git a/scripts/check-sidecar.mjs b/scripts/check-sidecar.mjs index 4d0c087341..cd8f4b9044 100644 --- a/scripts/check-sidecar.mjs +++ b/scripts/check-sidecar.mjs @@ -53,7 +53,7 @@ try { await invoke('terminal-open', { sessionId, id: terminalId, cols: 90, rows: 25 }); await invoke('terminal-input', { sessionId, id: terminalId, data: process.platform === 'win32' ? "Write-Output ('PACKAGED_' + 'TERMINAL_OK')\r" : "printf 'PACKAGED_%s\\n' 'TERMINAL_OK'\r" }); let terminalOutput = ''; - const terminalDeadline = Date.now() + 15000; + const terminalDeadline = Date.now() + 60000; while (!terminalOutput.includes('PACKAGED_TERMINAL_OK')) { if (Date.now() > terminalDeadline) throw Error('Packaged PTY input/output failed'); terminalOutput = (await invoke('terminal-snapshot', { sessionId, id: terminalId })).output; await new Promise(r => setTimeout(r, 50)); } await invoke('terminal-close', { sessionId, id: terminalId }); if (fs.existsSync(marker)) throw new Error('Executable loaded untrusted bunfig preload'); diff --git a/scripts/check-ui.mjs b/scripts/check-ui.mjs index c58eaafb68..3e840e2521 100644 --- a/scripts/check-ui.mjs +++ b/scripts/check-ui.mjs @@ -202,6 +202,8 @@ fs.mkdirSync(SHOTS, { recursive: true }); async function checkWindowChrome(scheme) { for (const nativeTitlebar of [false, true]) { const context = await browser.newContext({ viewport: { width: 1440, height: 900 }, colorScheme: scheme }); + // CI runner 上冷启动和首次渲染明显更慢,默认 30 秒的断言窗口不够。 + context.setDefaultTimeout(60000); const page = await context.newPage(); page.on('pageerror', error => console.error('Window chrome runtime error: ' + error.message)); try { @@ -679,10 +681,10 @@ try { for (const scheme of (process.env.JSRA_UI_SCHEME ? [process.env.JSRA_UI_SC await page.locator('.settings-nav').getByRole('button', { name: '外观', exact: true }).click(); const opposite = scheme === 'dark' ? 'light' : 'dark'; await page.locator('.theme-option').filter({ hasText: opposite === 'dark' ? '深色' : '浅色' }).click(); - await page.locator('html[data-theme="' + opposite + '"]').waitFor({ state: 'attached', timeout: 3000 }); + await page.locator('html[data-theme="' + opposite + '"]').waitFor({ state: 'attached', timeout: 30000 }); ok(await page.locator('html').getAttribute('data-theme') === opposite, '[' + scheme + '] 主题切换没有生效'); await page.locator('.theme-option').filter({ hasText: '跟随系统' }).click(); - await page.locator('html[data-theme="' + scheme + '"]').waitFor({ state: 'attached', timeout: 3000 }); + await page.locator('html[data-theme="' + scheme + '"]').waitFor({ state: 'attached', timeout: 30000 }); ok(await page.locator('html').getAttribute('data-theme') === scheme, '[' + scheme + '] 跟随系统没有恢复'); await page.locator('.screen-head button').first().click(); @@ -775,7 +777,7 @@ try { for (const scheme of (process.env.JSRA_UI_SCHEME ? [process.env.JSRA_UI_SC await page.getByRole('button', { name: '恢复网页执行', exact: true }).waitFor(); await page.screenshot({ path: path.join(SHOTS, scheme + '-browser-paused.png') }); await page.getByRole('button', { name: '恢复网页执行', exact: true }).click(); - await page.getByRole('button', { name: '恢复网页执行', exact: true }).waitFor({ state: 'detached', timeout: 8000 }).catch(async e => { throw new Error(e.message + '\n通知:' + (await page.locator('[data-sonner-toast]').allTextContents()).join(' | ')); }); + await page.getByRole('button', { name: '恢复网页执行', exact: true }).waitFor({ state: 'detached', timeout: 30000 }).catch(async e => { throw new Error(e.message + '\n通知:' + (await page.locator('[data-sonner-toast]').allTextContents()).join(' | ')); }); await debugUiCdp.detach(); await page.keyboard.press('Control+Alt+3'); @@ -787,7 +789,7 @@ try { for (const scheme of (process.env.JSRA_UI_SCHEME ? [process.env.JSRA_UI_SC await page.getByRole('button', { name: '刷新', exact: true }).click(); await page.keyboard.press('Control+Alt+3'); } - await page.locator('.req-row').filter({ hasText: /api/ }).first().waitFor({ timeout: 15000 }); + await page.locator('.req-row').filter({ hasText: /api/ }).first().waitFor({ timeout: 45000 }); const pauseNetwork = page.getByRole('button', { name: '暂停抓取网络请求', exact: true }); ok(await pauseNetwork.getAttribute('aria-pressed') === 'true', '[' + scheme + '] 记录中状态应显示暂停操作'); await pauseNetwork.click(); @@ -799,7 +801,7 @@ try { for (const scheme of (process.env.JSRA_UI_SCHEME ? [process.env.JSRA_UI_SC ok(await page.locator('.bar').filter({ has: page.getByPlaceholder('过滤 URL / 参数名') }).innerText().then(text => !/网络请求\s*\d/.test(text)), '[' + scheme + '] 不应重复网络请求标题与数量'); await page.locator('.req-row').filter({ hasText: /api/ }).first().click(); await page.locator('.detail-tabs').getByRole('tab', { name: '响应', exact: true }).click(); - await page.waitForFunction(() => document.querySelector('.detail-body')?.textContent.includes('UI capture verified'), { timeout: 10000 }); + await page.waitForFunction(() => document.querySelector('.detail-body')?.textContent.includes('UI capture verified'), { timeout: 30000 }); ok((await page.locator('.detail-body').innerText()).includes('UI capture verified'), '[' + scheme + '] 请求响应没有正确显示'); await page.screenshot({ path: path.join(SHOTS, scheme + '-network.png') }); await page.keyboard.press('Control+Alt+2'); @@ -1184,11 +1186,11 @@ try { for (const scheme of (process.env.JSRA_UI_SCHEME ? [process.env.JSRA_UI_SC await page.waitForFunction(() => document.activeElement?.matches('.terminal-screen .xterm-helper-textarea')); // 等 shell 真正打印提示符并完成 PSReadLine 绑定,再模拟用户输入(冷启动的 Windows 尤其慢)。 const promptRe = process.platform === 'win32' ? /PS [^>\r\n]*>/ : /[$#] /; - const promptDeadline = Date.now() + 15000; + const promptDeadline = Date.now() + 45000; while (!promptRe.test((await rpc('terminal-snapshot', { sessionId: workChat.id, id: terminalId })).output)) { if (Date.now() > promptDeadline) throw Error('Terminal prompt not ready'); await page.waitForTimeout(100); } await page.waitForTimeout(800); await page.keyboard.type(process.platform === 'win32' ? "Write-Output ('UI_TERMINAL_' + 'VERIFIED')" : "printf 'UI_TERMINAL_%s\\n' 'VERIFIED'"); await page.keyboard.press('Enter'); - const terminalDeadline = Date.now() + 12000; + const terminalDeadline = Date.now() + 30000; while (!(await rpc('terminal-snapshot', { sessionId: workChat.id, id: terminalId })).output.includes('UI_TERMINAL_VERIFIED')) { if (Date.now() > terminalDeadline) throw Error('Terminal UI input failed'); await page.waitForTimeout(50); } ok(await page.getByRole('menu').count() === 0, '[' + scheme + '] 终端输入回车不能重新打开新建标签菜单'); await page.screenshot({ path: path.join(SHOTS, scheme + '-terminal-tabs.png') }); diff --git a/sidecar/test/shell-encoding.test.mjs b/sidecar/test/shell-encoding.test.mjs index 3d8bfaec17..c8825c097c 100644 --- a/sidecar/test/shell-encoding.test.mjs +++ b/sidecar/test/shell-encoding.test.mjs @@ -13,7 +13,7 @@ const tmp = () => fs.mkdtempSync(path.join(os.tmpdir(), 'wdh-enc-')); test('shell output survives PowerShell console code page', { skip: !win }, async () => { const dir = tmp(); try { - const r = await shellWorkspace(dir, { cmd: 'Write-Output "中文里程碑:扫描完成 584 台,命中 3"', timeoutMs: 30000 }); + const r = await shellWorkspace(dir, { cmd: 'Write-Output "中文里程碑:扫描完成 584 台,命中 3"', timeoutMs: 90000 }); assert.equal(r.isError, false); assert.match(r.text, /中文里程碑:扫描完成 584 台,命中 3/); assert.doesNotMatch(r.text, /\uFFFD/); @@ -25,9 +25,9 @@ test('reading a UTF-8 Chinese file and matching it works', { skip: !win }, async fs.mkdirSync(root, { recursive: true }); fs.writeFileSync(path.join(root, 'note.txt'), '里程碑:已完成 584 台主机探测\n第二行\n', 'utf8'); try { - const read = await shellWorkspace(dir, { cmd: 'Get-Content note.txt', timeoutMs: 30000 }); + const read = await shellWorkspace(dir, { cmd: 'Get-Content note.txt', timeoutMs: 90000 }); assert.match(read.text, /里程碑:已完成 584 台主机探测/); - const matched = await shellWorkspace(dir, { cmd: 'Get-Content note.txt | Select-String "里程碑"', timeoutMs: 30000 }); + const matched = await shellWorkspace(dir, { cmd: 'Get-Content note.txt | Select-String "里程碑"', timeoutMs: 90000 }); assert.match(matched.text, /里程碑/); } finally { killAllJobs(dir); } }); @@ -36,13 +36,13 @@ test('writing through PowerShell then reading back keeps Chinese intact', { skip const dir = tmp(), root = path.join(dir, 'workspace'); fs.mkdirSync(root, { recursive: true }); try { - const written = await shellWorkspace(dir, { cmd: 'Set-Content -Path out.txt -Value "回环中文"; Out-File -FilePath out2.txt -InputObject "第二份"', timeoutMs: 30000 }); + const written = await shellWorkspace(dir, { cmd: 'Set-Content -Path out.txt -Value "回环中文"; Out-File -FilePath out2.txt -InputObject "第二份"', timeoutMs: 90000 }); assert.equal(written.isError, false); // PowerShell 5.1 的 -Encoding utf8 会写 BOM;宿主读取必须剥掉,否则正文开头多一个不可见字符。 const raw = fs.readFileSync(path.join(root, 'out.txt'), 'utf8'); assert.match(raw.replace(/^\uFEFF/, ''), /^回环中文/); - await shellWorkspace(dir, { cmd: 'Write-Output ok', timeoutMs: 30000 }); - const back = await shellWorkspace(dir, { cmd: 'Get-Content out.txt, out2.txt', timeoutMs: 30000 }); + await shellWorkspace(dir, { cmd: 'Write-Output ok', timeoutMs: 90000 }); + const back = await shellWorkspace(dir, { cmd: 'Get-Content out.txt, out2.txt', timeoutMs: 90000 }); assert.match(back.text, /回环中文/); assert.match(back.text, /第二份/); assert.doesNotMatch(back.text, /\uFFFD/); @@ -54,7 +54,7 @@ test('Chinese directory and file names round-trip', { skip: !win }, async () => try { const r = await shellWorkspace(dir, { cmd: 'New-Item -ItemType Directory -Force -Path "中文目录" | Out-Null; Write-Output "创建成功" | Tee-Object -FilePath "中文目录/报告.txt"; Get-Content "中文目录/报告.txt"', - timeoutMs: 30000, + timeoutMs: 90000, }); assert.equal(r.isError, false); assert.match(r.text, /创建成功/); @@ -65,7 +65,7 @@ test('Chinese directory and file names round-trip', { skip: !win }, async () => test('error text is readable instead of mojibake', { skip: !win }, async () => { const dir = tmp(); try { - const r = await shellWorkspace(dir, { cmd: 'throw "中文错误:目标不可达"', timeoutMs: 30000 }); + const r = await shellWorkspace(dir, { cmd: 'throw "中文错误:目标不可达"', timeoutMs: 90000 }); assert.equal(r.isError, true); assert.match(r.text, /中文错误:目标不可达/); assert.doesNotMatch(r.text, /\uFFFD/); diff --git a/sidecar/test/workspace-tabs.test.mjs b/sidecar/test/workspace-tabs.test.mjs index 2563e6d674..a57d890139 100644 --- a/sidecar/test/workspace-tabs.test.mjs +++ b/sidecar/test/workspace-tabs.test.mjs @@ -16,10 +16,14 @@ function fixture(t) { t.after(async () => { await manager.closeAll(); assert.equal(path.dirname(directory), os.tmpdir()); assert.ok(path.basename(directory).startsWith('dh-tabs-')); fs.rmSync(directory, { recursive: true, force: true }); }); return { s, manager, directory }; } -const wait = async (check, message = 'Terminal verification timed out') => { const end = Date.now() + 20000; while (!check()) { if (Date.now() > end) throw Error(message); await new Promise(r => setTimeout(r, 25)); } }; -const waitPrompt = async (s, id) => { if (process.platform !== 'win32') return; await wait(() => /PS [^>\r\n]*>/.test(s.terminals.snapshot(id).output), 'PowerShell prompt timed out'); await new Promise(r => setTimeout(r, 800)); }; -const expectOutput = async (check, s, id) => { try { await wait(check); } catch (error) { const tail = s.terminals.snapshot(id).output.slice(-600); throw Error(error.message + '; output tail=' + JSON.stringify(tail)); } }; -const runCommand = async (s, id, command) => { s.terminals.input(id, command); await new Promise(r => setTimeout(r, 500)); s.terminals.input(id, '\r'); }; +const wait = async (check, message = 'Terminal verification timed out', budget = 20000) => { + const end = Date.now() + budget; while (!check()) { if (Date.now() > end) throw Error(message); await new Promise(r => setTimeout(r, 25)); } }; +// CI runner 上 PowerShell 冷启动要 20s 以上,提示符窗口必须单独放宽,否则内层比用例的 timeout 还短。 +const waitPrompt = async (s, id) => { if (process.platform !== 'win32') return; await wait(() => /PS [^>\r\n]*>/.test(s.terminals.snapshot(id).output), 'PowerShell prompt timed out', 45000); await new Promise(r => setTimeout(r, 800)); }; +const expectOutput = async (check, s, id) => { try { await wait(check, 'Terminal verification timed out', 40000); } catch (error) { const tail = s.terminals.snapshot(id).output.slice(-600); throw Error(error.message + '; output tail=' + JSON.stringify(tail)); } }; +// 命令与回车分两次写入时,慢的 runner 上回车可能先到,命令被拦腰截断(实测出现过 +// `ISOLATED_'` 后换行再补 `('FIRST')`)。一次写完,由终端自己按 UTF-8 字符顺序处理。 +const runCommand = async (s, id, command) => { s.terminals.input(id, command + '\r'); }; test('work tabs preserve stable IDs and choose a neighbour on close; v1 migrates network into browser', () => { let dock = addDockTab(emptyDock(), 'files'), original = dock.activeId; dock = addDockTab(dock, 'document', { path: 'report.md', workspace: 'test' }); dock = addDockTab(dock, 'terminal'); dock = closeDockTab(dock, dock.activeId); assert.equal(dock.tabs.length, 2); assert.equal(dock.tabs[0].id, original); assert.equal(dock.activeId, dock.tabs[1].id); @@ -48,7 +52,7 @@ test('rapid browser selection serializes the latest intent and rejects missing p const tabs = new BrowserTabs(s); const a = tabs.select('a'), b = tabs.select('b'); await Promise.all([a, b]); assert.deepEqual(seen, ['b']); await assert.rejects(tabs.select('missing'), /已关闭/); }); -test('interactive terminal enforces permission, accepts real input and closes with its chat', { timeout: 60000 }, async t => { +test('interactive terminal enforces permission, accepts real input and closes with its chat', { timeout: 150000 }, async t => { const { s, manager } = fixture(t), id = crypto.randomUUID(); s.setMode('read-only'); await assert.rejects(s.terminals.open({ id }), /只读/); s.setMode('standard'); s.approve = async () => false; await assert.rejects(s.terminals.open({ id }), /未批准/); assert.equal(s.terminals.entries.size, 0); @@ -69,7 +73,7 @@ test('terminal approval never changes an unrelated Agent automation run', async assert.equal(s.pending.size, 1); assert.ok(!s.permissionBlocked); assert.equal(s.activeRun.status, 'running'); s.replyApproval([...s.pending.keys()][0], { allow: false }); await rejected; }); -test('multiple terminals isolate output and closing one keeps its sibling usable', { timeout: 75000 }, async t => { +test('multiple terminals isolate output and closing one keeps its sibling usable', { timeout: 210000 }, async t => { const { s } = fixture(t), a = crypto.randomUUID(), b = crypto.randomUUID(); s.setMode('full-access'); await Promise.all([s.terminals.open({ id: a }), s.terminals.open({ id: b })]); await Promise.all([waitPrompt(s, a), waitPrompt(s, b)]);