/** * Unit tests for getDutyMarkerRows and buildDutyItemTimePrefix logic. * Covers: sorting order preservation, idx=0 with total>1 and startSameDay. */ import { describe, it, expect, beforeAll } from "vitest"; import { getDutyMarkerRows } from "./hints.js"; const FROM = "from"; const TO = "until"; const SEP = "\u00a0"; describe("getDutyMarkerRows", () => { beforeAll(() => { document.body.innerHTML = '
'; }); it("preserves input order (caller must sort by start_at before passing)", () => { const hintDay = "2025-02-25"; const duties = [ { full_name: "Иванов", start_at: "2025-02-25T14:00:00", end_at: "2025-02-25T18:00:00", }, { full_name: "Петров", start_at: "2025-02-25T09:00:00", end_at: "2025-02-25T14:00:00", }, ]; const rows = getDutyMarkerRows(duties, hintDay, SEP, FROM, TO); expect(rows).toHaveLength(2); expect(rows[0].fullName).toBe("Иванов"); expect(rows[1].fullName).toBe("Петров"); }); it("first of multiple with startSameDay shows full range (from HH:MM to HH:MM)", () => { const hintDay = "2025-02-25"; const duties = [ { full_name: "Иванов", start_at: "2025-02-25T09:00:00", end_at: "2025-02-25T14:00:00", }, { full_name: "Петров", start_at: "2025-02-25T14:00:00", end_at: "2025-02-25T18:00:00", }, ].sort((a, b) => new Date(a.start_at) - new Date(b.start_at)); const rows = getDutyMarkerRows(duties, hintDay, SEP, FROM, TO); expect(rows).toHaveLength(2); expect(rows[0].fullName).toBe("Иванов"); expect(rows[0].timePrefix).toContain("09:00"); expect(rows[0].timePrefix).toContain("14:00"); expect(rows[0].timePrefix).toContain(FROM); expect(rows[0].timePrefix).toContain(TO); }); it("first of multiple continuation from previous day shows only end time", () => { const hintDay = "2025-02-25"; const duties = [ { full_name: "Иванов", start_at: "2025-02-24T22:00:00", end_at: "2025-02-25T06:00:00", }, { full_name: "Петров", start_at: "2025-02-25T09:00:00", end_at: "2025-02-25T14:00:00", }, ].sort((a, b) => new Date(a.start_at) - new Date(b.start_at)); const rows = getDutyMarkerRows(duties, hintDay, SEP, FROM, TO); expect(rows).toHaveLength(2); expect(rows[0].fullName).toBe("Иванов"); expect(rows[0].timePrefix).not.toContain(FROM); expect(rows[0].timePrefix).toContain(TO); expect(rows[0].timePrefix).toContain("06:00"); }); it("multiple duties in one day — correct order when input is pre-sorted", () => { const hintDay = "2025-02-25"; const duties = [ { full_name: "A", start_at: "2025-02-25T09:00:00", end_at: "2025-02-25T12:00:00" }, { full_name: "B", start_at: "2025-02-25T12:00:00", end_at: "2025-02-25T15:00:00" }, { full_name: "C", start_at: "2025-02-25T15:00:00", end_at: "2025-02-25T18:00:00" }, ].sort((a, b) => new Date(a.start_at) - new Date(b.start_at)); const rows = getDutyMarkerRows(duties, hintDay, SEP, FROM, TO); expect(rows.map((r) => r.fullName)).toEqual(["A", "B", "C"]); expect(rows[0].timePrefix).toContain("09:00"); expect(rows[1].timePrefix).toContain("12:00"); expect(rows[2].timePrefix).toContain("15:00"); }); });