/**********************************************************************
 * Copyright (C) 2025 Red Hat, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 ***********************************************************************/
import type { TelemetryLogger } from '@podman-desktop/api';
import { beforeEach, expect, test, vi } from 'vitest';

import { HYPER_V_DOC_LINKS } from '/@/checks/windows/constants';
import type { PowerShellClient } from '/@/utils/powershell';
import { getPowerShellClient } from '/@/utils/powershell';

import { HyperVRunningCheck } from './hyper-v-running-check';

vi.mock(import('@podman-desktop/api'));
vi.mock(import('../../utils/powershell'), () => ({
  getPowerShellClient: vi.fn(),
}));

const mockTelemetryLogger = {} as TelemetryLogger;

const POWERSHELL_CLIENT: PowerShellClient = {
  isUserAdmin: vi.fn(),
  isHyperVInstalled: vi.fn(),
  isHyperVRunning: vi.fn(),
  isVirtualMachineAvailable: vi.fn(),
  isRunningElevated: vi.fn(),
};

beforeEach(() => {
  vi.resetAllMocks();
  vi.mocked(getPowerShellClient).mockResolvedValue(POWERSHELL_CLIENT);
});

test('expect HyperVRunningCheck preflight check return failure result if HyperV not running', async () => {
  vi.mocked(POWERSHELL_CLIENT.isHyperVRunning).mockResolvedValue(false);

  const hyperVRunningCheck = new HyperVRunningCheck(mockTelemetryLogger);
  const result = await hyperVRunningCheck.execute();
  expect(result.successful).toBeFalsy();
  expect(result.description).equal('Hyper-V is not running on your system.');
  expect(result.docLinks?.[0].url).equal(HYPER_V_DOC_LINKS.url);
  expect(result.docLinks?.[0].title).equal(HYPER_V_DOC_LINKS.title);
});

test('expect HyperVRunningCheck preflight check return OK if HyperV is running', async () => {
  vi.mocked(POWERSHELL_CLIENT.isHyperVRunning).mockResolvedValue(true);

  const hyperVRunningCheck = new HyperVRunningCheck(mockTelemetryLogger);
  const result = await hyperVRunningCheck.execute();
  expect(result.successful).toBeTruthy();
  expect(result.description).toBeUndefined();
  expect(result.docLinks).toBeUndefined();
});
