Mostly perfectionist import/key sorting and curly braces from --fix, plus: - spread instead of .apply() in the Meta Pixel stub - non-capturing group in the locale-code pattern - React 19 use(Context) / <Context value> in TurnstileProvider - email regex rewritten without super-linear backtracking (covered by a test) - console.warn/error allowed (deliberate server diagnostics) - test/tsconfig.json so the project service and tsc see test files Co-Authored-By: Claude Opus 5.5 <[email protected]>
54 lines
1.9 KiB
TypeScript
54 lines
1.9 KiB
TypeScript
import type { Config } from 'payload'
|
|
|
|
import { describe, expect, test, vi } from 'vitest'
|
|
|
|
import type { I18nConfig } from '../src/modules/i18n/types.js'
|
|
|
|
import { ipalKit } from '../src/plugin.js'
|
|
|
|
const i18n: I18nConfig = {
|
|
defaultLocale: 'pl',
|
|
locales: [
|
|
{ code: 'pl', label: 'Polski' },
|
|
{ code: 'en', label: 'English' },
|
|
],
|
|
}
|
|
|
|
const base = (): Config =>
|
|
({
|
|
collections: [{ slug: 'users', auth: true, fields: [] }],
|
|
secret: 'test',
|
|
}) as unknown as Config
|
|
|
|
describe('ipalKit — config transform', () => {
|
|
test('adds globals, endpoint, localization and roles', async () => {
|
|
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, twoFactor: false })(base())
|
|
|
|
expect(config.globals?.map((g) => g.slug)).toEqual(
|
|
expect.arrayContaining(['site-settings', 'site-integrations']),
|
|
)
|
|
expect(config.endpoints?.some((e) => e.path === '/ipal/test-email')).toBe(true)
|
|
expect(config.localization && config.localization.defaultLocale).toBe('pl')
|
|
|
|
const users = config.collections?.find((c) => c.slug === 'users')
|
|
expect(users?.fields.some((f) => 'name' in f && f.name === 'roles')).toBe(true)
|
|
})
|
|
|
|
test('enforces 2FA unless explicitly disabled', async () => {
|
|
await expect(ipalKit({ i18n })(base())).rejects.toThrow(/twoFactor\.issuer is required/)
|
|
|
|
const totp = vi.fn(() => (c: Config) => c)
|
|
await ipalKit({ i18n, twoFactor: { issuer: 'Acme', plugin: totp as never } })(base())
|
|
expect(totp).toHaveBeenCalledWith(expect.objectContaining({ forceSetup: true, totp: { issuer: 'Acme' } }))
|
|
})
|
|
|
|
test('enabled: false leaves the config untouched', async () => {
|
|
const config = base()
|
|
expect(await ipalKit({ enabled: false, i18n, twoFactor: false })(config)).toBe(config)
|
|
})
|
|
|
|
test('invalid i18n fails fast', () => {
|
|
expect(() => ipalKit({ i18n: { defaultLocale: 'de', locales: i18n.locales }, twoFactor: false })).toThrow()
|
|
})
|
|
})
|