import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import test from 'node:test'; import { expandHtmlPartials, htmlPartialsPlugin } from '../scripts/html-partials-plugin.ts'; const distIndexPath = path.resolve('dist/index.html'); // Why: the plugin only runs at build time, so these assertions must inspect the // deployable artifact instead of the source document. const distHtml = fs.readFileSync(distIndexPath, 'utf-8'); function findMetaByProperty(html, property) { return [...html.matchAll(/]*>/gi)] .map((match) => match[0]) .find((tag) => tag.toLowerCase().includes(`property="${property}"`)); } function extractHeaderMarkup(html) { const headerMarkup = html.match(//i)?.[0]; assert.ok(headerMarkup, 'Built HTML must contain an expanded header partial'); return headerMarkup; } function findDisclosureButton(headerMarkup) { const disclosureButton = [...headerMarkup.matchAll(/]*>/gi)] .map((match) => match[0]) .find((tag) => /\baria-controls="site-menu"/i.test(tag)); assert.ok(disclosureButton, 'Header must have a disclosure button controlling #site-menu'); return disclosureButton; } /** * Locate a service card by its

heading and return its enclosing card
. * Why: avoids coupling tests to HTML comments that minification may strip. */ function extractCardByHeading(html, headingText) { const headingMatch = new RegExp(`]*>[^<]*${headingText}[^<]*

`, 'i').exec(html); if (!headingMatch) { return undefined; } const cardOpenPattern = /]*class="[^"]*\brounded-2xl\b[^"]*"[^>]*>/gi; let cardStart = -1; for (const match of html.matchAll(cardOpenPattern)) { if (match.index <= headingMatch.index) { cardStart = match.index; } else { break; } } if (cardStart === -1) { return undefined; } const divTagPattern = /<\/?div\b[^>]*>/gi; divTagPattern.lastIndex = cardStart; let depth = 0; for (const tagMatch of html.matchAll(divTagPattern)) { if (tagMatch.index < cardStart) { continue; } depth += tagMatch[0].startsWith(' { await t.test('Criterion 2: built header uses root-relative navigation links', () => { const headerMarkup = extractHeaderMarkup(distHtml); for (const href of [ '/', '/#servicios', '/#otros-espacios', '/#acerca', '/#filosofia', '/#contacto', ]) { assert.ok( headerMarkup.includes(`href="${href}"`), `Built header must use the root-relative href="${href}"`, ); } }); await t.test('Criterion 2: built header does not expose a /kundalini/ nav link yet', () => { const headerMarkup = extractHeaderMarkup(distHtml); // Why: /kundalini/ belongs to a later issue; this slice only wires the existing sections. assert.doesNotMatch( headerMarkup, /href="\/kundalini\//i, 'Built header must not expose a /kundalini/ nav link yet', ); }); await t.test( 'Criterion 2: built header marks the root brand link with aria-current="page"', () => { const headerMarkup = extractHeaderMarkup(distHtml); const brandLink = [...headerMarkup.matchAll(/]*>/gi)] .map((match) => match[0]) .find((tag) => /\bhref="\/"/i.test(tag)); assert.ok(brandLink, 'Brand link in built header must target "/"'); assert.match( brandLink, /\baria-current="page"/i, 'Root brand link must carry aria-current="page" on the home route', ); }, ); await t.test('Criterion 2: built header contains exactly one #site-menu navigation tree', () => { const headerMarkup = extractHeaderMarkup(distHtml); assert.equal( headerMarkup.match(/id="site-menu"/gi)?.length ?? 0, 1, 'Header must contain exactly one #site-menu navigation tree', ); }); await t.test('Criterion 2: header disclosure button exposes aria-expanded state', () => { const disclosureButton = findDisclosureButton(extractHeaderMarkup(distHtml)); assert.match( disclosureButton, /\baria-expanded="(?:true|false)"/i, 'Disclosure button must expose aria-expanded state', ); }); await t.test('Criterion 2: header disclosure button meets the 44px minimum touch target', () => { const disclosureButton = findDisclosureButton(extractHeaderMarkup(distHtml)); // Why: 44px is the minimum touch target; Tailwind h-11/w-11 equal 44px. assert.match( disclosureButton, /(?:min-)?h-(?:\[44px\]|11)|size-11/i, 'Disclosure button must have at least a 44px height', ); assert.match( disclosureButton, /(?:min-)?w-(?:\[44px\]|11)|size-11/i, 'Disclosure button must have at least a 44px width', ); }); await t.test( 'Criterion 1: built footer partial is expanded with the official Facebook link', () => { const footerMarkup = distHtml.match(//i)?.[0]; assert.ok(footerMarkup, 'Built HTML must contain an expanded footer partial'); assert.match( footerMarkup, /href="https:\/\/www\.facebook\.com\/elcorazoncomoportal\/"/i, 'Built footer must retain the official Facebook link from the footer partial', ); }, ); await t.test( 'Criterion 1: built WhatsApp FAB partial is expanded with the approved wa.me link', () => { const fabMarkup = distHtml.match( /]*class="[^"]*fixed[^"]*bottom-[^"]*right-[^"]*z-50[^"]*"[^>]*>[\s\S]*?<\/a>/i, )?.[0]; assert.ok(fabMarkup, 'Built HTML must contain the expanded persistent WhatsApp FAB'); assert.match( fabMarkup, /href="https:\/\/wa\.me\/573215428972/i, 'Expanded FAB must keep the approved wa.me destination', ); }, ); await t.test('Criterion 1: build strips @include markers from built HTML', () => { assert.doesNotMatch( distHtml, /'), /does-not-exist\.html/i, 'A missing partial include must throw an error naming the unresolved partial', ); }, ); for (const disallowedSpecifier of [ '../package.json', 'src/partials/../package.json', 'src/partials/sub/header.html', 'src/partials/header.txt', ]) { await t.test(`expandHtmlPartials rejects disallowed @include "${disallowedSpecifier}"`, () => { assert.throws( () => expandHtmlPartials(``), /Unsupported @include|Unsafe @include/i, `Disallowed @include "${disallowedSpecifier}" must be rejected`, ); }); } await t.test('transformIndexHtml marks kundalini current and strips home hint in dev ctx', () => { const plugin = htmlPartialsPlugin(); plugin.configResolved?.({ root: process.cwd() }); const result = plugin.transformIndexHtml.handler(navSampleHtml, { path: '/kundalini/', filename: path.resolve('index.html'), server: {}, }); assert.match(result, /href="\/kundalini\/"[^>]*\baria-current="page"/i); assert.doesNotMatch(result, /href="\/"[^>]*\baria-current="page"/i); assert.doesNotMatch(result, /\bdata-nav-current\b/i); }); await t.test( 'transformIndexHtml marks kundalini current and strips home hint in build ctx', () => { const plugin = htmlPartialsPlugin(); plugin.configResolved?.({ root: process.cwd() }); const result = plugin.transformIndexHtml.handler(navSampleHtml, { path: '/index.html', filename: path.resolve('kundalini/index.html'), }); assert.match(result, /href="\/kundalini\/"[^>]*\baria-current="page"/i); assert.doesNotMatch(result, /href="\/"[^>]*\baria-current="page"/i); assert.doesNotMatch(result, /\bdata-nav-current\b/i); }, ); await t.test( 'transformIndexHtml strips all data-nav-current attributes without setting aria-current on non-matching routes', () => { const plugin = htmlPartialsPlugin(); plugin.configResolved?.({ root: process.cwd() }); const result = plugin.transformIndexHtml.handler(navSampleHtml, { path: '/unknown/', filename: path.resolve('index.html'), server: {}, }); assert.doesNotMatch(result, /\baria-current=/i); assert.doesNotMatch(result, /\bdata-nav-current\b/i); }, ); });