subscription_test.go raw
1 package bridge
2
3 import (
4 "strings"
5 "testing"
6
7 "git.smesh.lol/nostrino/pkg/nostr"
8 )
9
10 // TestOnlyTheAnswerToOurQueryIsAccepted: the reply is matched by the request it answers, because a
11 // relay may have several gateways asking at once.
12 func TestOnlyTheAnswerToOurQueryIsAccepted(t *testing.T) {
13 event := &nostr.Event{
14 Kind: 23199,
15 Tags: nostr.Tags{{"t", "subscription"}, {"e", "abc"}},
16 Content: `{"paid":true,"remaining_seconds":3600,"rate_sats_per_day":1000,"paid_until":"2026-10-27T12:00:00Z"}`,
17 }
18
19 health, ok := decodeSubscription(event, "abc")
20
21 if !ok || !health.Paid || health.RemainingSeconds != 3600 || health.RateSatsPerDay != 1000 {
22 t.Fatalf("answer: %+v %v", health, ok)
23 }
24
25 if _, ok := decodeSubscription(event, "other"); ok {
26 t.Fatal("an answer to a different query was accepted")
27 }
28
29 if _, ok := decodeSubscription(&nostr.Event{Kind: 23199, Tags: nostr.Tags{{"e", "abc"}}, Content: "not json"}, "abc"); ok {
30 t.Fatal("a malformed answer was accepted")
31 }
32 }
33
34 // TestTheWarningSaysWhatToDo: an operator reading health should see either nothing, a countdown, or
35 // the reason access stopped.
36 func TestTheWarningSaysWhatToDo(t *testing.T) {
37 cases := []struct {
38 name string
39 health RelayHealth
40 want string
41 }{
42 {"free relay", RelayHealth{Authorized: true}, ""},
43 {"paid and plentiful", RelayHealth{RateSatsPerDay: 1000, Paid: true, RemainingSeconds: 5 * 86400}, ""},
44 {"paid but expiring", RelayHealth{RateSatsPerDay: 1000, Paid: true, RemainingSeconds: 3600}, "hours of paid access left"},
45 {"lapsed", RelayHealth{RateSatsPerDay: 1000}, "no paid access"},
46 {"blocked", RelayHealth{RateSatsPerDay: 1000, Blocked: true}, "blocked"},
47 }
48
49 for _, test := range cases {
50 got := subscriptionWarning(test.health)
51
52 if test.want == "" && got != "" {
53 t.Fatalf("%s: unexpected warning %q", test.name, got)
54 }
55
56 if !strings.Contains(got, test.want) {
57 t.Fatalf("%s: warning %q does not contain %q", test.name, got, test.want)
58 }
59 }
60 }
61