test(chat): cover project groups in the conversation list API
个人仓库的 main 已包含会话项目分组统计。此次补充 HTTP 接口回归测试,覆盖社区在官方 v1.8.0 反馈的历史会话显示问题。 测试通过真实 SQLite 数据库请求默认工作区和指定项目第二页,检查当前页的会话、分页总数及侧栏所需的 projectGroups。用例包含搜索不匹配和当前用户无权查看的会话,确保两者都不计入分组数量。
This commit is contained in:
parent
1f6cf47e88
commit
550331e8e3
|
|
@ -3,6 +3,7 @@ package handler
|
|||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
|
|
@ -116,3 +117,67 @@ func performConversationRequest(user *database.RBACUser, method, path string, bo
|
|||
handler(c)
|
||||
return w
|
||||
}
|
||||
|
||||
func TestListConversationsProjectGroups(t *testing.T) {
|
||||
db, user := setupConversationRBACTest(t)
|
||||
project, err := db.CreateProject(&database.Project{Name: "Project history"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.AssignResourceToUser(user.ID, "project", project.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
unbound, err := db.CreateConversation("history default", database.ConversationCreateMeta{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.SetResourceOwner("conversation", unbound.ID, user.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, title := range []string{"history first", "history second", "unrelated"} {
|
||||
if _, err := db.CreateConversation(title, database.ConversationCreateMeta{ProjectID: project.ID}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err := db.CreateConversation("history private", database.ConversationCreateMeta{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
handler := NewConversationHandler(db, zap.NewNop())
|
||||
for _, tt := range []struct {
|
||||
name, filter, wantProject string
|
||||
offset, wantTotal int
|
||||
}{
|
||||
{"default workspace", database.ProjectFilterUnbound, "", 0, 1},
|
||||
{"named project second page", project.ID, project.ID, 1, 2},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
path := fmt.Sprintf("/api/conversations?group_by_project=1&search=history&limit=1&offset=%d&project_id=%s", tt.offset, tt.filter)
|
||||
w := performConversationRequest(user, http.MethodGet, path, nil, handler.ListConversations)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
var response struct {
|
||||
Conversations []database.Conversation
|
||||
Total int
|
||||
ProjectGroups []struct {
|
||||
ProjectID string
|
||||
Total int
|
||||
}
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if response.Total != tt.wantTotal || len(response.Conversations) != 1 || response.Conversations[0].ProjectID != tt.wantProject {
|
||||
t.Fatalf("unexpected project page: %s", w.Body.String())
|
||||
}
|
||||
totals := map[string]int{}
|
||||
for _, group := range response.ProjectGroups {
|
||||
totals[group.ProjectID] = group.Total
|
||||
}
|
||||
if len(response.ProjectGroups) != 2 || totals[""] != 1 || totals[project.ID] != 2 {
|
||||
t.Fatalf("projectGroups = %#v, want default workspace (1) and named project (2)", response.ProjectGroups)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue