Files
faunosteo/packages/domain/tests/archive.branding-labels.test.ts
T

138 lines
3.7 KiB
TypeScript

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: '<svg xmlns="http://www.w3.org/2000/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/>' },
}),
],
[
'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,
)
})
})
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<PdfLabels> = 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)
})
})