Files
go-sms-V2/internal/service/settle/reconcile_test.go
T
Tory bb60d60529
ci / test (push) Canceled after 0s
ci / web-test (push) Canceled after 0s
ci / build (push) Canceled after 0s
ci / deploy-dev (push) Canceled after 0s
S4-D1/D2: C1 回调推送引擎+C2 上行自动拉黑——验收项「死信可查/拒收即拉黑」✅: ①C1=callback_task 队列表(迁移 v21,状态机 pending→sent/dead+attempt/next_retry_at/last_code)+PushWorker(2s 扫/批 100/并发 8/2^n 秒指数退避封顶 10min/10 次耗尽死信/UPDATE CAS 防竞态)+双挂点(report 终态首锁[Consumer+PairService,与 CMPP Deliver 同点同语义]+uplink 消费尾)+URL 门控 account_config.report_url/uplink_url(60s TTL 缓存,未配置零任务)+GET /ops/callback(列表+状态计数面板)/POST /ops/callback/:id/resend(casbin ops callback:read/write v21 所种)+前端回调管理页(ops-callback 路由系统管理组+api/callback.ts+状态面板/筛选/载荷展开/死信重派;matrix+guard 33 菜单显式更)+metrics callback_push_total{type,result}/blacklist_total+openapi v1.4.0;②C2=AutoBlacklister 等值词表(trim+lower 防误伤,默认 R/TD/退订/拒收;SystemConfig uplink.blacklist.keywords 可配 60s 刷新)→policy_blacklist(source=2/账号级/uk 幂等)+Redis Set 即时生效,拉黑不影响上行推送(需求原文);③真机全链六步实证(dev 中间件):回执 DELIVRD→detail 终态→HTTP 回调 JSON 送达(200/sent)+上行 R→拉黑(source=2+Redis=1)+推送送达+坏 URL→attempt10→dead→resend ISC:000→修复重投 sent+status_counts 面板;一次性探针 cmd/cbprobe 用后即删+冒烟数据全清;④单测 +13(callback 6[入队门控/载荷/状态机全链/并发批]+uplink 5[词表/配置覆盖/幂等缓存/消费链]+report 2[首锁一次/nil]);⑤门禁=build/vet/-race 40 包/lint 0/eslint0/vue-tsc0/vitest 148/build 全绿;迁移测试 v15..v21 两遍幂等+dev 实跑 v21;gofmt 存量 8 文件顺手清零(S2 遗留);⑥口径诚实标注:载荷无签名挂账/拦截终态 HTTP 未挂(CMPP 侧已有)/PushWorker 单实例 at-least-once;验收单 s4-d1d2-callback.md+CHANGELOG+memory-bank 回填
2026-11-16 11:19:55 +08:00

176 lines
6.3 KiB
Go

