import { describe, expect, it } from 'vitest';
import { resolveRoute, upstreamModel } from '../inference/provider.js';

describe('resolveRoute', () => {
  it('routes to swap when agent is null', () => {
    expect(resolveRoute(null)).toEqual({ route: 'swap', flags: null });
  });

  it('routes to swap when agent has no llama_extra_args', () => {
    expect(resolveRoute({ llama_extra_args: null })).toEqual({ route: 'swap', flags: null });
  });

  it('routes to swap when agent has empty llama_extra_args', () => {
    expect(resolveRoute({ llama_extra_args: [] })).toEqual({ route: 'swap', flags: null });
  });

  it('routes to sidecar when agent has llama_extra_args', () => {
    const result = resolveRoute({ llama_extra_args: ['--top-k', '20'] });
    expect(result.route).toBe('sidecar');
    expect(result.flags).toEqual(['--top-k', '20']);
  });
});

describe('upstreamModel', () => {
  const swapConfig = { LLAMA_SWAP_URL: 'http://localhost:8401' };
  const fullConfig = {
    LLAMA_SWAP_URL: 'http://localhost:8401',
    LLAMA_SIDECAR_URL: 'http://localhost:8402',
  };

  it('returns a model for swap route (no agent)', () => {
    const model = upstreamModel(swapConfig, 'test-model');
    expect(model).toBeDefined();
    expect((model as any).modelId).toBe('test-model');
  });

  it('returns a model for swap route (agent without extra args)', () => {
    const model = upstreamModel(swapConfig, 'test-model', { llama_extra_args: null });
    expect(model).toBeDefined();
  });

  it('returns a model for sidecar route', () => {
    const model = upstreamModel(fullConfig, 'test-model', { llama_extra_args: ['--top-k', '20'] });
    expect(model).toBeDefined();
    expect((model as any).modelId).toBe('test-model');
  });

  it('throws when sidecar route requested but URL missing', () => {
    expect(() =>
      upstreamModel(swapConfig, 'test-model', { llama_extra_args: ['--top-k', '20'] }),
    ).toThrow(/LLAMA_SIDECAR_URL/);
  });

  it('routes to swap for empty llama_extra_args array', () => {
    const model = upstreamModel(swapConfig, 'test-model', { llama_extra_args: [] });
    expect(model).toBeDefined();
  });
});
