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 ['a@b.co', 'jan.kowalski@mail.example.com', 'x+tag@sub.domain.pl']) { expect(isEmail(ok)).toBe(true) } for (const bad of ['', 'a', 'a@b', 'a b@c.pl', 'a@b c.pl', '@b.pl', 'a@.pl', 'a@@b.pl']) { expect(isEmail(bad)).toBe(false) } }) })