package settle
// reconcile_test.go S2-D5:三方对账单测——全链一致性(明细→日汇总→账单)
// diff=0;篡改各事实源逐项暴露差异;缺账单=流程状态不判失败。
import (
"context"
"testing"
"time"
"gorm.io/gorm"
"go-sms/internal/model/entity"
)
// seedReconcile 全链种子:明细(11-10:acc1 ch1 2成功+1未知;acc2 ch2 1成功)
// → AggregateDay → 双向出账。ch1 绑上家 7 且 success_unknown 口径,ch2 绑 7 默认 success。
func seedReconcile(t *testing.T, db *gorm.DB) {
t.Helper()
day, _ := time.ParseInLocation("2006-01-02 15:04", "2026-11-10 12:00", time.Local)
details := []entity.MessageDetail{
{SnowflakeModel: entity.SnowflakeModel{ID: 9200000001, CreatedAt: day},
TaskID: 900001, AccountID: 1, ChannelID: 1, Mobile: "13000000001",
Content: "t", Status: 2, Business: "sms", Fee: 1},
{SnowflakeModel: entity.SnowflakeModel{ID: 9200000002, CreatedAt: day},
TaskID: 900002, AccountID: 1, ChannelID: 1, Mobile: "13000000002",
Content: "t", Status: 2, Business: "sms", Fee: 1},
{SnowflakeModel: entity.SnowflakeModel{ID: 9200000003, CreatedAt: day},
TaskID: 900003, AccountID: 1, ChannelID: 1, Mobile: "13000000003",
Content: "t", Status: 4, Business: "sms", Fee: 1},
{SnowflakeModel: entity.SnowflakeModel{ID: 9200000004, CreatedAt: day},
TaskID: 900004, AccountID: 2, ChannelID: 2, Mobile: "13000000004",
Content: "t", Status: 2, Business: "sms", Fee: 1},
}
if err := db.Create(&details).Error; err != nil {
t.Fatal(err)
}
if err := db.Create(&[]entity.ChannelChannel{
{Model: entity.Model{ID: 1}, Name: "c1", Code: "rcc1", UpstreamID: 7, BillingMode: "success_unknown"},
{Model: entity.Model{ID: 2}, Name: "c2", Code: "rcc2", UpstreamID: 7},
}).Error; err != nil {
t.Fatal(err)
}
svc := NewService(db)
ctx := context.Background()
if _, _, err := svc.AggregateDay(ctx, day); err != nil {
t.Fatal(err)
}
if _, err := svc.GenerateCustomerBills(ctx, "2026-11"); err != nil {
t.Fatal(err)
}
if _, err := svc.GenerateUpstreamBills(ctx, "2026-11"); err != nil {
t.Fatal(err)
}
}
// TestReconcile_AllZero 全链一致:三方零差异(S2 出口判据)。
// 账号:acc1 明细2=汇总2=账单2;acc2 1=1=1。通道:ch1 2=2;ch2 1=1。
// 上家 7:重推(2+1未知)+(1)=4=账单 4。
func TestReconcile_AllZero(t *testing.T) {
db := newBillDB(t)
seedReconcile(t, db)
rep, err := NewService(db).ReconcilePeriod(context.Background(), "2026-11")
if err != nil {
t.Fatal(err)
}
s := rep.Summary
if !s.AllZero || s.AccountDiffs != 0 || s.ChannelDiffs != 0 || s.UpstreamDiffs != 0 {
t.Fatalf("应全零: %+v rows=%+v/%+v/%+v", s, rep.Accounts, rep.Channels, rep.Upstreams)
}
if s.AccountsChecked != 2 || s.BillMissing != 0 || s.ChannelsChecked != 2 || s.UpstreamsChecked != 1 {
t.Fatalf("checked 计数错: %+v", s)
}
var bill1, bill2 entity.SettleCustomer
var up7 entity.SettleUpstream
if err := db.Where("account_id = 1").First(&bill1).Error; err != nil {
t.Fatal(err)
}
if err := db.Where("account_id = 2").First(&bill2).Error; err != nil {
t.Fatal(err)
}
if err := db.Where("upstream_id = 7").First(&up7).Error; err != nil {
t.Fatal(err)
}
if bill1.SuccessCount != 2 || bill2.SuccessCount != 1 || up7.SuccessCount != 4 {
t.Fatalf("账单快照错: %+v/%+v/%+v", bill1, bill2, up7)
}
}
// TestReconcile_ExposesDiffs 篡改三处事实源逐项暴露:
// ①consume acc1 success 2→5 → AccountDiffs=1(明细2/汇总5/账单2 同行列出);
// ②stats ch1 success 2→9 → ChannelDiffs=1(明细2/日汇总9)+ 上家重推(9+1)+1=11
//
// vs 账单4 → UpstreamDiffs=1;③删 acc2 账单 → BillMissing=1 且 AllZero 仍真
// (在①②还原后单独验证:此处先验证 missing 计数,AllZero 断言只看 diffs)。
func TestReconcile_ExposesDiffs(t *testing.T) {
db := newBillDB(t)
seedReconcile(t, db)
svc := NewService(db)
ctx := context.Background()
// ① 篡改消耗日汇总。
if err := db.Model(&entity.BillingConsumeLog{}).
Where("account_id = 1").Update("success_count", 5).Error; err != nil {
t.Fatal(err)
}
// ② 篡改通道日汇总。
if err := db.Model(&entity.StatsChannelDaily{}).
Where("channel_id = 1").Update("success_count", 9).Error; err != nil {
t.Fatal(err)
}
rep, err := svc.ReconcilePeriod(ctx, "2026-11")
if err != nil {
t.Fatal(err)
}
if rep.Summary.AccountDiffs != 1 || len(rep.Accounts) != 1 {
t.Fatalf("账号差异应 1 行: %+v rows=%+v", rep.Summary, rep.Accounts)
}
a := rep.Accounts[0]
if a.AccountID != 1 || a.DetailSuccess != 2 || a.ConsumeSuccess != 5 ||
a.BillSuccess == nil || *a.BillSuccess != 2 {
t.Fatalf("账号差异行错: %+v", a)
}
if rep.Summary.ChannelDiffs != 1 || len(rep.Channels) != 1 ||
rep.Channels[0].ChannelID != 1 || rep.Channels[0].DetailSuccess != 2 || rep.Channels[0].DailySuccess != 9 {
t.Fatalf("通道差异行错: %+v", rep.Channels)
}
if rep.Summary.UpstreamDiffs != 1 || len(rep.Upstreams) != 1 {
t.Fatalf("上家差异应 1 行: %+v", rep.Upstreams)
}
u := rep.Upstreams[0]
if u.UpstreamID != 7 || u.DailyCount != 11 || u.BillSuccess == nil || *u.BillSuccess != 4 {
t.Fatalf("上家差异行错(重推 9+1未知+1=11 vs 账单 4): %+v", u)
}
if rep.Summary.AllZero {
t.Fatal("存在差异时 AllZero 应为 false")
}
// ③ 还原①②后删下家账单:BillMissing=1,但 diffs 全零 → AllZero 仍真。
if err := db.Model(&entity.BillingConsumeLog{}).
Where("account_id = 1").Update("success_count", 2).Error; err != nil {
t.Fatal(err)
}
if err := db.Model(&entity.StatsChannelDaily{}).
Where("channel_id = 1").Update("success_count", 2).Error; err != nil {
t.Fatal(err)
}
if err := db.Where("account_id = 2").Delete(&entity.SettleCustomer{}).Error; err != nil {
t.Fatal(err)
}
rep, err = svc.ReconcilePeriod(ctx, "2026-11")
if err != nil {
t.Fatal(err)
}
if rep.Summary.BillMissing != 1 || rep.Summary.AccountDiffs != 0 {
t.Fatalf("缺账单应 BillMissing=1 且 diffs=0: %+v", rep.Summary)
}
if !rep.Summary.AllZero {
t.Fatalf("缺账单=流程状态,不应判失败: %+v", rep.Summary)
}
var missing int
for _, r := range rep.Accounts {
if r.AccountID == 2 && r.BillSuccess == nil {
missing++
}
}
if missing != 1 {
t.Fatalf("acc2 应以 bill_success=nil 列出: %+v", rep.Accounts)
}
// ④ 非法 period → ISC:400。
if _, err := svc.ReconcilePeriod(ctx, "2026-1"); err == nil {
t.Fatal("非法 period 应报错")
}
}