import test from 'node:test';
import assert from 'node:assert/strict';
import { OpenAICompatibleProvider } from '../lib/llm/openai-compatible.mjs';
import { createLLMProvider } from '../lib/llm/index.mjs';

test('OpenAI-compatible provider sends configurable payload', async () => {
  const provider = new OpenAICompatibleProvider({
    name: 'local-openai',
    baseUrl: 'http://localhost:1234/v1/',
    apiKey: 'local-key',
    model: 'local-model',
    temperature: 0.1,
    maxTokens: 123,
    timeoutMs: 5000,
  });

  let capturedUrl;
  let capturedBody;
  const originalFetch = globalThis.fetch;
  globalThis.fetch = async (url, opts) => {
    capturedUrl = url;
    capturedBody = JSON.parse(opts.body);
    assert.equal(opts.headers.Authorization, 'Bearer local-key');
    return {
      ok: true,
      json: async () => ({
        choices: [{ message: { content: 'ok' } }],
        usage: { prompt_tokens: 2, completion_tokens: 3 },
        model: 'local-model',
      }),
    };
  };

  try {
    const result = await provider.complete('system', 'user');
    assert.equal(capturedUrl, 'http://localhost:1234/v1/chat/completions');
    assert.equal(capturedBody.model, 'local-model');
    assert.equal(capturedBody.temperature, 0.1);
    assert.equal(capturedBody.max_tokens, 123);
    assert.equal(result.text, 'ok');
    assert.deepEqual(result.usage, { inputTokens: 2, outputTokens: 3 });
  } finally {
    globalThis.fetch = originalFetch;
  }
});

test('factory supports lmstudio and openai-compatible aliases', () => {
  const lmstudio = createLLMProvider({ provider: 'lmstudio', model: 'local-model' });
  assert.ok(lmstudio instanceof OpenAICompatibleProvider);
  assert.equal(lmstudio.baseUrl, 'http://localhost:1234/v1');
  assert.equal(lmstudio.isConfigured, true);

  const compatible = createLLMProvider({
    provider: 'openai-compatible',
    baseUrl: 'http://llm:8000/v1',
    apiKey: 'token',
    model: 'qwen',
  });
  assert.ok(compatible instanceof OpenAICompatibleProvider);
  assert.equal(compatible.baseUrl, 'http://llm:8000/v1');
});
