import { afterEach, describe, expect, it } from "vitest";
import {
  clearAuthSessionHint,
  hasAuthSessionHint,
  isProtectedAppPath,
  markAuthSessionHint,
  shouldAttemptAuthRefresh
} from "./authSessionHint";

describe("authSessionHint", () => {
  afterEach(() => {
    sessionStorage.clear();
  });

  it("tracks session hint in sessionStorage", () => {
    expect(hasAuthSessionHint()).toBe(false);
    markAuthSessionHint();
    expect(hasAuthSessionHint()).toBe(true);
    clearAuthSessionHint();
    expect(hasAuthSessionHint()).toBe(false);
  });

  it("detects protected app paths", () => {
    expect(isProtectedAppPath("/admin")).toBe(true);
    expect(isProtectedAppPath("/admin/users")).toBe(true);
    expect(isProtectedAppPath("/profile")).toBe(true);
    expect(isProtectedAppPath("/login")).toBe(false);
  });

  it("attempts refresh on protected paths even without hint", () => {
    expect(shouldAttemptAuthRefresh("/admin")).toBe(true);
    expect(shouldAttemptAuthRefresh("/login")).toBe(false);
  });

  it("attempts refresh when hint is present", () => {
    markAuthSessionHint();
    expect(shouldAttemptAuthRefresh("/login")).toBe(true);
  });
});
