// components/ui/__tests__/WhisperFile.spec.tsx

import React from "react";
import { render, act } from "@testing-library/react-native";
import { WhisperFile } from "@/app/lib/whisper"; // Adjust the import path as necessary

describe("WhisperFile", () => {
  let whisperFile: WhisperFile;

  beforeEach(() => {
    whisperFile = new WhisperFile("small");
  });

  it("should initialize correctly", () => {
    expect(whisperFile).toBeInstanceOf(WhisperFile);
  });

  describe("getModelFileSize", () => {
    it("should return the correct model file size", async () => {
      expect(whisperFile.size).toBeUndefined();
      await whisperFile.updateMetadata();
      expect(whisperFile.size).toBeGreaterThan(1000);
    });
  });

  describe("getWhisperDownloadStatus", () => {
    it("should return the correct download status", async () => {
      const mockStatus = {
        doesTargetExist: true,
        isDownloadComplete: false,
        hasDownloadStarted: true,
        progress: {
          current: 100,
          total: 200,
          remaining: 100,
          percentRemaining: 50.0,
        },
      };
      jest
        .spyOn(whisperFile, "getDownloadStatus")
        .mockResolvedValue(mockStatus);

      const result = await whisperFile.getDownloadStatus();

      expect(result).toEqual(mockStatus);
    });
  });

  describe("initiateWhisperDownload", () => {
    it("should initiate the download with default options", async () => {
      const mockModelLabel = "small";
      jest
        .spyOn(whisperFile, "createDownloadResumable")
        .mockResolvedValue(true);

      await whisperFile.initiateWhisperDownload(mockModelLabel);

      expect(whisperFile.createDownloadResumable).toHaveBeenCalledWith(
        mockModelLabel
      );
    });

    it("should initiate the download with custom options", async () => {
      const mockModelLabel = "small";
      const mockOptions = { force_redownload: true };
      jest
        .spyOn(whisperFile, "createDownloadResumable")
        .mockResolvedValue(true);

      await whisperFile.initiateWhisperDownload(mockModelLabel, mockOptions);

      expect(whisperFile.createDownloadResumable).toHaveBeenCalledWith(
        mockModelLabel,
        mockOptions
      );
    });

    it("should return the correct download status when target exists and is complete", async () => {
      jest.spyOn(whisperFile, "doesTargetExist").mockResolvedValue(true);
      jest.spyOn(whisperFile, "isDownloadComplete").mockResolvedValue(true);

      expect(await whisperFile.doesTargetExist()).toEqual(true);
      expect(await whisperFile.isDownloadComplete()).toEqual(true);
    });

    it("should return the correct download status when target does not exist", async () => {
      jest.spyOn(whisperFile, "doesTargetExist").mockResolvedValue(false);

      const result = await whisperFile.getDownloadStatus();

      expect(result).toEqual({
        doesTargetExist: false,
        isDownloadComplete: false,
        hasDownloadStarted: false,
        progress: undefined,
      });
    });
  });

  // Add more tests as needed for other methods in WhisperFile
});