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( 'Issue #6: built header exposes a "Kundalini Yoga" link to /kundalini/ between Servicios and Otros Espacios', () => { const headerMarkup = extractHeaderMarkup(distHtml); const kundaliniLink = headerMarkup.match( /]*\bhref="\/kundalini\/"[^>]*>([\s\S]*?)<\/a>/i, ); assert.ok(kundaliniLink, 'Built header must contain a link with href="/kundalini/"'); assert.match( kundaliniLink[1].replace(/<[^>]*>/g, ' '), /Kundalini Yoga/, 'The /kundalini/ header link must be labelled "Kundalini Yoga"', ); // Why: spec places the route link right after Servicios so the nav mirrors page order. const serviciosIndex = headerMarkup.indexOf('href="/#servicios"'); const kundaliniIndex = headerMarkup.indexOf('href="/kundalini/"'); const otrosIndex = headerMarkup.indexOf('href="/#otros-espacios"'); assert.ok( serviciosIndex !== -1 && serviciosIndex < kundaliniIndex && kundaliniIndex < otrosIndex, 'The /kundalini/ link must sit between /#servicios and /#otros-espacios', ); }, ); await t.test('Issue #6: /kundalini/ header link is not aria-current on the home route', () => { const headerMarkup = extractHeaderMarkup(distHtml); const kundaliniTag = [...headerMarkup.matchAll(/]*>/gi)] .map((match) => match[0]) .find((tag) => /\bhref="\/kundalini\/"/i.test(tag)); assert.ok(kundaliniTag, 'Built header must contain a link with href="/kundalini/"'); // Why: on "/" only the brand link is current; a second current item would mislead AT users. assert.doesNotMatch( kundaliniTag, /\baria-current=/i, 'The /kundalini/ link must not carry aria-current on the home route', ); }); 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); }, ); // Why: these exercise the real header partial (not a synthetic sample) so the // partial's data-nav-current hints and the plugin's route map stay in sync. const realHeaderHtml = ''; function transformRealHeaderFor(routeFile) { const plugin = htmlPartialsPlugin(); plugin.configResolved?.({ root: process.cwd() }); return plugin.transformIndexHtml.handler(realHeaderHtml, { path: '/index.html', filename: path.resolve(routeFile), }); } function anchorTagByHref(html, href) { return [...html.matchAll(/]*>/gi)] .map((match) => match[0]) .find((tag) => tag.includes(`href="${href}"`)); } await t.test( 'real header partial marks /kundalini/ link aria-current on kundalini/index.html build', () => { const result = transformRealHeaderFor('kundalini/index.html'); const kundaliniTag = anchorTagByHref(result, '/kundalini/'); assert.ok(kundaliniTag, 'Header partial must contain a href="/kundalini/" link'); assert.match(kundaliniTag, /\baria-current="page"/i); assert.doesNotMatch(anchorTagByHref(result, '/') ?? '', /\baria-current=/i); }, ); for (const routeFile of ['volver-a-ti/index.html', 'descargo/index.html']) { await t.test(`real header partial sets no aria-current on ${routeFile} build`, () => { const result = transformRealHeaderFor(routeFile); assert.ok( anchorTagByHref(result, '/kundalini/'), 'Header partial must contain a href="/kundalini/" link', ); // Why: these routes have no nav entry, so no item may claim to be current. assert.doesNotMatch(result, /\baria-current=/i); assert.doesNotMatch(result, /\bdata-nav-current\b/i); }); } });