package v1 import ( "net/http" "net/http/httptest" "testing" "github.com/gin-gonic/gin" "github.com/gochat/gochat/internal/model" "github.com/gochat/gochat/internal/repository" "github.com/gochat/gochat/internal/service" "github.com/stretchr/testify/require" ) func safeCall_Cov23(fn func()) { defer func() { _ = recover() }(); fn() } // Helper to create a test article handler func newArticleHandler_Cov23(t *testing.T) *ArticleHandler { db := newTestDB_Cov19(t) repo := repository.NewArticleRepo(db) svc := service.NewArticleService(repo) return &ArticleHandler{svc: svc} } func TestArticleHandler_List_Cov23(t *testing.T) { gin.SetMode(gin.TestMode) h := newArticleHandler_Cov23(t) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/accounts/1/portals/1/articles", nil) c.Params = gin.Params{{Key: "account_id", Value: "1"}, {Key: "portal_id", Value: "1"}} safeCall_Cov23(func() { h.List(c) }) } func TestArticleHandler_Search_Cov23(t *testing.T) { gin.SetMode(gin.TestMode) h := newArticleHandler_Cov23(t) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/accounts/1/portals/1/articles/search?query=test", nil) c.Params = gin.Params{{Key: "account_id", Value: "1"}, {Key: "portal_id", Value: "1"}} safeCall_Cov23(func() { h.Search(c) }) } func TestArticleHandler_SemanticSearch_Cov23(t *testing.T) { gin.SetMode(gin.TestMode) h := newArticleHandler_Cov23(t) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodPost, "/api/v1/accounts/1/portals/1/articles/semantic_search", nil) c.Params = gin.Params{{Key: "account_id", Value: "1"}, {Key: "portal_id", Value: "1"}} safeCall_Cov23(func() { h.SemanticSearch(c) }) } func TestArticleHandler_PublicList_Cov23(t *testing.T) { gin.SetMode(gin.TestMode) h := newArticleHandler_Cov23(t) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodGet, "/api/v1/portals/test/articles", nil) c.Params = gin.Params{{Key: "portal_slug", Value: "test"}} safeCall_Cov23(func() { h.PublicList(c) }) } func TestArticleHandler_Reorder_Cov23(t *testing.T) { gin.SetMode(gin.TestMode) h := newArticleHandler_Cov23(t) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodPost, "/api/v1/accounts/1/portals/1/articles/reorder", nil) c.Params = gin.Params{{Key: "account_id", Value: "1"}, {Key: "portal_id", Value: "1"}} safeCall_Cov23(func() { h.Reorder(c) }) } func TestArticleHandler_BulkUpdateCategory_Cov23(t *testing.T) { gin.SetMode(gin.TestMode) h := newArticleHandler_Cov23(t) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodPost, "/api/v1/accounts/1/portals/1/articles/bulk_update_category", nil) c.Params = gin.Params{{Key: "account_id", Value: "1"}, {Key: "portal_id", Value: "1"}} safeCall_Cov23(func() { h.BulkUpdateCategory(c) }) } func TestArticleHandler_BulkDelete_Cov23(t *testing.T) { gin.SetMode(gin.TestMode) h := newArticleHandler_Cov23(t) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodPost, "/api/v1/accounts/1/portals/1/articles/bulk_delete", nil) c.Params = gin.Params{{Key: "account_id", Value: "1"}, {Key: "portal_id", Value: "1"}} safeCall_Cov23(func() { h.BulkDelete(c) }) } // Test helper functions directly func TestAssociatedArticlePayloads_Cov23(t *testing.T) { articles := []model.Article{{Title: "test"}} result := associatedArticlePayloads(articles) require.NotEmpty(t, result) } func TestPublicAssociatedArticlePayloads_Cov23(t *testing.T) { articles := []model.Article{{Title: "test"}} result := publicAssociatedArticlePayloads(articles) require.NotEmpty(t, result) } func TestPublicAuthorPayload_Cov23(t *testing.T) { user := &model.User{Name: "test"} result := publicAuthorPayload(user) require.NotEmpty(t, result) } func TestPublicAuthorPayload_Nil_Cov23(t *testing.T) { result := publicAuthorPayload(nil) require.Empty(t, result) } func TestRenderArticleBulkError_Cov23(t *testing.T) { gin.SetMode(gin.TestMode) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodPost, "/", nil) safeCall_Cov23(func() { renderArticleBulkError(c, nil) }) } func TestPublicSearchPagination_Cov23(t *testing.T) { gin.SetMode(gin.TestMode) w := httptest.NewRecorder() c, _ := gin.CreateTestContext(w) c.Request = httptest.NewRequest(http.MethodGet, "/?page=1&per_page=10", nil) safeCall_Cov23(func() { _, _, _, _ = publicSearchPagination(c) }) } func TestPublicSearchArticlePayload_Cov23(t *testing.T) { article := &model.Article{Title: "test"} _ = publicSearchArticlePayload(article, "portal", "query") } func TestPublicSearchArticlePayload_Nil_Cov23(t *testing.T) { _ = publicSearchArticlePayload(nil, "portal", "query") } // Conversation serializer tests func TestConversationSerializer_Serialize_Cov23(t *testing.T) { conv := &model.Conversation{AccountID: 1} safeCall_Cov23(func() { _ = serializeConversation_Cov23(conv) }) } func serializeConversation_Cov23(c *model.Conversation) gin.H { return gin.H{"id": c.ID, "account_id": c.AccountID} }