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

describe('auth client origin contract', () => {
  afterEach(() => {
    vi.unstubAllGlobals();
    vi.resetModules();
  });

  it('uses the same-origin BetterAuth mount at /api/auth', async () => {
    const fetchMock = vi.fn<typeof fetch>();
    fetchMock.mockResolvedValue(
      new Response(JSON.stringify({ session: null, user: null }), {
        status: 200,
        headers: { 'Content-Type': 'application/json' },
      }),
    );
    vi.stubGlobal('fetch', fetchMock);

    const { authClient } = await import('./auth-client');
    await authClient.getSession();

    expect(fetchMock).toHaveBeenCalledOnce();
    const firstCall = fetchMock.mock.calls.at(0);
    expect(firstCall).toBeDefined();
    const requestURL = new URL(String(firstCall?.[0]), window.location.origin);
    expect(requestURL.origin).toBe(window.location.origin);
    expect(requestURL.pathname).toBe('/api/auth/get-session');
  });
});
