/**
 * Tests for WebTool._selectVisionModel() — catalog-driven.
 * Uses recommended_for and tier instead of hardcoded name lists.
 */

import { jest, describe, test, expect } from '@jest/globals';

function selectVisionModel(context) {
  const modelsService = context.aiService?.modelsService;
  if (!modelsService) return null;

  const allModels = modelsService.getModels?.() || [];
  const visionModels = allModels.filter(m => m.supportsVision === true);
  if (visionModels.length === 0) return null;

  const recommended = visionModels.find(m => m.recommended_for?.includes('vision'));
  if (recommended) return recommended.name;

  const budget = visionModels.find(m => m.tier === 'budget');
  if (budget) return budget.name;

  return visionModels[0].name;
}

function makeContext(models) {
  return { aiService: { modelsService: { getModels: () => models } } };
}

describe('WebTool._selectVisionModel (catalog-driven)', () => {

  test('selects model recommended for vision', () => {
    const models = [
      { name: 'vision-model', supportsVision: true, recommended_for: ['vision'], tier: 'budget' },
      { name: 'other', supportsVision: true, recommended_for: [], tier: 'standard' },
    ];
    expect(selectVisionModel(makeContext(models))).toBe('vision-model');
  });

  test('selects budget vision model as fallback', () => {
    const models = [
      { name: 'premium', supportsVision: true, recommended_for: [], tier: 'premium' },
      { name: 'cheap', supportsVision: true, recommended_for: [], tier: 'budget' },
    ];
    expect(selectVisionModel(makeContext(models))).toBe('cheap');
  });

  test('returns first available when no tags match', () => {
    const models = [
      { name: 'only-one', supportsVision: true, recommended_for: [], tier: 'premium' },
    ];
    expect(selectVisionModel(makeContext(models))).toBe('only-one');
  });

  test('skips non-vision models', () => {
    const models = [
      { name: 'text-only', supportsVision: false, recommended_for: ['vision'], tier: 'budget' },
      { name: 'actual-vision', supportsVision: true, recommended_for: [], tier: 'standard' },
    ];
    expect(selectVisionModel(makeContext(models))).toBe('actual-vision');
  });

  test('returns null when no models available', () => {
    expect(selectVisionModel(makeContext([]))).toBeNull();
  });

  test('returns null when no aiService', () => {
    expect(selectVisionModel({})).toBeNull();
  });

  test('falls back to o4-mini when _selectVisionModel returns null', () => {
    // The actual code does: this._selectVisionModel(context) || 'o4-mini'
    const result = selectVisionModel({}) || 'o4-mini';
    expect(result).toBe('o4-mini');
  });
});
