Files
pn-new-crm/tests/unit/document-sends-validators.test.ts
Matt 221ae5784e chore(autonomous-session): consolidate uncommitted work from prior session
Bundles the prior autonomous-session output that was sitting unstaged:

- Em-dash sweep across src/ + tests/ (en-dash/em-dash to hyphen, ~2280 instances)
- country-flag-icons rollout (CountryFlag component, replaces emoji glyphs that
  never rendered on Windows; lazy-loads the 3x2 SVG index as a single chunk
  after the per-subpath dynamic-import approach silently failed in webpack)
- Admin IA Phase 1+2: 7-domain regroup, 41 to 38 pages, /admin/berths index,
  redirects (ocr to ai, reports to dashboard, invitations to users),
  docs/admin-ia-proposal.md
- Per-template email tester (registry + endpoint + UI on Email admin page)
- Cancel-document mode picker (delete-from-Documenso vs keep-for-audit)
- Dashboard PDF report: 25 widgets, SVG charts, date-range picker, 11 resolvers
- Customize-widgets per-region sortables at xl+ (charts/rails/feed); single
  flat sortable below xl when the layout stacks; per-viewport saved orders
- Audit doc updates capturing each shipped item
- Lint fixes: react-compiler immutability in DonutChart (reduce instead of
  let-reassign), set-state-in-effect disables in CountryFlag and
  UploadForSigning preview-bytes effect, unused 'confirm' destructures in
  interest contract + reservation tabs, unescaped apostrophe in test-template
  card copy
2026-05-23 00:52:59 +02:00

143 lines
3.9 KiB
TypeScript

/**
* Phase 7 - validator-level guarantees for the send-out flow.
*
* §14.7 mitigation: recipient typo (the strict email regex is the first
* line of defense; the confirmation modal is the second).
*/
import { describe, expect, it } from 'vitest';
import {
sendBerthPdfSchema,
sendBrochureSchema,
previewBodySchema,
listSendsQuerySchema,
} from '@/lib/validators/document-sends';
import { createBrochureSchema, registerBrochureVersionSchema } from '@/lib/validators/brochures';
describe('sendBerthPdfSchema', () => {
it('requires either clientId or email', () => {
const r = sendBerthPdfSchema.safeParse({
berthId: 'b1',
recipient: { interestId: 'i1' },
});
expect(r.success).toBe(false);
});
it('accepts clientId-only recipient', () => {
const r = sendBerthPdfSchema.safeParse({
berthId: 'b1',
recipient: { clientId: 'c1' },
});
expect(r.success).toBe(true);
});
it('rejects an obviously bad email', () => {
const r = sendBerthPdfSchema.safeParse({
berthId: 'b1',
recipient: { email: 'not an email' },
});
expect(r.success).toBe(false);
});
it('caps custom body length at 50KB', () => {
const r = sendBerthPdfSchema.safeParse({
berthId: 'b1',
recipient: { clientId: 'c1' },
customBodyMarkdown: 'x'.repeat(60_000),
});
expect(r.success).toBe(false);
});
});
describe('sendBrochureSchema', () => {
it('allows brochureId to be omitted (defaults at service level)', () => {
const r = sendBrochureSchema.safeParse({
recipient: { clientId: 'c1' },
});
expect(r.success).toBe(true);
});
});
describe('previewBodySchema', () => {
it('requires documentKind', () => {
const r = previewBodySchema.safeParse({ recipient: { clientId: 'c1' } });
expect(r.success).toBe(false);
});
it('accepts a minimal preview payload', () => {
const r = previewBodySchema.safeParse({
documentKind: 'berth_pdf',
recipient: { clientId: 'c1' },
berthId: 'b1',
});
expect(r.success).toBe(true);
});
});
describe('listSendsQuerySchema', () => {
it('coerces limit from string', () => {
const r = listSendsQuerySchema.safeParse({ limit: '50' });
expect(r.success).toBe(true);
if (r.success) expect(r.data.limit).toBe(50);
});
it('rejects out-of-range limit', () => {
const r = listSendsQuerySchema.safeParse({ limit: '99999' });
expect(r.success).toBe(false);
});
});
describe('createBrochureSchema', () => {
it('requires a non-empty label', () => {
const r = createBrochureSchema.safeParse({ label: ' ' });
expect(r.success).toBe(false);
});
it('caps label at 120 chars', () => {
const r = createBrochureSchema.safeParse({ label: 'a'.repeat(200) });
expect(r.success).toBe(false);
});
});
describe('registerBrochureVersionSchema', () => {
it('rejects path-traversal in storageKey', () => {
const r = registerBrochureVersionSchema.safeParse({
storageKey: '../etc/passwd',
fileName: 'b.pdf',
fileSizeBytes: 100,
contentSha256: 'a'.repeat(64),
});
expect(r.success).toBe(false);
});
it('rejects malformed sha256', () => {
const r = registerBrochureVersionSchema.safeParse({
storageKey: 'port/brochures/abc/x.pdf',
fileName: 'b.pdf',
fileSizeBytes: 100,
contentSha256: 'NOTHEX',
});
expect(r.success).toBe(false);
});
it('rejects upload over 100MB', () => {
const r = registerBrochureVersionSchema.safeParse({
storageKey: 'port/brochures/abc/x.pdf',
fileName: 'b.pdf',
fileSizeBytes: 200 * 1024 * 1024,
contentSha256: 'a'.repeat(64),
});
expect(r.success).toBe(false);
});
it('accepts a valid payload', () => {
const r = registerBrochureVersionSchema.safeParse({
storageKey: 'port/brochures/abc/x.pdf',
fileName: 'b.pdf',
fileSizeBytes: 1024,
contentSha256: 'a'.repeat(64),
});
expect(r.success).toBe(true);
});
});