package registry import ( "encoding/json" "strings" "testing" "time" "git.smesh.lol/nostrino/pkg/nostr" ) // TestATipBelongsToItsChain: a gateway that serves two chains must not publish one number that // reads as both chains' height. Two chains in a swap need not be a parent and a fork. func TestATipBelongsToItsChain(t *testing.T) { info := Info{ Chains: []string{"btc", "blake"}, Tips: map[string]int64{"btc": 972945, "blake": 978795}, Relays: []string{"wss://example"}, } event, err := Event(info, [32]byte{1}) if err != nil { t.Fatal(err) } var back Info if err := json.Unmarshal([]byte(event.Content), &back); err != nil { t.Fatal(err) } if back.Tip != 0 { t.Fatalf("an aggregate tip of %d was published for two chains", back.Tip) } if back.Tips["btc"] != 972945 || back.Tips["blake"] != 978795 { t.Fatalf("per-chain tips %v", back.Tips) } } // TestAnAnnouncementExpires: a presence record that never expires is a client // connecting to a gateway that was decommissioned months ago. func TestAnAnnouncementExpires(t *testing.T) { event, err := Event(Info{Chains: []string{"btc"}}, [32]byte{2}) if err != nil { t.Fatal(err) } var tagExpiry int64 found := false for _, tag := range event.Tags { if len(tag) >= 2 && tag[0] == "expiration" { found = true tagExpiry = announcedExpiry(event) } } if !found { t.Fatal("the announcement carries no expiration tag") } var payload Info if err := json.Unmarshal([]byte(event.Content), &payload); err != nil { t.Fatal(err) } if payload.ExpiresAt != tagExpiry { t.Fatalf("content expires_at %d, tag says %d: one value, two sources", payload.ExpiresAt, tagExpiry) } if want := event.CreatedAt + int64(AnnouncementTTL/time.Second); tagExpiry != want { t.Fatalf("expiry %d, want %d", tagExpiry, want) } } // TestAnOldAnnouncementIsTimedFromItsCreation: a payload from before the tag // existed is bounded by its own created_at rather than trusted for ever. func TestAnOldAnnouncementIsTimedFromItsCreation(t *testing.T) { event := &nostr.Event{Kind: KindGateway, CreatedAt: 1_700_000_000} got := announcedExpiry(event) if want := int64(1_700_000_000) + int64(AnnouncementTTL/time.Second); got != want { t.Fatalf("expiry %d, want %d", got, want) } } // TestOnIgnoresTheSchemesAndSlash: a relay URL is an identity of host and port, // so a gateway naming wss://relay.example/ is on the client's relay.example. func TestOnIgnoresTheSchemesAndSlash(t *testing.T) { info := Info{Relays: []string{"wss://relay.example/"}} if !info.On("ws://relay.example") { t.Fatal("a gateway announcing wss://relay.example/ is not seen on ws://relay.example") } if info.On("wss://other.example") { t.Fatal("a gateway is seen on a relay it does not name") } served := Info{ServedBy: "wss://relay.example"} if !served.On("wss://relay.example/") { t.Fatal("the relay that served the announcement is not a relay the gateway is on") } } // TestTheAnnouncementContentIsNotHTMLEscaped: the URI carries an ampersand, and // Go's encoder would write it as \u0026. That is a different string, it differs // from what a browser produces for the same value, and the backslash it puts in // the content is the one case a canonical encoder can get wrong. func TestTheAnnouncementContentIsNotHTMLEscaped(t *testing.T) { info := Info{ Chains: []string{"btc"}, URI: "nostr+chaindata://abc?chains=btc%2Cblake&relay=wss%3A%2F%2Fexample&v=1", Relays: []string{"wss://example"}, Tips: map[string]int64{"btc": 1}, } event, err := Event(info, [32]byte{1}) if err != nil { t.Fatalf("event: %v", err) } if !strings.Contains(event.Content, "&relay=") { t.Fatalf("content lost the ampersand: %s", event.Content) } if strings.Contains(event.Content, `\u00`) { t.Fatalf("content carries a Go HTML escape: %s", event.Content) } }