package nutreq import ( "strings" "testing" ) func TestRoundTrip(t *testing.T) { p := New(500, "https://mint.example.com", "musiquay access") p.Nut10 = &NUT10Option{Kind: "P2PK", Data: strings.Repeat("02", 33)} s, err := p.EncodeA() if err != nil { t.Fatal(err) } if !strings.HasPrefix(s, Prefix) { t.Fatalf("encoded request does not start with %q", Prefix) } got, err := Decode(s) if err != nil { t.Fatal(err) } if got.Amount != 500 || got.Unit != "sat" || got.Description != "musiquay access" { t.Fatalf("fields did not survive: %+v", got) } if len(got.Mints) != 1 || got.Mints[0] != "https://mint.example.com" { t.Fatalf("mint list did not survive: %v", got.Mints) } if got.Nut10 == nil || got.Nut10.Kind != "P2PK" || got.Nut10.Data != p.Nut10.Data { t.Fatalf("nut10 did not survive: %+v", got.Nut10) } } func TestAmountWithoutUnitRejected(t *testing.T) { if _, err := (&PaymentRequest{Amount: 10}).EncodeA(); err == nil { t.Fatal("encoded an amount without a unit") } // CBOR {"a": 10} with no unit. if _, err := Decode(Prefix + "oWFhCg"); err == nil { t.Fatal("decoded an amount without a unit") } } func TestDecodeRejectsOtherEncodings(t *testing.T) { for _, s := range []string{"", "cashuB...", "creqB1something"} { if _, err := Decode(s); err == nil { t.Fatalf("decoded %q", s) } } } func TestAcceptsMint(t *testing.T) { p := New(10, "https://mint.example.com/", "") if !p.AcceptsMint("https://mint.example.com") { t.Fatal("trailing slash broke mint matching") } if p.AcceptsMint("https://other.example") { t.Fatal("an unlisted mint was accepted") } empty := New(10, "", "") if !empty.AcceptsMint("https://anything") { t.Fatal("an empty mint list should accept any mint") } }