import { describe, it, expect, beforeEach } from "vitest";
import {
  setShouldRestoreHistory,
  setSavedHistory,
  getShouldRestoreHistory,
  getSavedHistory,
  clearHistoryRestore,
} from "./historyRestore";

describe("historyRestore module", () => {
  beforeEach(() => {
    clearHistoryRestore();
  });

  describe("initial state", () => {
    it("shouldRestoreHistory is false by default", () => {
      expect(getShouldRestoreHistory()).toBe(false);
    });

    it("savedHistory is null by default", () => {
      expect(getSavedHistory()).toBeNull();
    });
  });

  describe("setShouldRestoreHistory", () => {
    it("sets shouldRestoreHistory to true", () => {
      setShouldRestoreHistory(true);
      expect(getShouldRestoreHistory()).toBe(true);
    });

    it("sets shouldRestoreHistory to false", () => {
      setShouldRestoreHistory(true);
      setShouldRestoreHistory(false);
      expect(getShouldRestoreHistory()).toBe(false);
    });
  });

  describe("setSavedHistory", () => {
    it("sets the saved history string", () => {
      setSavedHistory("some history content");
      expect(getSavedHistory()).toBe("some history content");
    });

    it("sets the saved history to null", () => {
      setSavedHistory("some history content");
      setSavedHistory(null);
      expect(getSavedHistory()).toBeNull();
    });
  });

  describe("clearHistoryRestore", () => {
    it("resets shouldRestoreHistory to false", () => {
      setShouldRestoreHistory(true);
      clearHistoryRestore();
      expect(getShouldRestoreHistory()).toBe(false);
    });

    it("resets savedHistory to null", () => {
      setSavedHistory("some content");
      clearHistoryRestore();
      expect(getSavedHistory()).toBeNull();
    });

    it("clears both values at once", () => {
      setShouldRestoreHistory(true);
      setSavedHistory("history");
      clearHistoryRestore();
      expect(getShouldRestoreHistory()).toBe(false);
      expect(getSavedHistory()).toBeNull();
    });
  });
});
