import { beforeEach, describe, expect, it, vi } from "vitest";

const { mockJson, mockError } = vi.hoisted(() => ({
  mockJson: vi.fn((data, init?) => {
    return new Response(JSON.stringify(data), {
      status: init?.status ?? 200,
      headers: { "Content-Type": "application/json" },
    });
  }),
  mockError: vi.fn((status: number, message: string) => {
    throw { status, body: { message } };
  }),
}));

vi.mock("@sveltejs/kit", () => ({
  json: mockJson,
  error: mockError,
}));

import { GET } from "./+server";

describe("GET /api/auth/check", () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it("returns 401 when no access token", async () => {
    const event = {
      locals: { session: { accessToken: null } },
    };

    const response = GET(event as any);

    expect(mockJson).toHaveBeenCalledWith(
      { authenticated: false },
      { status: 401 },
    );
  });

  it("returns 401 when session is undefined", async () => {
    const event = {
      locals: {},
    };

    const response = GET(event as any);

    expect(mockJson).toHaveBeenCalledWith(
      { authenticated: false },
      { status: 401 },
    );
  });

  it("returns authenticated true when access token exists", async () => {
    const event = {
      locals: { session: { accessToken: "valid-token" } },
    };

    const response = GET(event as any);

    expect(mockJson).toHaveBeenCalledWith({ authenticated: true });
  });
});
