package v1 import ( "bytes" "context" "encoding/json" "errors" "fmt" "net" "net/http" "net/http/httptest" "strconv" "testing" "time" "github.com/alicebob/miniredis/v2" "github.com/gin-gonic/gin" "github.com/gochat/gochat/internal/llm" "github.com/gochat/gochat/internal/model" "github.com/gochat/gochat/internal/repository" "github.com/gochat/gochat/internal/service" "github.com/redis/go-redis/v9" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gorm.io/driver/sqlite" "gorm.io/gorm" ) func setupCaptainAssistantHandlerTest(t *testing.T) (*gin.Engine, *gorm.DB) { return setupCaptainAssistantHandlerTestWithSummaryProvider(t, nil) } func setupCaptainAssistantHandlerTestWithSummaryProvider(t *testing.T, provider llm.Provider) (*gin.Engine, *gorm.DB) { t.Helper() gin.SetMode(gin.TestMode) dbName := fmt.Sprintf("file:%s?mode=memory&cache=private", t.Name()) db, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{}) require.NoError(t, err) require.NoError(t, db.AutoMigrate( &model.Account{}, &model.User{}, &model.Inbox{}, &model.Contact{}, &model.Conversation{}, &model.Message{}, &model.ReportingEvent{}, &model.CaptainAssistant{}, &model.CaptainInbox{}, &model.CaptainDocument{}, &model.CaptainAssistantResponse{}, &model.CaptainMessageReport{}, &model.AgentBot{}, &model.AgentBotInbox{}, )) t.Cleanup(func() { sqlDB, _ := db.DB() sqlDB.Close() }) assistantRepo := repository.NewCaptainAssistantRepo(db) inboxRepo := repository.NewCaptainInboxRepo(db) documentRepo := repository.NewCaptainDocumentRepo(db) responseRepo := repository.NewCaptainAssistantResponseRepo(db) svc := service.NewCaptainAssistantService(assistantRepo, inboxRepo, documentRepo, responseRepo, provider) handler := NewCaptainAssistantHandler(svc) router := gin.New() router.Use(func(c *gin.Context) { c.Set("user_id", uint(9)); c.Next() }) assistants := router.Group("/api/v1/accounts/:account_id/captain/assistants") assistants.GET("/", handler.List) assistants.POST("/", handler.Create) assistants.GET("/tools", handler.Tools) assistants.GET("/:assistant_id", handler.Get) assistants.PUT("/:assistant_id", handler.Update) assistants.DELETE("/:assistant_id", handler.Delete) assistants.POST("/:assistant_id/playground", handler.GenerateResponse) assistants.GET("/:assistant_id/inboxes", handler.ListInboxes) assistants.POST("/:assistant_id/inboxes", handler.AssociateInbox) assistants.DELETE("/:assistant_id/inboxes/:inbox_id", handler.DissociateInbox) assistants.GET("/:assistant_id/stats", handler.Stats) assistants.GET("/:assistant_id/summary", handler.Summary) assistants.GET("/:assistant_id/drilldown", handler.Drilldown) router.POST("/api/v1/accounts/:account_id/captain/message_reports", handler.CreateMessageReport) return router, db } func TestCaptainAssistantHandler_OverviewDrilldownAndMessageReportContracts(t *testing.T) { router, db := setupCaptainAssistantHandlerTestWithSummaryProvider(t, &captainPlaygroundFakeProvider{content: "Captain performance is improving."}) account := seedCaptainAssistantAccount(t, db, "Captain Reports") assistant := &model.CaptainAssistant{AccountID: account.ID, Name: "Fin", Description: "Support", Config: json.RawMessage(`{}`), Status: model.AssistantStatusActive} require.NoError(t, db.Create(assistant).Error) inbox := &model.Inbox{AccountID: account.ID, Name: "Support", ChannelType: "api", ChannelID: 1} require.NoError(t, db.Create(inbox).Error) contact := &model.Contact{AccountID: account.ID, Name: "Ada"} require.NoError(t, db.Create(contact).Error) displayID := uint(42) conversation := &model.Conversation{AccountID: account.ID, InboxID: inbox.ID, ContactID: contact.ID, DisplayID: &displayID, Status: "open", ChannelType: "api", Channel: "api"} require.NoError(t, db.Create(conversation).Error) message := &model.Message{Base: model.Base{CreatedAt: time.Now().Add(-time.Hour)}, AccountID: account.ID, InboxID: inbox.ID, ConversationID: conversation.ID, SenderID: &assistant.ID, SenderType: "Captain::Assistant", Content: "Answer", ContentType: "text", MessageType: "outgoing"} require.NoError(t, db.Create(message).Error) require.NoError(t, db.Create(&model.ReportingEvent{AccountID: account.ID, Name: "conversation_captain_inference_resolved", ConversationID: &conversation.ID, EventStartTime: time.Now().Add(-time.Hour), EventEndTime: time.Now().Add(-30 * time.Minute)}).Error) base := fmt.Sprintf("/api/v1/accounts/%d/captain/assistants/%d", account.ID, assistant.ID) w := captainAssistantJSONRequest(t, router, http.MethodGet, base+"/stats?range=7&timezone_offset=0", nil) require.Equal(t, http.StatusOK, w.Code, w.Body.String()) stats := map[string]any{} require.NoError(t, json.Unmarshal(w.Body.Bytes(), &stats)) for _, key := range []string{"conversations_handled", "auto_resolution_rate", "handoff_rate", "hours_saved", "reopen_rate", "conversation_depth", "knowledge"} { assert.Contains(t, stats, key) } w = captainAssistantJSONRequest(t, router, http.MethodGet, base+"/drilldown?metric=conversations_handled&range=7", nil) require.Equal(t, http.StatusOK, w.Code, w.Body.String()) drilldown := map[string]any{} require.NoError(t, json.Unmarshal(w.Body.Bytes(), &drilldown)) assert.Contains(t, drilldown, "meta") assert.Len(t, drilldown["payload"], 1) w = captainAssistantJSONRequest(t, router, http.MethodGet, base+"/drilldown?metric=hours_saved&range=7", nil) require.Equal(t, http.StatusUnprocessableEntity, w.Code) w = captainAssistantJSONRequest(t, router, http.MethodGet, base+"/summary?range=7", nil) require.Equal(t, http.StatusOK, w.Code, w.Body.String()) summary := map[string]any{} require.NoError(t, json.Unmarshal(w.Body.Bytes(), &summary)) assert.NotEmpty(t, summary["message"]) w = captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/captain/message_reports", account.ID), map[string]any{"message_id": message.ID, "report_reason": "incorrect_information", "description": "Wrong"}) require.Equal(t, http.StatusOK, w.Code, w.Body.String()) report := map[string]any{} require.NoError(t, json.Unmarshal(w.Body.Bytes(), &report)) assert.Equal(t, "incorrect_information", report["report_reason"]) assert.Equal(t, float64(message.ID), report["message_id"]) human := &model.Message{AccountID: account.ID, InboxID: inbox.ID, ConversationID: conversation.ID, SenderType: "User", Content: "Human", ContentType: "text", MessageType: "outgoing"} require.NoError(t, db.Create(human).Error) w = captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/captain/message_reports", account.ID), map[string]any{"message_id": human.ID, "report_reason": "other"}) require.Equal(t, http.StatusUnprocessableEntity, w.Code) otherAccount := seedCaptainAssistantAccount(t, db, "Other") w = captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/captain/message_reports", otherAccount.ID), map[string]any{"message_id": message.ID, "report_reason": "other"}) require.Equal(t, http.StatusNotFound, w.Code) } func TestCaptainAssistantSummaryCachesOnlySuccessfulLLMResponses(t *testing.T) { _, db := setupCaptainAssistantHandlerTest(t) account := seedCaptainAssistantAccount(t, db, "Captain Summary") user := &model.User{Name: "Ada Lovelace", Email: "ada-summary@example.com"} require.NoError(t, db.Create(user).Error) assistant := &model.CaptainAssistant{AccountID: account.ID, Name: "Fin", Status: model.AssistantStatusActive} require.NoError(t, db.Create(assistant).Error) provider := &captainPlaygroundFakeProvider{content: "Ada, Captain performance is improving."} mini := miniredis.RunT(t) cache := redis.NewClient(&redis.Options{Addr: mini.Addr()}) t.Cleanup(func() { _ = cache.Close() }) svc := service.NewCaptainAssistantService(repository.NewCaptainAssistantRepo(db), repository.NewCaptainInboxRepo(db), repository.NewCaptainDocumentRepo(db), repository.NewCaptainAssistantResponseRepo(db), provider, cache) first, err := svc.Summary(context.Background(), account.ID, assistant.ID, user.ID, "7", 0) require.NoError(t, err) second, err := svc.Summary(context.Background(), account.ID, assistant.ID, user.ID, "7", 0) require.NoError(t, err) require.Equal(t, first, second) require.Equal(t, 1, provider.calls) failing := service.NewCaptainAssistantService(repository.NewCaptainAssistantRepo(db), repository.NewCaptainInboxRepo(db), repository.NewCaptainDocumentRepo(db), repository.NewCaptainAssistantResponseRepo(db), &captainPlaygroundFakeProvider{err: errors.New("provider unavailable")}) _, err = failing.Summary(context.Background(), account.ID, assistant.ID, user.ID, "30", 0) require.ErrorContains(t, err, "provider unavailable") } func TestCaptainAssistantStatsAndDrilldownUseExactResolvedReopenCohort(t *testing.T) { _, db := setupCaptainAssistantHandlerTest(t) account := seedCaptainAssistantAccount(t, db, "Captain Cohort") assistant := &model.CaptainAssistant{AccountID: account.ID, Name: "Fin", Status: model.AssistantStatusActive} require.NoError(t, db.Create(assistant).Error) inbox := &model.Inbox{AccountID: account.ID, Name: "Support", ChannelType: "api", ChannelID: 1} require.NoError(t, db.Create(inbox).Error) contact := &model.Contact{AccountID: account.ID, Name: "Ada"} require.NoError(t, db.Create(contact).Error) now := time.Now().UTC() conversations := make([]model.Conversation, 2) for i := range conversations { conversations[i] = model.Conversation{AccountID: account.ID, InboxID: inbox.ID, ContactID: contact.ID, Status: "open", ChannelType: "api", Channel: "api"} require.NoError(t, db.Create(&conversations[i]).Error) require.NoError(t, db.Create(&model.Message{Base: model.Base{CreatedAt: now.Add(-4 * time.Hour)}, AccountID: account.ID, InboxID: inbox.ID, ConversationID: conversations[i].ID, SenderID: &assistant.ID, SenderType: "Captain::Assistant", MessageType: "outgoing"}).Error) } seedEvent := func(conversationID uint, name string, createdAt, endAt time.Time, value float64) { require.NoError(t, db.Create(&model.ReportingEvent{Base: model.Base{CreatedAt: createdAt}, AccountID: account.ID, ConversationID: &conversationID, Name: name, Value: value, EventEndTime: endAt}).Error) } seedEvent(conversations[0].ID, "conversation_bot_resolved", now.Add(-3*time.Hour), now.Add(-3*time.Hour), 0) seedEvent(conversations[0].ID, "conversation_bot_handoff", now.Add(-2*time.Hour), now.Add(-2*time.Hour), 0) seedEvent(conversations[0].ID, "conversation_opened", now.Add(-time.Hour), now.Add(-time.Hour), 1) seedEvent(conversations[1].ID, "conversation_captain_inference_resolved", now.Add(-3*time.Hour), now.Add(-3*time.Hour), 0) seedEvent(conversations[1].ID, "conversation_bot_handoff", now.Add(-2*time.Hour), now.Add(-2*time.Hour), 0) seedEvent(conversations[1].ID, "conversation_opened", now.Add(-4*time.Hour), now.Add(-4*time.Hour), 1) seedEvent(conversations[1].ID, "conversation_opened", now.Add(-time.Hour), now.Add(-time.Hour), 1) svc := service.NewCaptainAssistantService(repository.NewCaptainAssistantRepo(db), repository.NewCaptainInboxRepo(db), repository.NewCaptainDocumentRepo(db), repository.NewCaptainAssistantResponseRepo(db), nil) stats, err := svc.Stats(context.Background(), account.ID, assistant.ID, "7", 0) require.NoError(t, err) require.Equal(t, 50.0, stats.AutoResolutionRate.Current) require.Equal(t, 100.0, stats.ReopenRate.Current) for _, metric := range []string{"auto_resolution_rate", "reopen_rate"} { result, drilldownErr := svc.Drilldown(context.Background(), account.ID, assistant.ID, service.CaptainDrilldownParams{Metric: metric, Range: "7"}) require.NoError(t, drilldownErr) require.Len(t, result.Payload, 1) require.Equal(t, conversations[1].ID, result.Payload[0]["conversation"].(map[string]any)["id"]) } } func setupCaptainAssistantHandlerTestWithProvider(t *testing.T, provider llm.Provider) (*gin.Engine, *gorm.DB) { t.Helper() gin.SetMode(gin.TestMode) dbName := fmt.Sprintf("file:%s-provider?mode=memory&cache=private", t.Name()) db, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{}) require.NoError(t, err) require.NoError(t, db.AutoMigrate( &model.Account{}, &model.Inbox{}, &model.CaptainAssistant{}, &model.CaptainInbox{}, &model.CaptainDocument{}, &model.CaptainAssistantResponse{}, )) t.Cleanup(func() { sqlDB, _ := db.DB() sqlDB.Close() }) assistantRepo := repository.NewCaptainAssistantRepo(db) inboxRepo := repository.NewCaptainInboxRepo(db) documentRepo := repository.NewCaptainDocumentRepo(db) responseRepo := repository.NewCaptainAssistantResponseRepo(db) svc := service.NewCaptainAssistantService(assistantRepo, inboxRepo, documentRepo, responseRepo, provider) handler := NewCaptainAssistantHandler(svc) router := gin.New() assistants := router.Group("/api/v1/accounts/:account_id/captain/assistants") assistants.POST("/:assistant_id/playground", handler.GenerateResponse) return router, db } func seedCaptainAssistantAccount(t *testing.T, db *gorm.DB, name string) *model.Account { t.Helper() account := &model.Account{Name: name, Locale: "en", Active: true} require.NoError(t, db.Create(account).Error) return account } func captainAssistantJSONRequest(t *testing.T, router *gin.Engine, method, path string, body any) *httptest.ResponseRecorder { t.Helper() var reader *bytes.Reader if body == nil { reader = bytes.NewReader(nil) } else { payload, err := json.Marshal(body) require.NoError(t, err) reader = bytes.NewReader(payload) } w := httptest.NewRecorder() req := httptest.NewRequest(method, path, reader) req.Header.Set("Content-Type", "application/json") router.ServeHTTP(w, req) return w } func TestCaptainAssistantHandler_CRUDUsesChatwootPayloadShape(t *testing.T) { router, db := setupCaptainAssistantHandlerTest(t) account := seedCaptainAssistantAccount(t, db, "Captain Org") basePath := "/api/v1/accounts/" + strconv.FormatUint(uint64(account.ID), 10) + "/captain/assistants" createBody := map[string]any{ "assistant": map[string]any{ "name": "Fin", "description": "Support copilot", "config": map[string]any{ "product_name": "GoChat", "temperature": 0.2, }, "guardrails": []string{"never ask for passwords"}, "response_guidelines": []string{"be concise"}, }, } w := captainAssistantJSONRequest(t, router, http.MethodPost, basePath+"/", createBody) assert.Equal(t, http.StatusOK, w.Code) var created map[string]any require.NoError(t, json.Unmarshal(w.Body.Bytes(), &created)) assert.NotContains(t, created, "success") assert.NotContains(t, created, "data") assert.Equal(t, float64(account.ID), created["account_id"]) assert.Equal(t, "Fin", created["name"]) assert.Equal(t, "Support copilot", created["description"]) assert.Equal(t, "GoChat", created["config"].(map[string]any)["product_name"]) assistantID := uint(created["id"].(float64)) w = captainAssistantJSONRequest(t, router, http.MethodGet, basePath+"/", nil) assert.Equal(t, http.StatusOK, w.Code) var listResp map[string]any require.NoError(t, json.Unmarshal(w.Body.Bytes(), &listResp)) assert.Len(t, listResp["payload"], 1) assert.Equal(t, float64(1), listResp["meta"].(map[string]any)["total_count"]) assert.Equal(t, float64(1), listResp["meta"].(map[string]any)["page"]) updateBody := map[string]any{"assistant": map[string]any{"name": "Fin Prime", "description": "Updated"}} w = captainAssistantJSONRequest(t, router, http.MethodPut, fmt.Sprintf("%s/%d", basePath, assistantID), updateBody) assert.Equal(t, http.StatusOK, w.Code) var updated map[string]any require.NoError(t, json.Unmarshal(w.Body.Bytes(), &updated)) assert.Equal(t, "Fin Prime", updated["name"]) assert.Equal(t, "Updated", updated["description"]) w = captainAssistantJSONRequest(t, router, http.MethodGet, fmt.Sprintf("%s/%d", basePath, assistantID), nil) assert.Equal(t, http.StatusOK, w.Code) w = captainAssistantJSONRequest(t, router, http.MethodDelete, fmt.Sprintf("%s/%d", basePath, assistantID), nil) assert.Equal(t, http.StatusNoContent, w.Code) } func TestCaptainAssistantHandler_AccountScopedShowAndInboxBinding(t *testing.T) { router, db := setupCaptainAssistantHandlerTest(t) account := seedCaptainAssistantAccount(t, db, "Account One") otherAccount := seedCaptainAssistantAccount(t, db, "Account Two") assistant := &model.CaptainAssistant{AccountID: account.ID, Name: "Fin", Description: "Support", Config: json.RawMessage(`{}`), Status: model.AssistantStatusActive} require.NoError(t, db.Create(assistant).Error) inbox := &model.Inbox{AccountID: account.ID, Name: "Primary", ChannelType: "web_widget"} require.NoError(t, db.Create(inbox).Error) otherInbox := &model.Inbox{AccountID: otherAccount.ID, Name: "Other", ChannelType: "web_widget"} require.NoError(t, db.Create(otherInbox).Error) basePath := "/api/v1/accounts/" + strconv.FormatUint(uint64(account.ID), 10) + "/captain/assistants" otherBasePath := "/api/v1/accounts/" + strconv.FormatUint(uint64(otherAccount.ID), 10) + "/captain/assistants" w := captainAssistantJSONRequest(t, router, http.MethodGet, fmt.Sprintf("%s/%d", otherBasePath, assistant.ID), nil) assert.Equal(t, http.StatusNotFound, w.Code) w = captainAssistantJSONRequest(t, router, http.MethodGet, basePath+"/tools", nil) assert.Equal(t, http.StatusOK, w.Code) var tools []map[string]any require.NoError(t, json.Unmarshal(w.Body.Bytes(), &tools)) assert.Len(t, tools, 7) assert.Equal(t, "add_contact_note", tools[0]["id"]) bindBody := map[string]any{"inbox": map[string]any{"inbox_id": inbox.ID}} w = captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("%s/%d/inboxes", basePath, assistant.ID), bindBody) assert.Equal(t, http.StatusOK, w.Code) var bound map[string]any require.NoError(t, json.Unmarshal(w.Body.Bytes(), &bound)) assert.Equal(t, float64(inbox.ID), bound["id"]) assert.NotContains(t, bound, "success") w = captainAssistantJSONRequest(t, router, http.MethodGet, fmt.Sprintf("%s/%d/inboxes", basePath, assistant.ID), nil) assert.Equal(t, http.StatusOK, w.Code) var inboxList map[string]any require.NoError(t, json.Unmarshal(w.Body.Bytes(), &inboxList)) assert.Len(t, inboxList["payload"], 1) wrongBindBody := map[string]any{"inbox": map[string]any{"inbox_id": otherInbox.ID}} w = captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("%s/%d/inboxes", basePath, assistant.ID), wrongBindBody) assert.Equal(t, http.StatusNotFound, w.Code) w = captainAssistantJSONRequest(t, router, http.MethodDelete, fmt.Sprintf("%s/%d/inboxes/%d", otherBasePath, assistant.ID, inbox.ID), nil) assert.Equal(t, http.StatusNotFound, w.Code) w = captainAssistantJSONRequest(t, router, http.MethodDelete, fmt.Sprintf("%s/%d/inboxes/%d", basePath, assistant.ID, inbox.ID), nil) assert.Equal(t, http.StatusNoContent, w.Code) } func TestCaptainAssistantHandler_PlaygroundProviderMissingFailsClosed(t *testing.T) { router, db := setupCaptainAssistantHandlerTest(t) account := seedCaptainAssistantAccount(t, db, "Captain Org") 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", "message_history": []map[string]any{ {"role": "user", "content": "Previous message"}, {"role": "assistant", "content": "Previous response", "agent_name": "billing_scenario"}, }, } 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.StatusServiceUnavailable, w.Code) assert.Contains(t, w.Body.String(), "COPILOT_NOT_CONFIGURED") } func TestCaptainAssistantHandler_PlaygroundDefaultsHistoryAndScopesAccount(t *testing.T) { router, db := setupCaptainAssistantHandlerTestWithProvider(t, &captainPlaygroundFakeProvider{content: "Assistant response"}) account := seedCaptainAssistantAccount(t, db, "Account One") otherAccount := seedCaptainAssistantAccount(t, db, "Account Two") assistant := &model.CaptainAssistant{AccountID: account.ID, Name: "Fin", Description: "Support", Config: json.RawMessage(`{}`), 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) w = captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/captain/assistants/%d/playground", otherAccount.ID, assistant.ID), body) assert.Equal(t, http.StatusNotFound, w.Code) } func TestCaptainAssistantHandler_PlaygroundV2AppendsCurrentMessageOnce(t *testing.T) { provider := &captainPlaygroundFakeProvider{content: "Assistant response"} 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","temperature":0.2}`), Status: model.AssistantStatusActive} require.NoError(t, db.Create(assistant).Error) body := map[string]any{ "message_content": "Hello assistant", "message_history": []map[string]any{ {"role": "user", "content": "Previous message"}, {"role": "assistant", "content": "Previous response", "agent_name": "billing_scenario"}, }, } 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, "Assistant response", payload["response"]) assert.NotContains(t, payload, "content") require.Len(t, provider.lastRequest.Messages, 4) assert.Equal(t, "Previous message", provider.lastRequest.Messages[1].Content) assert.Equal(t, "Previous response", provider.lastRequest.Messages[2].Content) assert.Equal(t, "Hello assistant", provider.lastRequest.Messages[3].Content) body = map[string]any{ "message_content": "Hello assistant", "message_history": []map[string]any{{"role": "user", "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) require.Len(t, provider.lastRequest.Messages, 2) assert.Equal(t, "Hello assistant", provider.lastRequest.Messages[1].Content) } func TestCaptainAssistantHandler_PlaygroundV2ProviderErrorFailsClosed(t *testing.T) { provider := &captainPlaygroundFakeProvider{err: &net.OpError{Op: "dial", Net: "tcp", Err: errors.New("connection refused")}} 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.StatusBadGateway, w.Code) assert.Contains(t, w.Body.String(), "COPILOT_PROVIDER_UNREACHABLE") } func TestCaptainAssistantHandler_PlaygroundEmbeddingFailureFailsClosed(t *testing.T) { provider := &captainPlaygroundFakeProvider{embeddingErr: &net.OpError{Op: "dial", Net: "tcp", Err: errors.New("connection refused")}} router, db := setupCaptainAssistantHandlerTestWithProvider(t, provider) account := seedCaptainAssistantAccount(t, db, "Captain Org") assistant := &model.CaptainAssistant{AccountID: account.ID, Name: "Fin", Config: json.RawMessage(`{"feature_faq":true}`), Status: model.AssistantStatusActive} require.NoError(t, db.Create(assistant).Error) w := captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/captain/assistants/%d/playground", account.ID, assistant.ID), map[string]any{"message_content": "Hello assistant"}) assert.Equal(t, http.StatusBadGateway, w.Code) assert.Contains(t, w.Body.String(), "COPILOT_PROVIDER_UNREACHABLE") assert.Zero(t, provider.calls) } func TestCaptainAssistantHandler_PlaygroundProviderTimeoutFailsClosed(t *testing.T) { provider := &captainPlaygroundFakeProvider{embeddingErr: &net.OpError{Op: "read", Net: "tcp", Err: context.DeadlineExceeded}} router, db := setupCaptainAssistantHandlerTestWithProvider(t, provider) account := seedCaptainAssistantAccount(t, db, "Captain Org") assistant := &model.CaptainAssistant{AccountID: account.ID, Name: "Fin", Status: model.AssistantStatusActive} require.NoError(t, db.Create(assistant).Error) w := captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/captain/assistants/%d/playground", account.ID, assistant.ID), map[string]any{"message_content": "Hello assistant"}) assert.Equal(t, http.StatusGatewayTimeout, w.Code) assert.JSONEq(t, `{"success":false,"error":{"code":"COPILOT_PROVIDER_TIMEOUT","message":"Copilot provider request timed out"}}`, w.Body.String()) assert.Equal(t, 1, provider.embeddingCalls) assert.Zero(t, provider.calls) } func TestCaptainAssistantHandler_PlaygroundEmptyEmbeddingFailsClosed(t *testing.T) { for name, embeddingResponse := range map[string]*llm.EmbeddingResponse{ "nil response": nil, "empty data": {}, "empty first vector": {Data: []llm.EmbeddingData{{}}}, } { t.Run(name, func(t *testing.T) { provider := &captainPlaygroundFakeProvider{embeddingResponse: embeddingResponse, embeddingResponseSet: true} router, db := setupCaptainAssistantHandlerTestWithProvider(t, provider) account := seedCaptainAssistantAccount(t, db, "Captain Org") assistant := &model.CaptainAssistant{AccountID: account.ID, Name: "Fin", Config: json.RawMessage(`{"feature_faq":true}`), Status: model.AssistantStatusActive} require.NoError(t, db.Create(assistant).Error) w := captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/captain/assistants/%d/playground", account.ID, assistant.ID), map[string]any{"message_content": "Hello assistant"}) assert.Equal(t, http.StatusBadGateway, w.Code) assert.Contains(t, w.Body.String(), "COPILOT_PROVIDER_UNREACHABLE") assert.Zero(t, provider.calls) assert.Equal(t, 1, provider.embeddingCalls) }) } } func TestCaptainAssistantHandler_PlaygroundFAQStoreFailureFailsClosed(t *testing.T) { provider := &captainPlaygroundFakeProvider{embeddingResponse: &llm.EmbeddingResponse{Data: []llm.EmbeddingData{{Embedding: []float64{0.1, 0.2, 0.3}}}}} router, db := setupCaptainAssistantHandlerTestWithProvider(t, provider) account := seedCaptainAssistantAccount(t, db, "Captain Org") assistant := &model.CaptainAssistant{AccountID: account.ID, Name: "Fin", Config: json.RawMessage(`{"feature_faq":true}`), Status: model.AssistantStatusActive} require.NoError(t, db.Create(assistant).Error) require.NoError(t, db.Create(&model.CaptainAssistantResponse{ AccountID: account.ID, AssistantID: assistant.ID, Question: "FAQ", Answer: "Answer", Status: model.ResponseStatusApproved, }).Error) require.NoError(t, db.Migrator().DropTable(&model.CaptainAssistantResponse{})) w := captainAssistantJSONRequest(t, router, http.MethodPost, fmt.Sprintf("/api/v1/accounts/%d/captain/assistants/%d/playground", account.ID, assistant.ID), map[string]any{"message_content": "Hello assistant"}) assert.Equal(t, http.StatusBadGateway, w.Code) assert.Contains(t, w.Body.String(), "COPILOT_PROVIDER_UNREACHABLE") assert.Zero(t, provider.calls) } func TestCaptainAssistantHandler_PlaygroundDisabledAssistantFailsClosed(t *testing.T) { router, db := setupCaptainAssistantHandlerTestWithProvider(t, &captainPlaygroundFakeProvider{content: "must not run"}) account := seedCaptainAssistantAccount(t, db, "Captain Org") assistant := &model.CaptainAssistant{AccountID: account.ID, Name: "Fin", Status: model.AssistantStatusArchived} 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.StatusConflict, w.Code) assert.Contains(t, w.Body.String(), "CAPTAIN_ASSISTANT_DISABLED") } type captainPlaygroundFakeProvider struct { content string err error embeddingResponse *llm.EmbeddingResponse embeddingResponseSet bool embeddingErr error embeddingCalls int calls int lastRequest llm.ChatRequest } func (p *captainPlaygroundFakeProvider) ChatCompletion(ctx context.Context, req llm.ChatRequest) (*llm.ChatResponse, error) { p.calls++ 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 } func (p *captainPlaygroundFakeProvider) CreateEmbedding(ctx context.Context, req llm.EmbeddingRequest) (*llm.EmbeddingResponse, error) { p.embeddingCalls++ if p.embeddingErr != nil || p.embeddingResponseSet || p.embeddingResponse != nil { return p.embeddingResponse, p.embeddingErr } return &llm.EmbeddingResponse{Data: []llm.EmbeddingData{{Embedding: []float64{0.1, 0.2, 0.3}}}}, nil } func (p *captainPlaygroundFakeProvider) ChatCompletionStream(ctx context.Context, req llm.ChatRequest, onChunk func(llm.StreamChunk) error) error { return nil }