package integration_test import ( "context" "encoding/json" "fmt" "net/http" "testing" "time" "github.com/google/uuid" "golang.org/x/crypto/bcrypt" ) func TestQualityFeedback(t *testing.T) { r, pool := setupAPIPool(t) ctx := context.Background() tok := adminLogin(t, r, "admin", "change-me") _, code := doAdminJSON(t, r, http.MethodGet, "/api/v1/admin/ask/feedback", nil, "") if code != http.StatusUnauthorized { t.Fatalf("expected 401, got %d", code) } limitedRoleID := uuid.New() _, err := pool.Exec(ctx, ` INSERT INTO admin_roles(id, name, system) VALUES ($1,$2,false)`, limitedRoleID, "qf_lim_"+limitedRoleID.String()[:8]) if err != nil { t.Fatal(err) } _, err = pool.Exec(ctx, ` INSERT INTO admin_role_permissions(role_id, code) VALUES ($1,'admin.ask.read')`, limitedRoleID) if err != nil { t.Fatal(err) } hash, err := bcrypt.GenerateFromPassword([]byte("limited-pass"), bcrypt.DefaultCost) if err != nil { t.Fatal(err) } limUser := fmt.Sprintf("qflim_%d", time.Now().UnixNano()) _, err = pool.Exec(ctx, ` INSERT INTO admin_accounts(username, password_hash, role_id) VALUES ($1,$2,$3)`, limUser, string(hash), limitedRoleID) if err != nil { t.Fatal(err) } t.Cleanup(func() { _, _ = pool.Exec(ctx, `DELETE FROM admin_accounts WHERE username=$1`, limUser) _, _ = pool.Exec(ctx, `DELETE FROM admin_roles WHERE id=$1`, limitedRoleID) }) limTok := adminLogin(t, r, limUser, "limited-pass") key := mustRegister(t, r) env, key := doJSON(t, r, http.MethodPost, "/api/v1/profiles", map[string]any{ "relation": "self", "birth_date": "1993-03-03", "display_name": "评", }, key) profileID := decodeData[map[string]any](t, env.Data)["id"].(string) env, key = doJSON(t, r, http.MethodPost, "/api/v1/ask/threads", map[string]any{ "profile_id": profileID, "scene": "self", }, key) threadID := decodeData[map[string]any](t, env.Data)["id"].(string) _, key = doJSON(t, r, http.MethodPost, "/api/v1/ask/threads/"+threadID+"/messages", map[string]any{ "content": "打分测试", }, key) _, code = doAdminJSON(t, r, http.MethodPost, "/api/v1/admin/ask/threads/"+threadID+"/feedback", map[string]any{"rating": 4, "tag": "helpful"}, limTok) if code != http.StatusForbidden { t.Fatalf("expected 403 without feedback.write, got %d", code) } _, code = doAdminJSON(t, r, http.MethodPost, "/api/v1/admin/ask/threads/"+threadID+"/feedback", map[string]any{"rating": 9}, tok) if code != http.StatusBadRequest { t.Fatalf("expected 400 bad rating, got %d", code) } env, code = doAdminJSON(t, r, http.MethodPost, "/api/v1/admin/ask/threads/"+threadID+"/feedback", map[string]any{"rating": 5, "tag": "helpful", "note": "ops ok"}, tok) if code != 200 { t.Fatalf("admin feedback http=%d msg=%s", code, env.Message) } start := time.Now() env, code = doAdminJSON(t, r, http.MethodGet, "/api/v1/admin/ask/feedback", nil, tok) if code != 200 || time.Since(start) > 500*time.Millisecond { t.Fatalf("list http=%d dur=%v", code, time.Since(start)) } var list struct { Items []struct { ThreadID string `json:"thread_id"` Source string `json:"source"` Rating int `json:"rating"` } `json:"items"` } _ = json.Unmarshal(env.Data, &list) found := false for _, it := range list.Items { if it.ThreadID == threadID && it.Source == "admin" && it.Rating == 5 { found = true break } } if !found { t.Fatalf("admin feedback missing: %#v", list.Items) } env, _, httpCode := doJSONExpect(t, r, http.MethodPost, "/api/v1/ask/threads/"+threadID+"/feedback", map[string]any{"rating": 3, "tag": "other"}, key, 0) if httpCode != 200 { t.Fatalf("user feedback http=%d body=%s", httpCode, env.Data) } env, code = doAdminJSON(t, r, http.MethodGet, "/api/v1/admin/audit-logs", nil, tok) if code != 200 { t.Fatalf("audit %d", code) } var audit struct { Items []struct { Action string `json:"action"` } `json:"items"` } _ = json.Unmarshal(env.Data, &audit) okAudit := false for _, it := range audit.Items { if it.Action == "ask.feedback.create" { okAudit = true break } } if !okAudit { t.Fatal("missing ask.feedback.create audit") } // cross-thread message_id must be rejected env, key = doJSON(t, r, http.MethodPost, "/api/v1/ask/threads", map[string]any{ "profile_id": profileID, "scene": "self", }, key) otherThread := decodeData[map[string]any](t, env.Data)["id"].(string) env, key = doJSON(t, r, http.MethodPost, "/api/v1/ask/threads/"+otherThread+"/messages", map[string]any{ "content": "另一线程", }, key) // fetch a message id from other thread via admin transcript env, code = doAdminJSON(t, r, http.MethodGet, "/api/v1/admin/ask/threads/"+otherThread+"/messages", nil, tok) if code != 200 { t.Fatalf("other transcript http=%d", code) } var otherDetail struct { Messages []struct { ID string `json:"id"` } `json:"messages"` } _ = json.Unmarshal(env.Data, &otherDetail) if len(otherDetail.Messages) == 0 { t.Fatal("expected messages on other thread") } foreignMsg := otherDetail.Messages[0].ID _, code = doAdminJSON(t, r, http.MethodPost, "/api/v1/admin/ask/threads/"+threadID+"/feedback", map[string]any{"rating": 2, "tag": "other", "message_id": foreignMsg}, tok) if code != http.StatusBadRequest { t.Fatalf("expected 400 for foreign message_id, got %d", code) } _ = key }