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, until } 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 model = await compatibleFixture(t, () => completion([], '本地HTTP验收：已核对本次记忆上下文。'));
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');

  fs.rmSync(path.join(f.projectRoot, 'oversized-source.mjs'));
  await dialog.getByRole('button', { name: '重新核对', exact: true }).click();
  await expect(currentCard).toContainText('当前');
  await page.keyboard.press('Escape');
  await host.api('/config', { mode: 'compatible', baseUrl: model.baseUrl, model: 'owned-memory-ui', apiKey: 'owned-ui-fixture-key', contextLimit: 24000 });
  await host.api('/sessions/' + session.id + '/messages', { content: '读取当前项目记忆，说明你获得的项目上下文。' });
  await until(async () => (await host.api('/sessions/' + session.id)).status === 'completed');
  assert.equal(model.requests.length, 1);
  const actual = model.requests[0].input;
  assert.ok(actual.messages.some(value => value.role === 'system' && value.content.includes('"project_memory":')));
  assert.equal(JSON.stringify(actual).includes('owned-ui-fixture-key'), false);
  await page.setViewportSize({ width: 1440, height: 1000 });
  await page.reload(); await expect(page.getByRole('button', { name: '查看任务记录', exact: true })).toBeVisible();
  await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
  const context = page.getByLabel('请求上下文', { exact: true });
  await expect(context).toContainText('1 条来源已核对的记录');
  await context.getByRole('button', { name: '查看本次项目记忆', exact: true }).click();
  const artifact = page.getByRole('dialog', { name: '执行结果工件', exact: true });
  await expect(artifact.locator('pre')).toContainText('扫描范围记忆');
  await expect(artifact.locator('pre')).toContainText('sourceFingerprint');
  await page.screenshot({ path: path.join(evidenceDir, 'desktop-sent-memory.png') });
  await page.keyboard.press('Escape');
  await page.setViewportSize({ width: 390, height: 844 });
  await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
  await expect(context).toBeVisible();
  await context.getByRole('button', { name: '查看本次项目记忆', exact: true }).click();
  await expect(artifact.locator('pre')).toContainText('扫描范围记忆');
  assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390));
  await page.screenshot({ path: path.join(evidenceDir, 'mobile-sent-memory.png') });
  mark('desktop and 390px request records expose the exact memory snapshot used by one owned local HTTP request');
  assert.deepEqual(pageErrors, []); assert.deepEqual(requestFailures, []);
  fs.writeFileSync(path.join(evidenceDir, 'result.json'), JSON.stringify({ success: true, url: host.url, viewportWidths: [1440, 390],
    assertions: marks, pageErrors, requestFailures, localFixtureModelRequests: model.requests.length, realModelRequests: 0,
    screenshots: ['desktop-expired-memory.png', 'mobile-incomplete-memory.png', 'desktop-sent-memory.png', 'mobile-sent-memory.png'] }, null, 2));
  console.log('EVIDENCE ' + evidenceDir);
} catch (error) {
  await page.screenshot({ path: path.join(evidenceDir, 'failure.png') }).catch(() => {});
  fs.writeFileSync(path.join(evidenceDir, 'result.json'), JSON.stringify({ success: false, error: error.message, assertions: marks, pageErrors, requestFailures }, null, 2));
  console.log('EVIDENCE ' + evidenceDir);
  throw error;
} finally {
  await browser.close();
  for (const dispose of disposers.reverse()) await dispose();
}
