Files
go-sms-V2/internal/service/interceptor/interceptor_test.go
T

114 lines
3.6 KiB
Go

// interceptor_test.go Day 42:链顺序/短路/Recorder 落库调用——验收「命中顺序符合配置」。
package interceptor
import (
"context"
"testing"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"go-sms/internal/model/entity"
)
// newTestDB 建 sqlite 内存库并建策略/拦截日志表。
// 注:policy_blacklist 与 policy_whitelist 同用索引名 uk_mobile_account,
// glebarez sqlite 索引名为库级作用域(与 MySQL 表级不同),第二表建索引报错但表已建好,
// 故忽略该错误(列结构完整,查询不受影响)。
func newTestDB(t *testing.T) *gorm.DB {
t.Helper()
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
t.Fatal(err)
}
if sqlDB, e := db.DB(); e == nil {
sqlDB.SetMaxOpenConns(1)
}
_ = db.AutoMigrate(&entity.PolicyBlacklist{})
_ = db.AutoMigrate(&entity.PolicyWhitelist{})
if err := db.AutoMigrate(&entity.PolicyFrequency{}, &entity.SendBlockLog{}); err != nil {
t.Fatal(err)
}
for _, tbl := range []string{"policy_blacklist", "policy_whitelist"} {
if !db.Migrator().HasTable(tbl) {
t.Fatalf("表 %s 未建成", tbl)
}
}
return db
}
// fakeInt 可控行为拦截器。
type fakeInt struct {
name string
hit bool
}
func (f *fakeInt) Name() string { return f.name }
func (f *fakeInt) Check(_ context.Context, _ *Req) *Block {
if !f.hit {
return nil
}
return &Block{Interceptor: f.name, Code: "ISC:000", Msg: "hit " + f.name}
}
// memRec 内存 Recorder。
type memRec struct{ rows []SendBlockLogRow }
// SendBlockLogRow Recorder 收到的简化行。
type SendBlockLogRow struct {
Mobile, Interceptor, Code string
}
func (m *memRec) Record(_ context.Context, r *Req, b *Block) {
m.rows = append(m.rows, SendBlockLogRow{Mobile: r.Mobile, Interceptor: b.Interceptor, Code: b.Code})
}
// TestChainOrderAndShortCircuit 验收:默认链顺序固定;首个命中短路;Recorder 收到命中行。
func TestChainOrderAndShortCircuit(t *testing.T) {
// 顺序:黑名单→频次→签名→退订→地区(硬拦截在前)
c := NewDefaultChain(newTestDB(t), nil, nil, Options{})
want := []string{"blacklist", "frequency", "sign", "unsubscribe", "region"}
got := c.Names()
if len(got) != len(want) {
t.Fatalf("链长 %d != %d", len(got), len(want))
}
for i := range want {
if got[i] != want[i] {
t.Fatalf("链顺序不符: %v", got)
}
}
// 短路:首拦截器命中,后续不再执行;Recorder 收到一次。
rec := &memRec{}
a, b := &fakeInt{"a", true}, &fakeInt{"b", true}
ch := NewChain(rec, a, b)
blk := ch.Check(context.Background(), &Req{Mobile: "13800000000"})
if blk == nil || blk.Interceptor != "a" {
t.Fatalf("应命中首个拦截器 a: %+v", blk)
}
if len(rec.rows) != 1 || rec.rows[0].Interceptor != "a" {
t.Fatalf("Recorder 应收到 1 行 a: %+v", rec.rows)
}
// 放行链:全过。
ch2 := NewChain(nil, &fakeInt{"a", false}, &fakeInt{"b", false})
if ch2.Check(context.Background(), &Req{}) != nil {
t.Fatal("全放行链应返回 nil")
}
}
// TestDBRecorder 验收:拦截原因落库 send_block_log。
func TestDBRecorder(t *testing.T) {
db := newTestDB(t)
rec := NewDBRecorder(db)
rec.Record(context.Background(), &Req{TaskNo: "T1", AccountID: 7, Mobile: "13911112222"},
&Block{Interceptor: "blacklist", Code: "ISC:006", Msg: "黑名单号码"})
var rows []entity.SendBlockLog
if err := db.Find(&rows).Error; err != nil || len(rows) != 1 {
t.Fatalf("应落 1 行: err=%v n=%d", err, len(rows))
}
r := rows[0]
if r.TaskNo != "T1" || r.Mobile != "13911112222" || r.Interceptor != "blacklist" || r.Code != "ISC:006" {
t.Fatalf("落库字段不符: %+v", r)
}
}