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]>
89 lines
3.5 KiB
TypeScript
89 lines
3.5 KiB
TypeScript
import { describe, expect, test } from 'vitest'
|
|
|
|
import type { I18nConfig } from '../src/modules/i18n/types.js'
|
|
|
|
import { isHoneypotTripped } from '../src/modules/forms/honeypot.js'
|
|
import { isEmail } from '../src/modules/forms/validators.js'
|
|
import { buildLocalizedPath, switchLocalePath } from '../src/modules/i18n/localizedPath.js'
|
|
import { matchAcceptLanguage, negotiateLocale } from '../src/modules/i18n/negotiateLocale.js'
|
|
|
|
const config: I18nConfig = {
|
|
defaultLocale: 'pl',
|
|
locales: [
|
|
{ code: 'pl', label: 'Polski' },
|
|
{ code: 'en', label: 'English' },
|
|
],
|
|
}
|
|
const single: I18nConfig = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] }
|
|
|
|
describe('negotiateLocale', () => {
|
|
test('cookie → Accept-Language → default', () => {
|
|
expect(negotiateLocale({ acceptLanguage: 'en', config, cookieLocale: 'pl' })).toBe('pl')
|
|
expect(negotiateLocale({ acceptLanguage: 'de-DE,en-US;q=0.8', config })).toBe('en')
|
|
expect(negotiateLocale({ acceptLanguage: 'fr', config, cookieLocale: 'xx' })).toBe('pl')
|
|
})
|
|
|
|
test('respects q-values', () => {
|
|
expect(matchAcceptLanguage('en;q=0.5,pl;q=0.9', config)).toBe('pl')
|
|
expect(matchAcceptLanguage(null, config)).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('buildLocalizedPath', () => {
|
|
const slugs = { en: 'about', pl: 'o-nas' }
|
|
|
|
test('prefixes the locale', () => {
|
|
expect(buildLocalizedPath({ config, locale: 'en', slugs })).toBe('/en/about')
|
|
})
|
|
|
|
test('home collapses to the locale root, per-locale home slugs', () => {
|
|
const home = { en: 'home', pl: 'strona-glowna' }
|
|
expect(buildLocalizedPath({ config, homeSlug: home, locale: 'pl', slugs: home })).toBe('/pl')
|
|
expect(buildLocalizedPath({ config, homeSlug: home, locale: 'en', slugs: home })).toBe('/en')
|
|
})
|
|
|
|
test('single-locale sites drop the prefix', () => {
|
|
expect(buildLocalizedPath({ config: single, locale: 'pl', slugs })).toBe('/o-nas')
|
|
expect(buildLocalizedPath({ config: single, locale: 'pl', slugs: { pl: 'home' } })).toBe('/')
|
|
})
|
|
|
|
test('archive entries need a prefix in that locale', () => {
|
|
const prefix = { pl: 'artykuly' }
|
|
expect(buildLocalizedPath({ config, locale: 'pl', prefix, slugs })).toBe('/pl/artykuly/o-nas')
|
|
expect(buildLocalizedPath({ config, locale: 'en', prefix, slugs })).toBeUndefined()
|
|
})
|
|
|
|
test('unknown locale or missing slug → undefined', () => {
|
|
expect(buildLocalizedPath({ config, locale: 'de', slugs })).toBeUndefined()
|
|
expect(buildLocalizedPath({ config, locale: 'en', slugs: { pl: 'x' } })).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('switchLocalePath', () => {
|
|
test('never dead-ends: falls back to archive, then locale root', () => {
|
|
expect(switchLocalePath({ config, prefix: { en: 'articles', pl: 'artykuly' }, slugs: { pl: 'post' }, targetLocale: 'en' })).toBe(
|
|
'/en/articles',
|
|
)
|
|
expect(switchLocalePath({ config, slugs: { pl: 'o-nas' }, targetLocale: 'en' })).toBe('/en')
|
|
})
|
|
})
|
|
|
|
describe('honeypot', () => {
|
|
test('filled → tripped', () => {
|
|
expect(isHoneypotTripped({ company_url: 'http://spam' })).toBe(true)
|
|
expect(isHoneypotTripped({ company_url: ' ' })).toBe(false)
|
|
expect(isHoneypotTripped({})).toBe(false)
|
|
})
|
|
})
|
|
|
|
describe('isEmail', () => {
|
|
test('accepts real addresses, rejects malformed ones', () => {
|
|
for (const ok of ['[email protected]', '[email protected]', '[email protected]']) {
|
|
expect(isEmail(ok)).toBe(true)
|
|
}
|
|
for (const bad of ['', 'a', 'a@b', 'a [email protected]', 'a@b c.pl', '@b.pl', '[email protected]', 'a@@b.pl']) {
|
|
expect(isEmail(bad)).toBe(false)
|
|
}
|
|
})
|
|
})
|