test: cover captain reopen rate, range whitelist, and summary caching
This commit is contained in:
@@ -252,6 +252,48 @@ RSpec.describe 'Api::V1::Accounts::Captain::Assistants', type: :request do
|
||||
end
|
||||
end
|
||||
|
||||
describe 'GET /api/v1/accounts/{account.id}/captain/assistants/{id}/summary' do
|
||||
let(:assistant) { create(:captain_assistant, account: account) }
|
||||
let(:alice) { create(:user, account: account, role: :administrator, name: 'Alice Adams') }
|
||||
let(:bob) { create(:user, account: account, role: :administrator, name: 'Bob Brown') }
|
||||
let(:summary_service) { instance_double(Captain::OverviewSummaryService) }
|
||||
|
||||
def get_summary(user)
|
||||
get "/api/v1/accounts/#{account.id}/captain/assistants/#{assistant.id}/summary",
|
||||
params: { range: '30' },
|
||||
headers: user.create_new_auth_token,
|
||||
as: :json
|
||||
end
|
||||
|
||||
before do
|
||||
# Test env uses a null store; swap in a real store so caching behaviour is observable.
|
||||
allow(Rails).to receive(:cache).and_return(ActiveSupport::Cache::MemoryStore.new)
|
||||
allow(Captain::OverviewSummaryService).to receive(:new).and_return(summary_service)
|
||||
end
|
||||
|
||||
it 'caches the summary per viewer so one user never receives another user\'s greeting' do
|
||||
allow(summary_service).to receive(:perform).and_return({ message: 'Hi Alice' })
|
||||
|
||||
get_summary(alice)
|
||||
get_summary(alice) # served from Alice's cache, no regeneration
|
||||
get_summary(bob) # distinct cache key, regenerated for Bob
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(Captain::OverviewSummaryService).to have_received(:new).twice
|
||||
end
|
||||
|
||||
it 'does not cache failures so a transient error is retried' do
|
||||
allow(summary_service).to receive(:perform).and_return({ error: 'LLM unavailable' })
|
||||
|
||||
get_summary(alice)
|
||||
get_summary(alice)
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_content)
|
||||
expect(json_response[:error]).to eq('LLM unavailable')
|
||||
expect(Captain::OverviewSummaryService).to have_received(:new).twice
|
||||
end
|
||||
end
|
||||
|
||||
describe 'POST /api/v1/accounts/{account.id}/captain/assistants/{id}/playground' do
|
||||
let(:assistant) { create(:captain_assistant, account: account) }
|
||||
let(:valid_params) do
|
||||
|
||||
Reference in New Issue
Block a user