Files
Agent-Workbench/scripts/verify-task-checkpoints-ui.mjs
2026-10-30 18:41:56 +08:00

62 lines
6.0 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { chromium, expect } from 'playwright/test';
import { fixture, launchWorkbench, compatibleFixture, completion, call, until } from '../tests/helpers.mjs';
const directory = path.resolve('.qa', 'task-checkpoint-ui-' + randomUUID()); fs.mkdirSync(directory, { recursive: true });
const disposers = [], t = { after: dispose => disposers.push(dispose) }, f = fixture(t); f.store.close();
let failing = true;
const model = await compatibleFixture(t, (_url, _body, requests) => requests.length === 1 ? completion([call('checkpoint-ui-read', 'read_file', { path: 'README.md' })])
: failing ? { status: 401, body: { error: { message: '独立本地HTTP故障样本。' } } } : completion([], '同一任务已从原结果继续,未重新读取文件。'));
const host = await launchWorkbench(t, f.data); assert.notEqual(new URL(host.url).port, '4318');
await host.api('/config', { mode: 'compatible', baseUrl: model.baseUrl, model: 'owned-checkpoint-ui', apiKey: 'owned-checkpoint-ui-key', contextLimit: 24000 });
await host.api('/sessions/' + f.session.id + '/messages', { content: '读取README一次,保留原任务,出现本地HTTP故障后等待显式继续。' });
await until(async () => (await host.api('/sessions/' + f.session.id)).status === 'failed');
const before = await host.api('/sessions/' + f.session.id);
const browser = await chromium.launch({ channel: 'msedge', headless: true });
const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } }), pageErrors = [], requestFailures = [], passed = [];
page.on('pageerror', error => pageErrors.push(error.message)); page.on('requestfailed', request => requestFailures.push(request.url() + ': ' + request.failure()?.errorText));
const mark = value => { passed.push(value); console.log('PASS ' + value); };
try {
await page.addInitScript(({ projectId, sessionId }) => { localStorage.setItem('awb-project', projectId); localStorage.setItem('awb-session', sessionId); }, { projectId: f.project.id, sessionId: f.session.id });
await page.goto(host.url); await expect(page.getByRole('button', { name: '查看任务记录', exact: true })).toBeVisible();
await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
const panel = page.getByLabel('任务恢复检查点', { exact: true }); await expect(panel).toBeVisible();
await panel.getByRole('button', { name: '保存恢复检查点', exact: true }).click();
await expect(panel.locator('.checkpoint-card')).toHaveCount(1); await expect(panel).toContainText('1 项已完成工具');
assert.equal(model.requests.length, 2);
await panel.getByRole('button', { name: '核对恢复条件', exact: true }).click();
await expect(panel).toContainText('当前可以续接原任务');
await panel.getByRole('button', { name: '查看原恢复包', exact: true }).click();
const artifact = page.getByRole('dialog', { name: '执行结果工件', exact: true });
await expect(artifact.locator('pre')).toContainText('task-recovery-checkpoint'); await expect(artifact.locator('pre')).toContainText('resultArtifact');
await page.screenshot({ path: path.join(directory, 'desktop-original-checkpoint.png') }); await page.keyboard.press('Escape');
mark('desktop saves and inspects the full original Task and tool provenance without another model request');
const filename = path.join(f.projectRoot, 'README.md'), original = fs.readFileSync(filename); fs.appendFileSync(filename, '\nOutside UI edit.\n');
await panel.getByRole('button', { name: '核对恢复条件', exact: true }).click();
await expect(panel).toContainText('工程源码已变化'); await expect(panel.getByRole('button', { name: '从已核对检查点继续', exact: true })).toHaveCount(0);
assert.equal(model.requests.length, 2); mark('an outside source change is visible and removes the checkpoint continuation action');
fs.writeFileSync(filename, original);
await page.setViewportSize({ width: 390, height: 844 }); await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
await panel.getByRole('button', { name: '核对恢复条件', exact: true }).click();
await expect(panel).toContainText('当前可以续接原任务'); await panel.scrollIntoViewIfNeeded();
assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390));
await page.screenshot({ path: path.join(directory, 'mobile-checkpoint-ready.png') });
failing = false; await panel.getByRole('button', { name: '从已核对检查点继续', exact: true }).click();
await until(async () => (await host.api('/sessions/' + f.session.id)).status === 'completed');
const after = await host.api('/sessions/' + f.session.id);
assert.equal(after.taskId, before.taskId); assert.equal(after.execution.run.resumeOf, before.runId);
assert.equal(after.execution.progress.totalTools, 1); assert.equal(model.requests.length, 3);
await expect(panel.locator('.checkpoint-card')).toHaveCount(1);
await panel.getByRole('button', { name: '核对恢复条件', exact: true }).click(); await expect(panel).toContainText('已有新运行');
mark('390px explicit continuation keeps the same Task and one original read; a second resume of the old checkpoint is blocked');
assert.deepEqual(pageErrors, []); assert.deepEqual(requestFailures, []);
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify({ success: true, passed, viewportWidths: [1440, 390], pageErrors, requestFailures,
screenshots: ['desktop-original-checkpoint.png', 'mobile-checkpoint-ready.png'], localFixtureModelRequests: model.requests.length, realModelRequests: 0 }, null, 2));
} catch (error) {
await page.screenshot({ path: path.join(directory, 'failure.png') }).catch(() => {});
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify({ success: false, error: error.message, passed, pageErrors, requestFailures }, null, 2)); throw error;
} finally { console.log('EVIDENCE ' + directory); await browser.close(); for (const dispose of disposers.reverse()) await dispose(); }