import { describe, it, expect } from 'vitest';
import { resolveOllamaUrl } from './config.js';

describe('resolveOllamaUrl', () => {
  it('returns default localhost when no env vars set', () => {
    expect(resolveOllamaUrl({})).toBe('http://localhost:11434');
  });

  it('uses OLLAMA_URL when set', () => {
    expect(resolveOllamaUrl({ OLLAMA_URL: 'http://myhost:11434' })).toBe('http://myhost:11434');
  });

  it('uses OLLAMA_HOST when set', () => {
    expect(resolveOllamaUrl({ OLLAMA_HOST: 'http://otherhost:11434' })).toBe(
      'http://otherhost:11434'
    );
  });

  it('prefers OLLAMA_URL over OLLAMA_HOST', () => {
    expect(
      resolveOllamaUrl({
        OLLAMA_URL: 'http://primary:11434',
        OLLAMA_HOST: 'http://secondary:11434',
      })
    ).toBe('http://primary:11434');
  });

  it('normalizes URL without scheme', () => {
    expect(resolveOllamaUrl({ OLLAMA_URL: 'myhost:11434' })).toBe('http://myhost:11434');
  });

  it('strips trailing slashes', () => {
    expect(resolveOllamaUrl({ OLLAMA_URL: 'http://myhost:11434///' })).toBe('http://myhost:11434');
  });

  it('trims whitespace', () => {
    expect(resolveOllamaUrl({ OLLAMA_URL: '  http://myhost:11434  ' })).toBe('http://myhost:11434');
  });

  it('preserves https scheme', () => {
    expect(resolveOllamaUrl({ OLLAMA_URL: 'https://secure:11434' })).toBe('https://secure:11434');
  });
});
