feat: add global config for captain settings (#13141)
Co-authored-by: aakashb95 <aakashbakhle@gmail.com> Co-authored-by: Aakash Bakhle <48802744+aakashb95@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe 'Api::V1::Accounts::Captain::Preferences', type: :request do
|
||||
let(:account) { create(:account) }
|
||||
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
|
||||
def json_response
|
||||
JSON.parse(response.body, symbolize_names: true)
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/captain/preferences' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
get "/api/v1/accounts/#{account.id}/captain/preferences",
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an agent' do
|
||||
it 'returns captain config' do
|
||||
get "/api/v1/accounts/#{account.id}/captain/preferences",
|
||||
headers: agent.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(json_response).to have_key(:providers)
|
||||
expect(json_response).to have_key(:models)
|
||||
expect(json_response).to have_key(:features)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an admin' do
|
||||
it 'returns captain config' do
|
||||
get "/api/v1/accounts/#{account.id}/captain/preferences",
|
||||
headers: admin.create_new_auth_token,
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(json_response).to have_key(:providers)
|
||||
expect(json_response).to have_key(:models)
|
||||
expect(json_response).to have_key(:features)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'PUT /api/v1/accounts/{account.id}/captain/preferences' do
|
||||
context 'when it is an unauthenticated user' do
|
||||
it 'returns unauthorized' do
|
||||
put "/api/v1/accounts/#{account.id}/captain/preferences",
|
||||
params: { captain_models: { editor: 'gpt-4.1-mini' } },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an agent' do
|
||||
it 'returns forbidden' do
|
||||
put "/api/v1/accounts/#{account.id}/captain/preferences",
|
||||
headers: agent.create_new_auth_token,
|
||||
params: { captain_models: { editor: 'gpt-4.1-mini' } },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:unauthorized)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when it is an admin' do
|
||||
it 'updates captain_models' do
|
||||
put "/api/v1/accounts/#{account.id}/captain/preferences",
|
||||
headers: admin.create_new_auth_token,
|
||||
params: { captain_models: { editor: 'gpt-4.1-mini' } },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(json_response).to have_key(:providers)
|
||||
expect(json_response).to have_key(:models)
|
||||
expect(json_response).to have_key(:features)
|
||||
expect(account.reload.captain_models['editor']).to eq('gpt-4.1-mini')
|
||||
end
|
||||
|
||||
it 'updates captain_features' do
|
||||
put "/api/v1/accounts/#{account.id}/captain/preferences",
|
||||
headers: admin.create_new_auth_token,
|
||||
params: { captain_features: { editor: true } },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(json_response).to have_key(:providers)
|
||||
expect(json_response).to have_key(:models)
|
||||
expect(json_response).to have_key(:features)
|
||||
expect(account.reload.captain_features['editor']).to be true
|
||||
end
|
||||
|
||||
it 'merges with existing captain_models' do
|
||||
account.update!(captain_models: { 'editor' => 'gpt-4.1-mini', 'assistant' => 'gpt-5.1' })
|
||||
|
||||
put "/api/v1/accounts/#{account.id}/captain/preferences",
|
||||
headers: admin.create_new_auth_token,
|
||||
params: { captain_models: { editor: 'gpt-4.1' } },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(json_response).to have_key(:providers)
|
||||
expect(json_response).to have_key(:models)
|
||||
expect(json_response).to have_key(:features)
|
||||
models = account.reload.captain_models
|
||||
expect(models['editor']).to eq('gpt-4.1')
|
||||
expect(models['assistant']).to eq('gpt-5.1') # Preserved
|
||||
end
|
||||
|
||||
it 'merges with existing captain_features' do
|
||||
account.update!(captain_features: { 'editor' => true, 'assistant' => false })
|
||||
|
||||
put "/api/v1/accounts/#{account.id}/captain/preferences",
|
||||
headers: admin.create_new_auth_token,
|
||||
params: { captain_features: { editor: false } },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(json_response).to have_key(:providers)
|
||||
expect(json_response).to have_key(:models)
|
||||
expect(json_response).to have_key(:features)
|
||||
features = account.reload.captain_features
|
||||
expect(features['editor']).to be false
|
||||
expect(features['assistant']).to be false # Preserved
|
||||
end
|
||||
|
||||
it 'updates both models and features in single request' do
|
||||
put "/api/v1/accounts/#{account.id}/captain/preferences",
|
||||
headers: admin.create_new_auth_token,
|
||||
params: {
|
||||
captain_models: { editor: 'gpt-4.1-mini' },
|
||||
captain_features: { editor: true }
|
||||
},
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(json_response).to have_key(:providers)
|
||||
expect(json_response).to have_key(:models)
|
||||
expect(json_response).to have_key(:features)
|
||||
account.reload
|
||||
expect(account.captain_models['editor']).to eq('gpt-4.1-mini')
|
||||
expect(account.captain_features['editor']).to be true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -218,4 +218,59 @@ RSpec.describe Account do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'captain_preferences' do
|
||||
let(:account) { create(:account) }
|
||||
|
||||
describe 'with no saved preferences' do
|
||||
it 'returns defaults from llm.yml' do
|
||||
prefs = account.captain_preferences
|
||||
|
||||
expect(prefs[:features].values).to all(be false)
|
||||
|
||||
Llm::Models.feature_keys.each do |feature|
|
||||
expect(prefs[:models][feature]).to eq(Llm::Models.default_model_for(feature))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with saved model preferences' do
|
||||
it 'returns saved preferences merged with defaults' do
|
||||
account.update!(captain_models: { 'editor' => 'gpt-4.1-mini', 'assistant' => 'gpt-5.2' })
|
||||
|
||||
prefs = account.captain_preferences
|
||||
|
||||
expect(prefs[:models]['editor']).to eq('gpt-4.1-mini')
|
||||
expect(prefs[:models]['assistant']).to eq('gpt-5.2')
|
||||
expect(prefs[:models]['copilot']).to eq(Llm::Models.default_model_for('copilot'))
|
||||
end
|
||||
end
|
||||
|
||||
describe 'with saved feature preferences' do
|
||||
it 'returns saved feature states' do
|
||||
account.update!(captain_features: { 'editor' => true, 'assistant' => true })
|
||||
|
||||
prefs = account.captain_preferences
|
||||
|
||||
expect(prefs[:features]['editor']).to be true
|
||||
expect(prefs[:features]['assistant']).to be true
|
||||
expect(prefs[:features]['copilot']).to be false
|
||||
end
|
||||
end
|
||||
|
||||
describe 'validation' do
|
||||
it 'rejects invalid model for a feature' do
|
||||
account.captain_models = { 'label_suggestion' => 'gpt-5.1' }
|
||||
|
||||
expect(account).not_to be_valid
|
||||
expect(account.errors[:captain_models].first).to include('not a valid model for label_suggestion')
|
||||
end
|
||||
|
||||
it 'accepts valid model for a feature' do
|
||||
account.captain_models = { 'editor' => 'gpt-4.1-mini', 'label_suggestion' => 'gpt-4.1-nano' }
|
||||
|
||||
expect(account).to be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
134
spec/models/concerns/captain_featurable_spec.rb
Normal file
134
spec/models/concerns/captain_featurable_spec.rb
Normal file
@@ -0,0 +1,134 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe CaptainFeaturable do
|
||||
let(:account) { create(:account) }
|
||||
|
||||
describe 'dynamic method generation' do
|
||||
it 'generates enabled? methods for all features' do
|
||||
Llm::Models.feature_keys.each do |feature_key|
|
||||
expect(account).to respond_to("captain_#{feature_key}_enabled?")
|
||||
end
|
||||
end
|
||||
|
||||
it 'generates model accessor methods for all features' do
|
||||
Llm::Models.feature_keys.each do |feature_key|
|
||||
expect(account).to respond_to("captain_#{feature_key}_model")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'feature enabled methods' do
|
||||
context 'when no features are explicitly enabled' do
|
||||
it 'returns false for all features' do
|
||||
Llm::Models.feature_keys.each do |feature_key|
|
||||
expect(account.send("captain_#{feature_key}_enabled?")).to be false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when features are explicitly enabled' do
|
||||
before do
|
||||
account.update!(captain_features: { 'editor' => true, 'assistant' => true })
|
||||
end
|
||||
|
||||
it 'returns true for enabled features' do
|
||||
expect(account.captain_editor_enabled?).to be true
|
||||
expect(account.captain_assistant_enabled?).to be true
|
||||
end
|
||||
|
||||
it 'returns false for disabled features' do
|
||||
expect(account.captain_copilot_enabled?).to be false
|
||||
expect(account.captain_label_suggestion_enabled?).to be false
|
||||
end
|
||||
end
|
||||
|
||||
context 'when captain_features is nil' do
|
||||
before do
|
||||
account.update!(captain_features: nil)
|
||||
end
|
||||
|
||||
it 'returns false for all features' do
|
||||
Llm::Models.feature_keys.each do |feature_key|
|
||||
expect(account.send("captain_#{feature_key}_enabled?")).to be false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'model accessor methods' do
|
||||
context 'when no models are explicitly configured' do
|
||||
it 'returns default models for all features' do
|
||||
Llm::Models.feature_keys.each do |feature_key|
|
||||
expected_default = Llm::Models.default_model_for(feature_key)
|
||||
expect(account.send("captain_#{feature_key}_model")).to eq(expected_default)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when models are explicitly configured' do
|
||||
before do
|
||||
account.update!(captain_models: {
|
||||
'editor' => 'gpt-4.1-mini',
|
||||
'assistant' => 'gpt-5.1',
|
||||
'label_suggestion' => 'gpt-4.1-nano'
|
||||
})
|
||||
end
|
||||
|
||||
it 'returns configured models for configured features' do
|
||||
expect(account.captain_editor_model).to eq('gpt-4.1-mini')
|
||||
expect(account.captain_assistant_model).to eq('gpt-5.1')
|
||||
expect(account.captain_label_suggestion_model).to eq('gpt-4.1-nano')
|
||||
end
|
||||
|
||||
it 'returns default models for unconfigured features' do
|
||||
expect(account.captain_copilot_model).to eq(Llm::Models.default_model_for('copilot'))
|
||||
expect(account.captain_audio_transcription_model).to eq(Llm::Models.default_model_for('audio_transcription'))
|
||||
end
|
||||
end
|
||||
|
||||
context 'when configured with invalid model' do
|
||||
before do
|
||||
account.captain_models = { 'editor' => 'invalid-model' }
|
||||
end
|
||||
|
||||
it 'falls back to default model' do
|
||||
expect(account.captain_editor_model).to eq(Llm::Models.default_model_for('editor'))
|
||||
end
|
||||
end
|
||||
|
||||
context 'when captain_models is nil' do
|
||||
before do
|
||||
account.update!(captain_models: nil)
|
||||
end
|
||||
|
||||
it 'returns default models for all features' do
|
||||
Llm::Models.feature_keys.each do |feature_key|
|
||||
expected_default = Llm::Models.default_model_for(feature_key)
|
||||
expect(account.send("captain_#{feature_key}_model")).to eq(expected_default)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe 'integration with existing captain_preferences' do
|
||||
it 'enabled? methods use the same logic as captain_preferences[:features]' do
|
||||
account.update!(captain_features: { 'editor' => true, 'copilot' => true })
|
||||
prefs = account.captain_preferences
|
||||
|
||||
Llm::Models.feature_keys.each do |feature_key|
|
||||
expect(account.send("captain_#{feature_key}_enabled?")).to eq(prefs[:features][feature_key])
|
||||
end
|
||||
end
|
||||
|
||||
it 'model methods use the same logic as captain_preferences[:models]' do
|
||||
account.update!(captain_models: { 'editor' => 'gpt-4.1-mini', 'assistant' => 'gpt-5.2' })
|
||||
prefs = account.captain_preferences
|
||||
|
||||
Llm::Models.feature_keys.each do |feature_key|
|
||||
expect(account.send("captain_#{feature_key}_model")).to eq(prefs[:models][feature_key])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user