import type { BasePayload, Config } from 'payload' import { buildConfig } from 'payload' import { vi } from 'vitest' /** * The only way a test builds a real Payload config. Outside production * getPayload() spawns a detached `payload generate:types` child; with no * PAYLOAD_CONFIG_PATH it never finds a config (tsconfig rootDir "./" makes * Payload's findUpSync loop forever) and is left spinning after vitest exits. * test/testConfig.test.ts fails when a test calls buildConfig directly. */ export const buildTestConfig = (config: Config) => buildConfig({ ...config, typescript: { ...config.typescript, autoGenerate: false } }) type FakeOptions = { forms?: Record[] }> integrations?: Record } /** The slice of the Local API the form/Turnstile code touches. */ export function fakePayload({ forms = {}, integrations = {} }: FakeOptions = {}) { const create = vi.fn(async () => ({ id: 'sub-1' })) const payload = { create, findByID: vi.fn(async ({ id }: { id: string }) => { const form = forms[id] if (!form) {throw new Error('Not Found')} return { id, ...form } }), findGlobal: vi.fn(async () => integrations), logger: { error: vi.fn(), info: vi.fn(), warn: vi.fn() }, updateGlobal: vi.fn(async () => integrations), } return { create, payload: payload as unknown as BasePayload } }