| 1 | package daemon |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/FelineStateMachine/tinyrelay/internal/event" |
| 9 | ) |
| 10 | |
| 11 | func TestBrowseProfileReturnsTheHeldProfileAndWriteRelays(t *testing.T) { |
| 12 | h := newRoomHarness(t) |
| 13 | h.must("owner" , event.KIND_PROFILE, nil, `{"name":"dami","about":"relay keeper","lud16":"dami@wallet.example"}` ) |
| 14 | h.must("owner" , 10002, [][]string{{"r" , "wss://relay.one/" }, {"r" , "wss://read.only" , "read" }, {"r" , "wss://relay.two" , "write" }}, "" ) |
| 15 | result, err := h.tenant.Execute(h.ctx, h.keys["owner" ], "browseprofile" , []json.RawMessage{rawJSON(map[string]any{})}) |
| 16 | if err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | raw, _ := json.Marshal(result) |
| 20 | var got struct { |
| 21 | Pubkey string `json:"pubkey"` |
| 22 | Profile map[string]any `json:"profile"` |
| 23 | Relays []string `json:"relays"` |
| 24 | Event *event.Event `json:"event"` |
| 25 | } |
| 26 | if err := json.Unmarshal(raw, &got); err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if got.Pubkey != h.keys["owner" ] || got.Profile["name" ] != "dami" || got.Profile["lud16" ] != "dami@wallet.example" || got.Event == nil || got.Event.Kind != 0 { |
| 30 | t.Fatalf("own profile = %s" , raw) |
| 31 | } |
| 32 | if strings.Join(got.Relays, " " ) != "wss://relay.one/ wss://relay.two" { |
| 33 | t.Fatalf("write relays = %v" , got.Relays) |
| 34 | } |
| 35 | // Another member reads the owner's profile by key; an unknown key is empty. |
| 36 | result, err = h.tenant.Execute(h.ctx, h.keys["alice" ], "browseprofile" , []json.RawMessage{rawJSON(map[string]any{"pubkey" : h.keys["owner" ]})}) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | raw, _ = json.Marshal(result) |
| 41 | if !strings.Contains(string(raw), `"name":"dami"` ) { |
| 42 | t.Fatalf("profile by key = %s" , raw) |
| 43 | } |
| 44 | result, err = h.tenant.Execute(h.ctx, h.keys["alice" ], "browseprofile" , []json.RawMessage{rawJSON(map[string]any{"pubkey" : strings.Repeat("e" , 64)})}) |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | raw, _ = json.Marshal(result) |
| 49 | if !strings.Contains(string(raw), `"event":null` ) || !strings.Contains(string(raw), `"profile":{}` ) { |
| 50 | t.Fatalf("unknown profile = %s" , raw) |
| 51 | } |
| 52 | if _, err := h.tenant.Execute(h.ctx, h.keys["alice" ], "browseprofile" , []json.RawMessage{rawJSON(map[string]any{"pubkey" : "nope" })}); err == nil || !strings.HasPrefix(err.Error(), "invalid:" ) { |
| 53 | t.Fatalf("bad key: %v" , err) |
| 54 | } |
| 55 | } |