220 lines
8.6 KiB
Go
220 lines
8.6 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) string {
|
|
period := time.Now().Format("2006-01")
|
|
t.Helper()
|
|
// S4·D3:明细造数须在热窗内(archive.HotFloor 之前明细侧退化为两方基线);
|
|
// day=现在截断小时(period=当月动态推导,规避月初跨月边界)。
|
|
day := time.Now().Truncate(time.Hour)
|
|
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, period); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := svc.GenerateUpstreamBills(ctx, period); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return period
|
|
}
|
|
|
|
// 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)
|
|
period := seedReconcile(t, db)
|
|
// S4·D3:时钟注入=period 月 3 号(对账窗起点[月初]落在热窗内→三方比对;
|
|
// 造数明细 day=真实 now 同在窗内,数值比较不受影响)。
|
|
nowAt, _ := time.ParseInLocation("2006-01", period, time.Local)
|
|
svc0 := NewService(db)
|
|
svc0.nowFn = func() time.Time { return nowAt.AddDate(0, 0, 2) }
|
|
rep, err := svc0.ReconcilePeriod(context.Background(), period)
|
|
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_HotWindowFallback S4·D3:对账窗起点在热窗外(月中,明细部分
|
|
// 已归档 CH)→ DetailPartial=true,明细↔汇总比对退化为不判(篡改汇总不报
|
|
// 差异——两方基线=日汇总↔结算单,归档清退≠数据丢失);时钟固定月 20 号
|
|
// 任何日历日运行稳定。
|
|
func TestReconcile_HotWindowFallback(t *testing.T) {
|
|
db := newBillDB(t)
|
|
period := seedReconcile(t, db)
|
|
svc := NewService(db)
|
|
nowAt, _ := time.ParseInLocation("2006-01", period, time.Local)
|
|
svc.nowFn = func() time.Time { return nowAt.AddDate(0, 0, 19) } // 月中=窗起点早于热窗下限
|
|
// 仅制造「明细↔汇总」不一致:汇总与账单同步篡改至同值(两方[汇总↔账单]
|
|
// 一致;明细侧若参与比对必报 2≠9 差异)。
|
|
db.Model(&entity.BillingConsumeLog{}).Where("account_id = 1").Update("success_count", 9)
|
|
db.Model(&entity.SettleCustomer{}).Where("account_id = ? AND period = ?", 1, period).
|
|
Update("success_count", 9)
|
|
db.Model(&entity.StatsChannelDaily{}).Where("channel_id = 1").Update("success_count", 9)
|
|
// 上家账单同步=重推值(ch1[9+1unknown]+ch2[1]=11)。
|
|
db.Model(&entity.SettleUpstream{}).Where("period = ?", period).Update("success_count", 11)
|
|
rep, err := svc.ReconcilePeriod(context.Background(), period)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !rep.DetailPartial {
|
|
t.Fatal("月中对账应 DetailPartial=true")
|
|
}
|
|
if rep.Summary.AccountDiffs != 0 || rep.Summary.ChannelDiffs != 0 {
|
|
t.Fatalf("两方基线不应判明细↔汇总差异: %+v", rep.Summary)
|
|
}
|
|
if !rep.Summary.AllZero {
|
|
t.Fatalf("两方基线应 AllZero: %+v", rep.Summary)
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
period := seedReconcile(t, db)
|
|
svc := NewService(db)
|
|
nowAt, _ := time.ParseInLocation("2006-01", period, time.Local)
|
|
svc.nowFn = func() time.Time { return nowAt.AddDate(0, 0, 2) } // 月 3 号=窗起点在热窗内
|
|
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, period)
|
|
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, period)
|
|
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 应报错")
|
|
}
|
|
}
|