import { describe, expect, it } from "vitest";
import {
  resolveDeveloperAccessGate,
  shouldFetchDeveloperRequestStatus,
  shouldShowDeveloperAccessLoading,
} from "./developerAccessGate";

describe("developer access gate", () => {
  it("fetches request status only for user roles", () => {
    expect(shouldFetchDeveloperRequestStatus("user")).toBe(true);
    expect(shouldFetchDeveloperRequestStatus("super_admin")).toBe(false);
  });

  it("resolves access and request states from the request status", () => {
    expect(
      resolveDeveloperAccessGate("super_admin", {
        status: "pending",
        pendingPages: ["overview"],
      }),
    ).toEqual({
      hasDeveloperAccess: true,
      isDeveloperRequestPending: true,
      canRequestDeveloperAccess: false,
    });

    expect(
      resolveDeveloperAccessGate("user", {
        status: "approved",
        approvedPages: ["overview"],
      }),
    ).toEqual({
      hasDeveloperAccess: true,
      isDeveloperRequestPending: false,
      canRequestDeveloperAccess: false,
    });

    expect(
      resolveDeveloperAccessGate(
        "user",
        {
          status: "pending",
          pendingPages: ["audit"],
        },
        ["audit"],
      ),
    ).toEqual({
      hasDeveloperAccess: false,
      isDeveloperRequestPending: true,
      canRequestDeveloperAccess: false,
    });

    expect(
      resolveDeveloperAccessGate(
        "user",
        {
          status: "approved",
          approvedPages: ["overview"],
        },
        ["audit"],
      ),
    ).toEqual({
      hasDeveloperAccess: false,
      isDeveloperRequestPending: false,
      canRequestDeveloperAccess: true,
    });

    expect(resolveDeveloperAccessGate("user", { status: "none" })).toEqual({
      hasDeveloperAccess: false,
      isDeveloperRequestPending: false,
      canRequestDeveloperAccess: true,
    });
  });

  it("shows the loading gate only for user requests", () => {
    expect(shouldShowDeveloperAccessLoading("user", true, false)).toBe(true);
    expect(shouldShowDeveloperAccessLoading("user", false, true)).toBe(true);
    expect(shouldShowDeveloperAccessLoading("super_admin", true, true)).toBe(
      false,
    );
  });
});
