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 } from '../tests/helpers.mjs'; const evidenceDir = path.resolve('.qa', 'project-memory-ui-' + randomUUID()); fs.mkdirSync(evidenceDir, { recursive: true }); const disposers = [], t = { after: dispose => disposers.push(dispose) }; const f = fixture(t); f.store.close(); const host = await launchWorkbench(t, f.data); assert.notEqual(new URL(host.url).port, '4318'); const browser = await chromium.launch({ channel: 'msedge', headless: true }); const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } }); const project = f.project, session = f.session; const pageErrors = [], requestFailures = [], marks = []; page.on('pageerror', error => pageErrors.push(error.message)); page.on('requestfailed', request => requestFailures.push(request.url() + ': ' + request.failure()?.errorText)); const mark = value => { marks.push(value); console.log('PASS ' + value); }; try { await page.addInitScript(({ projectId, sessionId }) => { localStorage.setItem('awb-project', projectId); localStorage.setItem('awb-session', sessionId); }, { projectId: project.id, sessionId: session.id }); await page.goto(host.url); await page.waitForLoadState('networkidle'); await page.getByRole('button', { name: '项目记忆', exact: true }).click(); const dialog = page.getByRole('dialog', { name: '项目记忆', exact: true }); await expect(dialog).toBeVisible(); await expect(dialog.locator('.memory-editor')).toBeVisible(); const editor = dialog.locator('.memory-editor'); await editor.getByLabel('标题', { exact: true }).fill('文件来源记忆'); await editor.getByLabel('来源文件(可选)', { exact: true }).fill('README.md'); await editor.getByLabel('内容', { exact: true }).fill('这条事实绑定README的实际字节和项目源码指纹。'); await editor.getByRole('button', { name: '保存记忆', exact: true }).click(); const firstCard = dialog.locator('.memory-card').filter({ hasText: '文件来源记忆' }); await expect(firstCard).toContainText('当前'); await expect(firstCard).toContainText('来源 SHA'); const initialExecution = await host.api('/sessions/' + session.id + '/execution'); assert.equal(Object.hasOwn(initialExecution, 'memories'), false); fs.appendFileSync(path.join(f.projectRoot, 'README.md'), '\nExternal fixture change.\n'); await dialog.getByRole('button', { name: '重新核对', exact: true }).click(); await expect(firstCard).toContainText('已过期'); await expect(firstCard).toContainText('来源文件已变化'); assert.equal((await host.api('/projects/' + project.id + '/memories')).summary.expired, 1); mark('source hashes remain visible and a changed file is explicitly expired with retained provenance'); await page.screenshot({ path: path.join(evidenceDir, 'desktop-expired-memory.png') }); await editor.getByLabel('标题', { exact: true }).fill('扫描范围记忆'); await editor.getByLabel('来源文件(可选)', { exact: true }).fill(''); await editor.getByLabel('内容', { exact: true }).fill('不完整工程扫描时不能把记忆冒充当前事实。'); await editor.getByRole('button', { name: '保存记忆', exact: true }).click(); const currentCard = dialog.locator('.memory-card').filter({ hasText: '扫描范围记忆' }); await expect(currentCard).toContainText('当前'); fs.writeFileSync(path.join(f.projectRoot, 'oversized-source.mjs'), Buffer.alloc(1024 * 1024 + 1)); await dialog.getByRole('button', { name: '重新核对', exact: true }).click(); await expect(dialog.locator('.memory-scope-warning')).toContainText('源码扫描未覆盖完整工程'); await expect(currentCard).toContainText('扫描不完整'); await expect(editor.getByRole('button', { name: '保存记忆', exact: true })).toBeDisabled(); const incomplete = await host.api('/projects/' + project.id + '/memories'); assert.equal(incomplete.summary.current, 0); assert.equal(incomplete.summary.unverified, 1); assert.equal(incomplete.memories.find(value => value.title === '扫描范围记忆').status, 'unverified'); assert.equal((await host.api('/sessions/' + session.id + '/execution')).memories, undefined); mark('incomplete inventory marks existing facts unverified and blocks new saves'); await page.setViewportSize({ width: 390, height: 844 }); assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390), '390px viewport must not overflow horizontally'); await page.screenshot({ path: path.join(evidenceDir, 'mobile-incomplete-memory.png'), fullPage: false }); mark('390px memory review stays within the viewport with the incomplete-scan warning visible'); assert.deepEqual(pageErrors, []); assert.deepEqual(requestFailures, []); fs.writeFileSync(path.join(evidenceDir, 'result.json'), JSON.stringify({ url: host.url, viewportWidths: [1440, 390], assertions: marks, pageErrors, requestFailures, screenshots: ['desktop-expired-memory.png', 'mobile-incomplete-memory.png'] }, null, 2)); console.log('EVIDENCE ' + evidenceDir); } finally { await browser.close(); for (const dispose of disposers.reverse()) await dispose(); }