import { describe, expect, it } from "vitest"; import { buildGoalCriteriaApiPayload, normalizeGoalCriteriaInput, TARGET_DURATION_MODE_LEGACY, TARGET_DURATION_MODE_WEEKS, } from "@/views/dashboards/departmentDashboard/modules/goals/functions/goalCriteriaPayload"; describe("goal criteria payload helpers", () => { it("omits target duration fields in legacy mode and serializes day boundaries as ISO date-time", () => { const payload = buildGoalCriteriaApiPayload( { label: "Quarter Goal", type: "REVENUE", target: 1000, start: "2026-03-01", end: "2026-03-31", departments: [5], }, { targetDurationMode: TARGET_DURATION_MODE_LEGACY, } ); expect(payload).not.toHaveProperty("target_duration"); expect(payload).not.toHaveProperty("target_duration_every"); expect(payload.start).toMatch(/^2026-03-01T00:00:00(?:Z|[+-]\d{2}:\d{2})$/); expect(payload.end).toMatch(/^2026-03-31T23:59:59(?:Z|[+-]\d{2}:\d{2})$/); }); it("includes cadence fields when duration mode requires them", () => { const payload = buildGoalCriteriaApiPayload( { label: "Bi-weekly Goal", type: "PRODUCT", target: 42, start: "2026-04-01", end: "2026-04-30", departments: [2], target_duration_every: 2, }, { targetDurationMode: TARGET_DURATION_MODE_WEEKS, } ); expect(payload.target_duration).toBe("WEEKS"); expect(payload.target_duration_every).toBe(2); }); it("normalizes legacy camelCase fields to canonical snake_case", () => { const normalized = normalizeGoalCriteriaInput( { targetDuration: "months", targetDurationEvery: "3", progressAlertFrequency: "weekly", progressAlertDestination: "email", progressAlertProgressType: "count_only", progressAlertStyle: "collective", progressAlertFormat: "Report: {percent}%", progressAlertWeekdays: ["monday", "friday", "invalid"], progressAlertTimeOfDay: "09:30+01:00", departmentDailyTargets: { 2: "14", invalid: "x" }, start: "2026-01-15T12:00:00+01:00", end: "2026-02-20T18:30:00+01:00", }, [8] ); expect(normalized.target_duration).toBe("MONTHS"); expect(normalized.target_duration_every).toBe(3); expect(normalized.progress_alert_frequency).toBe("WEEKLY"); expect(normalized.progress_alert_destination).toBe("EMAIL"); expect(normalized.progress_alert_progress_type).toBe("COUNT_ONLY"); expect(normalized.progress_alert_style).toBe("COLLECTIVE"); expect(normalized.progress_alert_format).toBe("Report: {percent}%"); expect(normalized.progress_alert_weekdays).toEqual(["MONDAY", "FRIDAY"]); expect(normalized.progress_alert_time_of_day).toBe("09:30+01:00"); expect(normalized.department_daily_targets).toEqual({ 2: 14 }); expect(normalized.start).toBe("2026-01-15"); expect(normalized.end).toBe("2026-02-20"); expect(normalized.departments).toEqual([8]); }); it("converts weekly department targets to daily values in API payload", () => { const payload = buildGoalCriteriaApiPayload( { label: "Dept Weekly Goal", type: "VISITS", target: 10, start: "2026-05-01", end: "2026-05-31", departments: [9], department_daily_targets: { 9: 21 }, }, { departmentTargetUnit: "weeks", targetDurationMode: TARGET_DURATION_MODE_LEGACY, } ); expect(payload.department_daily_targets["9"]).toBe(3); }); });