hi-server/pkg/payment/platform_test.go
shanshanzhong 0c544268e5
Some checks failed
Build docker and publish / build (20.15.1) (push) Failing after 8m1s
refactor payment platform canonicalization and order method consistency
2026-03-04 02:15:32 -08:00

70 lines
2.1 KiB
Go

package payment
import "testing"
func TestParsePlatform(t *testing.T) {
testCases := []struct {
name string
input string
expected Platform
}{
{name: "exact AppleIAP", input: "AppleIAP", expected: AppleIAP},
{name: "snake apple_iap", input: "apple_iap", expected: AppleIAP},
{name: "kebab apple-iap", input: "apple-iap", expected: AppleIAP},
{name: "compact appleiap", input: "appleiap", expected: AppleIAP},
{name: "trimmed value", input: " apple_iap ", expected: AppleIAP},
{name: "legacy exact CryptoSaaS", input: "CryptoSaaS", expected: CryptoSaaS},
{name: "snake crypto_saas", input: "crypto_saas", expected: CryptoSaaS},
{name: "unsupported", input: "unknown_gateway", expected: UNSUPPORTED},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
got := ParsePlatform(testCase.input)
if got != testCase.expected {
t.Fatalf("ParsePlatform(%q) = %v, expected %v", testCase.input, got, testCase.expected)
}
})
}
}
func TestPlatformStringIsCanonical(t *testing.T) {
testCases := []struct {
name string
input Platform
expected string
}{
{name: "stripe", input: Stripe, expected: "Stripe"},
{name: "alipay", input: AlipayF2F, expected: "AlipayF2F"},
{name: "epay", input: EPay, expected: "EPay"},
{name: "balance", input: Balance, expected: "balance"},
{name: "crypto", input: CryptoSaaS, expected: "CryptoSaaS"},
{name: "apple", input: AppleIAP, expected: "AppleIAP"},
{name: "unsupported", input: UNSUPPORTED, expected: "unsupported"},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
got := testCase.input.String()
if got != testCase.expected {
t.Fatalf("Platform.String() = %q, expected %q", got, testCase.expected)
}
})
}
}
func TestCanonicalPlatformName(t *testing.T) {
canonical, ok := CanonicalPlatformName("apple_iap")
if !ok {
t.Fatalf("expected apple_iap to be supported")
}
if canonical != "AppleIAP" {
t.Fatalf("canonical name mismatch: got %q", canonical)
}
_, ok = CanonicalPlatformName("not_exists")
if ok {
t.Fatalf("expected unsupported platform to return ok=false")
}
}