import test from 'node:test';
import assert from 'node:assert/strict';
import { createRequire } from 'node:module';
import { fileURLToPath } from 'node:url';
import { build } from 'esbuild';
import { DOMParser } from '@xmldom/xmldom';
import { createElement, type ComponentType, type ReactNode } from 'react';
import { renderToString } from 'react-dom/server';
import type { PublicSiteConfig } from '../../packages/site/truth.ts';
// Follow the existing geometry SSR convention: bundle actual components in
// memory, with React external. No browser, HTTP server, email, or saved build.
const root = fileURLToPath(new URL('../../', import.meta.url));
const bundle = await build({
absWorkingDir: root,
stdin: { contents: `export { render } from './apps/web/src/entry-server.tsx';
export { DemoCompanion } from './apps/web/src/components/DemoCompanion.tsx';
export { AnimationProvider } from './apps/web/src/animation/orchestration/AnimationProvider.tsx';`, resolveDir: root, sourcefile: 'demo-ssr-test-entry.ts' },
bundle: true, write: false, format: 'cjs', platform: 'node', packages: 'external',
jsx: 'automatic', loader: { '.css': 'empty' }, logLevel: 'silent',
});
type CompanionStatus = 'idle' | 'pending' | 'accepted' | 'error';
type Renderer = {
render(path: string, config: PublicSiteConfig): string;
DemoCompanion: ComponentType<{ status: CompanionStatus; intent: null }>;
AnimationProvider: ComponentType<{ children?: ReactNode }>;
};
const serverModule = { exports: {} as Renderer };
new Function('require', 'module', 'exports', bundle.outputFiles[0].text)(createRequire(import.meta.url), serverModule, serverModule.exports);
const renderer = serverModule.exports;
// An explicit public fixture avoids reading an operator's .env or truth export.
const config: PublicSiteConfig = {
deployment: 'preview', canonicalOrigin: 'https://thatstupidcomputer.com',
siteName: 'That Stupid Computer', websiteId: 'https://thatstupidcomputer.com/#website',
contact: {}, crawlerPolicy: { training: 'deny', googleExtended: 'deny', source: 'preview-default' },
truthStatus: 'unavailable-preview',
};
function parse(markup: string) {
return new DOMParser().parseFromString(`
${markup}`, 'application/xhtml+xml');
}
function elements(markup: string, tag: string) { return Array.from(parse(markup).getElementsByTagName(tag)); }
function byId(markup: string, id: string) {
const element = elements(markup, '*').find(node => node.getAttribute('id') === id);
assert.ok(element, `Expected server-rendered #${id}`);
return element;
}
function htmlAttribute(element: ReturnType, name: string) {
// HTML attribute names are insensitive to case; the XML inspection tree is not.
return Array.from(element.attributes).find(attribute => attribute.name.toLowerCase() === name.toLowerCase())?.value ?? null;
}
test('home SSR includes ordinary demo CTAs and three native intent links before JavaScript', () => {
const markup = renderer.render('/', config);
const links = elements(markup, 'a');
const hero = links.find(node => node.getAttribute('class')?.split(' ').includes('hero-demo'));
assert.ok(hero);
assert.equal(hero.getAttribute('href'), '/contact/');
assert.equal(hero.textContent?.trim(), 'Get my demo');
assert.equal(hero.hasAttribute('aria-hidden'), false);
const intentLabels = { design: 'I need a new website', rescue: 'My website needs rescuing', search: 'Help customers find me' };
for (const [project, label] of Object.entries(intentLabels)) {
const link = links.find(node => node.getAttribute('class')?.split(' ').includes('computer-intent') && node.getAttribute('aria-label') === label);
assert.ok(link, label);
assert.equal(link.getAttribute('href'), `/contact/?project=${project}&from=computer#demo-request`);
assert.equal(link.hasAttribute('aria-hidden'), false);
assert.equal(link.getElementsByTagName('strong')[0]?.textContent, label, 'The visible key label matches its accessible name.');
}
// The caption and tactile keys remain ordinary initial HTML, not a hidden chat panel.
const voice = elements(markup, 'div').find(node => node.getAttribute('class')?.split(' ').includes('computer-voice'));
assert.ok(voice);
assert.match(voice.getElementsByTagName('p')[0].textContent || '', /I have a few ideas for your website/);
assert.equal(elements(markup, 'details').some(node => node.getAttribute('class') === 'computer-chat'), false);
assert.doesNotMatch(markup, /The server did not finish this Suspense boundary|