Align GoChat with Chatwoot frontend contracts
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -292,13 +293,40 @@ func TestCaptainAssistantHandler_PlaygroundV2AppendsCurrentMessageOnce(t *testin
|
||||
assert.Equal(t, "Hello assistant", provider.lastRequest.Messages[1].Content)
|
||||
}
|
||||
|
||||
func TestCaptainAssistantHandler_PlaygroundV2ProviderErrorReturnsChatwootFallback(t *testing.T) {
|
||||
provider := &captainPlaygroundFakeProvider{err: errors.New("provider unavailable")}
|
||||
router, db := setupCaptainAssistantHandlerTestWithProvider(t, provider)
|
||||
account := seedCaptainAssistantAccount(t, db, "Captain Org")
|
||||
account.FeatureFlags = `{"captain_integration_v2":true}`
|
||||
require.NoError(t, db.Save(account).Error)
|
||||
assistant := &model.CaptainAssistant{AccountID: account.ID, Name: "Fin", Description: "Support", Config: json.RawMessage(`{"model":"gpt-test"}`), Status: model.AssistantStatusActive}
|
||||
require.NoError(t, db.Create(assistant).Error)
|
||||
|
||||
body := map[string]any{"message_content": "Hello assistant"}
|
||||
w := captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/captain/assistants/%d/playground", account.ID, assistant.ID), body)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
var payload map[string]any
|
||||
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &payload))
|
||||
assert.Equal(t, "conversation_handoff", payload["response"])
|
||||
assert.Equal(t, false, payload["handoff_tool_called"])
|
||||
assert.Contains(t, payload["reasoning"], "Error occurred: llm generation failed: provider unavailable")
|
||||
assert.NotContains(t, payload, "content")
|
||||
assert.NotContains(t, payload, "success")
|
||||
assert.NotContains(t, payload, "data")
|
||||
}
|
||||
|
||||
type captainPlaygroundFakeProvider struct {
|
||||
content string
|
||||
err error
|
||||
lastRequest llm.ChatRequest
|
||||
}
|
||||
|
||||
func (p *captainPlaygroundFakeProvider) ChatCompletion(ctx context.Context, req llm.ChatRequest) (*llm.ChatResponse, error) {
|
||||
p.lastRequest = req
|
||||
if p.err != nil {
|
||||
return nil, p.err
|
||||
}
|
||||
return &llm.ChatResponse{Choices: []llm.ChatChoice{{Message: llm.ChatMessage{Role: "assistant", Content: p.content}}}}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user