320 lines
12 KiB
Go
320 lines
12 KiB
Go
// review_test.go Day 43:消息审核单测——三模式分流/人工审核原子翻转/
|
|
// 列表过滤/取消改期/48h 超时扫描幂等回退。
|
|
package audit
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
|
|
"go-sms/internal/model/entity"
|
|
errs "go-sms/internal/pkg/errors"
|
|
)
|
|
|
|
// codeIs 断言业务错误码。
|
|
func codeIs(err error, code string) bool {
|
|
if e, ok := err.(*errs.Error); ok {
|
|
return e.Code == code
|
|
}
|
|
return false
|
|
}
|
|
|
|
// newReviewDB 测试库(审核域全量表)。
|
|
// 注:CreateTable 而非 AutoMigrate——message_task 与 account_balance_log 的
|
|
// idx_account_time 索引在 SQLite 中全局同名冲突(MySQL 索引仅表内唯一,线上无碍)。
|
|
func newReviewDB(t *testing.T) *gorm.DB {
|
|
t.Helper()
|
|
db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, m := range []any{
|
|
&entity.MessageTask{}, &entity.AuditRecord{}, &entity.PolicyKeyword{},
|
|
&entity.AccountConfig{}, &entity.AccountUser{},
|
|
} {
|
|
if err := db.Migrator().CreateTable(m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
// balance_log 原生建表:其 idx_account_time 与 message_task 撞名,SQLite 全局唯一。
|
|
db.Exec(`CREATE TABLE account_balance_log (
|
|
id integer primary key autoincrement, created_at datetime,
|
|
account_id integer, biz_type text, amount integer, balance_after integer,
|
|
ref_id text, operator text, remark text)`)
|
|
return db
|
|
}
|
|
|
|
// mkTask 造一条任务(audit 0 待审)。
|
|
func mkTask(t *testing.T, db *gorm.DB, accountID int64, content string) *entity.MessageTask {
|
|
t.Helper()
|
|
task := &entity.MessageTask{TaskNo: "T" + time.Now().Format("150405.000000000"),
|
|
AccountID: accountID, SendType: 1, Content: content, Sign: "测试签名", MobileCount: 1}
|
|
if err := db.Create(task).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return task
|
|
}
|
|
|
|
// setMode 设置账号审核模式(sms 业务线)。
|
|
func setMode(t *testing.T, db *gorm.DB, accountID int64, mode int8) {
|
|
t.Helper()
|
|
if err := db.Create(&entity.AccountConfig{
|
|
AccountID: accountID, Business: "sms", Protocol: "http", AuditMode: mode,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// TestRouteModes 三模式分流:免审→3 无留痕/手动→0/自动无词→1/自动命中→2(一级词优先)。
|
|
func TestRouteModes(t *testing.T) {
|
|
db := newReviewDB(t)
|
|
s := NewReviewService(db)
|
|
ctx := context.Background()
|
|
setMode(t, db, 1, ModeExempt) // 免审
|
|
setMode(t, db, 2, ModeManual) // 手动
|
|
setMode(t, db, 3, ModeAuto) // 自动:词库含二级词
|
|
db.Create(&entity.PolicyKeyword{Word: "发票", Level: 1, Status: 1})
|
|
db.Create(&entity.PolicyKeyword{Word: "兼职", Level: 2, Status: 1})
|
|
db.Create(&entity.PolicyKeyword{Word: "停用词", Level: 1, Status: 0}) // 停用不参与
|
|
|
|
// 免审→3,无 AuditRecord
|
|
t1 := mkTask(t, db, 1, "促销内容")
|
|
if task, rec, err := s.Route(ctx, t1.ID); err != nil || task.AuditStatus != AuditExempt || rec != nil {
|
|
t.Fatalf("免审分流 got status=%d rec=%v err=%v", task.AuditStatus, rec, err)
|
|
}
|
|
// 免审二次分流→冲突(幂等)
|
|
if _, _, err := s.Route(ctx, t1.ID); !codeIs(err, "ISC:037") {
|
|
t.Fatalf("免审重复分流应 ISC:037,got %v", err)
|
|
}
|
|
// 手动→保持 0 待审
|
|
t2 := mkTask(t, db, 2, "正常内容")
|
|
if task, rec, err := s.Route(ctx, t2.ID); err != nil || task.AuditStatus != AuditPending || rec != nil {
|
|
t.Fatalf("手动分流 got status=%d err=%v", task.AuditStatus, err)
|
|
}
|
|
// 自动:无词→1 过,留痕"自动审核通过"
|
|
t3 := mkTask(t, db, 3, "您好,您的快递已到")
|
|
task, rec, err := s.Route(ctx, t3.ID)
|
|
if err != nil || task.AuditStatus != AuditPassed || rec == nil || rec.Auditor != "system" {
|
|
t.Fatalf("自动通过 got status=%d rec=%v err=%v", task.AuditStatus, rec, err)
|
|
}
|
|
if rec.Reason != "自动审核通过" || rec.BizType != "sms" || rec.BizID != t3.ID {
|
|
t.Fatalf("自动通过留痕异常: %+v", rec)
|
|
}
|
|
// 自动:命中一级词→2 拒,留痕含词
|
|
t4 := mkTask(t, db, 3, "低价代开发票")
|
|
task, rec, err = s.Route(ctx, t4.ID)
|
|
if err != nil || task.AuditStatus != AuditRejected || rec == nil {
|
|
t.Fatalf("自动档黑词拒绝 got status=%d err=%v", task.AuditStatus, err)
|
|
}
|
|
if rec.Reason != "命中敏感词: 发票" {
|
|
t.Fatalf("拒绝留痕应含命中词, got %q", rec.Reason)
|
|
}
|
|
// 无业务配置行→默认免审
|
|
t5 := mkTask(t, db, 99, "任意")
|
|
if task, _, err := s.Route(ctx, t5.ID); err != nil || task.AuditStatus != AuditExempt {
|
|
t.Fatalf("无配置默认免审 got err=%v", err)
|
|
}
|
|
}
|
|
|
|
// TestApproveReject 人工审核:通过/拒绝原子翻转+留痕;重复审→ISC:037;空原因拒→ISC:400。
|
|
func TestApproveReject(t *testing.T) {
|
|
db := newReviewDB(t)
|
|
s := NewReviewService(db)
|
|
ctx := context.Background()
|
|
task := mkTask(t, db, 7, "待审内容")
|
|
|
|
if _, _, err := s.Approve(ctx, task.ID, "ops", ""); err != nil {
|
|
t.Fatalf("通过失败: %v", err)
|
|
}
|
|
var cnt int64
|
|
db.Model(&entity.AuditRecord{}).Where("biz_id = ? AND status = ?", task.ID, AuditPassed).Count(&cnt)
|
|
if cnt != 1 {
|
|
t.Fatalf("应留痕 1 条通过记录, got %d", cnt)
|
|
}
|
|
// 已通过再审→冲突
|
|
if _, _, err := s.Approve(ctx, task.ID, "ops", ""); !codeIs(err, "ISC:037") {
|
|
t.Fatalf("重复审核应 ISC:037, got %v", err)
|
|
}
|
|
if _, _, err := s.Reject(ctx, task.ID, "ops", "原因"); !codeIs(err, "ISC:037") {
|
|
t.Fatalf("已过再拒应 ISC:037, got %v", err)
|
|
}
|
|
// 拒绝原因必填
|
|
t2 := mkTask(t, db, 7, "再来一条")
|
|
if _, _, err := s.Reject(ctx, t2.ID, "ops", ""); !codeIs(err, "ISC:400") {
|
|
t.Fatalf("空原因应 ISC:400, got %v", err)
|
|
}
|
|
if _, rec, err := s.Reject(ctx, t2.ID, "ops", "内容违规"); err != nil || rec.Reason != "内容违规" {
|
|
t.Fatalf("拒绝失败: %v", err)
|
|
}
|
|
var after entity.MessageTask
|
|
db.First(&after, t2.ID)
|
|
if after.AuditStatus != AuditRejected {
|
|
t.Fatalf("任务应置拒绝, got %d", after.AuditStatus)
|
|
}
|
|
}
|
|
|
|
// TestListTasksCancelReschedule 列表过滤分页+取消/改期状态机。
|
|
func TestListTasksCancelReschedule(t *testing.T) {
|
|
db := newReviewDB(t)
|
|
s := NewReviewService(db)
|
|
ctx := context.Background()
|
|
ta := mkTask(t, db, 1, "群发")
|
|
tb := mkTask(t, db, 1, "定时")
|
|
db.Model(tb).Updates(map[string]any{"send_type": int8(3),
|
|
"schedule_at": time.Now().Add(2 * time.Hour), "audit_status": AuditPassed})
|
|
tc := mkTask(t, db, 2, "已完成")
|
|
db.Model(tc).Updates(map[string]any{"status": TaskDone, "audit_status": AuditPassed})
|
|
|
|
// 全量 3 条;account 过滤 2;audit 待审 1;分页
|
|
if _, total, _ := s.ListTasks(ctx, TaskQuery{AuditStatus: -1, Status: -1}); total != 3 {
|
|
t.Fatalf("全量应 3, got %d", total)
|
|
}
|
|
if _, total, _ := s.ListTasks(ctx, TaskQuery{AccountID: 1, AuditStatus: -1, Status: -1}); total != 2 {
|
|
t.Fatalf("账号过滤应 2, got %d", total)
|
|
}
|
|
if _, total, _ := s.ListTasks(ctx, TaskQuery{AuditStatus: AuditPending, Status: -1}); total != 1 {
|
|
t.Fatalf("待审应 1, got %d", total)
|
|
}
|
|
rows, _, _ := s.ListTasks(ctx, TaskQuery{AuditStatus: -1, Status: -1, Page: 2, PageSize: 2})
|
|
if len(rows) != 1 {
|
|
t.Fatalf("第 2 页应 1 条, got %d", len(rows))
|
|
}
|
|
|
|
// 取消:待发可取消→3;再取消/已完成取消→ISC:038
|
|
if task, err := s.Cancel(ctx, ta.ID); err != nil || task.Status != TaskCanceled {
|
|
t.Fatalf("取消失败: %v", err)
|
|
}
|
|
if _, err := s.Cancel(ctx, ta.ID); !codeIs(err, "ISC:038") {
|
|
t.Fatalf("重复取消应 ISC:038, got %v", err)
|
|
}
|
|
if _, err := s.Cancel(ctx, tc.ID); !codeIs(err, "ISC:038") {
|
|
t.Fatalf("已完成取消应 ISC:038, got %v", err)
|
|
}
|
|
|
|
// 改期:过去时刻→ISC:400;定时待发可改;非定时→ISC:038
|
|
if _, err := s.Reschedule(ctx, tb.ID, time.Now().Add(-time.Minute)); !codeIs(err, "ISC:400") {
|
|
t.Fatalf("过去时刻应 ISC:400, got %v", err)
|
|
}
|
|
future := time.Now().Add(24 * time.Hour).Truncate(time.Second)
|
|
if task, err := s.Reschedule(ctx, tb.ID, future); err != nil || task.ScheduleAt.Unix() != future.Unix() {
|
|
t.Fatalf("改期失败: %v", err)
|
|
}
|
|
if _, err := s.Reschedule(ctx, ta.ID, future); !codeIs(err, "ISC:038") {
|
|
t.Fatalf("非定时任务改期应 ISC:038, got %v", err)
|
|
}
|
|
}
|
|
|
|
// TestTimeoutScanner 48h 超时:置 4+幂等回退(FeeTotal>0 一条回退,<0 一条跳过,
|
|
// 新任务不动);重复扫描零副作用。
|
|
func TestTimeoutScanner(t *testing.T) {
|
|
db := newReviewDB(t)
|
|
s := NewTimeoutScanner(db).WithTimeout(48 * time.Hour)
|
|
ctx := context.Background()
|
|
db.Create(&entity.AccountUser{Model: entity.Model{ID: 9}, Username: "acct9", PasswordHash: "x",
|
|
Balance: 100, Status: 1})
|
|
|
|
old1 := mkTask(t, db, 9, "超时且有费")
|
|
db.Model(old1).Updates(map[string]any{"fee_total": 5.0, "created_at": time.Now().Add(-49 * time.Hour)})
|
|
old2 := mkTask(t, db, 9, "超时零费")
|
|
db.Model(old2).Update("created_at", time.Now().Add(-49*time.Hour))
|
|
fresh := mkTask(t, db, 9, "未超时")
|
|
_ = fresh
|
|
|
|
expired, refunded, err := s.ScanOnce(ctx)
|
|
if err != nil || expired != 2 || refunded != 1 {
|
|
t.Fatalf("首轮 got expired=%d refunded=%d err=%v", expired, refunded, err)
|
|
}
|
|
var u entity.AccountUser
|
|
db.First(&u, 9)
|
|
if u.Balance != 105 { // 100 + 5 回退
|
|
t.Fatalf("余额应 105, got %d", u.Balance)
|
|
}
|
|
var logCnt int64
|
|
db.Model(&entity.AccountBalanceLog{}).Where("biz_type = ?", "refund").Count(&logCnt)
|
|
if logCnt != 1 {
|
|
t.Fatalf("回退流水应 1 条, got %d", logCnt)
|
|
}
|
|
var statuses []int8
|
|
db.Model(&entity.MessageTask{}).Order("id").Pluck("audit_status", &statuses)
|
|
if statuses[0] != AuditTimeout || statuses[1] != AuditTimeout || statuses[2] != AuditPending {
|
|
t.Fatalf("超时态异常: %v", statuses)
|
|
}
|
|
// 二轮:全部已流转,零副作用(幂等)
|
|
expired, refunded, err = s.ScanOnce(ctx)
|
|
if err != nil || expired != 0 || refunded != 0 {
|
|
t.Fatalf("二轮应零副作用 got expired=%d refunded=%d err=%v", expired, refunded, err)
|
|
}
|
|
db.First(&u, 9)
|
|
if u.Balance != 105 {
|
|
t.Fatalf("重复扫描不得重复回退, got %d", u.Balance)
|
|
}
|
|
}
|
|
|
|
// TestBatchReviewAndCounts Day 45/D21:批量审核(通过/拒绝/重放幂等/参数校验)+审核态计数。
|
|
func TestBatchReviewAndCounts(t *testing.T) {
|
|
db := newReviewDB(t)
|
|
svc := NewReviewService(db)
|
|
ctx := context.Background()
|
|
t1 := mkTask(t, db, 1001, "批量A")
|
|
t2 := mkTask(t, db, 1001, "批量B")
|
|
t3 := mkTask(t, db, 1002, "已过")
|
|
if _, _, err := svc.Approve(ctx, t3.ID, "admin", "先过"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// 计数:2 待审 + 1 已通过。
|
|
total, by, err := svc.CountByStatus(ctx)
|
|
if err != nil || total != 3 || by[AuditPending] != 2 || by[AuditPassed] != 1 {
|
|
t.Fatalf("counts: total=%d by=%v err=%v", total, by, err)
|
|
}
|
|
// 批量拒绝无原因→400。
|
|
if ok, err := svc.BatchReview(ctx, []int64{t1.ID}, false, "admin", ""); err == nil || !codeIs(err, "ISC:400") || ok != 0 {
|
|
t.Fatalf("空原因应 400: ok=%d err=%v", ok, err)
|
|
}
|
|
// 批量通过 [t1,t2,t3]:t3 已审跳过→ok=2。
|
|
ok, err := svc.BatchReview(ctx, []int64{t1.ID, t2.ID, t3.ID}, true, "admin", "")
|
|
if err != nil || ok != 2 {
|
|
t.Fatalf("批量通过: ok=%d err=%v", ok, err)
|
|
}
|
|
if get := getAudit(t, db, t1.ID); get != AuditPassed {
|
|
t.Fatalf("t1 应已通过: %d", get)
|
|
}
|
|
// 重放→ok=0 幂等。
|
|
ok, err = svc.BatchReview(ctx, []int64{t1.ID, t2.ID}, true, "admin", "")
|
|
if err != nil || ok != 0 {
|
|
t.Fatalf("重放应 ok=0: ok=%d err=%v", ok, err)
|
|
}
|
|
// 批量拒绝(新任务,带原因)→status=-1 口径=2 拒绝。
|
|
t4 := mkTask(t, db, 1001, "批量拒")
|
|
if ok, err := svc.BatchReview(ctx, []int64{t4.ID}, false, "admin", "内容不合规"); err != nil || ok != 1 {
|
|
t.Fatalf("批量拒绝: ok=%d err=%v", ok, err)
|
|
}
|
|
if get := getAudit(t, db, t4.ID); get != AuditRejected {
|
|
t.Fatalf("t4 应已拒绝: %d", get)
|
|
}
|
|
// 空 ids→400。
|
|
if _, err := svc.BatchReview(ctx, nil, true, "admin", ""); err == nil || !codeIs(err, "ISC:400") {
|
|
t.Fatalf("空 ids 应 400: %v", err)
|
|
}
|
|
// 拒绝留痕。
|
|
var rec entity.AuditRecord
|
|
if err := db.Where("biz_id = ? AND status = ?", t4.ID, AuditRejected).First(&rec).Error; err != nil {
|
|
t.Fatal("批量拒绝应留痕")
|
|
}
|
|
if rec.Reason != "内容不合规" || rec.Auditor != "admin" {
|
|
t.Fatalf("留痕不符: %+v", rec)
|
|
}
|
|
}
|
|
|
|
// getAudit 取任务审核态。
|
|
func getAudit(t *testing.T, db *gorm.DB, id int64) int8 {
|
|
t.Helper()
|
|
var task entity.MessageTask
|
|
if err := db.Select("audit_status").First(&task, id).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return task.AuditStatus
|
|
}
|