import { afterEach, describe, expect, test, vi } from 'vitest' import { createSubmitAction } from '../src/modules/forms/createSubmitAction.js' import { fakePayload } from './helpers.js' vi.mock('server-only', () => ({})) const forms = { contact: { fields: [{ name: 'name', blockType: 'text', required: true }] } } describe('createSubmitAction', () => { afterEach(() => vi.restoreAllMocks()) const make = (ip: string) => { const { create, payload } = fakePayload({ forms }) ;(payload as unknown as { findGlobal: unknown }).findGlobal = vi.fn(async ({ slug }: { slug: string }) => slug === 'notifications' ? { form: { success: 'Dzięki!', validation: 'Uzupełnij pola' } } : {}, ) const sendForm = createSubmitAction({ getCachedPayload: async () => payload, headers: () => new Headers({ 'x-real-ip': ip }) }) return { create, sendForm } } test('valid submission → stored, message from Notifications', async () => { const { create, sendForm } = make('10.8.0.1') expect(await sendForm('contact', 'pl', { name: 'Ala' })).toEqual({ message: 'Dzięki!', success: true }) expect(create).toHaveBeenCalledOnce() }) test('honeypot → fake success, nothing stored; field is stripped otherwise', async () => { const { create, sendForm } = make('10.8.0.2') expect((await sendForm('contact', 'pl', { name: 'x', company_url: 'http://spam' })).success).toBe(true) expect(create).not.toHaveBeenCalled() expect((await sendForm('contact', 'pl', { name: 'Ala', company_url: '' })).success).toBe(true) expect(create).toHaveBeenCalledOnce() }) test('failure carries reason and field for the renderer', async () => { const { sendForm } = make('10.8.0.3') expect(await sendForm('contact', 'pl', {})).toMatchObject({ field: 'name', reason: 'validation', success: false }) }) })