import type { Config } from 'payload' import { describe, expect, test } from 'vitest' import { bottomOverlap } from '../src/modules/consent/CookieButton.js' import { TOTP_TEXTS } from '../src/modules/security/totp/totpTexts.js' import { iapl } from '../src/plugin.js' import { withTotpParts } from './helpers.js' const i18n = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }, { code: 'en', label: 'English' }] } /** Stand-in for @clocklimited/payload-2fa: its own texts and the two views, as the package adds them. */ const fakeTotp = (o: { collection: string }) => (config: Config) => ({ ...withTotpParts(config, o.collection), admin: { ...config.admin, components: { ...config.admin?.components, views: { ...config.admin?.components?.views, SetupTOTP: { Component: { path: 'pkg#TOTPSetup' }, path: 'setup-totp' }, VerifyTOTP: { Component: { path: 'pkg#TOTPVerify' }, path: 'verify-totp' }, }, }, }, i18n: { ...config.i18n, translations: { ...config.i18n?.translations, en: { ...config.i18n?.translations?.en, totpPlugin: { verify: { title: 'Verification' } } }, pl: { ...config.i18n?.translations?.pl, totpPlugin: { verify: { title: 'Weryfikacja' } } }, }, }, }) const base = (translations?: Record): Config => ({ collections: [{ slug: 'users', auth: true, fields: [] }], i18n: translations ? { translations } : undefined, secret: 'test' }) as unknown as Config type Translations = Record; verify: { title: string } } }> describe('2FA texts', () => { test('IAPL texts go over the package, the project over both; screens get a tab title', async () => { const config = await iapl({ i18n, twoFactor: { issuer: 'Acme', plugin: fakeTotp } } as never)( base({ en: { totpPlugin: { verify: { title: 'Project check' } } } }), ) const t = config.i18n!.translations as Translations expect(t.pl.totpPlugin.verify.title).toBe('Weryfikacja dwuetapowa') expect(t.pl.totpPlugin.setup.enterCode).toBe('Wpisz {digits}-cyfrowy kod z aplikacji:') expect(t.en.totpPlugin.verify.title).toBe('Project check') const views = config.admin!.components!.views as Record expect(views.VerifyTOTP.meta?.title).toBe('Weryfikacja dwuetapowa') expect(views.SetupTOTP.meta?.title).toBe('Weryfikacja dwuetapowa') }) test("texts: false keeps the package's", async () => { const config = await iapl({ i18n, twoFactor: { issuer: 'Acme', plugin: fakeTotp, texts: false } } as never)(base()) expect((config.i18n!.translations as Translations).pl.totpPlugin.verify.title).toBe('Weryfikacja') }) test('same keys in English and Polish, short and formal', () => { const keys = (o: object, p = ''): string[] => Object.entries(o).flatMap(([k, v]) => (typeof v === 'object' ? keys(v as object, `${p}${k}.`) : [`${p}${k}`])) expect(keys(TOTP_TEXTS.pl)).toEqual(keys(TOTP_TEXTS.en)) const all = [...Object.values(TOTP_TEXTS.pl.totpPlugin.setup), TOTP_TEXTS.pl.totpPlugin.fieldDescription] expect(all.some((s) => /\b(?:Tw[oó]j|Będziesz|Masz)\b/.test(s))).toBe(false) expect(TOTP_TEXTS.en.totpPlugin.forceTotp.label.split(' ').length).toBeLessThanOrEqual(3) }) }) describe('CookieButton lift above the footer', () => { test('moves up by the visible part of the footer', () => { expect(bottomOverlap([], 800)).toBe(0) expect(bottomOverlap([1200], 800)).toBe(0) // footer below the fold expect(bottomOverlap([650], 800)).toBe(150) expect(bottomOverlap([700, 650], 800)).toBe(150) // the highest one counts }) test('never pushes the button off the screen', () => { expect(bottomOverlap([-500], 800)).toBe(704) }) })