const { test } = require('node:test');
const assert = require('node:assert/strict');

const ENV_KEYS = ['TRANSCRIBE_PROVIDER'];

function withEnv(overrides, fn) {
  const previous = {};
  ENV_KEYS.forEach((key) => { previous[key] = process.env[key]; delete process.env[key]; });
  Object.entries(overrides).forEach(([key, value]) => { process.env[key] = value; });
  try {
    return fn();
  } finally {
    ENV_KEYS.forEach((key) => {
      if (previous[key] === undefined) delete process.env[key];
      else process.env[key] = previous[key];
    });
  }
}

test('STT provider respects explicit provider override', () => {
  const sttProvider = require('../src/utils/sttProvider');
  withEnv({ TRANSCRIBE_PROVIDER: 'litellm' }, () => {
    assert.equal(sttProvider.getSTTProvider({ google: true }), 'litellm');
  });
});

test('STT provider only auto-detects LiteLLM', () => {
  const sttProvider = require('../src/utils/sttProvider');
  withEnv({}, () => {
    assert.equal(sttProvider.getSTTProvider({ google: true, aws: true, local: true, litellm: true, openai: true }), 'litellm');
    assert.equal(sttProvider.getSTTProvider({ litellm: true, openai: true }), 'litellm');
    assert.equal(sttProvider.getSTTProvider({ openai: true }), 'none');
    assert.equal(sttProvider.getSTTProvider({}), 'none');
  });
});

test('STT model lists preserve admin defaults', () => {
  const sttProvider = require('../src/utils/sttProvider');
  assert.deepEqual(sttProvider.getSTTModelLists(), {
    litellm: ['local-whisper-large-v3-turbo', 'local-whisper-large-v3', 'local-parakeet-v3', 'local-whisper-1', 'gemini-2.0-flash', 'gemini-2.5-flash']
  });
});

test('LiteLLM STT discovery prefers audio_transcription metadata', () => {
  const sttProvider = require('../src/utils/sttProvider');
  assert.equal(sttProvider.isLiteLLMSTTModel({ id: 'custom-asr', model_info: { mode: 'audio_transcription' } }), true);
  assert.equal(sttProvider.isLiteLLMSTTModel({ id: 'local-parakeet-v3', model_info: { mode: 'audio_transcription' } }), true);
  assert.equal(sttProvider.isLiteLLMSTTModel({ id: 'local-chatterbox-turbo', model_info: { mode: 'audio_speech' } }), false);
});

test('LiteLLM STT discovery ignores names when metadata is absent', () => {
  const sttProvider = require('../src/utils/sttProvider');
  assert.equal(sttProvider.isLiteLLMSTTModel({ id: 'groq-whisper-large-v3' }), false);
  assert.equal(sttProvider.isLiteLLMSTTModel({ id: 'mistral-voxtral-mini-transcribe' }), false);
  assert.equal(sttProvider.isLiteLLMSTTModel({ id: 'openrouter-nova-pro-v1' }), false);
  assert.equal(sttProvider.isLiteLLMSTTModel({ id: 'openrouter-gemini-2.5-flash' }), false);
  assert.equal(sttProvider.isLiteLLMSTTModel({ id: 'openrouter-gpt-audio' }), false);
  assert.equal(sttProvider.isLiteLLMSTTModel({ id: 'local-chatterbox-turbo' }), false);
});

test('LiteLLM STT model extraction filters by metadata only', () => {
  const sttProvider = require('../src/utils/sttProvider');
  assert.deepEqual(sttProvider.getLiteLLMSTTModels([
    { model_name: 'custom-asr', model_info: { mode: 'audio_transcription' } },
    { model_name: 'local-parakeet-v3', model_info: { mode: 'audio_transcription' } },
    { model_name: 'local-chatterbox-turbo', model_info: { mode: 'audio_speech' } },
    { id: 'groq-whisper-large-v3' },
    { id: 'gpt-4.1' }
  ]), ['custom-asr', 'local-parakeet-v3']);
});
