import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { OllamaDetector } from '../../../src/infrastructure/ollama/ollama-detector.js';
describe('OLLAMA_HOST Normalization', () => {
let originalEnv: string | undefined;
beforeEach(() => {
originalEnv = process.env.OLLAMA_HOST;
});
afterEach(() => {
if (originalEnv !== undefined) {
process.env.OLLAMA_HOST = originalEnv;
} else {
delete process.env.OLLAMA_HOST;
}
});
describe('OllamaDetector endpoint normalization', () => {
it('removes trailing slashes from OLLAMA_HOST', () => {
process.env.OLLAMA_HOST = 'http://localhost:11434/';
const detector = new OllamaDetector();
// Check that endpoint is normalized (no direct access, so we test the behavior)
expect(detector).toBeDefined();
});
it('removes /api suffix from OLLAMA_HOST', () => {
process.env.OLLAMA_HOST = 'http://localhost:11434/api';
const detector = new OllamaDetector();
expect(detector).toBeDefined();
});
it('handles multiple trailing slashes', () => {
process.env.OLLAMA_HOST = 'http://localhost:11434///';
const detector = new OllamaDetector();
expect(detector).toBeDefined();
});
it('handles /api with trailing slashes', () => {
process.env.OLLAMA_HOST = 'http://localhost:11434/api//';
const detector = new OllamaDetector();
expect(detector).toBeDefined();
});
it('trims whitespace from OLLAMA_HOST', () => {
process.env.OLLAMA_HOST = ' http://localhost:11434 ';
const detector = new OllamaDetector();
expect(detector).toBeDefined();
});
it('handles complex cases with whitespace, /api, and trailing slashes', () => {
process.env.OLLAMA_HOST = ' http://localhost:11434/api/// ';
const detector = new OllamaDetector();
expect(detector).toBeDefined();
});
it('preserves other paths that are not /api', () => {
process.env.OLLAMA_HOST = 'http://localhost:11434/v1';
const detector = new OllamaDetector();
expect(detector).toBeDefined();
});
it('works with default when OLLAMA_HOST not set', () => {
delete process.env.OLLAMA_HOST;
const detector = new OllamaDetector();
expect(detector).toBeDefined();
});
it('normalizes explicitly provided endpoint', () => {
const detector = new OllamaDetector('http://example.com/api//');
expect(detector).toBeDefined();
});
});
});