import { describe, expect, it } from "vitest"; import { getInvitationReferenceLabel } from "../components/client/invitationReference"; import { buildDeliveryConfirmationPayload, buildSelectedSlotFromInvitation, getClientDeliveryHeroDescription, groupSlotsFromInvitation, } from "./ClientDeliveryPage"; describe("ClientDeliveryPage helpers", () => { it("maps invitation slots in YYYY-MM-DD format to UI slots", () => { expect( groupSlotsFromInvitation({ availableSlots: [ "2026-04-15, До обеда", "2026-04-15, После обеда", ], }, new Date("2026-04-14T09:00:00Z")), ).toEqual([ { id: "slot-0-2026-04-15, До обеда", date: "2026-04-15", time: "До обеда", }, { id: "slot-1-2026-04-15, После обеда", date: "2026-04-15", time: "После обеда", }, ]); }); it("builds confirmation payload from the selected slot date and time", () => { expect( buildDeliveryConfirmationPayload({ slot: { id: "slot-1", date: "2026-04-16", time: "После обеда", }, invitation: { deliveryDate: "2026-04-15", }, searchDate: "2026-04-14", }), ).toEqual({ deliveryType: "delivery", deliveryDate: "2026-04-16", deliveryTime: "После обеда", }); }); it("keeps the explicit slot dates when invitation already has a delivery date", () => { expect( groupSlotsFromInvitation({ deliveryDate: "2026-04-14", deliveryTime: "До обеда", availableSlots: [ "2026-04-14, До обеда", "2026-04-14, После обеда", "2026-04-15, До обеда", "2026-04-15, После обеда", ], }, new Date("2026-04-13T09:00:00Z")), ).toEqual([ { id: "slot-0-2026-04-14, До обеда", date: "2026-04-14", time: "До обеда", }, { id: "slot-1-2026-04-14, После обеда", date: "2026-04-14", time: "После обеда", }, { id: "slot-2-2026-04-15, До обеда", date: "2026-04-15", time: "До обеда", }, { id: "slot-3-2026-04-15, После обеда", date: "2026-04-15", time: "После обеда", }, ]); }); it("builds a selected slot from invitation data", () => { expect( buildSelectedSlotFromInvitation( { deliveryDate: "2026-04-15", deliveryTime: "После обеда", }, [ { id: "slot-1", date: "2026-04-15", time: "После обеда", }, ], ), ).toEqual({ id: "slot-1", date: "2026-04-15", time: "После обеда", }); }); it("ignores malformed slots and keeps object-based slots usable", () => { expect( groupSlotsFromInvitation({ deliveryDate: "2026-04-15", availableSlots: [ null, { id: "slot-object", date: "2026-04-15", time: "Первая половина дня" }, 42, ], }, new Date("2026-04-14T09:00:00Z")), ).toEqual([ { id: "slot-object", date: "2026-04-15", time: "Первая половина дня", }, ]); }); it("keeps only tomorrow and the day after tomorrow", () => { expect( groupSlotsFromInvitation( { availableSlots: [ "2026-04-15, Первая половина дня", "2026-04-15, Вторая половина дня", "2026-04-16, Первая половина дня", "2026-04-16, Вторая половина дня", "2026-04-17, Первая половина дня", "2026-04-17, Вторая половина дня", ], }, new Date("2026-04-14T09:00:00Z"), ), ).toEqual([ { id: "slot-0-2026-04-15, Первая половина дня", date: "2026-04-15", time: "Первая половина дня", }, { id: "slot-1-2026-04-15, Вторая половина дня", date: "2026-04-15", time: "Вторая половина дня", }, { id: "slot-2-2026-04-16, Первая половина дня", date: "2026-04-16", time: "Первая половина дня", }, { id: "slot-3-2026-04-16, Вторая половина дня", date: "2026-04-16", time: "Вторая половина дня", }, ]); }); it("prefers the invoice number from order items over the customer name", () => { expect( getInvitationReferenceLabel({ orderNumber: "9787464846|04.05.26", customerName: "Конаков Сергей Алексеевич (ДАРТС)", orderItems: [ { name: "СФ Т\\ЕА-28687", quantity: "" }, { name: "СФ Т\\ЕА-28700", quantity: "" }, ], }), ).toBe("Счета: СФ Т\\ЕА-28687, СФ Т\\ЕА-28700"); }); it("hides the hero helper text after the client saves the choice", () => { expect(getClientDeliveryHeroDescription(true, true)).toBe(""); expect(getClientDeliveryHeroDescription(true, false)).toBe( "Вам предложены варианты доставки. Выберите удобную дату и время.", ); expect(getClientDeliveryHeroDescription(false, false)).toBe( "По этому заказу согласование доставки завершено или передано логисту.", ); }); it("skips Sunday when generating fallback delivery slots from Friday", () => { // 2026-04-17 is Friday → next workday = Saturday 18, then Monday 20 (skip Sunday 19) const slots = groupSlotsFromInvitation( { availableSlots: [], deliveryDate: null }, new Date("2026-04-17T09:00:00Z"), ); const dates = [...new Set(slots.map((s) => s.date))]; expect(dates).not.toContain("2026-04-19"); // Sunday expect(dates).toContain("2026-04-18"); // Saturday expect(dates).toContain("2026-04-20"); // Monday }); it("skips Sunday when generating fallback delivery slots from Saturday", () => { // 2026-04-18 is Saturday → next workday = Monday 20, then Tuesday 21 const slots = groupSlotsFromInvitation( { availableSlots: [], deliveryDate: null }, new Date("2026-04-18T09:00:00Z"), ); const dates = [...new Set(slots.map((s) => s.date))]; expect(dates).not.toContain("2026-04-19"); // Sunday expect(dates).toContain("2026-04-20"); // Monday expect(dates).toContain("2026-04-21"); // Tuesday }); });