87 lines
2.6 KiB
Go
87 lines
2.6 KiB
Go
package usecase
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"testing"
|
|
|
|
"apps/backend/internal/module/radar/domain"
|
|
"apps/backend/internal/module/radar/repository"
|
|
)
|
|
|
|
type fakeHealth struct{ level, advice string }
|
|
|
|
func (f fakeHealth) WorstLevel(context.Context, int64) (string, string, error) {
|
|
return f.level, f.advice, nil
|
|
}
|
|
|
|
func TestMarkReplyUsed_ManualCopyAndThrottle(t *testing.T) {
|
|
ctx := context.Background()
|
|
mem := repository.NewMemory()
|
|
svc := New(mem)
|
|
uid := int64(3)
|
|
now := domain.NowNano()
|
|
|
|
o := &domain.Opportunity{
|
|
ID: "o1", OwnerUID: uid, ExternalID: "e1", Permalink: "https://x/e1",
|
|
AuthorHandle: "a", Text: "需要幫忙", PostedAt: now, Status: domain.OppQualified,
|
|
IntentScore: 80, IntentBand: domain.BandHigh,
|
|
Reasons: []domain.OpportunityReason{
|
|
{Dimension: domain.DimAuthenticity, Score: 20, Reason: "a"},
|
|
{Dimension: domain.DimIntent, Score: 20, Reason: "i"},
|
|
{Dimension: domain.DimRegion, Score: 10, Reason: "r"},
|
|
{Dimension: domain.DimFreshness, Score: 15, Reason: "f"},
|
|
{Dimension: domain.DimFit, Score: 15, Reason: "fit"},
|
|
},
|
|
RegionMatch: domain.RegionUnknown, MatchedTerms: []string{"x"}, CreatedAt: now, UpdatedAt: now,
|
|
}
|
|
if _, err := mem.UpsertByExternalID(ctx, o); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
pub := &domain.ReplyVariant{
|
|
ID: "r1", OwnerUID: uid, OpportunityID: "o1", Variant: domain.ReplyPublicComment,
|
|
Text: "嗨", CreatedAt: now,
|
|
}
|
|
dm := &domain.ReplyVariant{
|
|
ID: "r2", OwnerUID: uid, OpportunityID: "o1", Variant: domain.ReplyDM,
|
|
Text: "私訊", CreatedAt: now,
|
|
}
|
|
if err := mem.SaveReply(ctx, pub); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := mem.SaveReply(ctx, dm); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
got, _, err := svc.MarkReplyUsed(ctx, uid, "o1", "r1", domain.SentManualCopy)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got.UsedAt == 0 || got.SentChannel != domain.SentManualCopy {
|
|
t.Fatalf("manual mark: %+v", got)
|
|
}
|
|
|
|
if _, _, err := svc.MarkReplyUsed(ctx, uid, "o1", "r2", domain.SentOutbox); err == nil {
|
|
t.Fatal("dm outbox should fail")
|
|
}
|
|
|
|
svc.Health = fakeHealth{level: "throttle", advice: "慢一點"}
|
|
_, _, err = svc.MarkReplyUsed(ctx, uid, "o1", "r1", domain.SentOutbox)
|
|
// r1 already used; still exercise throttle on a fresh reply
|
|
pub2 := &domain.ReplyVariant{
|
|
ID: "r3", OwnerUID: uid, OpportunityID: "o1", Variant: domain.ReplyPublicComment,
|
|
Text: "再一則", CreatedAt: now,
|
|
}
|
|
_ = mem.SaveReply(ctx, pub2)
|
|
_, advice, err := svc.MarkReplyUsed(ctx, uid, "o1", "r3", domain.SentOutbox)
|
|
if err == nil {
|
|
t.Fatal("expected throttle block")
|
|
}
|
|
if !strings.Contains(err.Error(), "throttle") {
|
|
t.Fatalf("err = %v", err)
|
|
}
|
|
if advice != "慢一點" {
|
|
t.Fatalf("advice = %q", advice)
|
|
}
|
|
}
|