import { describe, expect, it } from 'vitest' import { BrandingSchema, PDF_LABEL_KEYS, PdfLabelSetSchema, type Branding, type PdfLabels, } from '../src' export function sampleBranding(): Branding { return { siteUrl: 'https://faunosteo.com', headerLogo: { kind: 'png', base64: 'iVBORw0KGgo=' }, footerLogo: null, poweredBy: { logo: null, url: 'https://zenquality.fr', fallbackText: 'zenquality.fr', }, partner: { logo: { kind: 'svg', svg: '' }, url: 'https://www.instagram.com/paws.iin.harmony', fallbackText: '@paws.iin.harmony', }, } } export function sampleLabels(): PdfLabels { return Object.fromEntries( PDF_LABEL_KEYS.map((key) => [key, key]), ) as PdfLabels } describe('BrandingSchema', () => { it('accepts the current identity and both image kinds', () => { expect(BrandingSchema.parse(sampleBranding())).toEqual(sampleBranding()) const bare = { siteUrl: 'http://localhost:5173', headerLogo: null, footerLogo: null, poweredBy: null, partner: null, } expect(BrandingSchema.parse(bare)).toEqual(bare) }) it.each([ ['unknown key', (b: Branding) => ({ ...b, extra: 1 })], [ 'non-http URL', (b: Branding) => ({ ...b, siteUrl: 'javascript:alert(1)' }), ], ['ftp URL', (b: Branding) => ({ ...b, siteUrl: 'ftp://x.test/a' })], [ 'URL over 2048 chars', (b: Branding) => ({ ...b, siteUrl: `https://x.test/${'a'.repeat(2048)}`, }), ], [ 'PNG base64 over 512 KiB', (b: Branding) => ({ ...b, headerLogo: { kind: 'png', base64: 'A'.repeat(512 * 1024 + 4) }, }), ], [ 'PNG base64 with foreign characters', (b: Branding) => ({ ...b, headerLogo: { kind: 'png', base64: '' }, }), ], [ 'SVG over 256 KiB', (b: Branding) => ({ ...b, footerLogo: { kind: 'svg', svg: 'x'.repeat(256 * 1024 + 1) }, }), ], [ 'fallback text over 128 chars', (b: Branding) => ({ ...b, poweredBy: { ...b.poweredBy!, fallbackText: 'x'.repeat(129) }, }), ], [ 'credit with unknown key', (b: Branding) => ({ ...b, partner: { ...b.partner!, extra: true } }), ], ])('rejects %s', (_name, mutate) => { expect(BrandingSchema.safeParse(mutate(sampleBranding())).success).toBe( false, ) }) }) /** Closes a PDF `(` string literal and chains an action: must never reach the engine. */ const INJECTION_URL = 'https://a.example/) /Next << /S /JavaScript /JS (app.alert\\(1\\)) >> /Z (' describe('BrandingSchema URLs', () => { it.each([ 'https://faunosteo.com', 'https://zenquality.fr', 'https://www.instagram.com/paws.iin.harmony', 'http://localhost:5173', 'https://x.test/a-b_c~d?e=1&f=2#frag', `https://x.test/${'a'.repeat(2048 - 'https://x.test/'.length)}`, ])('accepts %s', (url) => { const branding = { ...sampleBranding(), siteUrl: url } expect(BrandingSchema.safeParse(branding).success).toBe(true) }) it.each([ ['a closing parenthesis', 'https://a.example/)'], ['an opening parenthesis', 'https://a.example/('], ['a PDF action injection', INJECTION_URL], ['a backslash', 'https://a.example/a\\b'], ['a space', 'https://a.example/a b'], ['a tab', 'https://a.example/a\tb'], ['a control character', 'https://a.example/a\u0001b'], ['DEL', 'https://a.example/a\u007fb'], ['an angle bracket', 'https://a.example/'], ['a double quote', 'https://a.example/"x'], ['a single quote', "https://a.example/'x"], ['a non-ASCII character', 'https://a.example/é'], ['no host', 'https://'], ['a scheme in upper case', 'HTTPS://a.example/'], ])('rejects %s in the site URL and in a credit URL', (_name, url) => { const site = { ...sampleBranding(), siteUrl: url } expect(BrandingSchema.safeParse(site).success).toBe(false) const base = sampleBranding() const credit = { ...base, partner: { ...base.partner!, url } } expect(BrandingSchema.safeParse(credit).success).toBe(false) }) }) describe('PdfLabelSetSchema', () => { it('lists 25 distinct keys and accepts a complete set in both locales', () => { expect(PDF_LABEL_KEYS).toHaveLength(25) expect(new Set(PDF_LABEL_KEYS).size).toBe(25) const set = { fr: sampleLabels(), en: sampleLabels() } expect(PdfLabelSetSchema.parse(set)).toEqual(set) }) it('rejects a missing key, an extra key, a long value, a missing locale', () => { const missing: Partial = sampleLabels() delete missing['pdf.empty'] expect( PdfLabelSetSchema.safeParse({ fr: missing, en: sampleLabels() }).success, ).toBe(false) expect( PdfLabelSetSchema.safeParse({ fr: { ...sampleLabels(), 'pdf.extra': 'x' }, en: sampleLabels(), }).success, ).toBe(false) expect( PdfLabelSetSchema.safeParse({ fr: { ...sampleLabels(), 'pdf.subject': 'x'.repeat(257) }, en: sampleLabels(), }).success, ).toBe(false) expect(PdfLabelSetSchema.safeParse({ fr: sampleLabels() }).success).toBe( false, ) expect( PdfLabelSetSchema.safeParse({ fr: sampleLabels(), en: sampleLabels(), de: sampleLabels(), }).success, ).toBe(false) }) })