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

// Mock next/server
vi.mock('next/server', () => ({
  NextResponse: {
    json: (body, init) => ({
      _body: body,
      status: init?.status || 200,
      json: () => Promise.resolve(body),
    }),
  },
}));

const mockCookieStore = {
  delete: vi.fn(),
};
vi.mock('next/headers', () => ({
  cookies: vi.fn(() => Promise.resolve(mockCookieStore)),
}));

vi.mock('@/lib/auth/dashboardSession', () => ({
  clearDashboardAuthCookie: vi.fn((store) => store.delete('auth_token')),
}));

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

  it('clears auth_token and OIDC cookies and returns success', async () => {
    const { POST } = await import('./route.js');
    const res = await POST();

    expect(res.status).toBe(200);
    expect(res._body.success).toBe(true);
    expect(mockCookieStore.delete).toHaveBeenCalledWith('auth_token');
    expect(mockCookieStore.delete).toHaveBeenCalledWith('oidc_state');
    expect(mockCookieStore.delete).toHaveBeenCalledWith('oidc_nonce');
    expect(mockCookieStore.delete).toHaveBeenCalledWith('oidc_code_verifier');
  });

  it('always returns success even if cookie clearing would fail', async () => {
    // The route doesn't catch errors, but also doesn't throw for cookie operations
    const { POST } = await import('./route.js');
    const res = await POST();

    expect(res._body.success).toBe(true);
  });
});
