192 lines
7.7 KiB
Go
192 lines
7.7 KiB
Go
package feedback_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/feedback"
|
|
)
|
|
|
|
type stubRepo struct {
|
|
calls int
|
|
lastRecord feedback.Record
|
|
result feedback.Result
|
|
err error
|
|
}
|
|
|
|
func (s *stubRepo) RecordFeedback(_ context.Context, record feedback.Record) (feedback.Result, error) {
|
|
s.calls++
|
|
s.lastRecord = record
|
|
return s.result, s.err
|
|
}
|
|
|
|
type sequenceGenerator struct {
|
|
ids []string
|
|
errs map[int]error
|
|
calls int
|
|
}
|
|
|
|
func (g *sequenceGenerator) Generate() (string, error) {
|
|
index := g.calls
|
|
g.calls++
|
|
if err := g.errs[index]; err != nil {
|
|
return "", err
|
|
}
|
|
if index >= len(g.ids) {
|
|
return "", errors.New("no id configured")
|
|
}
|
|
return g.ids[index], nil
|
|
}
|
|
|
|
func TestNewServiceValidation(t *testing.T) {
|
|
repo := &stubRepo{}
|
|
gen := &sequenceGenerator{ids: []string{"a", "b"}, errs: map[int]error{}}
|
|
for _, tc := range []struct {
|
|
name string
|
|
tenant string
|
|
channel string
|
|
repo feedback.Repository
|
|
gens []feedback.IDGenerator
|
|
}{
|
|
{name: "empty tenant", channel: "dingtalk", repo: repo},
|
|
{name: "empty channel", tenant: "tenant", repo: repo},
|
|
{name: "nil repository", tenant: "tenant", channel: "dingtalk"},
|
|
{name: "nil generator", tenant: "tenant", channel: "dingtalk", repo: repo, gens: []feedback.IDGenerator{nil}},
|
|
{name: "too many generators", tenant: "tenant", channel: "dingtalk", repo: repo, gens: []feedback.IDGenerator{gen, gen}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if _, err := feedback.NewService(tc.tenant, tc.channel, tc.repo, tc.gens...); err == nil {
|
|
t.Fatal("expected constructor error")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyUpUsesServerScopeTrimsAndClearsReason(t *testing.T) {
|
|
repo := &stubRepo{result: feedback.Result{Created: true, Value: feedback.ValueUp}}
|
|
gen := &sequenceGenerator{ids: []string{"feedback-id", "actor-id"}, errs: map[int]error{}}
|
|
service, err := feedback.NewService(" tenant-a ", " dingtalk ", repo, gen)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
result, err := service.Apply(context.Background(), feedback.Command{
|
|
ExternalActorID: " user-a ", OutTrackID: " track-a ", Value: feedback.ValueUp,
|
|
ReasonCode: feedback.ReasonIncorrect, Source: feedback.SourceDingTalkCard,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !result.Created || repo.calls != 1 || gen.calls != 2 {
|
|
t.Fatalf("result=%+v repo_calls=%d gen_calls=%d", result, repo.calls, gen.calls)
|
|
}
|
|
got := repo.lastRecord
|
|
if got.ID != "feedback-id" || got.ActorID != "actor-id" || got.TenantKey != "tenant-a" || got.Channel != "dingtalk" || got.ExternalActorID != "user-a" || got.OutTrackID != "track-a" || got.Value != feedback.ValueUp || got.ReasonCode != "" || got.Source != feedback.SourceDingTalkCard {
|
|
t.Fatalf("record=%+v", got)
|
|
}
|
|
}
|
|
|
|
func TestApplyDownAcceptsEveryReasonAndEmpty(t *testing.T) {
|
|
reasons := []string{
|
|
feedback.ReasonEmpty, feedback.ReasonIncorrect, feedback.ReasonNotResolved,
|
|
feedback.ReasonIrrelevant, feedback.ReasonUnclear, feedback.ReasonOutdated, feedback.ReasonOther,
|
|
feedback.ReasonContentMismatch, feedback.ReasonContentIncomplete, feedback.ReasonIllogical,
|
|
feedback.ReasonFactualError, feedback.ReasonSensitiveContent, feedback.ReasonAnswerNotWanted,
|
|
}
|
|
for _, reason := range reasons {
|
|
t.Run("reason_"+reason, func(t *testing.T) {
|
|
repo := &stubRepo{result: feedback.Result{Changed: true, Value: feedback.ValueDown, ReasonCode: reason}}
|
|
gen := &sequenceGenerator{ids: []string{"feedback-id", "actor-id"}, errs: map[int]error{}}
|
|
service, _ := feedback.NewService("tenant", "dingtalk", repo, gen)
|
|
result, err := service.Apply(context.Background(), feedback.Command{
|
|
ExternalActorID: "user", OutTrackID: "track", Value: feedback.ValueDown,
|
|
ReasonCode: " " + reason + " ", Source: feedback.SourceDingTalkCard,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if repo.lastRecord.ReasonCode != reason || result.ReasonCode != reason {
|
|
t.Fatalf("record=%+v result=%+v", repo.lastRecord, result)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyRejectsInvalidCommandsBeforeGeneratingIDs(t *testing.T) {
|
|
tooLong := strings.Repeat("x", 257)
|
|
for _, tc := range []struct {
|
|
name string
|
|
cmd feedback.Command
|
|
}{
|
|
{name: "empty actor", cmd: feedback.Command{OutTrackID: "track", Value: feedback.ValueUp, Source: feedback.SourceDingTalkCard}},
|
|
{name: "empty track", cmd: feedback.Command{ExternalActorID: "user", Value: feedback.ValueUp, Source: feedback.SourceDingTalkCard}},
|
|
{name: "long actor", cmd: feedback.Command{ExternalActorID: tooLong, OutTrackID: "track", Value: feedback.ValueUp, Source: feedback.SourceDingTalkCard}},
|
|
{name: "long track", cmd: feedback.Command{ExternalActorID: "user", OutTrackID: tooLong, Value: feedback.ValueUp, Source: feedback.SourceDingTalkCard}},
|
|
{name: "invalid value", cmd: feedback.Command{ExternalActorID: "user", OutTrackID: "track", Value: "maybe", Source: feedback.SourceDingTalkCard}},
|
|
{name: "invalid source", cmd: feedback.Command{ExternalActorID: "user", OutTrackID: "track", Value: feedback.ValueUp, Source: "web"}},
|
|
{name: "invalid down reason", cmd: feedback.Command{ExternalActorID: "user", OutTrackID: "track", Value: feedback.ValueDown, ReasonCode: "secret", Source: feedback.SourceDingTalkCard}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
repo := &stubRepo{}
|
|
gen := &sequenceGenerator{ids: []string{"feedback-id", "actor-id"}, errs: map[int]error{}}
|
|
service, _ := feedback.NewService("tenant", "dingtalk", repo, gen)
|
|
if _, err := service.Apply(context.Background(), tc.cmd); !errors.Is(err, feedback.ErrInvalidCommand) {
|
|
t.Fatalf("error=%v", err)
|
|
}
|
|
if repo.calls != 0 || gen.calls != 0 {
|
|
t.Fatalf("repo_calls=%d gen_calls=%d", repo.calls, gen.calls)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyIDFailureNeverCallsRepository(t *testing.T) {
|
|
idErr := errors.New("entropy unavailable")
|
|
for _, failureIndex := range []int{0, 1} {
|
|
t.Run(string(rune('0'+failureIndex)), func(t *testing.T) {
|
|
repo := &stubRepo{}
|
|
gen := &sequenceGenerator{ids: []string{"feedback-id", "actor-id"}, errs: map[int]error{failureIndex: idErr}}
|
|
service, _ := feedback.NewService("tenant", "dingtalk", repo, gen)
|
|
_, err := service.Apply(context.Background(), feedback.Command{ExternalActorID: "user", OutTrackID: "track", Value: feedback.ValueUp, Source: feedback.SourceDingTalkCard})
|
|
if !errors.Is(err, idErr) || repo.calls != 0 {
|
|
t.Fatalf("error=%v repo_calls=%d", err, repo.calls)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestApplyPassesThroughRepositoryResultAndError(t *testing.T) {
|
|
repoErr := errors.New("repository failed")
|
|
for _, tc := range []struct {
|
|
name string
|
|
result feedback.Result
|
|
err error
|
|
}{
|
|
{name: "replayed", result: feedback.Result{Replayed: true, Value: feedback.ValueDown, ReasonCode: feedback.ReasonUnclear}},
|
|
{name: "error", err: repoErr},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
repo := &stubRepo{result: tc.result, err: tc.err}
|
|
gen := &sequenceGenerator{ids: []string{"feedback-id", "actor-id"}, errs: map[int]error{}}
|
|
service, _ := feedback.NewService("tenant", "dingtalk", repo, gen)
|
|
result, err := service.Apply(context.Background(), feedback.Command{ExternalActorID: "user", OutTrackID: "track", Value: feedback.ValueDown, ReasonCode: feedback.ReasonUnclear, Source: feedback.SourceDingTalkCard})
|
|
if !errors.Is(err, tc.err) || result != tc.result {
|
|
t.Fatalf("result=%+v error=%v", result, err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRecordCannotBeSerializedIntoExternalJSON(t *testing.T) {
|
|
record := feedback.Record{ID: "feedback-id", TenantKey: "tenant", Channel: "dingtalk", ActorID: "actor-id", ExternalActorID: "user", OutTrackID: "provider-track", Value: feedback.ValueDown, ReasonCode: feedback.ReasonOther, Source: feedback.SourceDingTalkCard}
|
|
encoded, err := json.Marshal(record)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if string(encoded) != "{}" {
|
|
t.Fatalf("internal record must not be JSON-exposable: %s", encoded)
|
|
}
|
|
}
|