275 lines
9.8 KiB
Go
275 lines
9.8 KiB
Go
// submit_test.go Day 43 修复循环:最小提交链单测——立即/定时受理+明细雪花落库/
|
|
// 三模式分流接线/余额只读校验/任务号幂等。
|
|
package submit
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
|
|
"go-sms/internal/model/entity"
|
|
errs "go-sms/internal/pkg/errors"
|
|
"go-sms/internal/service/audit"
|
|
"go-sms/internal/util"
|
|
)
|
|
|
|
// errCode 业务错误码(非业务错返回空串)。
|
|
func errCode(err error) string {
|
|
if e, ok := err.(*errs.Error); ok {
|
|
return e.Code
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// newSubmitDB 测试库(提交域全量表;索引撞名坑见 review_test.go 同款注记)。
|
|
func newSubmitDB(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.MessageDetail{},
|
|
&entity.PolicyKeyword{}, &entity.AccountConfig{}, &entity.AccountUser{},
|
|
} {
|
|
if err := db.Migrator().CreateTable(m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
// account_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)`)
|
|
// audit_record 原生建表:其 idx_status_time 与 message_detail 撞名(SQLite 索引全局唯一)。
|
|
db.Exec(`CREATE TABLE audit_record (
|
|
id integer primary key autoincrement, created_at datetime,
|
|
biz_type text, biz_id integer, account_id integer, content text,
|
|
status integer, auditor text, audit_at datetime, reason text)`)
|
|
return db
|
|
}
|
|
|
|
func newSubmitSvc(t *testing.T, db *gorm.DB) *SubmitService {
|
|
t.Helper()
|
|
sf, err := util.NewSnowflake(1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return NewSubmitService(db, sf, audit.NewReviewService(db)) // 真 ReviewService:分流链路全真
|
|
}
|
|
|
|
func mkAcct(t *testing.T, db *gorm.DB, id int64, balance int64, mode int8) {
|
|
t.Helper()
|
|
if err := db.Create(&entity.AccountUser{Model: entity.Model{ID: id},
|
|
Username: "acct" + time.Now().Format("150405.000000000"), Role: "customer",
|
|
Status: 1, Balance: balance,
|
|
}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.Create(&entity.AccountConfig{AccountID: id, Business: "sms",
|
|
Protocol: "http", AuditMode: mode}).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// TestSubmitBatchExempt 立即发送+免审:受理落任务(audit=3/status=0)+明细逐号雪花ID 唯一。
|
|
func TestSubmitBatchExempt(t *testing.T) {
|
|
db := newSubmitDB(t)
|
|
s := newSubmitSvc(t, db)
|
|
mkAcct(t, db, 1, 10000, 0) // 免审
|
|
|
|
req := &SubmitReq{AccountID: 1, SendType: SendTypeBatch,
|
|
Mobiles: []string{"13800138000", "13800138001", "13800138000"}, // 含重复
|
|
Content: "【测】促销通知", Sign: "【测】"}
|
|
res, err := s.Submit(context.Background(), req, nil, req.Content, "127.0.0.1")
|
|
if err != nil {
|
|
t.Fatalf("submit err=%v", err)
|
|
}
|
|
if res.Task.AuditStatus != 3 || res.Task.Status != 0 {
|
|
t.Fatalf("免审受理 audit=%d status=%d", res.Task.AuditStatus, res.Task.Status)
|
|
}
|
|
if res.Estimate.ValidCount != 2 || res.Task.MobileCount != 2 || res.Task.FeeTotal != 2 {
|
|
t.Fatalf("去重计费 valid=%d count=%d fee=%v", res.Estimate.ValidCount, res.Task.MobileCount, res.Task.FeeTotal)
|
|
}
|
|
var n int64
|
|
db.Model(&entity.MessageDetail{}).Where("task_id = ?", res.Task.ID).Count(&n)
|
|
if n != 2 {
|
|
t.Fatalf("明细行数 = %d, 期望 2(去重)", n)
|
|
}
|
|
var ids []int64
|
|
db.Model(&entity.MessageDetail{}).Where("task_id = ?", res.Task.ID).Pluck("id", &ids)
|
|
if len(ids) == 2 && ids[0] == ids[1] {
|
|
t.Fatalf("雪花 ID 重复: %v", ids)
|
|
}
|
|
if res.Task.TaskNo == "" {
|
|
t.Fatal("缺省任务号未生成")
|
|
}
|
|
}
|
|
|
|
// TestSubmitTimer 定时受理:ScheduleAt 落库;过去时刻拒绝。
|
|
func TestSubmitTimer(t *testing.T) {
|
|
db := newSubmitDB(t)
|
|
s := newSubmitSvc(t, db)
|
|
mkAcct(t, db, 1, 10000, 0)
|
|
|
|
future := time.Now().Add(2 * time.Hour).Format("2006-01-02 15:04")
|
|
req := &SubmitReq{AccountID: 1, SendType: SendTypeTimer, SendAt: future,
|
|
Mobiles: []string{"13800138000"}, Content: "【测】定时", Sign: "【测】"}
|
|
res, err := s.Submit(context.Background(), req, nil, req.Content, "")
|
|
if err != nil {
|
|
t.Fatalf("定时受理 err=%v", err)
|
|
}
|
|
if res.Task.ScheduleAt == nil || !res.Task.ScheduleAt.After(time.Now().Add(time.Hour)) {
|
|
t.Fatalf("ScheduleAt 落库错误: %v", res.Task.ScheduleAt)
|
|
}
|
|
// 过去时刻 → 400
|
|
past := time.Now().Add(-time.Hour).Format("2006-01-02 15:04")
|
|
req2 := &SubmitReq{AccountID: 1, SendType: SendTypeTimer, SendAt: past,
|
|
Mobiles: []string{"13800138000"}, Content: "【测】定时", Sign: "【测】"}
|
|
if _, err := s.Submit(context.Background(), req2, nil, req2.Content, ""); err == nil {
|
|
t.Fatal("过去时刻应拒绝")
|
|
}
|
|
}
|
|
|
|
// TestSubmitAuditModes 手动→0 待审/自动命中黑词→2+留痕(真 ReviewService 接线)。
|
|
func TestSubmitAuditModes(t *testing.T) {
|
|
db := newSubmitDB(t)
|
|
s := newSubmitSvc(t, db)
|
|
mkAcct(t, db, 2, 10000, 1) // 手动
|
|
mkAcct(t, db, 3, 10000, 2) // 自动
|
|
db.Create(&entity.PolicyKeyword{Word: "发票", Level: 1, Status: 1})
|
|
|
|
// 手动 → 保持待审 0
|
|
req := &SubmitReq{AccountID: 2, SendType: 1, Mobiles: []string{"13800138000"},
|
|
Content: "【测】代开发票", Sign: "【测】"}
|
|
res, err := s.Submit(context.Background(), req, nil, req.Content, "")
|
|
if err != nil || res.Task.AuditStatus != 0 {
|
|
t.Fatalf("手动模式 audit=%d err=%v(期望 0 待审)", res.Task.AuditStatus, err)
|
|
}
|
|
// 自动+命中 → 2 + AuditRecord(auditor=system)
|
|
req2 := &SubmitReq{AccountID: 3, SendType: 1, Mobiles: []string{"13800138000"},
|
|
Content: "【测】代开发票", Sign: "【测】"}
|
|
res2, err := s.Submit(context.Background(), req2, nil, req2.Content, "")
|
|
if err != nil || res2.Task.AuditStatus != 2 {
|
|
t.Fatalf("自动命中 audit=%d err=%v(期望 2 拒)", res2.Task.AuditStatus, err)
|
|
}
|
|
if res2.Record == nil || res2.Record.Auditor != "system" {
|
|
t.Fatalf("自动档留痕缺失: %+v", res2.Record)
|
|
}
|
|
}
|
|
|
|
// TestSubmitArrears 余额只读校验:不足 → ISC:007。
|
|
func TestSubmitArrears(t *testing.T) {
|
|
db := newSubmitDB(t)
|
|
s := newSubmitSvc(t, db)
|
|
mkAcct(t, db, 1, 1, 0) // 余额 1 条
|
|
req := &SubmitReq{AccountID: 1, SendType: 1,
|
|
Mobiles: []string{"13800138000", "13800138001"}, Content: "【测】两号", Sign: "【测】"}
|
|
_, err := s.Submit(context.Background(), req, nil, req.Content, "")
|
|
if err == nil {
|
|
t.Fatal("余额不足应拒绝")
|
|
}
|
|
if got := errCode(err); got != "ISC:007" {
|
|
t.Fatalf("余额不足码 = %s, 期望 ISC:007", got)
|
|
}
|
|
}
|
|
|
|
// TestSubmitDupTaskNo 任务号幂等:同号二次 → ISC:400。
|
|
func TestSubmitDupTaskNo(t *testing.T) {
|
|
db := newSubmitDB(t)
|
|
s := newSubmitSvc(t, db)
|
|
mkAcct(t, db, 1, 10000, 0)
|
|
base := func() *SubmitReq {
|
|
return &SubmitReq{TaskNo: "DUP-001", AccountID: 1, SendType: 1,
|
|
Mobiles: []string{"13800138000"}, Content: "【测】幂等", Sign: "【测】"}
|
|
}
|
|
if _, err := s.Submit(context.Background(), base(), nil, base().Content, ""); err != nil {
|
|
t.Fatalf("首次提交 err=%v", err)
|
|
}
|
|
if _, err := s.Submit(context.Background(), base(), nil, base().Content, ""); errCode(err) != "ISC:400" {
|
|
t.Fatalf("重复任务号应 ISC:400,got %v", err)
|
|
}
|
|
}
|
|
|
|
// TestSubmitVideoBizType Day 49:video 须账号已开通(AccountConfig 业务行);
|
|
// 开通后 task.business=video 落库+事件载荷带 business;sms 默认不回归。
|
|
func TestSubmitVideoBizType(t *testing.T) {
|
|
db := newSubmitDB(t)
|
|
s := newSubmitSvc(t, db)
|
|
mkAcct(t, db, 1, 10000, 0) // 仅开通 sms 业务行
|
|
|
|
// ① 未开通 video:ISC:403 拒绝(发生在预扣前,无余额变动)。
|
|
req := &SubmitReq{AccountID: 1, SendType: SendTypeBatch,
|
|
Mobiles: []string{"13800138000"},
|
|
Content: "【测】视频 https://v.example.com/a.mp4", Sign: "【测】", Business: BusinessVideo}
|
|
if _, err := s.Submit(context.Background(), req, nil, req.Content, ""); errCode(err) != "ISC:403" {
|
|
t.Fatalf("未开通 video 应 ISC:403,got %v", err)
|
|
}
|
|
var bal int64
|
|
var acct entity.AccountUser
|
|
if err := db.First(&acct, 1).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bal = acct.Balance
|
|
if bal != 10000 {
|
|
t.Fatalf("拒绝不应动余额: %d", bal)
|
|
}
|
|
|
|
// ② 开通后受理成功:task.business=video。
|
|
db.Create(&entity.AccountConfig{AccountID: 1, Business: BusinessVideo, Protocol: "http", AuditMode: 0})
|
|
res, err := s.Submit(context.Background(), req, nil, req.Content, "")
|
|
if err != nil {
|
|
t.Fatalf("开通后 video 受理: %v", err)
|
|
}
|
|
if res.Task.Business != BusinessVideo {
|
|
t.Fatalf("task.business: %q", res.Task.Business)
|
|
}
|
|
var cnt int64
|
|
db.Model(&entity.MessageTask{}).Where("business = ?", BusinessVideo).Count(&cnt)
|
|
if cnt != 1 {
|
|
t.Fatalf("video 任务落库: %d", cnt)
|
|
}
|
|
|
|
// ③ 事件载荷带 business(走真实 ProducerSink 序列化)。
|
|
prod := &fakeQueueProd{}
|
|
s2 := newSubmitSvc(t, db)
|
|
s2.sink = ProducerSink{P: prod, Source: "test"}
|
|
req2 := &SubmitReq{AccountID: 1, SendType: SendTypeBatch,
|
|
Mobiles: []string{"13800138001"},
|
|
Content: "【测】视频 https://v.example.com/b.mp4", Sign: "【测】", Business: BusinessVideo}
|
|
if _, err := s2.Submit(context.Background(), req2, nil, req2.Content, ""); err != nil {
|
|
t.Fatalf("video 二次受理: %v", err)
|
|
}
|
|
if len(prod.vals) == 0 {
|
|
t.Fatal("应投递 sms-submit 事件")
|
|
}
|
|
var ev SubmitEvent
|
|
if err := json.Unmarshal(prod.vals[0], &ev); err != nil {
|
|
t.Fatalf("事件解析: %v", err)
|
|
}
|
|
if ev.Business != BusinessVideo {
|
|
t.Fatalf("事件载荷 business: %q (raw: %s)", ev.Business, prod.vals[0])
|
|
}
|
|
}
|
|
|
|
// fakeQueueProd sms-submit 投递捕获(Day52 R2:err 注入模拟 Kafka 故障降级)。
|
|
type fakeQueueProd struct {
|
|
topics []string
|
|
vals [][]byte
|
|
err error
|
|
}
|
|
|
|
func (p *fakeQueueProd) Send(topic string, key, value []byte) error {
|
|
if p.err != nil {
|
|
return p.err
|
|
}
|
|
p.topics = append(p.topics, topic)
|
|
p.vals = append(p.vals, value)
|
|
return nil
|
|
}
|