80 lines
3.5 KiB
Go
80 lines
3.5 KiB
Go
package dobotaiops
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
func qaRecordsFixture(t *testing.T, pool *pgxpool.Pool) string {
|
|
t.Helper()
|
|
dsn := buildFixture(t, pool)
|
|
execFixture(t, pool,
|
|
`ALTER TABLE messages ADD COLUMN actor_name_snapshot text NOT NULL DEFAULT '', ADD COLUMN trigger_person_snapshot text NOT NULL DEFAULT '', ADD COLUMN media_json jsonb NOT NULL DEFAULT '[]'`,
|
|
`DROP TABLE IF EXISTS answer_feedback`,
|
|
`CREATE TABLE answer_feedback(id text PRIMARY KEY,tenant_key text,channel text,turn_id text,answer_message_id text,actor_id text,value text)`,
|
|
`GRANT SELECT ON answer_feedback TO kg_ro_fixture`,
|
|
`UPDATE messages SET actor_name_snapshot='测试人员',trigger_person_snapshot='回调昵称' WHERE id='msg-req-3'`,
|
|
`UPDATE turns SET answer='根据引用,当前暂无已发布的权威知识,暂不建议操作。' WHERE id='turn-3'`,
|
|
`INSERT INTO answer_feedback VALUES ('fb1','default','dingtalk','turn-3','msg-ans-3','actor-row-1','up'),('fb2','default','dingtalk','turn-3','msg-ans-3','another-person','down'),('fb3','other-tenant','dingtalk','turn-3','msg-ans-3','actor-row-1','down')`,
|
|
)
|
|
return dsn
|
|
}
|
|
|
|
func TestQARecordSearchPreservesSourceAndDoesNotMultiplyFeedback(t *testing.T) {
|
|
pool := fixturePool(t)
|
|
client := buildReadOnlyClient(t, qaRecordsFixture(t, pool))
|
|
window := Window{TenantKey: "default", Start: time.Date(2026, 10, 14, 0, 0, 0, 0, time.UTC), End: time.Date(2026, 10, 15, 0, 0, 0, 0, time.UTC)}
|
|
query := RecordQuery{Window: window, Keyword: "第三", Limit: 20}
|
|
page, err := client.ListRecords(context.Background(), query)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if page.Total != 1 || len(page.Items) != 1 {
|
|
t.Fatalf("feedback multiplied turns: total=%d rows=%d", page.Total, len(page.Items))
|
|
}
|
|
item := page.Items[0]
|
|
if item.ID != "turn-3" || item.Question != "第三问(正文)" || item.Asker != "测试人员" || item.TriggerPerson != "回调昵称" {
|
|
t.Fatalf("explicit field mapping failed: %+v", item)
|
|
}
|
|
if item.Coverage.Verdict != "uncovered" || item.SourceCoverage.Verdict != "covered" {
|
|
t.Fatalf("governance must override reference-prioritized source without rewriting it: %+v", item)
|
|
}
|
|
if item.Feedback.AskerValue != "up" || item.Feedback.Up != 1 || item.Feedback.Down != 1 {
|
|
t.Fatalf("asker and aggregate feedback were conflated: %+v", item.Feedback)
|
|
}
|
|
query.Keyword = "%"
|
|
page, err = client.ListRecords(context.Background(), query)
|
|
if err != nil || page.Total != 0 {
|
|
t.Fatalf("keyword wildcard must be literal: total=%d err=%v", page.Total, err)
|
|
}
|
|
query.Keyword = ""
|
|
query.Limit = 1
|
|
page, err = client.ListRecords(context.Background(), query)
|
|
if err != nil || page.Total != 4 || len(page.Items) != 1 || !page.HasMore || page.NextCursor == "" {
|
|
t.Fatalf("invalid first page: %+v %v", page, err)
|
|
}
|
|
firstID := page.Items[0].ID
|
|
foundFeedback := false
|
|
for _, column := range page.Columns {
|
|
if column == "feedback" {
|
|
foundFeedback = true
|
|
}
|
|
}
|
|
if !foundFeedback {
|
|
t.Fatal("a populated column elsewhere in the filtered result must not be hidden by an empty first page")
|
|
}
|
|
query.Cursor = page.NextCursor
|
|
next, err := client.ListRecords(context.Background(), query)
|
|
if err != nil || len(next.Items) != 1 || next.Items[0].ID == firstID {
|
|
t.Fatalf("invalid cursor page: %+v %v", next, err)
|
|
}
|
|
query.Keyword = "changed"
|
|
if _, err := client.ListRecords(context.Background(), query); !errors.Is(err, ErrInvalidQuery) {
|
|
t.Fatalf("cursor must be scoped to its filters: %v", err)
|
|
}
|
|
}
|