cms.c2sgmbh/tests/int/i18n.int.spec.ts
Martin Porwoll 6ccb50c5f4 docs: consolidate and update documentation
- Remove obsolete instruction documents (PROMPT_*.md, SECURITY_FIXES.md)
- Update CLAUDE.md with security features, test suite, audit logs
- Merge Techstack_Dokumentation into INFRASTRUCTURE.md
- Update SECURITY.md with custom login route documentation
- Add changelog to TODO.md
- Update email service and data masking for SMTP error handling
- Extend test coverage for CSRF and data masking

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-09 09:25:00 +00:00

139 lines
4.2 KiB
TypeScript

import { getPayload, Payload } from 'payload'
import config from '@/payload.config'
import { describe, it, beforeAll, expect } from 'vitest'
import { locales, defaultLocale, isValidLocale, getLocaleFromPathname, addLocaleToPathname } from '@/lib/i18n'
let payload: Payload
describe('i18n Library', () => {
describe('locale validation', () => {
it('validates correct locales', () => {
expect(isValidLocale('de')).toBe(true)
expect(isValidLocale('en')).toBe(true)
})
it('rejects invalid locales', () => {
expect(isValidLocale('fr')).toBe(false)
expect(isValidLocale('es')).toBe(false)
expect(isValidLocale('')).toBe(false)
expect(isValidLocale('deutsch')).toBe(false)
})
})
describe('locale from pathname', () => {
it('extracts locale from pathname', () => {
expect(getLocaleFromPathname('/de/page')).toBe('de')
expect(getLocaleFromPathname('/en/page')).toBe('en')
})
it('returns default locale for paths without locale', () => {
expect(getLocaleFromPathname('/page')).toBe(defaultLocale)
expect(getLocaleFromPathname('/')).toBe(defaultLocale)
})
it('returns default locale for invalid locale in path', () => {
expect(getLocaleFromPathname('/fr/page')).toBe(defaultLocale)
})
})
describe('addLocaleToPathname', () => {
it('adds locale prefix to pathname', () => {
expect(addLocaleToPathname('/page', 'de')).toBe('/de/page')
expect(addLocaleToPathname('/page', 'en')).toBe('/en/page')
})
it('handles root path', () => {
expect(addLocaleToPathname('/', 'de')).toBe('/de')
expect(addLocaleToPathname('/', 'en')).toBe('/en')
})
it('replaces existing locale', () => {
expect(addLocaleToPathname('/de/page', 'en')).toBe('/en/page')
expect(addLocaleToPathname('/en/page', 'de')).toBe('/de/page')
})
})
describe('locales configuration', () => {
it('has correct default locale', () => {
expect(defaultLocale).toBe('de')
})
it('supports German and English', () => {
expect(locales).toContain('de')
expect(locales).toContain('en')
expect(locales.length).toBe(2)
})
})
})
describe('Payload Localization Integration', () => {
beforeAll(async () => {
const payloadConfig = await config
payload = await getPayload({ config: payloadConfig })
})
it('payload config has localization enabled', async () => {
const payloadConfig = await config
expect(payloadConfig.localization).toBeDefined()
expect(payloadConfig.localization?.locales).toBeDefined()
expect(payloadConfig.localization?.defaultLocale).toBe('de')
})
it('payload config has i18n enabled', async () => {
const payloadConfig = await config
expect(payloadConfig.i18n).toBeDefined()
expect(payloadConfig.i18n?.supportedLanguages).toBeDefined()
expect(payloadConfig.i18n?.fallbackLanguage).toBe('de')
})
// Note: Actual database operations require migration to be run first
// These tests verify the configuration is correct
})
describe('Search with Locale', () => {
beforeAll(async () => {
const payloadConfig = await config
payload = await getPayload({ config: payloadConfig })
})
it('search supports locale parameter', async () => {
// Import search functions
const { searchPosts } = await import('@/lib/search')
// Search with German locale
const resultDe = await searchPosts(payload, {
query: 'test',
locale: 'de',
limit: 10,
offset: 0,
})
expect(resultDe.filters.locale).toBe('de')
})
it('suggestions support locale parameter', async () => {
const { getSearchSuggestions } = await import('@/lib/search')
const suggestions = await getSearchSuggestions(payload, {
query: 'test',
locale: 'en',
limit: 5,
})
// Should return array (may be empty if no matching posts)
expect(Array.isArray(suggestions)).toBe(true)
})
it('getPostsByCategory supports locale parameter', async () => {
const { getPostsByCategory } = await import('@/lib/search')
const result = await getPostsByCategory(payload, {
locale: 'de',
page: 1,
limit: 10,
})
expect(result).toHaveProperty('docs')
expect(result).toHaveProperty('totalDocs')
})
})