/** The site of test/transactionPool*.test.ts: a contact form, accounts, mailAdapter with short SMTP time-outs. */ import { lexicalEditor } from '@payloadcms/richtext-lexical' import net from 'node:net' import os from 'node:os' import path from 'node:path' import { getPayload } from 'payload' import { mailAdapter } from '../src/modules/email/mailAdapter.js' import { iapl } from '../src/plugin.js' import { buildTestConfig } from './helpers.js' import { testDatabase } from './http/db.js' const text = (value: string) => ({ root: { type: 'root', children: [{ type: 'paragraph', children: [{ type: 'text', text: value, version: 1 }], direction: null, format: '', indent: 0, version: 1 }], direction: null, format: '', indent: 0, version: 1 }, }) /** An SMTP server that accepts the connection and never says a word. */ export async function silentServer(): Promise<{ close: () => Promise; port: number }> { const sockets = new Set() const server = net.createServer((socket) => { sockets.add(socket) socket.on('close', () => sockets.delete(socket)) }) await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)) return { close: () => { for (const s of sockets) {s.destroy()} return new Promise((resolve) => server.close(() => resolve())) }, port: (server.address() as net.AddressInfo).port, } } export async function site(name: string, { emailsInTransaction = false } = {}) { const payload = await getPayload({ config: buildTestConfig({ admin: { user: 'users' }, collections: [ { slug: 'users', auth: true, fields: [] }, { slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'iapl-test-pool-media') } }, { slug: 'pages', access: { read: () => true }, fields: [{ name: 'title', type: 'text' }] }, { slug: 'log', fields: [{ name: 'note', type: 'text' }] }, ], db: testDatabase(name), editor: lexicalEditor(), // Short SMTP time-outs: a server that hangs costs two seconds a send. email: mailAdapter({ smtp: { timeouts: { connectionMs: 2000, greetingMs: 2000, socketMs: 2000 } } }), plugins: [ iapl({ access: { authCollection: 'users' }, forms: { emailsInTransaction, formSubmissionOverrides: { hooks: { // a later hook that fails: the submission is rolled back afterChange: [ ({ doc }) => { if ((doc as { submissionData?: { value?: string }[] }).submissionData?.some((row) => row.value === 'roll-me-back')) {throw new Error('rolled back on purpose')} }, // a project's hook that writes through the Local API WITHOUT `req`: a transaction of its own, // begun while the submission's is still open async ({ doc, req }) => { const rows = (doc as { id: number | string; submissionData?: { value?: string }[] }).submissionData ?? [] if (!rows.some((row) => row.value?.startsWith('nested-'))) {return} await new Promise((resolve) => setTimeout(resolve, 50)) await req.payload.create({ collection: 'log' as never, data: { note: `submission ${doc.id}` } as never }) }, ], }, }, }, i18n: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] }, mail: { relay: false }, twoFactor: false, }), ], secret: 'transaction-pool-test-secret-0123456789', serverURL: 'https://example.com', telemetry: false, }), }) const form = await payload.create({ collection: 'forms' as never, data: { confirmationMessage: text('ok'), confirmationType: 'message', emails: [ { emailTo: 'biuro@example.com', message: text('{{message}}'), subject: 'New message' }, { emailTo: '{{email}}', message: text('Thank you'), subject: 'Thank you' }, ], fields: [ { name: 'email', blockType: 'email', label: 'E-mail', required: true }, { name: 'message', blockType: 'text', label: 'Message', required: true }, ], title: 'Contact', } as never, }) return { formId: String(form.id), payload } }