159 lines
4.7 KiB
Go
159 lines
4.7 KiB
Go
// blacklist_test.go S4·C2:上行自动拉黑——词表匹配/SystemConfig 可配/
|
|
// 落库幂等/Redis 缓存即时生效。
|
|
package uplink
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
|
|
"go-sms/internal/model/entity"
|
|
"go-sms/internal/service/interceptor"
|
|
|
|
"github.com/alicebob/miniredis/v2"
|
|
"github.com/redis/go-redis/v9"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func newBlacklistDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
db := newTestDB(t)
|
|
if err := db.AutoMigrate(&entity.PolicyBlacklist{}, &entity.SystemConfig{}); err != nil {
|
|
t.Fatalf("AutoMigrate 失败: %v", err)
|
|
}
|
|
return db
|
|
}
|
|
|
|
func mustJSON(t *testing.T, v any) []byte {
|
|
t.Helper()
|
|
b, err := json.Marshal(v)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return b
|
|
}
|
|
|
|
// TestBlacklistHit 词表等值匹配:R/r/退订/拒收 命中;含字/空/正常回复不误伤。
|
|
func TestBlacklistHit(t *testing.T) {
|
|
db := newBlacklistDB(t)
|
|
b := NewAutoBlacklister(db, nil)
|
|
cases := []struct {
|
|
content string
|
|
want bool
|
|
}{
|
|
{"R", true}, {"r", true}, {" R ", true},
|
|
{"TD", true}, {"退订", true}, {"拒收", true},
|
|
{"我要退订", false}, {"请别再发了", false}, {"", false}, {"RR", false},
|
|
}
|
|
for _, c := range cases {
|
|
if got := b.hit(c.content); got != c.want {
|
|
t.Fatalf("hit(%q)=%v 期望 %v", c.content, got, c.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestBlacklistReload SystemConfig 词表覆盖默认(等值;去掉 R 只认「回T」)。
|
|
func TestBlacklistReload(t *testing.T) {
|
|
db := newBlacklistDB(t)
|
|
db.Create(&entity.SystemConfig{CfgKey: CfgKeyBlacklistWords, CfgValue: "回T, N"})
|
|
b := NewAutoBlacklister(db, nil)
|
|
if err := b.Reload(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !b.hit("回T") || !b.hit("n") {
|
|
t.Fatal("自定义词表未生效")
|
|
}
|
|
if b.hit("R") {
|
|
t.Fatal("自定义词表应替换默认(R 不应命中)")
|
|
}
|
|
// 空配置回落默认词表。
|
|
db.Model(&entity.SystemConfig{}).Where("cfg_key = ?", CfgKeyBlacklistWords).
|
|
Update("cfg_value", " , ")
|
|
if err := b.Reload(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !b.hit("R") {
|
|
t.Fatal("空配置应回落默认词表")
|
|
}
|
|
}
|
|
|
|
// TestMaybeBlockIdempotent 拉黑落库幂等+Redis 缓存即时生效(uk 冲突跳过)。
|
|
func TestMaybeBlockIdempotent(t *testing.T) {
|
|
db := newBlacklistDB(t)
|
|
mr := miniredis.RunT(t)
|
|
rdb := redis.NewClient(&redis.Options{Addr: mr.Addr()})
|
|
b := NewAutoBlacklister(db, rdb)
|
|
|
|
if !b.MaybeBlock(context.Background(), 5, "13800001111", "R") {
|
|
t.Fatal("首次拉黑应返回 true")
|
|
}
|
|
if b.MaybeBlock(context.Background(), 5, "13800001111", "R") {
|
|
t.Fatal("重复拉黑应幂等跳过(false)")
|
|
}
|
|
var rows []entity.PolicyBlacklist
|
|
db.Find(&rows)
|
|
if len(rows) != 1 || rows[0].Source != 2 || rows[0].AccountID != 5 || rows[0].Operator != "uplink-auto" {
|
|
t.Fatalf("拉黑行异常: %+v", rows)
|
|
}
|
|
n, err := rdb.SIsMember(context.Background(), interceptor.BlacklistCacheKey, "13800001111").Result()
|
|
if err != nil || !n {
|
|
t.Fatalf("Redis 黑名单缓存未生效: n=%v err=%v", n, err)
|
|
}
|
|
// 非拉黑词:零副作用。
|
|
if b.MaybeBlock(context.Background(), 5, "13800002222", "收到谢谢") {
|
|
t.Fatal("非拉黑词不应触发")
|
|
}
|
|
var cnt int64
|
|
db.Model(&entity.PolicyBlacklist{}).Count(&cnt)
|
|
if cnt != 1 {
|
|
t.Fatalf("不应新增行, got %d", cnt)
|
|
}
|
|
}
|
|
|
|
// TestConsumerBlacklistAndPush 消费链挂点:拒收词→拉黑(不影响推送)+ 推送入队。
|
|
func TestConsumerBlacklistAndPush(t *testing.T) {
|
|
db := newBlacklistDB(t)
|
|
if err := db.AutoMigrate(&entity.CallbackTask{}, &entity.AccountConfig{}); err != nil {
|
|
t.Fatalf("AutoMigrate 失败: %v", err)
|
|
}
|
|
db.Create(&entity.AccountConfig{AccountID: 5, Business: "sms", UplinkURL: "http://cb/mo"})
|
|
b := NewAutoBlacklister(db, nil)
|
|
// 推送出口打桩:捕获上行载荷。
|
|
got := make(chan string, 1)
|
|
c := NewConsumer(db).WithBlacklist(b).WithCallback(stubUplinkEnq{got})
|
|
|
|
payload := mustJSON(t, Event{
|
|
UplinkID: 9, DestCode: "10690", SrcMobile: "13900001111",
|
|
Content: "R", AccountID: 5, ChannelID: 7,
|
|
RecvAt: "2026-11-16T10:00:00+08:00",
|
|
})
|
|
if err := c.HandlePayload(context.Background(), payload); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// 拉黑生效(source=2)。
|
|
var bl entity.PolicyBlacklist
|
|
if err := db.Where("mobile = ?", "13900001111").First(&bl).Error; err != nil {
|
|
t.Fatalf("拒收未拉黑: %v", err)
|
|
}
|
|
if bl.Source != 2 || bl.AccountID != 5 {
|
|
t.Fatalf("拉黑行异常: %+v", bl)
|
|
}
|
|
// 推送不因拉黑中断。
|
|
select {
|
|
case p := <-got:
|
|
if p == "" {
|
|
t.Fatal("推送载荷为空")
|
|
}
|
|
default:
|
|
t.Fatal("上行推送未触发")
|
|
}
|
|
}
|
|
|
|
type stubUplinkEnq struct{ ch chan string }
|
|
|
|
func (s stubUplinkEnq) EnqueueUplink(_ context.Context, uplinkID, accountID int64,
|
|
destCode, srcMobile, content, recvAt string) error {
|
|
s.ch <- srcMobile + ":" + content
|
|
return nil
|
|
}
|