From 93fa729ad707a6d3949284bb9bd9cd0b27cc9ade Mon Sep 17 00:00:00 2001 From: Sluijsens Date: Tue, 10 Nov 2026 23:10:14 +0100 Subject: [PATCH] cms-client: captcha-slot, initialSubject en apiEndpoint; fixes voor erasableSyntaxOnly en recursieve attempt Co-Authored-By: Claude Sonnet 5.5 --- src/cms-client/README.md | 7 + .../__tests__/createContactClient.test.ts | 30 ++- .../__tests__/useContactForm.captcha.test.tsx | 221 ++++++++++++++++++ src/cms-client/contact/createContactClient.ts | 5 +- src/cms-client/contact/messages.nl.ts | 3 + src/cms-client/contact/types.ts | 12 +- src/cms-client/contact/useContactForm.ts | 94 ++++++-- src/cms-client/http/problem.ts | 5 +- .../landing/components/ContactFormView.tsx | 1 + src/variants/e/TerminalForm.tsx | 1 + 10 files changed, 360 insertions(+), 19 deletions(-) create mode 100644 src/cms-client/contact/__tests__/useContactForm.captcha.test.tsx diff --git a/src/cms-client/README.md b/src/cms-client/README.md index 5ea3925..4b331c3 100644 --- a/src/cms-client/README.md +++ b/src/cms-client/README.md @@ -7,3 +7,10 @@ Headless client for the SLP CMS API, written inside the SlpSoftware frontend fir - **Contract**: see `aidlc-docs/features/redesign-modern-timetravel/inception/application-design/contact-api-contract.md` and the CMS file `openapi/slpsoftware.v1.json`. - **No markup, no styling**: sites render their own fields from `useContactForm().getFieldProps`. - **Privacy**: nothing here logs form values, tokens or idempotency keys. + +## Captcha slot and initial subject + +- The token response may carry `captcha: { provider, siteKey, apiEndpoint }` (`apiEndpoint` is `global` or `eu`, default `global`). `useContactForm` returns `captcha`, `captchaRequired`, `captchaToken`, `setCaptchaToken(token | null)` and `captchaResetCount`. The package renders no widget: the site mounts the provider widget client-side, calls `setCaptchaToken(response)` when solved and `setCaptchaToken(null)` when it expires or fails, and resets the widget whenever `captchaResetCount` changes (a response can be used once). +- With captcha required, submit sends nothing until a response is set and shows `messages.captchaRequired`; the payload then carries `captchaToken`. Without captcha the field is omitted. +- `initialSubject` (option of `useContactForm`) applies a subject chosen before the form was mounted, as soon as it exists in `subjectOptions` and unless the visitor already picked one. Unknown values are ignored. +- Subject values are the readable labels (they appear in the mail and the CMS message list); a site keeps its own ids only for links/preselect. diff --git a/src/cms-client/contact/__tests__/createContactClient.test.ts b/src/cms-client/contact/__tests__/createContactClient.test.ts index 9b14929..60405ba 100644 --- a/src/cms-client/contact/__tests__/createContactClient.test.ts +++ b/src/cms-client/contact/__tests__/createContactClient.test.ts @@ -39,7 +39,9 @@ describe('getToken', () => { kind: 'ok', token: { token: 'abc', minSeconds: 3, captcha: null }, }); - expect((fetchMock.mock.calls[0] as unknown as [string])[0]).toBe('/api/v1/contact-messages/token'); + expect((fetchMock.mock.calls[0] as unknown as [string])[0]).toBe( + '/api/v1/contact-messages/token', + ); }); it('parses captcha info', async () => { @@ -47,7 +49,31 @@ describe('getToken', () => { json({ token: 'abc', minSeconds: 3, captcha: { provider: 'fc', siteKey: 'k' } }, 200), ); const result = await api.getToken(); - expect(result.kind === 'ok' && result.token.captcha).toEqual({ provider: 'fc', siteKey: 'k' }); + expect(result.kind === 'ok' && result.token.captcha).toEqual({ + provider: 'fc', + siteKey: 'k', + apiEndpoint: 'global', + }); + }); + + it('takes the captcha api endpoint from the server and falls back to global', async () => { + const eu = client( + json( + { token: 'a', minSeconds: 3, captcha: { provider: 'fc', siteKey: 'k', apiEndpoint: 'eu' } }, + 200, + ), + ); + const euResult = await eu.api.getToken(); + expect(euResult.kind === 'ok' && euResult.token.captcha?.apiEndpoint).toBe('eu'); + + const odd = client( + json( + { token: 'a', minSeconds: 3, captcha: { provider: 'fc', siteKey: 'k', apiEndpoint: 'x' } }, + 200, + ), + ); + const oddResult = await odd.api.getToken(); + expect(oddResult.kind === 'ok' && oddResult.token.captcha?.apiEndpoint).toBe('global'); }); it.each([ diff --git a/src/cms-client/contact/__tests__/useContactForm.captcha.test.tsx b/src/cms-client/contact/__tests__/useContactForm.captcha.test.tsx new file mode 100644 index 0000000..c6a3e11 --- /dev/null +++ b/src/cms-client/contact/__tests__/useContactForm.captcha.test.tsx @@ -0,0 +1,221 @@ +import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'; +import { describe, expect, it, vi } from 'vitest'; +import { useContactForm } from '../useContactForm'; +import type { ContactClient, ContactPayload, ContactToken, SubmitResult } from '../types'; + +const OPTIONS = [ + { value: 'Website', label: 'Website' }, + { value: 'Overig', label: 'Overig' }, +]; + +const CAPTCHA_TOKEN: ContactToken = { + token: 'tok', + minSeconds: 3, + captcha: { provider: 'friendlycaptcha', siteKey: 'FCMKEY', apiEndpoint: 'global' }, +}; +const PLAIN_TOKEN: ContactToken = { token: 'tok', minSeconds: 3, captcha: null }; + +function makeClient(results: SubmitResult[] = [{ kind: 'ok' }]) { + const payloads: ContactPayload[] = []; + const submit = vi.fn(async (payload: ContactPayload) => { + payloads.push(payload); + return results.shift() ?? ({ kind: 'ok' } as SubmitResult); + }); + const client: ContactClient = { + getToken: async () => ({ kind: 'ok', token: PLAIN_TOKEN }), + submit, + }; + return { client, payloads, submit }; +} + +function Harness(props: { + token: ContactToken; + client: ContactClient; + options?: typeof OPTIONS; + initialSubject?: string | null; +}) { + const form = useContactForm({ + idPrefix: 'c', + subjectOptions: props.options ?? OPTIONS, + initialSubject: props.initialSubject, + client: props.client, + token: props.token, + refreshToken: async () => ({ kind: 'ok', token: props.token }), + fallbackAddress: 'info@example.nl', + }); + const name = form.getFieldProps('name'); + const email = form.getFieldProps('email'); + const subject = form.getFieldProps('subject'); + const message = form.getFieldProps('message'); + return ( +
+ + + +