MAJOR=4
MINOR=46
DEVNAME=tty46
  
   Í239Ì¾Ê  ï    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access this resource.</p>
</body></html>
  F   ˆl–Ãa¾”ªi?u8 ¸Û71hß�þHón,ã�f1�­½#ƒþÉ„l"yÏÈ_�uÐb&=LyZËcõÀÌ ?÷     # Copyright (C) 2012-2026 Zammad Foundation, https://zammad-foundation.org/

require 'rails_helper'
require 'system/examples/pagination_examples'

RSpec.describe 'AI > Provider Connections', type: :system do
  context 'when ajax pagination' do
    include_examples 'pagination', model: :ai_provider_connection, klass: AI::ProviderConnection, path: 'ai/providers'
  end

  context 'with connections', authenticated_as: :admin do
    let(:admin)       { create(:admin) }
    let(:self_hosted) { false }

    # What the providers of these examples answer their model listing request with: OpenAI reports
    # its models under 'data', Ollama under 'models'.
    let(:model_list) do
      {
        'data'   => [{ 'id' => 'gpt-4.1' }, { 'id' => 'text-embedding-3-small' }],
        'models' => [{ 'model' => 'mistral-small3.2' }, { 'model' => 'bge-m3' }],
      }
    end

    before do
      Setting.set('system_online_service', !self_hosted)

      result = UserAgent::Result.new(
        success: true,
        code:    200,
        data:    model_list,
      )

      allow(UserAgent).to receive_messages(get: result, post: result)

      visit '/#ai/providers'
    end

    describe 'creating a connection' do
      it 'creates a connection via the wizard and marks it as default' do
        click '[data-type=new]'

        in_modal disappears: true do
          find('select[name=provider]').select('OpenAI')

          # The name is deliberately not pre-filled from the provider, it is up to the admin.
          expect(page).to have_field('Token').and(have_field('name', with: ''))

          fill_in 'name',  with: 'openai'
          fill_in 'Token', with: 'test-token'

          click_on 'Next'
        end

        in_modal disappears: true do
          # Only the models the provider serves, and only those that fit the field: the embedding
          # model is no candidate to run the connection on. Picking nothing has a consequence, so
          # the empty option names the model that answers for it.
          expect(page)
            .to have_select('config.model', options: ['Default (gpt-4.1)', 'gpt-4.1'])
            .and(have_select('config.ocr_model', options: ['Default (gpt-4.1)', 'gpt-4.1']))
            .and(have_select('config.embedding_model', options: ['Default (text-embedding-3-small)', 'text-embedding-3-small']))

          # A new connection starts on the default rather than on a model it was signed up for -
          # sized like a picked one, because that is the model it will embed with.
          expect(page)
            .to have_select('config.model', selected: 'Default (gpt-4.1)')
            .and(have_select('config.embedding_model', selected: 'Default (text-embedding-3-small)'))
            .and(have_field('Embedding dimensions', with: AI::Provider::EMBEDDING_SIZES['text-embedding-3-small']))

          # The step before this one is what the footer offers to go back to, not the cancel of a
          # single step dialog.
          expect(page).to have_link('Back').and(have_no_link('Cancel & Go Back'))

          find('select[name="config.model"]').select('gpt-4.1')

          # Sizing the picked model needs no further request: the listing carries both values.
          find('select[name="config.embedding_model"]').select('text-embedding-3-small')

          expect(page)
            .to have_field('Embedding dimensions', with: AI::Provider::EMBEDDING_SIZES['text-embedding-3-small'])
            .and(have_field('Context window size', with: AI::Provider::EMBEDDING_INPUT_LIMITS['text-embedding-3-small']))

          click_on 'Submit'
        end

        within :active_content do
          expect(page).to have_text('openai')

          # The firs