package handlers import ( "encoding/json" "net/http" "net/http/httptest" "testing" "gitea.futuresens.co.uk/futuresens/hardlink/v2/internal/types" ) func TestCreditCallModernPreauthReportsActualReturnedAmount(t *testing.T) { for _, total := range []string{"3100", "12000"} { t.Run(total, func(t *testing.T) { app := newCreditCallTestApp(t, func(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(map[string]any{"type": "result", "result": map[string]string{types.TransactionResult: "APPROVED", types.TransactionType: "SALE", types.TotalAmount: total, types.Reference: "SYNTH-HOLD"}}) }) database := preauthTestDatabase(t, app) app.creditCallReceipt = func(string) {} recorder := httptest.NewRecorder() app.preauthPayment(recorder, preauthStreamRequest("12000", "Sale", "2026-09-11 00:00:00 +0000")) frames := decodePaymentStream(t, recorder.Body) want := int64(12000) if total == "3100" { want = 3100 } if len(frames) != 1 || frames[0].Result == nil || frames[0].Result.Outcome != "approved" || frames[0].Result.AmountMinor != want { t.Errorf("Preauth(request12000,total%s) frames=%+v, want approved actual%d", total, frames, want) } if got := waitPreauthInsert(t, database)["TotalMinorUnits"]; got != total { t.Errorf("Preauth(total%s) SQLamount=%v, want unchanged provider amount", total, got) } }) } } func TestCreditCallModernPreauthMalformedAmountRequiresReception(t *testing.T) { for _, total := range []string{"bad", "-1", "9223372036854775808"} { t.Run(total, func(t *testing.T) { app := newCreditCallTestApp(t, func(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(map[string]any{"type": "result", "result": map[string]string{types.TransactionResult: "APPROVED", types.TransactionType: "ACCOUNT VERIFICATION", types.TotalAmount: total, types.Reference: "SYNTH-HOLD"}}) }) app.creditCallReceipt = func(string) {} recorder := httptest.NewRecorder() app.preauthPayment(recorder, preauthStreamRequest("12000", "Sale", "2026-09-11 00:00:00 +0000")) frames := decodePaymentStream(t, recorder.Body) if len(frames) != 1 || frames[0].Result == nil { t.Fatalf("Preauth(total%s) frames=%+v, want final uncertainty", total, frames) } final := frames[0].Result if final.Outcome != "unknown" || final.Retryable || final.AmountMinor != 0 || final.Reason != "amount_unverified" { t.Errorf("Preauth(total%s) final=%+v, want nonretryable unknown without fabricated amount", total, final) } }) } }