|
| 1 | +// One request, independent recipient states and returns over the packaged real backend. |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | +import {spawn} from 'node:child_process'; |
| 4 | +import {once} from 'node:events'; |
| 5 | +import {mkdir} from 'node:fs/promises'; |
| 6 | +import {resolve} from 'node:path'; |
| 7 | +import {createInterface} from 'node:readline'; |
| 8 | +import {testPythonCommand} from '../../../../scripts/test-python.mjs'; |
| 9 | +import {launchBrowser, loadPlaywright} from '../../../../examples/dashboard-browser-smoke-support.mjs'; |
| 10 | +const repo = resolve(import.meta.dirname, '../../../..'); |
| 11 | +const output = resolve(repo, 'output/playwright/multi-recipient'); |
| 12 | +await mkdir(output, {recursive:true}); |
| 13 | +const fixturePath = resolve(repo, 'examples/personal-workspace-browser/steward_requests_fixture.py'); |
| 14 | +const command = testPythonCommand(`import runpy; runpy.run_path(${JSON.stringify(fixturePath)}, run_name="__main__")`, {repoRoot:repo}); |
| 15 | +const child = spawn(command.command, [...command.args, '--status-file', resolve(repo, 'examples/status.example.json'), |
| 16 | + '--multi-recipient'], {cwd:repo, env:{...process.env, LOOPX_USAGE_PING:'0'}, stdio:['pipe','pipe','pipe']}); |
| 17 | +const exited = once(child, 'exit'); |
| 18 | +let stderr = ''; |
| 19 | +child.stderr.on('data', value => stderr += value); |
| 20 | +const queue = [], waiting = []; |
| 21 | +createInterface({input:child.stdout}).on('line', line => { |
| 22 | + try {const value=JSON.parse(line); if(waiting.length) waiting.shift()(value); else queue.push(value);} catch { /* CLI banner */ } |
| 23 | +}); |
| 24 | +const next = () => new Promise((resolve, reject) => { |
| 25 | + const timer=setTimeout(()=>reject(Error(`Fixture timeout: ${stderr}`)),90000); |
| 26 | + const receive=value=>{clearTimeout(timer);resolve(value);}; |
| 27 | + if(queue.length) receive(queue.shift()); else waiting.push(receive); |
| 28 | +}); |
| 29 | +async function fixture(action) {child.stdin.write(action+'\n');return next();} |
| 30 | +let browser; |
| 31 | +try { |
| 32 | + const ready=await next();assert.equal(ready.event,'ready'); |
| 33 | + browser=await launchBrowser(loadPlaywright().chromium); |
| 34 | + const page=await browser.newPage({viewport:{width:1200,height:888}}); |
| 35 | + await page.addInitScript(()=>localStorage.setItem('loopx-pw-locale','zh-CN')); |
| 36 | + await page.goto(ready.origin+'/chat/'); |
| 37 | + await page.getByRole('navigation',{name:'管家视图'}).getByRole('button',{name:/^(Chat|对话)$/}).click(); |
| 38 | + const cards=page.locator('.personal-collaboration').filter({hasText:'核对迁移方案 ·'}); |
| 39 | + await cards.first().waitFor(); assert.equal(await cards.count(),2); |
| 40 | + const builder=cards.filter({hasText:'核对迁移方案 · builder'}); |
| 41 | + const reviewer=cards.filter({hasText:'核对迁移方案 · reviewer'}); |
| 42 | + assert.match(await builder.innerText(),/尚未记录/); |
| 43 | + assert.match(await reviewer.innerText(),/已采纳/); |
| 44 | + await page.screenshot({path:resolve(output,'conversation.png')}); |
| 45 | + const summary=builder.locator('summary'); |
| 46 | + await summary.focus();await page.keyboard.press('Enter'); |
| 47 | + await builder.getByText('背景与补充',{exact:true}).waitFor(); |
| 48 | + await page.setViewportSize({width:390,height:844}); |
| 49 | + assert.equal(await page.evaluate(()=>document.documentElement.scrollWidth>innerWidth),false); |
| 50 | + await page.screenshot({path:resolve(output,'recipient-detail.png')}); |
| 51 | + await summary.press('Enter'); |
| 52 | + const before=await fixture('inspect'); |
| 53 | + await fixture('report-multi'); |
| 54 | + // The real pending-return observer must discover the reply without a reload. |
| 55 | + await page.waitForFunction(()=>[...document.querySelectorAll('.personal-collaboration')].some( |
| 56 | + row=>row.textContent.includes('核对迁移方案 · reviewer')&&row.textContent.includes('回复已送达')),null,{timeout:45000}); |
| 57 | + assert.doesNotMatch(await builder.innerText(),/回复已送达/); |
| 58 | + await page.getByText('reviewer 已完成独立核查;builder 的结论仍待返回。',{exact:true}).waitFor(); |
| 59 | + const after=await fixture('inspect'); |
| 60 | + assert.equal(after.turn_count,before.turn_count); |
| 61 | + assert.equal(after.posts,before.posts,'Observation and result refresh create no additional writes'); |
| 62 | + console.log(JSON.stringify({ok:true,recipient_count:2,automatic_return:true,remaining_recipient:'builder', |
| 63 | + additional_browser_writes:after.posts-before.posts,output})); |
| 64 | +} finally { |
| 65 | + await browser?.close(); |
| 66 | + if(child.exitCode===null){child.stdin.write('stop\n');await exited;} |
| 67 | +} |
0 commit comments