|
| 1 | +import { ScheduledTask } from "node-cron"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | +import { ScheduledJobRegistry } from "./cron-registry"; |
| 4 | + |
| 5 | +describe("ScheduledJobRegistry", () => { |
| 6 | + let mockTask: ScheduledTask; |
| 7 | + let stopSpy: ReturnType<typeof vi.fn>; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + // Create a mock ScheduledTask |
| 11 | + stopSpy = vi.fn().mockResolvedValue(undefined); |
| 12 | + mockTask = { |
| 13 | + id: "test-task-1", |
| 14 | + stop: stopSpy, |
| 15 | + getStatus: vi.fn().mockReturnValue("scheduled"), |
| 16 | + } as unknown as ScheduledTask; |
| 17 | + }); |
| 18 | + |
| 19 | + afterEach(async () => { |
| 20 | + // Clean up after each test |
| 21 | + await ScheduledJobRegistry.stopAll(); |
| 22 | + vi.clearAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + describe("register", () => { |
| 26 | + it("should register a scheduled task and return the task ID", () => { |
| 27 | + const taskId = ScheduledJobRegistry.register(mockTask); |
| 28 | + |
| 29 | + expect(taskId).toBe("test-task-1"); |
| 30 | + expect(ScheduledJobRegistry.hasTask("test-task-1")).toBe(true); |
| 31 | + expect(ScheduledJobRegistry.hasTask("test-task-2")).toBe(false); |
| 32 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(1); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should handle multiple task registrations", () => { |
| 36 | + const mockTask2 = { |
| 37 | + id: "test-task-2", |
| 38 | + stop: vi.fn().mockResolvedValue(undefined), |
| 39 | + getStatus: vi.fn().mockReturnValue("scheduled"), |
| 40 | + } as unknown as ScheduledTask; |
| 41 | + |
| 42 | + const taskId1 = ScheduledJobRegistry.register(mockTask); |
| 43 | + const taskId2 = ScheduledJobRegistry.register(mockTask2); |
| 44 | + |
| 45 | + expect(taskId1).toBe("test-task-1"); |
| 46 | + expect(taskId2).toBe("test-task-2"); |
| 47 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(2); |
| 48 | + expect(ScheduledJobRegistry.hasTask("test-task-1")).toBe(true); |
| 49 | + expect(ScheduledJobRegistry.hasTask("test-task-2")).toBe(true); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should overwrite task with same ID", () => { |
| 53 | + const mockTask2 = { |
| 54 | + id: "test-task-1", // Same ID as first task |
| 55 | + stop: vi.fn().mockResolvedValue(undefined), |
| 56 | + getStatus: vi.fn().mockReturnValue("scheduled"), |
| 57 | + } as unknown as ScheduledTask; |
| 58 | + |
| 59 | + ScheduledJobRegistry.register(mockTask); |
| 60 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(1); |
| 61 | + |
| 62 | + ScheduledJobRegistry.register(mockTask2); |
| 63 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(1); // Still 1, task was overwritten |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe("stop", () => { |
| 68 | + it("should stop and unregister a specific task", async () => { |
| 69 | + ScheduledJobRegistry.register(mockTask); |
| 70 | + expect(ScheduledJobRegistry.hasTask("test-task-1")).toBe(true); |
| 71 | + |
| 72 | + const result = await ScheduledJobRegistry.stop("test-task-1"); |
| 73 | + |
| 74 | + expect(result).toBe(true); |
| 75 | + expect(stopSpy).toHaveBeenCalledOnce(); |
| 76 | + expect(ScheduledJobRegistry.hasTask("test-task-1")).toBe(false); |
| 77 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(0); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should return false when trying to stop a non-existent task", async () => { |
| 81 | + const result = await ScheduledJobRegistry.stop("non-existent-task"); |
| 82 | + expect(result).toBe(false); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should handle task stop errors gracefully", async () => { |
| 86 | + const errorStopSpy = vi.fn().mockRejectedValue(new Error("Stop failed")); |
| 87 | + const errorTask = { |
| 88 | + id: "error-task", |
| 89 | + stop: errorStopSpy, |
| 90 | + getStatus: vi.fn().mockReturnValue("scheduled"), |
| 91 | + } as unknown as ScheduledTask; |
| 92 | + |
| 93 | + ScheduledJobRegistry.register(errorTask); |
| 94 | + expect(ScheduledJobRegistry.hasTask("error-task")).toBe(true); |
| 95 | + |
| 96 | + // The stop method should still throw the error |
| 97 | + await ScheduledJobRegistry.stop("error-task"); |
| 98 | + |
| 99 | + // But the task should still be removed from registry even if stop fails |
| 100 | + expect(ScheduledJobRegistry.hasTask("error-task")).toBe(false); |
| 101 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(0); |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe("stopAll", () => { |
| 106 | + it("should stop all registered tasks and clear the registry", async () => { |
| 107 | + const stopSpy1 = vi.fn().mockResolvedValue(undefined); |
| 108 | + const stopSpy2 = vi.fn().mockResolvedValue(undefined); |
| 109 | + |
| 110 | + const mockTask1 = { |
| 111 | + id: "task-1", |
| 112 | + stop: stopSpy1, |
| 113 | + getStatus: vi.fn().mockReturnValue("scheduled"), |
| 114 | + } as unknown as ScheduledTask; |
| 115 | + |
| 116 | + const mockTask2 = { |
| 117 | + id: "task-2", |
| 118 | + stop: stopSpy2, |
| 119 | + getStatus: vi.fn().mockReturnValue("scheduled"), |
| 120 | + } as unknown as ScheduledTask; |
| 121 | + |
| 122 | + ScheduledJobRegistry.register(mockTask1); |
| 123 | + ScheduledJobRegistry.register(mockTask2); |
| 124 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(2); |
| 125 | + |
| 126 | + await ScheduledJobRegistry.stopAll(); |
| 127 | + |
| 128 | + expect(stopSpy1).toHaveBeenCalledOnce(); |
| 129 | + expect(stopSpy2).toHaveBeenCalledOnce(); |
| 130 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(0); |
| 131 | + }); |
| 132 | + |
| 133 | + it("should handle empty registry gracefully", async () => { |
| 134 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(0); |
| 135 | + await expect(ScheduledJobRegistry.stopAll()).resolves.not.toThrow(); |
| 136 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(0); |
| 137 | + }); |
| 138 | + |
| 139 | + it("should continue stopping other tasks even if one fails", async () => { |
| 140 | + const stopSpy1 = vi.fn().mockResolvedValue(undefined); |
| 141 | + const stopSpy2 = vi.fn().mockResolvedValue(undefined); |
| 142 | + const errorStopSpy = vi.fn().mockRejectedValue(new Error("Stop failed")); |
| 143 | + |
| 144 | + const mockTask1 = { |
| 145 | + id: "task-1", |
| 146 | + stop: stopSpy1, |
| 147 | + getStatus: vi.fn().mockReturnValue("scheduled"), |
| 148 | + } as unknown as ScheduledTask; |
| 149 | + |
| 150 | + const errorTask = { |
| 151 | + id: "error-task", |
| 152 | + stop: errorStopSpy, |
| 153 | + getStatus: vi.fn().mockReturnValue("scheduled"), |
| 154 | + } as unknown as ScheduledTask; |
| 155 | + |
| 156 | + const mockTask2 = { |
| 157 | + id: "task-2", |
| 158 | + stop: stopSpy2, |
| 159 | + getStatus: vi.fn().mockReturnValue("scheduled"), |
| 160 | + } as unknown as ScheduledTask; |
| 161 | + |
| 162 | + ScheduledJobRegistry.register(mockTask1); |
| 163 | + ScheduledJobRegistry.register(errorTask); |
| 164 | + ScheduledJobRegistry.register(mockTask2); |
| 165 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(3); |
| 166 | + |
| 167 | + await ScheduledJobRegistry.stopAll(); |
| 168 | + |
| 169 | + expect(stopSpy1).toHaveBeenCalledOnce(); |
| 170 | + expect(errorStopSpy).toHaveBeenCalledOnce(); |
| 171 | + expect(stopSpy2).toHaveBeenCalledOnce(); |
| 172 | + |
| 173 | + // Registry should be cleared even with errors |
| 174 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(0); |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + describe("registry state management", () => { |
| 179 | + it("should properly manage registry state across multiple operations", async () => { |
| 180 | + // Start with empty registry |
| 181 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(0); |
| 182 | + |
| 183 | + // Register multiple tasks |
| 184 | + ScheduledJobRegistry.register(mockTask); |
| 185 | + const mockTask2 = { |
| 186 | + id: "task-2", |
| 187 | + stop: vi.fn().mockResolvedValue(undefined), |
| 188 | + getStatus: vi.fn().mockReturnValue("scheduled"), |
| 189 | + } as unknown as ScheduledTask; |
| 190 | + ScheduledJobRegistry.register(mockTask2); |
| 191 | + |
| 192 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(2); |
| 193 | + expect(ScheduledJobRegistry.hasTask("test-task-1")).toBe(true); |
| 194 | + expect(ScheduledJobRegistry.hasTask("task-2")).toBe(true); |
| 195 | + |
| 196 | + // Stop one task |
| 197 | + await ScheduledJobRegistry.stop("test-task-1"); |
| 198 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(1); |
| 199 | + expect(ScheduledJobRegistry.hasTask("test-task-1")).toBe(false); |
| 200 | + expect(ScheduledJobRegistry.hasTask("task-2")).toBe(true); |
| 201 | + |
| 202 | + // Stop all remaining tasks |
| 203 | + await ScheduledJobRegistry.stopAll(); |
| 204 | + expect(ScheduledJobRegistry.getTaskCount()).toBe(0); |
| 205 | + expect(ScheduledJobRegistry.hasTask("task-2")).toBe(false); |
| 206 | + }); |
| 207 | + }); |
| 208 | +}); |
0 commit comments