Files
app/src/__tests__/schema-caps.test.ts
BOHA e11765bf0e fix: resolve all 28 findings from the 2026-06-12 full audit (TDD-pinned)
Critical (data integrity):
- warehouse inventory confirm: throw (not return) inside $transaction so a
  failed deficit line rolls back the surplus corrective receipt — retries
  no longer accumulate phantom stock
- warehouse issue confirm: validate batches against the COMBINED quantity
  of all lines (duplicate FIFO-resolved lines drove batches negative)
- attendance delete: restore vacation_used/sick_used for the deleted day
  (in-transaction, clamped at 0)

High:
- auth refresh: terminated sessions (replaced_at only) get a plain 401 —
  the theft branch (family revocation) now fires only on replaced_by_hash
- POST /users strips role_id for non-admin callers (mirrors PUT guard)
- issued-order transition flushes unsaved edits via the full save payload
  when dirty; server contract (items+status in one PUT) pinned
- received-invoices list: usePaginatedQuery + pager (rows 26+ unreachable)
- received-invoice dates: nullableIsoDateString + NaN guard before NAS save
  (Czech-format dates corrupted month/year, orphaned NAS files)
- leave approval skips Czech public holidays and books each calendar year's
  hours against its own balance (mirrors createLeave)

Medium/Low (classes):
- 52 Zod caps aligned to DB column widths across 7 schemas (over-cap input
  500ed at Prisma instead of a Czech 400)
- FK pre-validation: projects update + warehouse receipts/issues return
  Czech 400s instead of P2003 500s
- invoice PDF degrades gracefully when the CNB rate is unavailable
  (recap omitted instead of 500 + lost NAS archival)
- date boundaries: local-day filters (warehouse lists/reports, audit-log),
  @db.Date coercion on invoice dates
- plan updateEntry re-checks the per-cell cap (self-excluding)
- {id} tiebreaks on customers/received-invoices/warehouse-items sorts;
  /items honors the client sort param
- htmlToPdf relaunches once when the shared browser died mid-render
- offer number release parses the year from the document number (cross-year
  finalize+delete left permanent sequence gaps)
- trips/vehicles km fields integer-coerced; AI budget regated to
  settings.company|settings.system; Settings System tab no longer clobbers
  Firma numbering patterns; draft invoices hide the dead PDF button;
  dashboard quick-trip invalidates ["vehicles"]; TOTP secret cap 64;
  audit-log + invoice month buckets day-shift fixes

Docs: corrected the stale "Chromium has no CSS margin-box footers" claim
(html-to-pdf.ts + CLAUDE.md — margin boxes render since Chrome 131); audit
report M3 withdrawn accordingly.

~65 new pinning tests; every finding reproduced RED against the real test
DB before its fix. Suite: 58 files / 634 tests green.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 23:00:19 +02:00

395 lines
10 KiB
TypeScript

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);
});
}
});