package paymentutil

import (
	"errors"
	"testing"
)

func TestDefaultCallbackAck(t *testing.T) {
	cases := []struct {
		provider    string
		success     bool
		status      int
		contentType string
		body        string
	}{
		{ProviderAlipay, true, 200, "text/plain; charset=utf-8", "success"},
		{ProviderWechatV2, false, 500, "application/xml; charset=utf-8", "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[FAIL]]></return_msg></xml>"},
		{ProviderWechatV3, true, 200, "application/json; charset=utf-8", `{"code":"SUCCESS","message":"成功"}`},
		{ProviderApple, true, 200, "", ""},
	}
	for _, tc := range cases {
		ack := DefaultCallbackAck(tc.provider, tc.success)
		if ack.StatusCode != tc.status || ack.ContentType != tc.contentType || string(ack.Body) != tc.body {
			t.Fatalf("%s ack = %#v", tc.provider, ack)
		}
	}
}

func TestCallbackFailureUsesProviderAck(t *testing.T) {
	fallback := DefaultCallbackAck(ProviderAlipay, false)
	want := DefaultCallbackAck(ProviderWechatV3, false)
	err := &CallbackError{Cause: errors.New("provider rejected"), Ack: want}
	got := CallbackFailure(err, fallback)
	if got.StatusCode != want.StatusCode || string(got.Body) != string(want.Body) {
		t.Fatalf("ack = %#v, want %#v", got, want)
	}
}
