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

// 1) Mock for destroySession
vi.mock("@/lib/auth/session", () => ({
	destroySession: vi.fn(),
}));

// 2) Import after mock
import { destroySession } from "@/lib/auth/session";
import { GET, dynamic } from "./route.js";

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

	it('exports dynamic="force-dynamic" (RHL-006)', () => {
		expect(dynamic).toBe("force-dynamic");
	});

	it("calls destroySession and returns ok: true", async () => {
		const response = await GET();
		const json = await response.json();

		expect(destroySession).toHaveBeenCalledTimes(1);

		expect(response.status).toBe(200);
		expect(json).toEqual({ ok: true });
	});

	it("returns 500 when destroySession throws an error", async () => {
		destroySession.mockImplementation(() => {
			throw new Error("boom");
		});

		const response = await GET();
		const body = await response.json();

		expect(destroySession).toHaveBeenCalledTimes(1);

		expect(response.status).toBe(500);
		expect(body).toEqual({
			error: {
				message: "Internal server error",
				code: "INTERNAL_SERVER_ERROR",
			},
		});
	});
});
