// Verifies the second round of competitive-gap closures: the Coverage // Dashboard's rule-based recommendations engine, the Faraid Calculator's // madhab selector (including honest Ja'fari gating), and the Wassiyah tab's // draft will document generator. const { chromium } = require('playwright'); const { signInFreshFamily, gotoTab } = require('./e2e-auth-helper.cjs'); const BASE = 'https://moslem04.falahos.my/'; const results = []; const consoleErrors = []; function record(name, pass, detail = '') { results.push({ name, pass, detail }); console.log(`${pass ? 'PASS' : 'FAIL'} ${name}${detail ? ' — ' + detail : ''}`); } async function main() { const browser = await chromium.launch(); const page = await browser.newPage({ viewport: { width: 390, height: 844 } }); page.on('console', m => { if (m.type() === 'error') consoleErrors.push(m.text()); }); page.on('pageerror', e => consoleErrors.push(e.message)); await signInFreshFamily(page, BASE, 'e2e-gaps2'); // ── Recommendations engine on Coverage Dashboard ── // Fresh family, no assets yet: no exposed-asset recommendation, but the // "no agent assigned" and "no insurance" rules should still fire since // they don't depend on estateTotal > 0. await page.waitForTimeout(600); const recSectionVisible = await page.locator('.recommendations').isVisible().catch(() => false); record('Coverage: recommendations section renders for a fresh family', recSectionVisible); const noAgentRec = await page.locator('.rec-row', { hasText: 'No estate agent' }).isVisible().catch(() => false); record('Coverage: flags missing estate agent/mutawalli', noAgentRec); // Add an asset to trigger the exposed-asset recommendation await gotoTab(page, 'Assets'); await page.waitForTimeout(500); await page.locator('.form-card .field:has-text("Description") input').fill('Savings account'); await page.locator('.form-card .field:has-text("Estimated value") input').fill('50000'); await page.locator('.form-card button.btn-primary', { hasText: 'Add asset' }).click(); await page.waitForTimeout(1000); await gotoTab(page, 'Coverage'); await page.waitForTimeout(800); const exposedRec = await page.locator('.rec-row', { hasText: 'exposed to the slow' }).isVisible().catch(() => false); record('Coverage: flags exposed asset once one is logged', exposedRec); const wassiyahRec = await page.locator('.rec-row', { hasText: 'No Wassiyah bequests' }).isVisible().catch(() => false); record('Coverage: flags missing Wassiyah once estate has value', wassiyahRec); // ── Faraid Calculator: madhab selector ── await gotoTab(page, 'Faraid'); await page.waitForTimeout(500); const madhabSelectVisible = await page.locator('.field:has-text("Madhab") select').isVisible().catch(() => false); record('Faraid: madhab selector is present', madhabSelectVisible); // Set up: wife only, no other heirs -> sole-heir radd divergence case await page.locator('.field:has-text("Number of surviving wives") input').fill('1'); await page.waitForTimeout(500); await page.locator('.field:has-text("Madhab") select').selectOption('shafii'); await page.waitForTimeout(500); const shafiiShare = await page.locator('.share-row', { hasText: 'Wife' }).locator('.share-frac').textContent(); record("Faraid: Shafi'i excludes spouse from radd (wife keeps 1/4)", shafiiShare.trim() === '1/4', shafiiShare); const unallocatedNoteVisible = await page.locator('.note-box', { hasText: 'unallocated' }).isVisible().catch(() => false); record("Faraid: Shafi'i shows unallocated-residue note for sole-heir spouse", unallocatedNoteVisible); await page.locator('.field:has-text("Madhab") select').selectOption('hanafi'); await page.waitForTimeout(500); const hanafiShare = await page.locator('.share-row', { hasText: 'Wife' }).locator('.share-frac').textContent(); record('Faraid: Hanafi extends radd to sole-heir spouse (takes whole estate)', hanafiShare.trim() === '1', hanafiShare); await page.locator('.field:has-text("Madhab") select').selectOption('jaafari'); await page.waitForTimeout(500); const jaafariGateVisible = await page.locator('.reframe-note', { hasText: "Ja'fari" }).isVisible().catch(() => false); const sharesHiddenForJaafari = await page.locator('.results').isVisible().catch(() => false); record("Faraid: Ja'fari is honestly gated as unsupported, not silently computed", jaafariGateVisible && !sharesHiddenForJaafari); // ── Wassiyah: draft will document generator ── await page.locator('.field:has-text("Madhab") select').selectOption('shafii'); // reset, avoid cross-test pollution await gotoTab(page, 'Wassiyah'); await page.waitForTimeout(500); await page.locator('.field:has-text("Full legal name") input').fill('Ahmad bin Ismail'); await page.locator('.field:has-text("Recipient name") input').fill('Nur Charity Foundation'); await page.locator('.field:has-text("Relation to you") input').fill('charity'); await page.locator('.field:has-text("Description") input').fill('Cash bequest'); await page.locator('.field:has-text("Value") input').first().fill('5000'); await page.locator('button.btn-primary', { hasText: 'Add bequest' }).click(); await page.waitForTimeout(1000); const [docPage] = await Promise.all([ page.waitForEvent('popup'), page.locator('button.btn-secondary', { hasText: 'Generate draft will document' }).click() ]); await docPage.waitForLoadState(); const docText = await docPage.locator('body').innerText(); record('Will document: opens a new tab with the testator name', docText.includes('Ahmad bin Ismail'), docText.slice(0, 100)); record('Will document: watermarked DRAFT / not executed', /draft.*not executed/i.test(docText)); record('Will document: includes the bequest recipient', docText.includes('Nur Charity Foundation')); record('Will document: includes witness attestation section', /Witness Attestation/i.test(docText)); await docPage.close(); record('No uncaught JS console errors during full session', consoleErrors.length === 0, consoleErrors.join(' || ')); await browser.close(); const passCount = results.filter(r => r.pass).length; const failCount = results.length - passCount; console.log(`\n${passCount} passed, ${failCount} failed, ${results.length} total`); if (failCount > 0) results.filter(r => !r.pass).forEach(r => console.log(` - ${r.name}: ${r.detail}`)); process.exit(failCount > 0 ? 1 : 0); } main().catch(e => { console.error('SCRIPT ERROR:', e); process.exit(2); });