// Checks the live-view hook in a real Chromium, outside the test suite (so the 201/18 baseline is not affected):
// 1. browser actions become plain sentences, and typed sign-in details are never captioned
// 2. a page with a password field produces NO frame and sets status.masked
// 3. a normal page produces frames again
// Run: npm run check:live-view (exit 1 on any failed check)
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { chromium, type TestInfo } from '@playwright/test';
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'fpt-live-check-'));
process.env.FPT_LIVE_VIEW_DIR = dir;
let failed = 0;
const check = (name: string, ok: boolean, detail = '') => { console.log(`${ok ? ' ok ' : ' FAIL'} ${name}${ok || !detail ? '' : ` -> ${detail}`}`); if (!ok) failed += 1; };
const status = () => JSON.parse(fs.readFileSync(path.join(dir, 'status.json'), 'utf8'));
const frame = () => path.join(dir, 'frame.jpg');
const wait = (ms: number) => new Promise((r) => setTimeout(r, ms));
const html = (body: string) => `data:text/html,${encodeURIComponent(`
t${body}`)}`;
(async () => {
const { actionSentence, beginLiveStatus, describeLocator, startLiveView } = await import('../src/live-view');
check('caption: click a named button', actionSentence('click', "getByRole('button', { name: 'Submit' })", []) === "Click 'Submit'");
check('caption: fill a labelled field', actionSentence('fill', "getByLabel('Quantity')", ['10']) === "Enter '10' in 'Quantity'");
check('caption: last part of a chained locator', describeLocator("getByRole('table', { name: 'Lines' }).getByRole('row').getByRole('checkbox', { name: 'Select' })").name === 'Select');
check('caption: a password is never shown', actionSentence('fill', "getByLabel('Password')", ['S3cret!']) === 'Enter the sign-in details');
check('caption: nothing typed on a sign-in page is shown', !actionSentence('fill', "getByLabel('User ID')", ['zz.buyer'], true).includes('zz.buyer'));
const testInfo = { tags: ['@P2P-APR-001'], title: 'P2P-APR-001 demo', status: 'passed', expectedStatus: 'passed' } as unknown as TestInfo;
const done = beginLiveStatus(testInfo);
const browser = await chromium.launch();
const page = await browser.newPage();
const stop = await startLiveView(page, testInfo);
check('hook started', !!stop && !!done);
// a sign-in page: no frame may ever be written
await page.goto(html(''));
await page.getByLabel('User ID').fill('zz.buyer');
await page.getByLabel('Password').fill('S3cret!');
await wait(1800);
check('sign-in page: masked flag set', status().masked === true, JSON.stringify(status().masked));
check('sign-in page: no frame written', !fs.existsSync(frame()));
check('sign-in page: typed values not in the status file', !fs.readFileSync(path.join(dir, 'status.json'), 'utf8').includes('S3cret') && !JSON.stringify(status()).includes('zz.buyer'));
// a normal page: frames resume and the action is captioned
await page.goto(html(''));
await page.getByLabel('Quantity').fill('10');
await page.getByRole('button', { name: 'Add to Cart' }).click();
await wait(2500);
check('normal page: masked flag cleared', status().masked === false, JSON.stringify(status().masked));
check('normal page: frame written', fs.existsSync(frame()) && fs.statSync(frame()).size > 1000);
check('normal page: current action captioned', status().action === "Click 'Add to Cart'", String(status().action));
check('normal page: actions counted', status().action_n === 4, String(status().action_n));
if (stop) await stop();
if (done) done();
await browser.close();
fs.rmSync(dir, { recursive: true, force: true });
console.log(failed ? `check-live-view: ${failed} check(s) FAILED` : 'check-live-view: all checks passed');
process.exit(failed ? 1 : 0);
})().catch((e) => { console.error(e); process.exit(1); });