package bridge import ( "strings" "testing" "git.smesh.lol/nostrino/pkg/nostr" ) // TestOnlyTheAnswerToOurQueryIsAccepted: the reply is matched by the request it answers, because a // relay may have several gateways asking at once. func TestOnlyTheAnswerToOurQueryIsAccepted(t *testing.T) { event := &nostr.Event{ Kind: 23199, Tags: nostr.Tags{{"t", "subscription"}, {"e", "abc"}}, Content: `{"paid":true,"remaining_seconds":3600,"rate_sats_per_day":1000,"paid_until":"2026-10-27T12:00:00Z"}`, } health, ok := decodeSubscription(event, "abc") if !ok || !health.Paid || health.RemainingSeconds != 3600 || health.RateSatsPerDay != 1000 { t.Fatalf("answer: %+v %v", health, ok) } if _, ok := decodeSubscription(event, "other"); ok { t.Fatal("an answer to a different query was accepted") } if _, ok := decodeSubscription(&nostr.Event{Kind: 23199, Tags: nostr.Tags{{"e", "abc"}}, Content: "not json"}, "abc"); ok { t.Fatal("a malformed answer was accepted") } } // TestTheWarningSaysWhatToDo: an operator reading health should see either nothing, a countdown, or // the reason access stopped. func TestTheWarningSaysWhatToDo(t *testing.T) { cases := []struct { name string health RelayHealth want string }{ {"free relay", RelayHealth{Authorized: true}, ""}, {"paid and plentiful", RelayHealth{RateSatsPerDay: 1000, Paid: true, RemainingSeconds: 5 * 86400}, ""}, {"paid but expiring", RelayHealth{RateSatsPerDay: 1000, Paid: true, RemainingSeconds: 3600}, "hours of paid access left"}, {"lapsed", RelayHealth{RateSatsPerDay: 1000}, "no paid access"}, {"blocked", RelayHealth{RateSatsPerDay: 1000, Blocked: true}, "blocked"}, } for _, test := range cases { got := subscriptionWarning(test.health) if test.want == "" && got != "" { t.Fatalf("%s: unexpected warning %q", test.name, got) } if !strings.Contains(got, test.want) { t.Fatalf("%s: warning %q does not contain %q", test.name, got, test.want) } } }