package commerce

import "testing"

func TestParseDecimalCents(t *testing.T) {
	tests := []struct {
		input string
		want  int
	}{
		{input: "12", want: 1200},
		{input: "12.3", want: 1230},
		{input: "12.34", want: 1234},
		{input: "0001.05", want: 105},
		{input: " 0.01 ", want: 1},
	}
	for _, test := range tests {
		t.Run(test.input, func(t *testing.T) {
			got, err := parseDecimalCents(test.input)
			if err != nil {
				t.Fatalf("parseDecimalCents(%q): %v", test.input, err)
			}
			if got != test.want {
				t.Fatalf("parseDecimalCents(%q) = %d, want %d", test.input, got, test.want)
			}
		})
	}
}

func TestParseDecimalCentsRejectsNonCanonicalOrUnsafeValues(t *testing.T) {
	for _, input := range []string{
		"", "0", "0.00", "1.", "+1.00", "-1.00", ".10", "1.234", "1e2", "1,00", "92233720368547759.00",
	} {
		t.Run(input, func(t *testing.T) {
			if got, err := parseDecimalCents(input); err == nil {
				t.Fatalf("parseDecimalCents(%q) = %d, want error", input, got)
			}
		})
	}
}

func TestPaymentMethodFromPluginID(t *testing.T) {
	tests := map[string]string{
		"epay_alipay":    "alipay",
		"payment_wxpay":  "wxpay",
		"pay_qqpay":      "qqpay",
		"custom_gateway": "custom_gateway",
	}
	for input, want := range tests {
		if got := paymentMethodFromPluginID(input); got != want {
			t.Fatalf("paymentMethodFromPluginID(%q) = %q, want %q", input, got, want)
		}
	}
}
