// C90 only: one synthetic new intake to demonstrate source_page preservation. Existing country and A/B proofs are not repeated. const { start, BASE: _BASE, DIR: _DIR, path: _path, assert, sanitized, } = require('./portal-visual-qa-lib.cjs'); void (async () => { const q = await start('form-entry-source-chromium-20261010'); q.proof.entry = q.prior.entry || { email: 'qa-source-entry-c90-20261010@example.invalid', name: 'QA página de entrada C90 20261010', }; let page, ctx; async function props(r) { const html = await r.text(); const e = html.match(/data-page="([^"]+)"/); if (e && e[1].startsWith('{')) return JSON.parse( e[1] .replace(/"/g, '"') .replace(/&/g, '&') .replace(/'/g, "'"), ).props; return JSON.parse( html.match( /]*type="application\/json"[^>]*>([\s\S]*?)<\/script>/, )[1], ).props; } try { q.phase('C90 source lookup prevents duplicated intake'); ({ page, ctx } = await q.context('manager-source', 1440)); await q.login(page, 'manager'); const management = { page, ctx }; const list = await props( await q.open( page, '/management/interests?q=' + encodeURIComponent(q.proof.entry.email), ), ); const existing = list.requests.data.find( (r) => r.email === q.proof.entry.email, ); if (existing) { q.proof.entry.id = existing.id; q.proof.entry.reference = existing.reference; q.proof.recovered_existing = true; q.save(); } if (!existing) { q.phase('One public investor-form intake after source correction'); ({ page, ctx } = await q.context('anonymous-C90', 390)); await q.open(page, '/formularios/investidor'); const form = page.locator('form').filter({ has: page.getByRole('button', { name: 'Enviar e obter protocolo', exact: true, }), }); await form .getByLabel('Nome do representante', { exact: true }) .fill(q.proof.entry.name); await form .getByLabel('Email', { exact: true }) .fill(q.proof.entry.email); await form .getByRole('combobox', { name: 'País do requerente', exact: true, }) .selectOption('PT'); await form .getByRole('combobox', { name: 'País de origem declarado desta solicitação', exact: true, }) .selectOption('ZA'); await form .getByLabel('Sector ou assunto', { exact: true }) .fill('Ensaio QA da página de entrada'); await form .getByLabel('Descreva o pedido', { exact: true }) .fill( 'Ensaio sintético QA para preservar a página de entrada do formulário; sem comunicação externa.', ); await form.getByRole('checkbox').check(); const saved = page.waitForResponse( (r) => r.request().method() === 'POST' && new URL(r.url()).pathname === '/apoio', ); await form .getByRole('button', { name: 'Enviar e obter protocolo', exact: true, }) .click(); assert.equal((await saved).status(), 302); await page.getByText(/Pedido registado\. Protocolo:/).waitFor(); q.proof.entry.reference = ( await page .getByText(/Pedido registado\. Protocolo:/) .innerText() ).match(/AP-\d{4}-[A-Z0-9]+/)[0]; q.proof.public_ui_protocol = true; q.save(); await q.shot(page, 'public-source-protocol-390'); await ctx.close(); } ({ page, ctx } = management); q.phase( 'Authorized management shows preserved entry page separately from country/origin', ); const found = await props( await q.open( page, '/management/interests?q=' + encodeURIComponent(q.proof.entry.reference), ), ); assert.equal(found.requests.data.length, 1); q.proof.entry.id = found.requests.data[0].id; const data = await props( await q.open(page, '/management/interests/' + q.proof.entry.id), ); assert.equal(data.interest.source_page, '/formularios/investidor'); assert.equal(data.interest.country, 'PT'); assert.equal(data.declaredOrigin.code, 'ZA'); await page .getByText('/formularios/investidor', { exact: true }) .waitFor(); await page.getByText('África do Sul (ZA)', { exact: true }).waitFor(); await q.shot(page, 'source-country-origin-management'); q.proof.checks = { public_intake_keeps_actual_same_origin_page: true, source_presented_separately_from_country_origin: true, recovered_without_duplicate: true, }; q.proof.result = 'passed'; } catch (e) { q.proof.result = 'failed'; q.proof.error = sanitized(e); q.save(); } finally { q.proof.finished_at = new Date().toISOString(); q.save(); await q.browser.close(); console.log( JSON.stringify({ result: q.proof.result, checks: q.proof.checks, receipt: q.receipt, }), ); if (q.proof.result !== 'passed') process.exitCode = 1; } })();