import { describe, it, expect } from "vitest"; import type { z } from "zod"; import { CreateOrderSchema, UpdateOrderSchema, CreateOrderFromQuotationSchema, } from "../schemas/orders.schema"; import { CreateInvoiceSchema, UpdateInvoiceSchema, } from "../schemas/invoices.schema"; import { CreateTripSchema, UpdateTripSchema } from "../schemas/trips.schema"; import { TotpEnableSchema } from "../schemas/auth.schema"; import { CreateReceivedInvoiceSchema, UpdateReceivedInvoiceSchema, } from "../schemas/received-invoices.schema"; import { CreateSupplierSchema, UpdateSupplierSchema, } from "../schemas/warehouse.schema"; import { CreateCustomerSchema, UpdateCustomerSchema, } from "../schemas/customers.schema"; /** * Pinning tests for the Zod-cap-vs-DB-column class (audit H2, M2, M4, M11, * L1, L8d, L8e + adjacent invoice item/bank caps): every form-facing string * cap must fit its DB column width, or an over-width value passes Zod and * dies at Prisma as a 500 (P2000) instead of a clean Czech 400. * * Each case asserts BOTH directions: width+1 chars must FAIL the schema, and * exactly width chars must PASS (guards against over-tightening — stored * values can never exceed the column width, so re-submitted edit forms stay * valid). */ interface CapCase { name: string; schema: z.ZodTypeAny; /** DB column width from prisma/schema.prisma */ width: number; build: (value: string) => unknown; } const tripBase = { vehicle_id: 1, trip_date: "2098-03-02", start_km: 0, end_km: 1, route_from: "A", route_to: "B", }; const receivedBase = { supplier_name: "X", month: 6, year: 2026 }; const CASES: CapCase[] = [ // ── orders.schema.ts vs order_items / orders ── { name: "CreateOrderSchema items.description", schema: CreateOrderSchema, width: 500, build: (v) => ({ items: [{ description: v }] }), }, { name: "CreateOrderSchema items.unit", schema: CreateOrderSchema, width: 20, build: (v) => ({ items: [{ unit: v }] }), }, { name: "CreateOrderSchema order_number", schema: CreateOrderSchema, width: 50, build: (v) => ({ order_number: v }), }, { name: "CreateOrderSchema customer_order_number", schema: CreateOrderSchema, width: 100, build: (v) => ({ customer_order_number: v }), }, { name: "CreateOrderSchema currency", schema: CreateOrderSchema, width: 10, build: (v) => ({ currency: v }), }, { name: "CreateOrderSchema language", schema: CreateOrderSchema, width: 5, build: (v) => ({ language: v }), }, { name: "UpdateOrderSchema customer_order_number", schema: UpdateOrderSchema, width: 100, build: (v) => ({ customer_order_number: v }), }, { name: "UpdateOrderSchema currency", schema: UpdateOrderSchema, width: 10, build: (v) => ({ currency: v }), }, { name: "UpdateOrderSchema language", schema: UpdateOrderSchema, width: 5, build: (v) => ({ language: v }), }, { name: "CreateOrderFromQuotationSchema customerOrderNumber", schema: CreateOrderFromQuotationSchema, width: 100, build: (v) => ({ quotationId: 1, customerOrderNumber: v }), }, // ── invoices.schema.ts vs invoice_items / invoices ── { name: "CreateInvoiceSchema items.description", schema: CreateInvoiceSchema, width: 500, build: (v) => ({ items: [{ description: v }] }), }, { name: "CreateInvoiceSchema items.unit", schema: CreateInvoiceSchema, width: 20, build: (v) => ({ items: [{ unit: v }] }), }, { name: "CreateInvoiceSchema invoice_number", schema: CreateInvoiceSchema, width: 50, build: (v) => ({ invoice_number: v }), }, { name: "CreateInvoiceSchema currency", schema: CreateInvoiceSchema, width: 10, build: (v) => ({ currency: v }), }, { name: "CreateInvoiceSchema language", schema: CreateInvoiceSchema, width: 5, build: (v) => ({ language: v }), }, { name: "CreateInvoiceSchema payment_method", schema: CreateInvoiceSchema, width: 50, build: (v) => ({ payment_method: v }), }, { name: "CreateInvoiceSchema constant_symbol", schema: CreateInvoiceSchema, width: 20, build: (v) => ({ constant_symbol: v }), }, { name: "CreateInvoiceSchema bank_swift", schema: CreateInvoiceSchema, width: 20, build: (v) => ({ bank_swift: v }), }, { name: "CreateInvoiceSchema bank_iban", schema: CreateInvoiceSchema, width: 50, build: (v) => ({ bank_iban: v }), }, { name: "CreateInvoiceSchema bank_account", schema: CreateInvoiceSchema, width: 50, build: (v) => ({ bank_account: v }), }, { name: "CreateInvoiceSchema billing_text", schema: CreateInvoiceSchema, width: 500, build: (v) => ({ billing_text: v }), }, { name: "UpdateInvoiceSchema currency", schema: UpdateInvoiceSchema, width: 10, build: (v) => ({ currency: v }), }, { name: "UpdateInvoiceSchema language", schema: UpdateInvoiceSchema, width: 5, build: (v) => ({ language: v }), }, { name: "UpdateInvoiceSchema payment_method", schema: UpdateInvoiceSchema, width: 50, build: (v) => ({ payment_method: v }), }, { name: "UpdateInvoiceSchema constant_symbol", schema: UpdateInvoiceSchema, width: 20, build: (v) => ({ constant_symbol: v }), }, { name: "UpdateInvoiceSchema bank_swift", schema: UpdateInvoiceSchema, width: 20, build: (v) => ({ bank_swift: v }), }, { name: "UpdateInvoiceSchema bank_iban", schema: UpdateInvoiceSchema, width: 50, build: (v) => ({ bank_iban: v }), }, { name: "UpdateInvoiceSchema bank_account", schema: UpdateInvoiceSchema, width: 50, build: (v) => ({ bank_account: v }), }, { name: "UpdateInvoiceSchema billing_text", schema: UpdateInvoiceSchema, width: 500, build: (v) => ({ billing_text: v }), }, // ── trips.schema.ts vs trips ── { name: "CreateTripSchema route_from", schema: CreateTripSchema, width: 100, build: (v) => ({ ...tripBase, route_from: v }), }, { name: "CreateTripSchema route_to", schema: CreateTripSchema, width: 100, build: (v) => ({ ...tripBase, route_to: v }), }, { name: "UpdateTripSchema route_from", schema: UpdateTripSchema, width: 100, build: (v) => ({ route_from: v }), }, { name: "UpdateTripSchema route_to", schema: UpdateTripSchema, width: 100, build: (v) => ({ route_to: v }), }, // ── auth.schema.ts vs users.totp_secret ── // The stored value is AES-256-GCM ciphertext (base64(IV+ct+tag)) of the // plaintext secret in a VarChar(255) column: a plaintext over ~164 chars // overflows after encryption. Real TOTP secrets are 16-32 chars; cap 64. { name: "TotpEnableSchema secret", schema: TotpEnableSchema, width: 64, build: (v) => ({ secret: v, code: "123456" }), }, // ── received-invoices.schema.ts vs received_invoices ── { name: "CreateReceivedInvoiceSchema description", schema: CreateReceivedInvoiceSchema, width: 500, build: (v) => ({ ...receivedBase, description: v }), }, { name: "CreateReceivedInvoiceSchema invoice_number", schema: CreateReceivedInvoiceSchema, width: 100, build: (v) => ({ ...receivedBase, invoice_number: v }), }, { name: "CreateReceivedInvoiceSchema currency", schema: CreateReceivedInvoiceSchema, width: 3, build: (v) => ({ ...receivedBase, currency: v }), }, { name: "UpdateReceivedInvoiceSchema description", schema: UpdateReceivedInvoiceSchema, width: 500, build: (v) => ({ description: v }), }, { name: "UpdateReceivedInvoiceSchema invoice_number", schema: UpdateReceivedInvoiceSchema, width: 100, build: (v) => ({ invoice_number: v }), }, { name: "UpdateReceivedInvoiceSchema currency", schema: UpdateReceivedInvoiceSchema, width: 3, build: (v) => ({ currency: v }), }, // ── warehouse.schema.ts (suppliers) vs sklad_suppliers ── { name: "CreateSupplierSchema ico", schema: CreateSupplierSchema, width: 20, build: (v) => ({ name: "X", ico: v }), }, { name: "CreateSupplierSchema dic", schema: CreateSupplierSchema, width: 20, build: (v) => ({ name: "X", dic: v }), }, { name: "UpdateSupplierSchema ico", schema: UpdateSupplierSchema, width: 20, build: (v) => ({ ico: v }), }, { name: "UpdateSupplierSchema dic", schema: UpdateSupplierSchema, width: 20, build: (v) => ({ dic: v }), }, // ── customers.schema.ts vs customers ── { name: "CreateCustomerSchema company_id", schema: CreateCustomerSchema, width: 50, build: (v) => ({ name: "X", company_id: v }), }, { name: "CreateCustomerSchema vat_id", schema: CreateCustomerSchema, width: 50, build: (v) => ({ name: "X", vat_id: v }), }, { name: "CreateCustomerSchema postal_code", schema: CreateCustomerSchema, width: 20, build: (v) => ({ name: "X", postal_code: v }), }, { name: "CreateCustomerSchema country", schema: CreateCustomerSchema, width: 100, build: (v) => ({ name: "X", country: v }), }, { name: "UpdateCustomerSchema company_id", schema: UpdateCustomerSchema, width: 50, build: (v) => ({ company_id: v }), }, { name: "UpdateCustomerSchema vat_id", schema: UpdateCustomerSchema, width: 50, build: (v) => ({ vat_id: v }), }, { name: "UpdateCustomerSchema postal_code", schema: UpdateCustomerSchema, width: 20, build: (v) => ({ postal_code: v }), }, { name: "UpdateCustomerSchema country", schema: UpdateCustomerSchema, width: 100, build: (v) => ({ country: v }), }, ]; describe("Zod caps fit DB column widths (audit H2/M2/M4/M11/L1/L8d/L8e + adjacent)", () => { for (const c of CASES) { it(`${c.name} rejects ${c.width + 1} chars`, () => { const result = c.schema.safeParse(c.build("x".repeat(c.width + 1))); expect(result.success).toBe(false); }); it(`${c.name} accepts ${c.width} chars`, () => { const result = c.schema.safeParse(c.build("x".repeat(c.width))); expect(result.success).toBe(true); }); } });