feat:采购日志
This commit is contained in:
@@ -101,7 +101,7 @@ func TestRefreshDashboard_UpdatesChangedCost(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPurchaseTokens_BlocksWhenUpstreamStockInsufficient(t *testing.T) {
|
||||
func TestPurchaseTokens_DowngradesWhenUpstreamStockInsufficient(t *testing.T) {
|
||||
db := newRockTestDB(t)
|
||||
var claimCalls int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -124,23 +124,30 @@ func TestPurchaseTokens_BlocksWhenUpstreamStockInsufficient(t *testing.T) {
|
||||
svc := NewRockService(db, NewRockClient(server.URL))
|
||||
setRockTestBearer(t, svc, "bearer-test")
|
||||
|
||||
_, err := svc.PurchaseTokens(context.Background(), 2, false)
|
||||
if err == nil || !strings.Contains(err.Error(), "上游可用库存不足") {
|
||||
t.Fatalf("库存不足时应阻止采购,实际 err=%v", err)
|
||||
tokens, err := svc.PurchaseTokens(context.Background(), 2, false)
|
||||
if err != nil {
|
||||
t.Fatalf("上游库存不足时应降级采购而非失败,实际 err=%v", err)
|
||||
}
|
||||
if calls := atomic.LoadInt32(&claimCalls); calls != 0 {
|
||||
t.Fatalf("库存不足时不应调用上游 claim,实际调用 %d 次", calls)
|
||||
if calls := atomic.LoadInt32(&claimCalls); calls != 1 {
|
||||
t.Fatalf("降级后应调用上游 claim 1 次,实际 %d 次", calls)
|
||||
}
|
||||
if len(tokens) != 1 {
|
||||
t.Fatalf("期望按库存上限入库 1 个 Token,实际 %d 个", len(tokens))
|
||||
}
|
||||
summary := svc.LastPurchaseSummary()
|
||||
if summary == nil {
|
||||
t.Fatal("缺少采购摘要")
|
||||
}
|
||||
if summary.PlannedCount != 1 || summary.LimitReason != "upstream_stock" {
|
||||
t.Fatalf("降级信息不符: planned=%d reason=%s", summary.PlannedCount, summary.LimitReason)
|
||||
}
|
||||
|
||||
var count int64
|
||||
db.Model(&model.RockToken{}).Count(&count)
|
||||
if count != 0 {
|
||||
t.Fatalf("库存不足时不应入库任何 Token,实际 %d 条", count)
|
||||
var entry model.RockPurchaseLog
|
||||
if err := db.Order("id DESC").First(&entry).Error; err != nil {
|
||||
t.Fatalf("查询采购日志失败: %v", err)
|
||||
}
|
||||
|
||||
cfg, _ := svc.GetConfig(context.Background())
|
||||
if !strings.Contains(cfg.LastError, "上游可用库存不足") {
|
||||
t.Fatalf("last_error 应记录库存不足,实际 %q", cfg.LastError)
|
||||
if entry.Status != "partial" || entry.MaxByStock != 1 {
|
||||
t.Fatalf("采购日志不符: status=%s max_by_stock=%d", entry.Status, entry.MaxByStock)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user