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

describe('frontend runtime config', () => {
  afterEach(() => {
    vi.unstubAllEnvs();
    vi.resetModules();
  });

  it('prefers explicit VITE_API_BASE_URL and trims trailing slashes', async () => {
    vi.stubEnv('VITE_API_BASE_URL', 'http://api.example.test:8000///');

    const config = await import('./config');

    expect(config.API_BASE_URL).toBe('http://api.example.test:8000');
  });

  it('infers the API host from the current browser hostname', async () => {
    const config = await import('./config');

    expect(config.API_BASE_URL).toBe('http://seg.local:8000');
  });

  it('derives websocket URL from API URL unless explicitly configured', async () => {
    vi.stubEnv('VITE_API_BASE_URL', 'https://seg.example.test');

    const config = await import('./config');

    expect(config.WS_PROGRESS_URL).toBe('wss://seg.example.test/ws/progress');
  });

  it('prefers explicit VITE_WS_PROGRESS_URL', async () => {
    vi.stubEnv('VITE_WS_PROGRESS_URL', 'ws://custom/ws/progress');

    const config = await import('./config');

    expect(config.WS_PROGRESS_URL).toBe('ws://custom/ws/progress');
  });
});
