176 lines
6.3 KiB
Go
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 应报错")
|
|
}
|
|
}
|