Files

48 lines
1.7 KiB
Go

package reconcile_test
import (
"context"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/wangjia/pay/config"
"github.com/wangjia/pay/internal/accounts"
"github.com/wangjia/pay/internal/model"
"github.com/wangjia/pay/internal/provider"
"github.com/wangjia/pay/internal/provider/crypto"
"github.com/wangjia/pay/internal/reconcile"
"github.com/wangjia/pay/internal/store"
)
func TestOrphanScanTaskRecordsUnmatched(t *testing.T) {
const addr = "TOrphanJobAddr0000000000000000000000"
now := time.Date(2026, 7, 10, 12, 0, 0, 0, time.UTC)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{"data":[
{"transaction_id":"TX-ORPHAN","to":"` + addr + `","type":"Transfer","value":"88880000","block_timestamp":` + strconv.FormatInt(now.Add(-3*time.Minute).UnixMilli(), 10) + `}
]}`))
}))
defer ts.Close()
t.Setenv("CRY_ADDRESS", addr)
db := model.OpenTestDB(t)
orders := store.NewOrderStore(db)
orphanStore := store.NewOrphanStore(db)
acctReg := accounts.New([]config.AccountConfig{{AccountID: "cry-1", Channel: "crypto", Enabled: true, CredentialEnvPrefix: "cry"}})
preg := provider.NewRegistry()
preg.Register(crypto.New(acctReg, crypto.WithBaseURL(ts.URL), crypto.WithHTTPClient(ts.Client()), crypto.WithNow(func() time.Time { return now })))
task := reconcile.OrphanScanTask(preg, acctReg, orders, orphanStore, time.Hour, func() time.Time { return now })
if err := task(context.Background()); err != nil {
t.Fatalf("task: %v", err)
}
var cnt int64
db.Model(&model.OrphanPayment{}).Where("tx_id = ?", "TX-ORPHAN").Count(&cnt)
if cnt != 1 {
t.Fatalf("应落 1 条孤儿, got %d", cnt)
}
}