feat: add tenant-scoped audit query API
This commit is contained in:
@@ -0,0 +1,179 @@
|
||||
package audit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type stubRepo struct {
|
||||
listFn func(context.Context, string, ListQuery) ([]TurnSummary, error)
|
||||
getFn func(context.Context, string, string) (TurnDetail, error)
|
||||
}
|
||||
|
||||
func (r *stubRepo) ListTurns(ctx context.Context, tenant string, query ListQuery) ([]TurnSummary, error) {
|
||||
if r.listFn == nil {
|
||||
return nil, nil
|
||||
}
|
||||
return r.listFn(ctx, tenant, query)
|
||||
}
|
||||
|
||||
func (r *stubRepo) GetTurn(ctx context.Context, tenant, id string) (TurnDetail, error) {
|
||||
if r.getFn == nil {
|
||||
return TurnDetail{}, ErrNotFound
|
||||
}
|
||||
return r.getFn(ctx, tenant, id)
|
||||
}
|
||||
|
||||
func TestNewServiceRequiresTenantAndRepository(t *testing.T) {
|
||||
repo := &stubRepo{}
|
||||
if _, err := NewService("", repo); !errors.Is(err, ErrInvalidArgument) {
|
||||
t.Fatalf("empty tenant error=%v", err)
|
||||
}
|
||||
if _, err := NewService("tenant", nil); !errors.Is(err, ErrInvalidArgument) {
|
||||
t.Fatalf("nil repository error=%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceListUsesServerTenantDefaultLimitAndFetchesExtra(t *testing.T) {
|
||||
var gotTenant string
|
||||
var gotQuery ListQuery
|
||||
repo := &stubRepo{listFn: func(_ context.Context, tenant string, query ListQuery) ([]TurnSummary, error) {
|
||||
gotTenant, gotQuery = tenant, query
|
||||
return []TurnSummary{}, nil
|
||||
}}
|
||||
svc, err := NewService(" tenant-a ", repo)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
page, err := svc.ListTurns(context.Background(), ListTurnsRequest{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if gotTenant != "tenant-a" || gotQuery.Limit != 21 {
|
||||
t.Fatalf("tenant=%q query=%+v", gotTenant, gotQuery)
|
||||
}
|
||||
if page.Items == nil || page.NextCursor != "" {
|
||||
t.Fatalf("page=%+v", page)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceListBuildsStableNextCursor(t *testing.T) {
|
||||
t3 := time.Date(2026, 7, 21, 3, 0, 0, 0, time.UTC)
|
||||
t2 := t3.Add(-time.Minute)
|
||||
t1 := t2.Add(-time.Minute)
|
||||
repo := &stubRepo{listFn: func(_ context.Context, _ string, query ListQuery) ([]TurnSummary, error) {
|
||||
if query.Limit != 3 {
|
||||
t.Fatalf("repository limit=%d want=3", query.Limit)
|
||||
}
|
||||
return []TurnSummary{{ID: "turn-3", CreatedAt: t3}, {ID: "turn-2", CreatedAt: t2}, {ID: "turn-1", CreatedAt: t1}}, nil
|
||||
}}
|
||||
svc, _ := NewService("tenant", repo)
|
||||
page, err := svc.ListTurns(context.Background(), ListTurnsRequest{Limit: 2})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page.Items) != 2 || page.Items[1].ID != "turn-2" || page.NextCursor == "" {
|
||||
t.Fatalf("page=%+v", page)
|
||||
}
|
||||
decoded, err := decodeCursor(page.NextCursor)
|
||||
if err != nil || decoded.ID != "turn-2" || !decoded.CreatedAt.Equal(t2) {
|
||||
t.Fatalf("next cursor=%+v err=%v", decoded, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceListDecodesCursorForRepository(t *testing.T) {
|
||||
created := time.Date(2026, 7, 21, 2, 0, 0, 123, time.UTC)
|
||||
cursor := encodeCursor(created, "turn-before")
|
||||
repo := &stubRepo{listFn: func(_ context.Context, _ string, query ListQuery) ([]TurnSummary, error) {
|
||||
if query.BeforeID != "turn-before" || query.BeforeCreatedAt == nil || !query.BeforeCreatedAt.Equal(created) {
|
||||
t.Fatalf("decoded query=%+v", query)
|
||||
}
|
||||
return nil, nil
|
||||
}}
|
||||
svc, _ := NewService("tenant", repo)
|
||||
if _, err := svc.ListTurns(context.Background(), ListTurnsRequest{Cursor: cursor}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceListValidation(t *testing.T) {
|
||||
svc, _ := NewService("tenant", &stubRepo{})
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
req ListTurnsRequest
|
||||
}{
|
||||
{name: "negative limit", req: ListTurnsRequest{Limit: -1}},
|
||||
{name: "too large limit", req: ListTurnsRequest{Limit: 101}},
|
||||
{name: "bad status", req: ListTurnsRequest{Status: "bogus"}},
|
||||
{name: "long channel", req: ListTurnsRequest{Channel: strings.Repeat("x", 129)}},
|
||||
{name: "bad cursor", req: ListTurnsRequest{Cursor: "not-a-cursor"}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if _, err := svc.ListTurns(context.Background(), tc.req); !errors.Is(err, ErrInvalidArgument) {
|
||||
t.Fatalf("error=%v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceListAcceptsMaximumLimitAndFilters(t *testing.T) {
|
||||
repo := &stubRepo{listFn: func(_ context.Context, _ string, query ListQuery) ([]TurnSummary, error) {
|
||||
if query.Limit != 101 || query.Channel != "dingtalk" || query.Status != StatusSucceeded {
|
||||
t.Fatalf("query=%+v", query)
|
||||
}
|
||||
return nil, nil
|
||||
}}
|
||||
svc, _ := NewService("tenant", repo)
|
||||
if _, err := svc.ListTurns(context.Background(), ListTurnsRequest{Limit: 100, Channel: " dingtalk ", Status: StatusSucceeded}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceListTruncatesUnicodeSummariesTo200Runes(t *testing.T) {
|
||||
long := strings.Repeat("初", 250)
|
||||
repo := &stubRepo{listFn: func(context.Context, string, ListQuery) ([]TurnSummary, error) {
|
||||
return []TurnSummary{{ID: "turn", CreatedAt: time.Now(), QuestionSummary: long, AnswerSummary: long}}, nil
|
||||
}}
|
||||
svc, _ := NewService("tenant", repo)
|
||||
page, err := svc.ListTurns(context.Background(), ListTurnsRequest{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len([]rune(page.Items[0].QuestionSummary)) != 200 || len([]rune(page.Items[0].AnswerSummary)) != 200 {
|
||||
t.Fatalf("summary lengths=%d/%d", len([]rune(page.Items[0].QuestionSummary)), len([]rune(page.Items[0].AnswerSummary)))
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceGetUsesServerTenant(t *testing.T) {
|
||||
var gotTenant, gotID string
|
||||
repo := &stubRepo{getFn: func(_ context.Context, tenant, id string) (TurnDetail, error) {
|
||||
gotTenant, gotID = tenant, id
|
||||
return TurnDetail{ID: id}, nil
|
||||
}}
|
||||
svc, _ := NewService("tenant-a", repo)
|
||||
if _, err := svc.GetTurn(context.Background(), " turn-1 "); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if gotTenant != "tenant-a" || gotID != "turn-1" {
|
||||
t.Fatalf("tenant=%q id=%q", gotTenant, gotID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServiceGetValidationAndNotFound(t *testing.T) {
|
||||
repo := &stubRepo{getFn: func(context.Context, string, string) (TurnDetail, error) {
|
||||
return TurnDetail{}, ErrNotFound
|
||||
}}
|
||||
svc, _ := NewService("tenant", repo)
|
||||
if _, err := svc.GetTurn(context.Background(), " "); !errors.Is(err, ErrInvalidArgument) {
|
||||
t.Fatalf("blank id error=%v", err)
|
||||
}
|
||||
if _, err := svc.GetTurn(context.Background(), strings.Repeat("x", 129)); !errors.Is(err, ErrInvalidArgument) {
|
||||
t.Fatalf("long id error=%v", err)
|
||||
}
|
||||
if _, err := svc.GetTurn(context.Background(), "missing"); !errors.Is(err, ErrNotFound) {
|
||||
t.Fatalf("missing error=%v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user