| 1 | package tinyclient |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/FelineStateMachine/tinyrelay/internal/policy" |
| 8 | ) |
| 9 | |
| 10 | func TestNIP19PointersMatchReferenceEncodings(t *testing.T) { |
| 11 | key := strings.Repeat("a" , 64) |
| 12 | if got := nprofile(key, "wss://relay.example" ); got != "nprofile1qqs242424242424242424242424242424242424242424242424242spzdmhxue69uhhyetvv9ujuetcv9khqmr9nxcavj" { |
| 13 | t.Fatalf("nprofile = %s" , got) |
| 14 | } |
| 15 | if got := naddr(39000, key, "_" , "wss://relay.example" ); got != "naddr1qqq47qgnwaehxw309aex2mrp0yhx27rpd4cxcegzyz424242424242424242424242424242424242424242424242425qcyqqqfskquzv50g" { |
| 16 | t.Fatalf("naddr = %s" , got) |
| 17 | } |
| 18 | if got := bech32("npub" , mustHex(key)); got != "npub1424242424242424242424242424242424242424242424242424qamrcaj" { |
| 19 | t.Fatalf("npub = %s" , got) |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestConnectionsExpandPlaceholdersAndHideUserLinksForGuests(t *testing.T) { |
| 24 | data := PageData{URL: "https://relay.example" , Identity: strings.Repeat("a" , 64), Policy: policy.Defaults(strings.Repeat("b" , 64))} |
| 25 | rows := []any{map[string]any{"template" : "repos" , "title" : "Repos" , "visibility" : "public" , "links" : []any{ |
| 26 | map[string]any{"label" : "Open" , "href" : "https://gitworkshop.dev/relay/{relay:host|enc}" }, |
| 27 | map[string]any{"Label" : "Copy clone command" , "Copy" : "git clone '{relay:web}/{user:npub}/{input:repo}.git'" }, |
| 28 | }}, map[string]any{"template" : "group" , "title" : "Group" , "qr" : "nostr:{relay:naddr}" , "links" : []any{map[string]any{"label" : "Open" , "href" : "https://app.flotilla.social/spaces/{relay:host|enc}" }}}} |
| 29 | cards := expandConnections(rows, data) |
| 30 | repos := valueMap(cards[0]) |
| 31 | links := browseRows(repos["links" ]) |
| 32 | if len(links) != 1 || valueMap(links[0])["href" ] != "https://gitworkshop.dev/relay/relay.example" || repos["hidden" ] != 1 { |
| 33 | t.Fatalf("guest repos card = %v" , repos) |
| 34 | } |
| 35 | group := valueMap(cards[1]) |
| 36 | if !strings.HasPrefix(plainString(group["qr" ]), "nostr:naddr1" ) { |
| 37 | t.Fatalf("group qr = %v" , group["qr" ]) |
| 38 | } |
| 39 | data.Actor = strings.Repeat("c" , 64) |
| 40 | cards = expandConnections(rows, data) |
| 41 | links = browseRows(valueMap(cards[0])["links" ]) |
| 42 | if len(links) != 2 || !strings.Contains(plainString(valueMap(links[1])["copy" ]), "/npub1enxvenxv" ) || !strings.Contains(plainString(valueMap(links[1])["copy" ]), "{input:repo}" ) { |
| 43 | t.Fatalf("member repos links = %v" , links) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func mustHex(value string) []byte { |
| 48 | out := make([]byte, len(value)/2) |
| 49 | for i := range out { |
| 50 | out[i] = hexNibble(value[2*i])<<4 | hexNibble(value[2*i+1]) |
| 51 | } |
| 52 | return out |
| 53 | } |
| 54 | |
| 55 | func hexNibble(c byte) byte { |
| 56 | switch { |
| 57 | case c >= '0' && c <= '9' : |
| 58 | return c - '0' |
| 59 | case c >= 'a' && c <= 'f' : |
| 60 | return c - 'a' + 10 |
| 61 | } |
| 62 | return 0 |
| 63 | } |