fedad36089
P01:在 RefundOrder 事务内、lockCommissionSource 之前,先扫描 system_logs 是否已存在该 order_no 的 333 (CommissionTypeRefund) 日志。命中即返回 OrderAlreadyRefunded,不写日志、不动 commission、不动 order.status, 堵住「同一订单被运营人重复退款 → 邀请人佣金被多次扣减」的写入路径。 P02:guard「已退款订单(status=6 + 333 日志)被重新激活」入口。 OrderStatusClaimed(6) 与 orderStatusRefunded(6) 共用同一枚举值, stuckOrderRecovery 把 10 分钟前的 status=6 当成「卡住的 claim」重置回 5 并重新入队 activate,进而让管理员可二次触发退款。新增 logmodel HasRefundCommissionLog helper: - queue/logic/order/stuckOrderRecoveryLogic: 跳过已有 333 日志的订单。 - queue/logic/order/activateOrderLogic.releaseClaim: 同一守卫(防御性)。 新增 internal/model/log/refund.go + 单元测试覆盖 6 个分支(命中、未命中、 子串误判、非法 JSON、空 order_no、DB 错误)。 新增 refundOrderLogic_test.go 覆盖:333 日志已存在 → 直接 rollback、 status==6 短路、status 非 2/5 短路;用 sqlmock 严格断言不再触发 commission 锁/更新/插入。 不做范围: - 不动 activateOrderLogic.calculateCommission(D03.3 单独立项)。 - 不动 OrderStatusClaimed(6) 与 OrderStatusRefunded 枚举值。 - 不动用户 34456 余额数据(D02 待架构师另行决策)。 Co-authored-by: multica-agent <github@multica.ai>
173 lines
5.1 KiB
Go
173 lines
5.1 KiB
Go
package log
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func TestHasRefundCommissionLog(t *testing.T) {
|
|
const orderNo = "ORD-REFUND-1"
|
|
|
|
t.Run("returns true when 333 log exists for the order", func(t *testing.T) {
|
|
db, mock, cleanup := newRefundLogTestDB(t)
|
|
defer cleanup()
|
|
|
|
mock.ExpectQuery("FROM `system_logs`").
|
|
WithArgs(uint8(33), fmt.Sprintf(`%%"order_no":"%s"%%`, orderNo)).
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "content"}).
|
|
AddRow(1, fmt.Sprintf(`{"type":331,"order_no":"%s","amount":100}`, orderNo)).
|
|
AddRow(2, fmt.Sprintf(`{"type":333,"order_no":"%s","amount":-100}`, orderNo)))
|
|
|
|
got, err := HasRefundCommissionLog(db, orderNo)
|
|
if err != nil {
|
|
t.Fatalf("HasRefundCommissionLog error: %v", err)
|
|
}
|
|
if !got {
|
|
t.Fatalf("HasRefundCommissionLog = false, want true")
|
|
}
|
|
assertRefundLogExpectations(t, mock)
|
|
})
|
|
|
|
t.Run("returns false when only 331/332 logs exist", func(t *testing.T) {
|
|
db, mock, cleanup := newRefundLogTestDB(t)
|
|
defer cleanup()
|
|
|
|
mock.ExpectQuery("FROM `system_logs`").
|
|
WithArgs(uint8(33), fmt.Sprintf(`%%"order_no":"%s"%%`, orderNo)).
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "content"}).
|
|
AddRow(1, fmt.Sprintf(`{"type":331,"order_no":"%s","amount":100}`, orderNo)).
|
|
AddRow(2, fmt.Sprintf(`{"type":332,"order_no":"%s","amount":50}`, orderNo)))
|
|
|
|
got, err := HasRefundCommissionLog(db, orderNo)
|
|
if err != nil {
|
|
t.Fatalf("HasRefundCommissionLog error: %v", err)
|
|
}
|
|
if got {
|
|
t.Fatalf("HasRefundCommissionLog = true, want false")
|
|
}
|
|
assertRefundLogExpectations(t, mock)
|
|
})
|
|
|
|
t.Run("returns false when no log exists for the order", func(t *testing.T) {
|
|
db, mock, cleanup := newRefundLogTestDB(t)
|
|
defer cleanup()
|
|
|
|
mock.ExpectQuery("FROM `system_logs`").
|
|
WithArgs(uint8(33), fmt.Sprintf(`%%"order_no":"%s"%%`, orderNo)).
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "content"}))
|
|
|
|
got, err := HasRefundCommissionLog(db, orderNo)
|
|
if err != nil {
|
|
t.Fatalf("HasRefundCommissionLog error: %v", err)
|
|
}
|
|
if got {
|
|
t.Fatalf("HasRefundCommissionLog = true, want false")
|
|
}
|
|
assertRefundLogExpectations(t, mock)
|
|
})
|
|
|
|
t.Run("returns false for empty order_no without querying", func(t *testing.T) {
|
|
db, mock, cleanup := newRefundLogTestDB(t)
|
|
defer cleanup()
|
|
|
|
got, err := HasRefundCommissionLog(db, "")
|
|
if err != nil {
|
|
t.Fatalf("HasRefundCommissionLog error: %v", err)
|
|
}
|
|
if got {
|
|
t.Fatalf("HasRefundCommissionLog = true, want false")
|
|
}
|
|
assertRefundLogExpectations(t, mock)
|
|
})
|
|
|
|
t.Run("ignores 333 log when order_no in content does not match", func(t *testing.T) {
|
|
// Defensive: LIKE pattern may match a substring; JSON match catches it.
|
|
db, mock, cleanup := newRefundLogTestDB(t)
|
|
defer cleanup()
|
|
|
|
mock.ExpectQuery("FROM `system_logs`").
|
|
WithArgs(uint8(33), fmt.Sprintf(`%%"order_no":"%s"%%`, orderNo)).
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "content"}).
|
|
AddRow(1, `{"type":333,"order_no":"OTHER","amount":-100}`))
|
|
|
|
got, err := HasRefundCommissionLog(db, orderNo)
|
|
if err != nil {
|
|
t.Fatalf("HasRefundCommissionLog error: %v", err)
|
|
}
|
|
if got {
|
|
t.Fatalf("HasRefundCommissionLog = true, want false (different order_no)")
|
|
}
|
|
assertRefundLogExpectations(t, mock)
|
|
})
|
|
|
|
t.Run("ignores malformed json content", func(t *testing.T) {
|
|
db, mock, cleanup := newRefundLogTestDB(t)
|
|
defer cleanup()
|
|
|
|
mock.ExpectQuery("FROM `system_logs`").
|
|
WithArgs(uint8(33), fmt.Sprintf(`%%"order_no":"%s"%%`, orderNo)).
|
|
WillReturnRows(sqlmock.NewRows([]string{"id", "content"}).
|
|
AddRow(1, `not a json`).
|
|
AddRow(2, fmt.Sprintf(`{"type":333,"order_no":"%s","amount":-100}`, orderNo)))
|
|
|
|
got, err := HasRefundCommissionLog(db, orderNo)
|
|
if err != nil {
|
|
t.Fatalf("HasRefundCommissionLog error: %v", err)
|
|
}
|
|
if !got {
|
|
t.Fatalf("HasRefundCommissionLog = false, want true")
|
|
}
|
|
assertRefundLogExpectations(t, mock)
|
|
})
|
|
|
|
t.Run("returns error when db query fails", func(t *testing.T) {
|
|
db, mock, cleanup := newRefundLogTestDB(t)
|
|
defer cleanup()
|
|
|
|
mock.ExpectQuery("FROM `system_logs`").
|
|
WithArgs(uint8(33), fmt.Sprintf(`%%"order_no":"%s"%%`, orderNo)).
|
|
WillReturnError(fmt.Errorf("connection lost"))
|
|
|
|
if _, err := HasRefundCommissionLog(db, orderNo); err == nil {
|
|
t.Fatalf("expected error, got nil")
|
|
}
|
|
assertRefundLogExpectations(t, mock)
|
|
})
|
|
}
|
|
|
|
func newRefundLogTestDB(t *testing.T) (*gorm.DB, sqlmock.Sqlmock, func()) {
|
|
t.Helper()
|
|
|
|
sqlDB, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherFunc(func(expectedSQL, actualSQL string) error {
|
|
if strings.Contains(actualSQL, expectedSQL) {
|
|
return nil
|
|
}
|
|
return fmt.Errorf("actual sql %q does not contain %q", actualSQL, expectedSQL)
|
|
})))
|
|
if err != nil {
|
|
t.Fatalf("create sqlmock: %v", err)
|
|
}
|
|
|
|
db, err := gorm.Open(mysql.New(mysql.Config{Conn: sqlDB, SkipInitializeWithVersion: true}), &gorm.Config{})
|
|
if err != nil {
|
|
_ = sqlDB.Close()
|
|
t.Fatalf("open gorm db: %v", err)
|
|
}
|
|
|
|
return db, mock, func() {
|
|
_ = sqlDB.Close()
|
|
}
|
|
}
|
|
|
|
func assertRefundLogExpectations(t *testing.T, mock sqlmock.Sqlmock) {
|
|
t.Helper()
|
|
if err := mock.ExpectationsWereMet(); err != nil {
|
|
t.Fatalf("unmet sql expectations: %v", err)
|
|
}
|
|
}
|