import { describe, it, expect, afterEach } from "vitest"; import prisma from "../config/database"; import { createOrder, listOrders } from "../services/orders.service"; const createdOrderIds: number[] = []; const createdCustomerIds: number[] = []; afterEach(async () => { for (const id of createdOrderIds) await prisma.orders.deleteMany({ where: { id } }); for (const id of createdCustomerIds) await prisma.customers.deleteMany({ where: { id } }); createdOrderIds.length = 0; createdCustomerIds.length = 0; await prisma.number_sequences.deleteMany({ where: { type: "shared" } }); }); const baseOrder = { status: "prijata" as const, currency: "CZK", language: "cs", vat_rate: 21, apply_vat: true, exchange_rate: 1, }; function failResult(res: unknown): never { throw new Error(`expected a success result, got ${JSON.stringify(res)}`); } async function makeCustomer() { const c = await prisma.customers.create({ data: { name: "Odběratel a.s." }, }); createdCustomerIds.push(c.id); return c; } const baseListParams = { page: 1, limit: 50, skip: 0, sort: "id", order: "desc" as const, }; describe("listOrders search filter", () => { it("returns the order when search matches its order_number", async () => { const customer = await makeCustomer(); const res = await createOrder({ ...baseOrder, customer_id: customer.id, create_project: false, }); if (!("id" in res)) failResult(res); createdOrderIds.push(res.id!); const list = await listOrders({ ...baseListParams, search: res.order_number!, }); expect(list.data.map((o) => o.id)).toContain(res.id); }); it("excludes the order when search matches nothing", async () => { const customer = await makeCustomer(); const res = await createOrder({ ...baseOrder, customer_id: customer.id, create_project: false, }); if (!("id" in res)) failResult(res); createdOrderIds.push(res.id!); const list = await listOrders({ ...baseListParams, search: "ZZZ-NO-SUCH-ORDER-ZZZ", }); expect(list.data.map((o) => o.id)).not.toContain(res.id); }); }); describe("listOrders month filter", () => { it("returns only orders whose created_at is in the given month", async () => { const customer = await makeCustomer(); const res = await createOrder({ ...baseOrder, customer_id: customer.id, create_project: false, }); if (!("id" in res)) failResult(res); createdOrderIds.push(res.id!); // Pin created_at to a known month so the range filter is deterministic. await prisma.orders.update({ where: { id: res.id }, data: { created_at: new Date(2026, 2, 15, 12, 0, 0) }, }); const inMonth = await listOrders({ ...baseListParams, month: 3, year: 2026, }); expect(inMonth.data.map((o) => o.id)).toContain(res.id); const otherMonth = await listOrders({ ...baseListParams, month: 4, year: 2026, }); expect(otherMonth.data.map((o) => o.id)).not.toContain(res.id); }); });