import { getConfiguredApiBaseUrl, getConfiguredAssetBaseUrl } from '../../utils/backendUrl';

describe('backendUrl utilities', () => {
  const originalApiUrl = process.env.EXPO_PUBLIC_API_URL;
  const originalBackendUrl = process.env.EXPO_PUBLIC_BACKEND_URL;
  const originalPaymentServerUrl = process.env.EXPO_PUBLIC_PAYMENT_SERVER_URL;

  afterEach(() => {
    process.env.EXPO_PUBLIC_API_URL = originalApiUrl;
    process.env.EXPO_PUBLIC_BACKEND_URL = originalBackendUrl;
    process.env.EXPO_PUBLIC_PAYMENT_SERVER_URL = originalPaymentServerUrl;
  });

  it('prefers EXPO_PUBLIC_API_URL when present', () => {
    process.env.EXPO_PUBLIC_API_URL = 'https://api.example.com/api';
    process.env.EXPO_PUBLIC_BACKEND_URL = 'https://backend.example.com';

    expect(getConfiguredApiBaseUrl()).toBe('https://api.example.com/api');
    expect(getConfiguredAssetBaseUrl()).toBe('https://api.example.com');
  });

  it('falls back to EXPO_PUBLIC_BACKEND_URL and appends /api', () => {
    delete process.env.EXPO_PUBLIC_API_URL;
    process.env.EXPO_PUBLIC_BACKEND_URL = 'https://backend.example.com';

    expect(getConfiguredApiBaseUrl()).toBe('https://backend.example.com/api');
    expect(getConfiguredAssetBaseUrl()).toBe('https://backend.example.com');
  });
});
