Lets the CI preview server stand in for the contact API

With contactForm.enabled=true the e2e and Lighthouse runs asked a token route
that CI does not have (404, console error, run 666). The preview server gets an
opt-in --contact-stub (token 200 without captcha, submit 202); the e2e server
helper and the Lighthouse start command switch it on. Tests that need another
answer still replace it per page with page.route.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 19:39:03 +01:00
co-authored by Claude Sonnet 5.5
parent 7e708cf3c7
commit 7737104a77
5 changed files with 87 additions and 4 deletions
+55 -1
View File
@@ -190,6 +190,37 @@ const searchOf = (target: string) => {
export interface PreviewOptions {
auditRoute?: boolean;
// Opt-in stand-in voor de contact-API (--contact-stub): CI heeft geen
// CMS-host, dus met contactForm.enabled=true kreeg de site een 404 op
// de tokenroute (console-fout, run 666). Met deze optie antwoordt de
// server zoals een open formulier zonder captcha: token 200, versturen
// 202. Productie gebruikt dit nooit; tests kunnen het per pagina met
// page.route vervangen (bijvoorbeeld 404 of een captcha).
contactStub?: boolean;
}
const CONTACT_API = '/api/v1/contact-messages';
// Antwoord van de stub, of undefined als het verzoek er niet voor is
export function contactStubResponse(
method: string | undefined,
path: string,
): { status: number; body: string } | undefined {
const json = (status: number, value: unknown) => ({
status,
body: JSON.stringify(value),
});
if (method === 'GET' && path === `${CONTACT_API}/token`) {
return json(200, {
token: 'preview-token',
minSeconds: 1,
captcha: null,
});
}
if (method === 'POST' && path === CONTACT_API) {
return json(202, { status: 'received' });
}
return undefined;
}
export function startPreviewServer(
@@ -207,6 +238,25 @@ export function startPreviewServer(
};
const handle = (request: IncomingMessage, response: ServerResponse) => {
if (options.contactStub) {
const stub = contactStubResponse(
request.method,
pathOf(request.url ?? '/'),
);
if (stub) {
send(
response,
stub.status,
{
'Content-Type': 'application/json',
'Cache-Control': 'no-store',
},
stub.body,
);
request.resume();
return;
}
}
if (request.method !== 'GET' && request.method !== 'HEAD') {
send(
response,
@@ -284,6 +334,7 @@ function parseArguments(args: string[]) {
strictPort: false,
dir: 'dist/client',
auditRoute: false,
contactStub: false,
};
for (let i = 0; i < args.length; i++) {
const [name, inline] = args[i].split('=', 2);
@@ -292,6 +343,7 @@ function parseArguments(args: string[]) {
else if (name === '--dir') options.dir = value();
else if (name === '--strictPort') options.strictPort = true;
else if (name === '--audit-route') options.auditRoute = true;
else if (name === '--contact-stub') options.contactStub = true;
}
return options;
}
@@ -299,7 +351,8 @@ function parseArguments(args: string[]) {
const MAX_PORT_TRIES = 10;
async function main(args: string[]) {
const { port, strictPort, dir, auditRoute } = parseArguments(args);
const { port, strictPort, dir, auditRoute, contactStub } =
parseArguments(args);
if (!Number.isInteger(port) || port < 0 || port > 65535) {
console.error(`Ongeldige poort: ${port}`);
process.exitCode = 1;
@@ -312,6 +365,7 @@ async function main(args: string[]) {
try {
const server = await startPreviewServer(dir, port + attempt, {
auditRoute,
contactStub,
});
const address = server.address();
const actual =