2048
    ¹ˆ¾�þB gÁä’”Ïìƒh¹ò¿ï     ¹2048
  !  »ˆÂ�þ[ B³�‘¹q±¶Åüÿì„}ßòÁ¿ï ?÷    »# Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'
require_relative 'shared_examples/no_model_listing'

# The per provider specs are tagged `integration` and need real credentials, hence they do not run
# in a regular pipeline - the normalization, capability mapping and error mapping of the model
# listing is covered here without network access instead.
RSpec.describe AI::Provider, '.models' do
  def stub_get(response)
    allow(UserAgent).to receive(:get).and_return(response)
  end

  def success_response(data)
    UserAgent::Result.new(success: true, code: 200, data:)
  end

  def failed_response(code, body = nil)
    UserAgent::Result.new(success: false, code:, body:)
  end

  # An entry of a normalized descriptor list, with everything the provider does not report unknown.
  def descriptor(id, capabilities: [:chat], embedding_input_limit: nil, embedding_size: nil)
    {
      id:,
      capabilities:,
      embedding_input_limit:,
      embedding_size:,
    }
  end

  shared_examples 'a provider mapping listing failures' do
    it 'maps a rejected token to the provider message' do
      stub_get(failed_response(401, { error: { message: 'Incorrect API key provided' } }.to_json))

      expect { provider.models(config) }
        .to raise_error(AI::Provider::ResponseError, 'Invalid API key - please check your configuration')
    end

    it 'maps a missing endpoint to the provider message' do
      stub_get(failed_response(404))

      expect { provider.models(config) }
        .to raise_error(AI::Provider::ResponseError, 'Not found - resource not found')
    end

    it 'maps a server error to the provider message' do
      stub_get(failed_response(500))

      expect { provider.models(config) }
        .to raise_error(AI::Provider::ResponseError, 'API server error - please try again')
    end

    # UserAgent answers transport failures with code 0 and no body at all.
    it 'maps an unreachable endpoint to the provider message' do
      stub_get(UserAgent::Result.new(success: false, code: 0, error: '#<Errno::ECONNREFUSED>'))

      expect { provider.models(config) }
        .to raise_error(AI::Provider::ResponseError, 'An unknown error occurred')
    end

    # A non-JSON body of an otherwise successful response fails to parse inside UserAgent, which
    # answers it like a transport failure.
    it 'maps a non-JSON body to the provider message' do
      stub_get(UserAgent::Result.new(success: false, code: 0, error: '#<JSON::ParserError>'))

      expect { provider.models(config) }
        .to raise_error(AI::Provider::ResponseError, 'An unknown error occurred')
    end

    it 'reports a payload that is not a model list' do
      stub_get(success_response({ 'object' => 'list' }))

      expect { provider.models(config) }
        .to raise_error(AI::Provider::ResponseError, 'The response could not be processed.')
    end
  end

  # An id that is not a usable string is no model to offer: it would travel through the capability
  # heuristics - raising there, which the dialog would see as an internal error - and end up in the
  # dropdown as if it were a model name.
  shared_examples 'a provider skipping unusable model entries' do
    it 'skips an entry without a usable id' do
      stub_get(success_response({
                                  list_key => [
                                    { id_key => 123 },
                                    { id_key => { 'nested' => true } },
                                    { id_key => '' },
                                    { 'other' => 'key' },
                                    'not an entry at all',
                                  ],
                                }))

      expect(provider.models(config)).to eq([])
    end
  end

  describe 'with OpenAI' do
    let(:provider) { AI::Provider::OpenAI }
    let(:config)   { { token: 'sk-123' } }
    let(:list_key) { 'data' }
    let(:id_key)   { 'id' }

    include