import { mockedGetLabs } from "@/testutils/mock-lab";
import { mockedGetMe, normalTestUser } from "@/testutils/mock-me-context";
import { renderServerComponent } from "@/testutils/render-server-component";
import { withRscLayoutEssentials } from "@/testutils/rsc-essentials";
import { enableActx, useActx } from "@/testutils/use-actx";
import { expect, test } from "vitest";

enableActx();

test("Authorized home layout", async () => {
  const actx = useActx();
  mockedGetMe.mockReturnValue(normalTestUser);
  mockedGetLabs.mockReturnValue(Promise.resolve([]));

  const { ProvidersWrapper } = await import("@/testutils/providers");

  const { layout } = await withRscLayoutEssentials(
    () => import("@/components/pages/root-layout"),
    {
      pathname: "/",
      params: {},
      children: <></>,
    }
  );

  const ui = await renderServerComponent(layout, {
    wrapper: ProvidersWrapper,
  });

  await actx(() => {});

  expect(ui.queryByRole("button", { name: "Sign up / Sign in" })).toBeNull();
});

test("Unauthorized home layout", async () => {
  mockedGetMe.mockReturnValue(null);
  mockedGetLabs.mockReturnValue(Promise.resolve([]));

  const { ProvidersWrapper } = await import("@/testutils/providers");

  const { layout } = await withRscLayoutEssentials(
    () => import("@/components/pages/root-layout"),
    {
      pathname: "/",
      params: {},
      children: <></>,
    }
  );

  const ui = await renderServerComponent(layout, {
    wrapper: ProvidersWrapper,
  });

  expect(
    ui.getByRole("button", { name: "Sign up / Sign in" })
  ).not.toBeDisabled();
  expect(ui.getByRole("button", { name: "Sign up / Sign in" })).toHaveAttribute(
    "href",
    `/login?redirect_uri=%2F`
  );
});
