import { runInBackground } from "../runInBackground"; import { shouldSkipHook } from "../validationHelper"; import { requestToSkipHook } from "../networkService"; jest.mock("../validationHelper", () => ({ shouldSkipHook: jest.fn(), })); jest.mock("../networkService", () => ({ requestToSkipHook: jest.fn(), })); jest.mock("../logger", () => ({ log_debug: jest.fn(), log_error: jest.fn(), log_info: jest.fn(), })); const mockShouldSkipHook = shouldSkipHook as jest.Mock; const mockRequestToSkipHook = requestToSkipHook as jest.Mock; const item = { id: "entry-1" }; const endpoint = { source: "https://skip-endpoint", mapping: {} }; describe("runInBackground", () => { let callback: jest.Mock; let presentUI: jest.Mock; beforeEach(() => { jest.clearAllMocks(); callback = jest.fn(); presentUI = jest.fn(); mockShouldSkipHook.mockResolvedValue(false); mockRequestToSkipHook.mockResolvedValue(false); }); it("presents the UI when no skip rules are configured", async () => { await runInBackground(item, callback, {}, presentUI); expect(presentUI).toHaveBeenCalled(); expect(callback).not.toHaveBeenCalled(); expect(mockShouldSkipHook).not.toHaveBeenCalled(); expect(mockRequestToSkipHook).not.toHaveBeenCalled(); }); it("finishes the hook when a storage key is found, without calling the endpoint", async () => { mockShouldSkipHook.mockResolvedValue(true); const configuration = { rules: { skip_hook_storage_key: "ns.key", skip_hook_endpoint: endpoint, }, }; await runInBackground(item, callback, configuration, presentUI); expect(mockShouldSkipHook).toHaveBeenCalledWith("ns.key"); expect(callback).toHaveBeenCalledWith({ success: true, error: null, payload: item, }); expect(mockRequestToSkipHook).not.toHaveBeenCalled(); expect(presentUI).not.toHaveBeenCalled(); }); it("falls through to the endpoint check when storage keys are not found", async () => { const configuration = { rules: { skip_hook_storage_key: "ns.key", skip_hook_endpoint: endpoint, }, }; await runInBackground(item, callback, configuration, presentUI); expect(mockRequestToSkipHook).toHaveBeenCalledWith(endpoint, item); expect(presentUI).toHaveBeenCalled(); expect(callback).not.toHaveBeenCalled(); }); it("finishes the hook when the endpoint allows skipping", async () => { mockRequestToSkipHook.mockResolvedValue(true); const configuration = { rules: { skip_hook_endpoint: endpoint }, }; await runInBackground(item, callback, configuration, presentUI); expect(callback).toHaveBeenCalledWith({ success: true, error: null, payload: item, }); expect(presentUI).not.toHaveBeenCalled(); }); it("ignores an endpoint configuration without a source", async () => { const configuration = { rules: { skip_hook_endpoint: { mapping: {} } }, }; await runInBackground(item, callback, configuration, presentUI); expect(mockRequestToSkipHook).not.toHaveBeenCalled(); expect(presentUI).toHaveBeenCalled(); }); it("presents the UI when the endpoint check throws", async () => { mockRequestToSkipHook.mockRejectedValue(new Error("network down")); const configuration = { rules: { skip_hook_endpoint: endpoint }, }; await runInBackground(item, callback, configuration, presentUI); expect(presentUI).toHaveBeenCalled(); expect(callback).not.toHaveBeenCalled(); }); it("presents the UI when the storage check throws", async () => { mockShouldSkipHook.mockRejectedValue(new Error("storage error")); const configuration = { rules: { skip_hook_storage_key: "ns.key" }, }; await runInBackground(item, callback, configuration, presentUI); expect(presentUI).toHaveBeenCalled(); expect(callback).not.toHaveBeenCalled(); }); });