144 lines
4.5 KiB
Go
144 lines
4.5 KiB
Go
// service_test.go Day 41:模板 CRUD+状态机+占位符解析。
|
|
package template
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/glebarez/sqlite"
|
|
"gorm.io/gorm"
|
|
|
|
"go-sms/internal/model/entity"
|
|
)
|
|
|
|
func newTplSvc(t *testing.T) *Service {
|
|
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)
|
|
}
|
|
if err := db.AutoMigrate(&entity.TemplateSMS{}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return NewService(db)
|
|
}
|
|
|
|
// TestBatchReview Day 44(D20):批量审核——待审全翻/已审跳过/幂等重放。
|
|
func TestBatchReview(t *testing.T) {
|
|
s := newTplSvc(t)
|
|
ctx := context.Background()
|
|
var ids []int64
|
|
for i := 0; i < 3; i++ {
|
|
tpl, err := s.Create(ctx, &CreateReq{AccountID: 1, Name: "b" + string(rune('a'+i)), Content: "【店】您好{name}"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ids = append(ids, tpl.ID)
|
|
}
|
|
// 批量通过:3 条全翻
|
|
ok, err := s.BatchReview(ctx, ids, true, "")
|
|
if err != nil || ok != 3 {
|
|
t.Fatalf("批量通过: ok=%d err=%v", ok, err)
|
|
}
|
|
for _, id := range ids {
|
|
got, _ := s.GetByID(ctx, id)
|
|
if got.Status != StatusPassed {
|
|
t.Fatalf("模板 %d 应已过审: %d", id, got.Status)
|
|
}
|
|
}
|
|
// 重放(已审跳过):ok=0 不报错
|
|
ok, err = s.BatchReview(ctx, ids, true, "")
|
|
if err != nil || ok != 0 {
|
|
t.Fatalf("重放应 0 成功: ok=%d err=%v", ok, err)
|
|
}
|
|
// 批量拒绝带原因
|
|
t4, _ := s.Create(ctx, &CreateReq{AccountID: 1, Name: "b4", Content: "【店】拒绝样例"})
|
|
if ok, err = s.BatchReview(ctx, []int64{t4.ID}, false, "含敏感词"); err != nil || ok != 1 {
|
|
t.Fatalf("批量拒绝: ok=%d err=%v", ok, err)
|
|
}
|
|
got, _ := s.GetByID(ctx, t4.ID)
|
|
if got.Status != StatusRefused || got.Reason != "含敏感词" {
|
|
t.Fatalf("拒绝态/原因: %+v", got)
|
|
}
|
|
// 空数组/超 100
|
|
if _, err = s.BatchReview(ctx, nil, true, ""); err == nil {
|
|
t.Fatal("空 ids 应拒绝")
|
|
}
|
|
}
|
|
|
|
func stPtr(v int8) *int8 { return &v }
|
|
|
|
// TestTemplateLifecycle 验收:状态机合法迁移——新建待审→过审可引用→编辑回待审→拒审可删。
|
|
func TestTemplateLifecycle(t *testing.T) {
|
|
s := newTplSvc(t)
|
|
ctx := context.Background()
|
|
|
|
// 变量超限拒绝(11 个)
|
|
tooMany := "a{v1}{v2}{v3}{v4}{v5}{v6}{v7}{v8}{v9}{v10}{v11}"
|
|
if _, err := s.Create(ctx, &CreateReq{AccountID: 1, Name: "x", Content: tooMany}); err == nil {
|
|
t.Fatal("11 变量应拒绝")
|
|
}
|
|
// 非法变量名({1a} 不匹配 [A-Za-z][A-Za-z0-9_]*)不计数,合法创建
|
|
tpl, err := s.Create(ctx, &CreateReq{AccountID: 1, Name: "验证码", Content: "您的验证码{code},5分钟内有效"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if tpl.Status != StatusPending || tpl.Variables != "code" {
|
|
t.Fatalf("新建应待审且变量提取: %+v", tpl)
|
|
}
|
|
// 过审
|
|
if err := s.Review(ctx, tpl.ID, true, ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ := s.GetByID(ctx, tpl.ID)
|
|
if got.Status != StatusPassed || got.AuditAt == nil {
|
|
t.Fatal("过审失败")
|
|
}
|
|
// 通过态不可删
|
|
if err := s.Delete(ctx, tpl.ID); err == nil {
|
|
t.Fatal("通过态删除应拒绝")
|
|
}
|
|
// 编辑内容 → 回待审
|
|
if err := s.Update(ctx, tpl.ID, map[string]any{"content": "新验证码{code}{ttl}"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ = s.GetByID(ctx, tpl.ID)
|
|
if got.Status != StatusPending || got.Variables != "code,ttl" || got.AuditAt != nil {
|
|
t.Fatalf("编辑应回待审并重提变量: %+v", got)
|
|
}
|
|
// 拒审(带理由) → 可删
|
|
if err := s.Review(ctx, tpl.ID, false, "内容不合规"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.Delete(ctx, tpl.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// 状态过滤:nil 全量 / 0 待审 / -1 拒绝
|
|
_, _ = s.Create(ctx, &CreateReq{AccountID: 1, Name: "a", Content: "aa"})
|
|
if _, total, _ := s.List(ctx, &Query{Status: stPtr(StatusRefused)}); total != 0 {
|
|
t.Fatal("拒绝态应为 0(已删)")
|
|
}
|
|
if _, total, _ := s.List(ctx, &Query{Status: nil}); total != 1 {
|
|
t.Fatalf("全量应 1,得 %d", total)
|
|
}
|
|
}
|
|
|
|
// TestRender 验收:占位符替换+缺参拒绝+多余忽略。
|
|
func TestRender(t *testing.T) {
|
|
s := newTplSvc(t)
|
|
tpl, err := s.Create(context.Background(), &CreateReq{AccountID: 1, Name: "n", Content: "你好{name},订单{order}已发货"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out, err := s.Render(tpl, map[string]string{"name": "张三", "order": "A1", "extra": "x"})
|
|
if err != nil || out != "你好张三,订单A1已发货" {
|
|
t.Fatalf("渲染错: %q err=%v", out, err)
|
|
}
|
|
if _, err := s.Render(tpl, map[string]string{"name": "张三"}); err == nil {
|
|
t.Fatal("缺 order 应拒绝")
|
|
}
|
|
}
|