const ScheduleService = require("../../src/services/scheduleManagement"); describe("ScheduleService", () => { let scheduleService; beforeEach(() => { scheduleService = new ScheduleService(); }); test("should validate a valid schedule", () => { const validSchedule = { operationType: "update", scheduledTime: new Date(Date.now() + 86400000), recurrence: "daily", enabled: true, config: { targetTag: "sale" }, status: "pending", }; const result = scheduleService.validateSchedule(validSchedule); expect(result).toBeNull(); }); test("should return error for missing operation type", () => { const invalidSchedule = { scheduledTime: new Date(Date.now() + 86400000), }; const result = scheduleService.validateSchedule(invalidSchedule); expect(result).toBe("Operation type is required"); }); test("should return error for invalid operation type", () => { const invalidSchedule = { operationType: "invalid", scheduledTime: new Date(Date.now() + 86400000), }; const result = scheduleService.validateSchedule(invalidSchedule); expect(result).toBe('Operation type must be "update" or "rollback"'); }); test("should generate unique IDs", () => { const existingSchedules = [ { id: "schedule_123_abc" }, { id: "schedule_456_def" }, ]; const id1 = scheduleService._generateId(existingSchedules); const id2 = scheduleService._generateId(existingSchedules); expect(id1).toMatch(/^schedule_\d+_[a-z0-9]+$/); expect(id2).toMatch(/^schedule_\d+_[a-z0-9]+$/); expect(id1).not.toBe(id2); expect(existingSchedules.some((s) => s.id === id1)).toBe(false); expect(existingSchedules.some((s) => s.id === id2)).toBe(false); }); test("should calculate next execution for daily recurrence", () => { const scheduledTime = new Date("2024-12-01T10:00:00.000Z"); const nextExecution = scheduleService._calculateNextExecution( scheduledTime, "daily" ); expect(nextExecution).toBeInstanceOf(Date); expect(nextExecution.getDate()).toBe(scheduledTime.getDate() + 1); }); test("should return null for once recurrence", () => { const scheduledTime = new Date("2024-12-01T10:00:00.000Z"); const nextExecution = scheduleService._calculateNextExecution( scheduledTime, "once" ); expect(nextExecution).toBeNull(); }); });