mirror of
https://github.com/bitbaum/sites.git
synced 2026-12-04 02:56:58 +01:00
* feat: import a sitekit repo, export a site as its own repo, compare any two renders loki ADR-0001 build steps 4, 8 and 9 (tooling half). - scripts/import-repo.ts + import-theme.ts: read a sitekit consumer's content module and globals.css into spec + theme + settings. Text tokens below WCAG AA move to the nearest passing shade, each change reported (camille: fg-muted 3.15→4.53:1, accent 3.94→4.54:1, nothing else). - scripts/export-site.ts + export/template: a site bundle becomes a static Next repo with the exact sitekit the engine pins, the engine's own stylesheet and font copier (copied, one source), one git commit per version, and its own verify. Refuses a bundle it could not render, and never writes over a directory. - scripts/compare-renders.mjs: render pages from two bases at 390/834/1440 (with --map to fetch a real host from a local port) and exit 1 on any difference a visitor would notice. Measured 2026-11-20: live camille repo vs the engine serving its imported row, and the engine vs camille's export built to static files — no differences on any page at any width. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat: a site's free address answers 308 to its verified own domain The apex before www, path kept, method and body kept (308, as apps do). Checked locally: camille.orangecat.ch/catering → 308 https://camille.example/catering once the domain is verified, both hosts serve, the ask approves the domain, and removing the row restores the free address. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat: contact forms — the first block with a server half, in the engine and in exports loki ADR-0001 build step 11, block 1. sitekit's new `form` section posts to /_sites/forms: - src/lib/forms.ts (shared by the engine and every export): only a form that exists on the named page is read, only its own fields, length-capped; a filled honeypot answers "sent" and is dropped; the owner's mail is plain text with reply-to the visitor; problems are worded in the site's language. - The engine route resolves the site by Host, never sends from the preview host, rate-limits five per visitor per site per ten minutes keyed on the LAST X-Forwarded-For hop (a forged first hop is still limited), mails settings.formTo through @bitbaum/mail-kit, and answers 303 with a RELATIVE Location (an absolute one built from req.url sent visitors to the loopback origin — caught locally, guarded by a test). - Export: a site with a form becomes a small standalone server with the same forms.ts and route, mailing through the owner's own provider; a site without one stays static files. Checked locally against camille with a form: every outcome (missing, bad email, unknown form, honeypot, delivery failure without a mail key, sixth message busy, forged first hop busy, preview never sends), the page notices, 390px layout (no sideways scroll, 44px button, honeypot off-screen), and compare-renders engine vs export (server variant and static variant): no differences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat: booking requests through the same form route; pin sitekit with map, gallery and card actions forms.ts checks date (YYYY-MM-DD), time (HH:MM) and people (1–50), words the problems in the site's language, and puts them in the owner's mail. sitekit is pinned to the kits commit that adds the gallery, map and card-action blocks, so the engine renders them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * chore: pin sitekit with the map control fix Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
145 lines
5.2 KiB
TypeScript
145 lines
5.2 KiB
TypeScript
/**
|
|
* A visitor's form submission: only a form that exists can be sent, only its
|
|
* own fields are read, a honeypot is dropped as "sent", and nothing typed
|
|
* becomes markup in the owner's mail.
|
|
*/
|
|
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { composeMail, isProblem, parseSubmission, problemText } from '../src/lib/forms';
|
|
|
|
const FORM = {
|
|
kind: 'form' as const,
|
|
anchor: 'kontakt',
|
|
fields: [
|
|
{ name: 'name' as const, label: 'Name', required: true },
|
|
{ name: 'email' as const, label: 'E-Mail', required: true },
|
|
{ name: 'message' as const, label: 'Nachricht' },
|
|
],
|
|
submitLabel: 'Senden',
|
|
sentText: 'Danke.',
|
|
privacyNote: 'Nur an uns.',
|
|
};
|
|
const SPEC = {
|
|
chrome: { name: 'Muster', tagline: 'Brot', footerNote: 'x' },
|
|
pages: [
|
|
{ path: '', title: 'Start', sections: [{ kind: 'prose' as const, paragraphs: ['Hallo.'] }] },
|
|
{ path: 'kontakt', title: 'Kontakt', sections: [FORM] },
|
|
],
|
|
};
|
|
const fd = (o: Record<string, string>) => {
|
|
const f = new FormData();
|
|
for (const [k, v] of Object.entries(o)) f.set(k, v);
|
|
return f;
|
|
};
|
|
const base = { _page: 'kontakt', _form: 'kontakt', name: 'Anna', email: 'anna@example.ch' };
|
|
|
|
test('a complete submission is read, trimmed and capped', () => {
|
|
const r = parseSubmission(SPEC, fd({ ...base, message: ' Hallo ', extra: 'ignored' }));
|
|
assert.ok(r.ok);
|
|
assert.deepEqual(r.values, { name: 'Anna', email: 'anna@example.ch', message: 'Hallo' });
|
|
assert.equal(r.trap, false);
|
|
const long = parseSubmission(SPEC, fd({ ...base, message: 'x'.repeat(9000) }));
|
|
assert.ok(long.ok && long.values.message!.length === 5000);
|
|
});
|
|
|
|
test('a form that is not on that page cannot be sent', () => {
|
|
for (const o of [
|
|
{ ...base, _page: '' },
|
|
{ ...base, _form: 'nope' },
|
|
{ ...base, _page: 'a/b' },
|
|
]) {
|
|
const r = parseSubmission(SPEC, fd(o));
|
|
assert.ok(!r.ok && r.problem === 'unknown-form', JSON.stringify(o));
|
|
}
|
|
});
|
|
|
|
test('missing, bad email and bad phone are named', () => {
|
|
const missing = parseSubmission(SPEC, fd({ ...base, name: ' ' }));
|
|
assert.ok(!missing.ok && missing.problem === 'missing');
|
|
const email = parseSubmission(SPEC, fd({ ...base, email: 'anna(at)example' }));
|
|
assert.ok(!email.ok && email.problem === 'email');
|
|
});
|
|
|
|
test('a filled honeypot is accepted as sent and marked, so it is never delivered', () => {
|
|
const r = parseSubmission(SPEC, fd({ ...base, website: 'http://spam' }));
|
|
assert.ok(r.ok && r.trap);
|
|
});
|
|
|
|
test('the owner mail is plain text, replies go to the visitor', () => {
|
|
const r = parseSubmission(SPEC, fd({ ...base, message: '<b>hi</b>' }));
|
|
assert.ok(r.ok);
|
|
const m = composeMail('Muster', 'muster.orangecat.ch', 'kontakt', r);
|
|
assert.equal(m.replyTo, 'anna@example.ch');
|
|
assert.match(m.text, /muster\.orangecat\.ch\/kontakt/);
|
|
assert.match(m.text, /<b>hi<\/b>/, 'kept as text, never rendered');
|
|
assert.equal(m.subject, 'Muster: message from Anna');
|
|
});
|
|
|
|
test('problems read in the site language, English otherwise', () => {
|
|
assert.match(problemText('de-CH', 'missing'), /Bitte/);
|
|
assert.match(problemText('fr', 'email'), /e-mail/);
|
|
assert.match(problemText('ja', 'busy'), /Too many/);
|
|
assert.ok(isProblem('busy') && !isProblem('drop table'));
|
|
});
|
|
|
|
test('the form route answers with a relative Location (never the loopback origin)', async () => {
|
|
const { readFileSync } = await import('node:fs');
|
|
const route = readFileSync('src/app/%5Fsites/forms/route.ts', 'utf8');
|
|
assert.ok(
|
|
!/NextResponse\.redirect\(/.test(route),
|
|
'NextResponse.redirect builds an absolute URL from req.url',
|
|
);
|
|
assert.match(route, /status: 303/);
|
|
});
|
|
|
|
test('a booking request: date, time and people are checked and reach the mail', () => {
|
|
const booking = {
|
|
...SPEC,
|
|
pages: [
|
|
{
|
|
path: 'reservieren',
|
|
title: 'Reservieren',
|
|
sections: [
|
|
{
|
|
kind: 'form' as const,
|
|
anchor: 'tisch',
|
|
fields: [
|
|
{ name: 'phone' as const, label: 'Telefon', required: true },
|
|
{ name: 'date' as const, label: 'Datum', required: true },
|
|
{ name: 'time' as const, label: 'Zeit', required: true },
|
|
{ name: 'people' as const, label: 'Personen', required: true },
|
|
],
|
|
submitLabel: 'Anfragen',
|
|
sentText: 'Danke.',
|
|
privacyNote: 'Nur für die Reservation.',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
};
|
|
const ok = {
|
|
_page: 'reservieren',
|
|
_form: 'tisch',
|
|
phone: '+41 44 000 12 34',
|
|
date: '2026-12-24',
|
|
time: '19:30',
|
|
people: '4',
|
|
};
|
|
const r = parseSubmission(booking, fd(ok));
|
|
assert.ok(r.ok);
|
|
const mail = composeMail('Muster', 'muster.orangecat.ch', 'reservieren', r);
|
|
assert.match(mail.text, /Date: 2026-12-24\nTime: 19:30\nPeople: 4/);
|
|
for (const [k, v, problem] of [
|
|
['date', '24.12.2026', 'date'],
|
|
['date', '2026-13-01', 'date'],
|
|
['time', '25:00', 'time'],
|
|
['people', '0', 'people'],
|
|
['people', '51', 'people'],
|
|
['people', 'vier', 'people'],
|
|
] as const) {
|
|
const bad = parseSubmission(booking, fd({ ...ok, [k]: v }));
|
|
assert.ok(!bad.ok && bad.problem === problem, `${k}=${v}`);
|
|
}
|
|
assert.match(problemText('de', 'people'), /zwischen 1 und 50/);
|
|
});
|