Files
Investidor-V2/tools/demography-browser-qa.cjs
T
2026-11-21 20:58:19 +02:00

84 lines
5.7 KiB
JavaScript

const { chromium } = require('C:/Users/macbook/.cache/codex-runtimes/codex-primary-runtime/dependencies/node/node_modules/playwright');
const fs = require('node:fs');
const path = require('node:path');
const assert = require('node:assert/strict');
const crypto = require('node:crypto');
const root = path.resolve(__dirname, '..');
const output = path.join(root, 'outputs/demography-20261113');
const proof = JSON.parse(fs.readFileSync(path.join(output, 'verification.json'), 'utf8'));
const ids = proof.comparison.territories.map(t => t.id);
const base = 'http://127.0.0.1:18813';
const evidence = { target: 'investmoz_demography_test:55441', checks: [], screenshots: [], errors: [] };
let activePage;
function totp(secret) {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
const bits = [...secret].map(c => alphabet.indexOf(c).toString(2).padStart(5, '0')).join('');
const key = Buffer.from(bits.match(/.{8}/g).map(x => parseInt(x, 2)));
const counter = Buffer.alloc(8); counter.writeBigUInt64BE(BigInt(Math.floor(Date.now() / 30000)));
const hmac = crypto.createHmac('sha1', key).update(counter).digest();
return String((hmac.readUInt32BE(hmac[19] & 15) & 0x7fffffff) % 1000000).padStart(6, '0');
}
(async () => {
const browser = await chromium.launch({ headless: true, executablePath: 'C:/Users/macbook/AppData/Local/ms-playwright/chromium-1243/chrome-win64/chrome.exe' });
try {
const context = await browser.newContext({ viewport: { width: 1440, height: 1000 }, locale: 'pt-MZ' });
const page = await context.newPage();
activePage = page;
page.on('pageerror', e => evidence.errors.push(e.message));
if (!process.argv.includes('--admin-only')) {
const params = new URLSearchParams({ start_year: '2026', end_year: '2027', sector: 'industry' });
ids.forEach(id => params.append('territories[]', id));
const response = await page.goto(`${base}/localizacoes/demografia?${params}`, { waitUntil: 'networkidle' });
assert.equal(response.headers()['x-investmoz-qa-target'], evidence.target);
await page.getByRole('heading', { name: 'Demografia e viabilidade territorial', exact: true }).waitFor();
await page.getByRole('heading', { name: 'Diferenças entre localizações', exact: true }).waitFor();
assert.equal(await page.locator('select[multiple] option:checked').count(), 2);
assert.equal(await page.locator('table').count(), 2);
evidence.checks.push('Public district comparison, values, versions, differences and preserved filters');
await page.screenshot({ path: path.join(output, 'comparison-desktop.png'), fullPage: true });
evidence.screenshots.push('comparison-desktop.png');
await page.setViewportSize({ width: 390, height: 844 });
assert(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), 'Mobile page must not overflow');
await page.screenshot({ path: path.join(output, 'comparison-mobile.png'), fullPage: true });
evidence.screenshots.push('comparison-mobile.png');
evidence.checks.push('Mobile layout at 390 pixels without page overflow');
if (process.argv.includes('--public-only')) {
fs.writeFileSync(path.join(output, 'browser-public-verification.json'), JSON.stringify(evidence, null, 2));
return;
}
}
const account = JSON.parse(fs.readFileSync(path.join(root, 'storage/app/private/demography-browser-account.json'), 'utf8'));
await page.setViewportSize({ width: 1440, height: 1000 });
await page.goto(`${base}/login`, { waitUntil: 'networkidle' });
await page.locator('input[name=email]').fill(account.email);
await page.locator('input[name=password]').fill(account.password);
await page.locator('button[type=submit]').click();
await page.waitForURL('**/two-factor-challenge');
evidence.phase = 'password accepted; TOTP challenge';
await page.locator('input[autocomplete="one-time-code"]').fill(totp(account.secret));
await page.locator('button[type=submit]').click();
await page.waitForURL(url => !url.pathname.includes('two-factor-challenge'));
evidence.phase = 'TOTP accepted';
await page.goto(`${base}/management/demography?batch=${proof.imports[0].id}`, { waitUntil: 'networkidle' });
await page.getByRole('heading', { name: 'Dados demográficos INE — QA', exact: true }).waitFor();
await page.getByText('Erros e advertências', { exact: true }).click();
assert((await page.locator('body').innerText()).includes('2031!E204'));
assert.equal(await page.locator('select').count(), 13);
await page.screenshot({ path: path.join(output, 'administration-desktop.png'), fullPage: true });
evidence.screenshots.push('administration-desktop.png');
evidence.checks.push('Real password and TOTP login, administrative preview, 13 territorial matches and original inconsistency references');
assert.deepEqual(evidence.errors, []);
await context.close();
} catch (error) {
evidence.failure = { message: error.message, url: activePage?.url(), text: (await activePage?.locator('body').innerText())?.slice(-3000) };
await activePage?.screenshot({ path: path.join(output, 'browser-diagnostic.png'), fullPage: true });
throw error;
} finally {
fs.writeFileSync(path.join(output, process.argv.includes('--public-only') ? 'browser-public-verification.json' : 'browser-admin-verification.json'), JSON.stringify(evidence, null, 2));
await browser.close();
}
console.log(JSON.stringify({ checks: evidence.checks, screenshots: evidence.screenshots, errors: evidence.errors }));
})().catch(error => { console.error(error.message); process.exitCode = 1; });