import { expect, test } from '@playwright/test';

test.describe('Proof First browser monitor', () => {
  test('fixed reference preserves the selected role', async ({ page }, testInfo) => {
    const consoleErrors: string[] = [];
    page.on('console', (message) => {
      if (message.type() === 'error') consoleErrors.push(message.text());
    });

    await page.goto('/web-qa');
    await expect(page.locator('[data-hydrated="true"]')).toBeVisible();
    const fixedReference = page.getByRole('button', { name: 'Fixed reference' });
    await fixedReference.click();
    await expect(fixedReference).toHaveClass(/bg-lime-300/);
    await page.getByLabel('Full name').fill('Synthetic Monitor');
    await page.getByLabel('Work email').fill('invalid-address');
    await page.getByLabel('Workspace role').selectOption('Admin');
    await page.getByRole('button', { name: 'Run validation path' }).click();

    await expect(page.getByText('Passed', { exact: true })).toBeVisible();
    await expect(page.getByText('Role is Admin', { exact: true })).toBeVisible();
    expect(consoleErrors).toEqual([]);

    await testInfo.attach('monitor-summary', {
      body: JSON.stringify({ mode: 'fixed-reference', passed: true, consoleErrors }),
      contentType: 'application/json',
    });
  });

  test('seeded positive control is detected', async ({ page }, testInfo) => {
    await page.goto('/web-qa');
    await expect(page.locator('[data-hydrated="true"]')).toBeVisible();
    await page.getByRole('button', { name: 'Seeded defect' }).click();
    await page.getByLabel('Full name').fill('Synthetic Monitor');
    await page.getByLabel('Work email').fill('invalid-address');
    await page.getByLabel('Workspace role').selectOption('Admin');
    await page.getByRole('button', { name: 'Run validation path' }).click();

    await expect(page.getByText('Defect detected', { exact: true })).toBeVisible();
    await expect(page.getByText('Role is Viewer', { exact: true })).toBeVisible();

    await testInfo.attach('positive-control-summary', {
      body: JSON.stringify({ mode: 'positive-control', defectDetected: true }),
      contentType: 'application/json',
    });
  });

  test('agent console runs a contained approval scenario without client errors', async ({ page }) => {
    const consoleErrors: string[] = [];
    page.on('console', (message) => {
      if (message.type() === 'error') consoleErrors.push(message.text());
    });

    await page.goto('/agent-monitoring');
    await expect(page.locator('[data-agent-console-hydrated="true"]')).toBeVisible();
    await page.getByRole('button', { name: 'Approval checkpoint' }).click();
    await page.getByRole('button', { name: 'Run synthetic check' }).click();

    await expect(page.getByText('approval', { exact: true })).toBeVisible();
    await expect(page.getByText('Human confirmation required', { exact: true })).toBeVisible();

    await page.getByRole('tab', { name: 'Agent guardrails' }).click();
    await expect(page.getByText('Prompt injection contained', { exact: true })).toBeVisible();
    expect(consoleErrors).toEqual([]);
  });
});
