import { describe, expect, test, mock, beforeEach, spyOn } from "bun:test"; import { actionService } from "./action.service"; import { systemEvents, EVENTS } from "@shared/lib/events"; import { dashboardService } from "@shared/modules/dashboard/dashboard.service"; describe("ActionService", () => { beforeEach(() => { // Clear any previous mock state mock.restore(); }); /** * Test Case: Command Reload * Requirement: Emits event and records to dashboard */ test("reloadCommands should emit RELOAD_COMMANDS event and record dashboard event", async () => { const emitSpy = spyOn(systemEvents, "emit"); const recordSpy = spyOn(dashboardService, "recordEvent").mockImplementation(() => Promise.resolve()); const result = await actionService.reloadCommands(); expect(result.success).toBe(true); expect(emitSpy).toHaveBeenCalledWith(EVENTS.ACTIONS.RELOAD_COMMANDS); expect(recordSpy).toHaveBeenCalledWith(expect.objectContaining({ type: "info", message: "Admin: Triggered command reload" })); }); /** * Test Case: Cache Clearance * Requirement: Emits event and records to dashboard */ test("clearCache should emit CLEAR_CACHE event and record dashboard event", async () => { const emitSpy = spyOn(systemEvents, "emit"); const recordSpy = spyOn(dashboardService, "recordEvent").mockImplementation(() => Promise.resolve()); const result = await actionService.clearCache(); expect(result.success).toBe(true); expect(emitSpy).toHaveBeenCalledWith(EVENTS.ACTIONS.CLEAR_CACHE); expect(recordSpy).toHaveBeenCalledWith(expect.objectContaining({ type: "info", message: "Admin: Triggered cache clearance" })); }); /** * Test Case: Maintenance Mode Toggle * Requirement: Emits event with correct payload and records to dashboard with warning type */ test("toggleMaintenanceMode should emit MAINTENANCE_MODE event and record dashboard event", async () => { const emitSpy = spyOn(systemEvents, "emit"); const recordSpy = spyOn(dashboardService, "recordEvent").mockImplementation(() => Promise.resolve()); const result = await actionService.toggleMaintenanceMode(true, "Test Reason"); expect(result.success).toBe(true); expect(result.enabled).toBe(true); expect(emitSpy).toHaveBeenCalledWith(EVENTS.ACTIONS.MAINTENANCE_MODE, { enabled: true, reason: "Test Reason" }); expect(recordSpy).toHaveBeenCalledWith(expect.objectContaining({ type: "warn", message: "Admin: Maintenance mode ENABLED (Test Reason)" })); }); });