import { afterEach, describe, expect, it, vi } from "vitest";
import {
  readWelcomeTourDismissed,
  WELCOME_TOUR_SCHEMA_VERSION,
  WELCOME_TOUR_STORAGE_KEY,
  writeWelcomeTourDismissed
} from "./welcome-tour-storage";

const mockStorage = () => {
  const map = new Map<string, string>();
  vi.stubGlobal("localStorage", {
    getItem: (k: string) => (map.has(k) ? map.get(k)! : null),
    setItem: (k: string, v: string) => void map.set(k, v),
    removeItem: (k: string) => void map.delete(k),
    clear: () => void map.clear(),
    key: (i: number) => [...map.keys()][i] ?? null,
    get length() {
      return map.size;
    }
  } as Storage);
};

describe("welcome tour storage (Gherkin @int-welcome-tour-dismiss)", () => {
  afterEach(() => {
    vi.unstubAllGlobals();
  });

  /** @int-welcome-tour-dismiss */
  it("enregistre la version courante lorsque le tour est fermé", () => {
    mockStorage();
    expect(readWelcomeTourDismissed()).toBe(false);
    writeWelcomeTourDismissed();
    expect(localStorage.getItem(WELCOME_TOUR_STORAGE_KEY)).toBe(WELCOME_TOUR_SCHEMA_VERSION);
    expect(readWelcomeTourDismissed()).toBe(true);
  });
});
