| 1 | package tinyclient |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "encoding/hex" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | ) |
| 9 | |
| 10 | func TestSocialNostrLinksRewritesKnownReferences(t *testing.T) { |
| 11 | pubkey := "5ac640e5df8f7945381c31f435288ba3f587fbd68efb27abaf941792f9c36369" |
| 12 | id := "0f8fc45a6e3cd2d022cf68f3ea0f413665a6d92c3d783e3ab6c2bc324432b5af" |
| 13 | pubkeyBytes, _ := hex.DecodeString(pubkey) |
| 14 | idBytes, _ := hex.DecodeString(id) |
| 15 | kind := make([]byte, 4) |
| 16 | binary.BigEndian.PutUint32(kind, 30023) |
| 17 | naddrData := append([]byte{0, 5}, []byte("hello" )...) |
| 18 | naddrData = append(naddrData, append([]byte{2, 32}, pubkeyBytes...)...) |
| 19 | naddrData = append(naddrData, append([]byte{3, 4}, kind...)...) |
| 20 | refs := []struct { |
| 21 | name string |
| 22 | ref string |
| 23 | want string |
| 24 | }{ |
| 25 | {"profile" , "nostr:" + bech32("npub" , pubkeyBytes), `<a href="/social?author=` + pubkey + `"><nostr-name pubkey="` + pubkey + `">` + shortID(pubkey) + `</nostr-name></a>` }, |
| 26 | {"event" , "nostr:" + bech32("note" , idBytes), `<a href="/e/` + id + `">Quoted post</a>` }, |
| 27 | {"article" , "nostr:" + bech32("naddr" , naddrData), `<a href="/a/30023:` + pubkey + `:hello">Article</a>` }, |
| 28 | } |
| 29 | for _, test := range refs { |
| 30 | rendered := `<p>See <a href="/open?target=` + test.ref + `">` + test.ref + `</a> today.</p>` |
| 31 | if got := socialNostrLinks(rendered); !strings.Contains(got, test.want) { |
| 32 | t.Errorf("%s: got %q, want %q" , test.name, got, test.want) |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestSocialNostrLinksLeavesUnknownMarkupAndCodeAlone(t *testing.T) { |
| 38 | input := `<p><a href="/open?target=nostr%3Ainvalid">nostr:invalid</a> <code>nostr:invalid</code> <a href="https://example.com">nostr:invalid</a></p>` |
| 39 | if got := socialNostrLinks(input); got != input { |
| 40 | t.Fatalf("got %q, want unchanged markup" , got) |
| 41 | } |
| 42 | } |