File size: 9,382 Bytes
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Captures the consultant screens (light + dark) into var/ui-screenshots/. Starts its OWN backend on a throwaway FPT_HOME
// (port 8813) and its own mock on 3100 if none is up; stops only the processes it started. Usage: node scripts/screenshots.mjs
import { chromium } from '@playwright/test';
import { spawn } from 'node:child_process';
import { existsSync, mkdirSync, mkdtempSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';

const here = dirname(fileURLToPath(import.meta.url));
const ROOT = resolve(here, '..', '..');
const OUT = join(ROOT, 'var', 'ui-screenshots');
const PORT = 8813;
const BASE = `http://127.0.0.1:${PORT}`;
const USER = 'shots_admin';
const PW = 'screenshots-only-password';
const PY = [join(ROOT, 'architecture', '.venv', 'Scripts', 'python.exe'), join(ROOT, 'architecture', '.venv', 'bin', 'python')].find(existsSync) || 'python';
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
const up = async (url) => { try { return (await fetch(url)).ok; } catch { return false; } };
const started = [];

async function main() {
  mkdirSync(OUT, { recursive: true });
  if (!(await up('http://127.0.0.1:3100/api/health'))) {
    started.push(spawn('node', ['server.js'], { cwd: join(ROOT, 'mock-app'), stdio: 'ignore' }));
  }
  const home = mkdtempSync(join(tmpdir(), 'fpt-shots-'));
  started.push(spawn(PY, ['-m', 'coworker', 'api', '--port', String(PORT)], { cwd: ROOT, stdio: 'ignore', env: { ...process.env, FPT_HOME: home,
    FPT_LIBRARY_GIT: join(home, 'library.git'), FPT_CASE_WRITEBACK: '0', PATCH_WATCH_ENABLED: '0', FPT_ORCHESTRATOR: 'langgraph',
    FPT_BOOTSTRAP_ADMIN_USER: USER, FPT_BOOTSTRAP_ADMIN_PASSWORD: PW, PLAYWRIGHT_MODE: 'stub' } }));
  for (let i = 0; i < 120 && !(await up(`${BASE}/api/ready`)); i++) await sleep(500);

  const tok = (await (await fetch(`${BASE}/api/auth/token`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: USER, password: PW }) })).json()).access_token;
  const api = async (path, method = 'GET', body) => {
    const r = await fetch(`${BASE}${path}`, { method, headers: { Authorization: `Bearer ${tok}`, 'Content-Type': 'application/json' }, body: body ? JSON.stringify(body) : undefined });
    return r.status === 204 ? null : r.json();
  };
  const waitStatus = async (rid, statuses) => { for (let i = 0; i < 400; i++) { const r = await api(`/api/runs/${rid}`); if (statuses.includes(r.status)) return r; await sleep(500); } throw new Error('timeout'); };

  const browser = await chromium.launch();
  const ctx = await browser.newContext({ viewport: { width: 1440, height: 900 } });
  const page = await ctx.newPage();
  await page.goto(`${BASE}/login`);
  await page.getByLabel('Username').fill(USER);
  await page.getByLabel('Password').fill(PW);
  await page.getByRole('button', { name: 'Sign in' }).click();
  await page.getByRole('heading', { name: 'Patch readiness' }).waitFor();

  const shot = async (name, path, prepare) => {
    for (const scheme of ['light', 'dark']) {
      await page.evaluate((s) => localStorage.setItem('mantine-color-scheme-value', s), scheme);
      await page.goto(`${BASE}${path}`);
      await page.waitForLoadState('networkidle').catch(() => {});
      if (prepare) await prepare();
      await sleep(600);
      await page.screenshot({ path: join(OUT, `${name}-${scheme}.png`), fullPage: true });
    }
  };

  await shot('c1-patch-readiness-before-testing', '/');
  const { run_id: rid } = await api('/api/runs', 'POST', { modules: ['P2P'], playwright_mode: 'stub', include_p2: false, stub_knobs: { STUB_DEFECT: 'P2P-APR-001', STUB_UI: 'P2P-INV-001' } });
  await waitStatus(rid, ['waiting_approval']);
  const openGroups = async () => {
    const unchanged = page.getByRole('button', { name: /^Unchanged \(\d+\)$/ });
    if (await unchanged.count()) await unchanged.click().catch(() => {});
    const req = page.getByTestId(/^step-group-(changed|unchanged)-Requisition$/).first();
    await req.getByRole('button', { name: /^Requisition/ }).click().catch(() => {});
    await req.getByRole('button', { name: /^Approve all \d+ Requisition scenarios$/ }).click().catch(() => {});
    await page.getByTestId('scenario-P2P-APR-001').getByRole('button').first().click().catch(() => {});
  };
  await shot('c3-scenario-review', `/runs/${rid}/scenarios`, openGroups);
  await shot('d1-scenario-review-grouped', `/runs/${rid}/scenarios`, openGroups);
  await shot('d2-run-page', `/runs/${rid}/overview`);

  // chat: an answer card, then a confirm card (one conversation per colour scheme; nothing is confirmed here)
  for (const scheme of ['light', 'dark']) {
    await page.evaluate((sc) => { localStorage.setItem('mantine-color-scheme-value', sc); for (const k of Object.keys(localStorage)) if (k.startsWith('fpt-chat-')) localStorage.removeItem(k); }, scheme);
    await page.goto(`${BASE}/`);
    await page.getByRole('heading', { name: 'Patch readiness' }).waitFor();
    await page.keyboard.press('Control+k');
    const dock = page.getByRole('complementary', { name: 'Ask the co-worker' });
    const input = dock.getByRole('textbox', { name: 'Ask the co-worker' });
    await input.fill('Is Procure-to-Pay ready for 26C?');
    await input.press('Enter');
    await dock.getByTestId('chat-card-readiness').waitFor();
    await sleep(500);
    await page.screenshot({ path: join(OUT, `d4-chat-answer-card-${scheme}.png`) });
    await input.fill('approve all Requisition scenarios');
    await input.press('Enter');
    await dock.getByTestId('chat-confirm').waitFor();
    await dock.getByRole('button', { name: 'Show the list' }).click();
    await sleep(500);
    await page.screenshot({ path: join(OUT, `d5-chat-confirm-card-${scheme}.png`) });
    await dock.getByTestId('chat-confirm').getByRole('button', { name: 'Cancel' }).click();
    await dock.getByRole('button', { name: 'Close the co-worker chat' }).click();
  }
  await page.evaluate(() => { for (const k of Object.keys(localStorage)) if (k.startsWith('fpt-chat-')) localStorage.removeItem(k); });
  const gate = await api(`/api/runs/${rid}/gate`);
  await api(`/api/runs/${rid}/gates/approve_cases`, 'POST', { approved: gate.pending.map((p) => p.id), rejected: [], comments: {} });
  await waitStatus(rid, ['waiting_review', 'completed']);
  await shot('c4-failure-review', `/runs/${rid}/failures`);
  await shot('c6-test-run-overview', `/runs/${rid}/overview`);
  const g2 = await api(`/api/runs/${rid}/gate`);
  if (g2) await api(`/api/runs/${rid}/gates/human_review`, 'POST', { review: Object.fromEntries(g2.items.map((i) => [i.case_id, i.ask === 'owner' ? { owner: 'automation' } : { confirmed: 'real_defect' }])) });
  await waitStatus(rid, ['completed', 'failed']);
  await api(`/api/runs/${rid}/signoff`, 'POST', { comment: 'Release owner informed.' });
  await shot('c5-report-signoff', `/runs/${rid}/report`);
  await shot('c1-patch-readiness', '/');
  await shot('c2-what-this-patch-changes', '/', async () => {
    await page.getByRole('heading', { name: 'What this patch changes' }).scrollIntoViewIfNeeded();
    await page.getByTestId(/^change-/).first().getByRole('button').click().catch(() => {});
  });
  await shot('c7-scenarios-library', '/scenarios');
  await shot('c8-test-runs', '/runs');
  await shot('c9-reports', '/reports');
  await shot('c10-admin-under-the-hood', '/admin');

  // live view: a small full browser run against the mock app; the Live tab while scenarios run (light, then dark)
  const liveIds = ['P2P-APR-001', 'P2P-APR-002', 'P2P-APR-003', 'P2P-APR-004', 'P2P-APR-005', 'P2P-APR-006', 'P2P-APR-007', 'P2P-APR-008', 'P2P-HLD-001', 'P2P-HLD-002',
    'P2P-HLD-003', 'P2P-HLD-004', 'P2P-HLD-005', 'P2P-INV-001', 'P2P-REQ-001', 'P2P-ACC-001', 'P2P-ACC-003', 'P2P-ACC-004'];
  const { run_id: lid } = await api('/api/runs', 'POST', { modules: ['P2P'], playwright_mode: 'live', case_ids: liveIds });
  await waitStatus(lid, ['waiting_approval']);
  const lg = await api(`/api/runs/${lid}/gate`);
  await api(`/api/runs/${lid}/gates/approve_cases`, 'POST', { approved: lg.pending.map((p) => p.id), rejected: [], comments: {} });
  await page.evaluate(() => localStorage.setItem('mantine-color-scheme-value', 'light'));
  await page.goto(`${BASE}/runs/${lid}/live`);
  const img = page.getByTestId('live-frame').locator('img');
  await img.waitFor({ timeout: 180000 });
  for (let i = 0; i < 120 && !(await img.evaluate((el) => el.naturalWidth > 0)); i++) await sleep(250);
  await sleep(2500);
  await page.screenshot({ path: join(OUT, 'd3-live-tab-light.png') });
  await page.getByRole('button', { name: 'Switch to dark theme' }).click();
  await sleep(1200);
  await page.screenshot({ path: join(OUT, 'd3-live-tab-dark.png') });
  await api(`/api/runs/${lid}/cancel`, 'POST', { reason: 'screenshots taken' });
  await browser.close();
  console.log(`screenshots written to ${OUT}`);
}

main().catch((e) => { console.error(e); process.exitCode = 1; }).finally(() => {
  for (const p of started) { try { if (process.platform === 'win32') spawn('taskkill', ['/PID', String(p.pid), '/T', '/F'], { stdio: 'ignore' }); else p.kill(); } catch { /* gone */ } }
});