/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import { callbackForForceMerge, callbackForForceMergeTimeout } from "./force_merge"; import { coreServicesMock, httpClientMock } from "../../../test/mocks"; import { ListenType } from "../../lib/JobScheduler"; import { CoreSetup } from "opensearch-dashboards/public"; const getMockFn = (response = {}, ok = true) => { return jest.fn().mockResolvedValue({ ok, response, }); }; const forceMergeMetaData = { interval: 0, extras: { toastId: "toastId", sourceIndex: ["sourceIndex"], taskId: "taskId", }, type: ListenType.FORCE_MERGE, }; const core = ({ ...coreServicesMock, http: httpClientMock, } as unknown) as CoreSetup; describe("callbackForForceMerge spec", () => { it("callback when error", async () => { httpClientMock.fetch = getMockFn({}, false); let result = await callbackForForceMerge(forceMergeMetaData, { core, }); expect(result).toBe(false); }); it("callback when not complete", async () => { httpClientMock.fetch = getMockFn({ found: true, _source: { completed: false, }, }); const result = await callbackForForceMerge(forceMergeMetaData, { core, }); expect(result).toBe(false); }); it("callback when complete", async () => { httpClientMock.fetch = getMockFn({ found: true, _source: { completed: true, }, }); const result = await callbackForForceMerge(forceMergeMetaData, { core, }); expect(result).toBe(true); expect(core.notifications.toasts.remove).toBeCalledWith("toastId"); expect(core.notifications.toasts.addSuccess).toBeCalledTimes(1); }); it("callback when some complete", async () => { httpClientMock.fetch = getMockFn({ found: true, _source: { completed: true, response: { _shards: { successful: 9, total: 10, }, }, }, }); const result = await callbackForForceMerge(forceMergeMetaData, { core, }); expect(result).toBe(true); expect(core.notifications.toasts.remove).toBeCalledWith("toastId"); expect(core.notifications.toasts.addWarning).toBeCalledTimes(1); }); it("callback when failed", async () => { httpClientMock.fetch = getMockFn({ found: true, _source: { completed: true, error: { reason: "reason", }, }, }); const result = await callbackForForceMerge(forceMergeMetaData, { core, }); expect(result).toBe(true); expect(core.notifications.toasts.remove).toBeCalledWith("toastId"); expect(core.notifications.toasts.addDanger).toBeCalledTimes(1); }); it("callback when timeout", async () => { const result = await callbackForForceMergeTimeout(forceMergeMetaData, { core, }); expect(result).toBe(true); expect(core.notifications.toasts.remove).toBeCalledWith("toastId"); expect(core.notifications.toasts.addWarning).toBeCalledTimes(1); }); });