| 1 | package tinyclient |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/FelineStateMachine/tinyrelay/internal/policy" |
| 12 | ) |
| 13 | |
| 14 | type pictureRoomsBackend struct{ *roomsBackend } |
| 15 | |
| 16 | func (b *pictureRoomsBackend) Query(ctx context.Context, method string, params []json.RawMessage, actor string) (any, error) { |
| 17 | result, err := b.roomsBackend.Query(ctx, method, params, actor) |
| 18 | if err != nil { |
| 19 | return result, err |
| 20 | } |
| 21 | page, ok := result.(map[string]any) |
| 22 | if !ok { |
| 23 | return result, nil |
| 24 | } |
| 25 | if method == "browserooms" { |
| 26 | for _, item := range page["items" ].([]any) { |
| 27 | item.(map[string]any)["picture" ] = "https://cdn.example/general.png" |
| 28 | } |
| 29 | } else if room, ok := page["room" ].(map[string]any); ok { |
| 30 | room["picture" ] = "https://cdn.example/general.png" |
| 31 | } |
| 32 | return page, nil |
| 33 | } |
| 34 | |
| 35 | func TestRoomSurfacesRenderStableSeedmarkFallbackAvatars(t *testing.T) { |
| 36 | app, _ := roomsApp(t, roomOwner) |
| 37 | for _, path := range []string{"/chat" , "/rooms/general" , "/rooms/general/thread/" + roomThread} { |
| 38 | body := roomsPage(t, app, path) |
| 39 | if !strings.Contains(body, `fallback="/avatars/v1/` ) { |
| 40 | t.Fatalf("%s has no Seedmark room fallback: %s" , path, body) |
| 41 | } |
| 42 | if strings.Count(body, "<room-avatar" ) == 0 { |
| 43 | t.Fatalf("%s has no room avatar element" , path) |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func TestRoomAvatarSeedAndTenantScoping(t *testing.T) { |
| 49 | first := roomAvatarURL("https://relay.example" , "general" ) |
| 50 | if first == "" || first != roomAvatarURL("https://relay.example" , "general" ) { |
| 51 | t.Fatalf("room avatar URL is not stable: %q" , first) |
| 52 | } |
| 53 | if first == roomAvatarURL("https://relay.example" , "build" ) { |
| 54 | t.Fatal("different room IDs share an avatar URL" ) |
| 55 | } |
| 56 | if scoped := injectBase(`<room-avatar fallback="` +first+`"><img src="` +first+`"></room-avatar>` , "/r/alice" ); !strings.Contains(scoped, `fallback="/r/alice` +first+`"` ) || !strings.Contains(scoped, `src="/r/alice` +first+`"` ) { |
| 57 | t.Fatalf("tenant prefix missed room avatar URL: %s" , scoped) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | func TestRoomAvatarUsesValidatedPictureForSSR(t *testing.T) { |
| 62 | base := &roomsBackend{fakeBackend: fakeBackend{policy: policy.Defaults(roomOwner)}} |
| 63 | app, err := New(&pictureRoomsBackend{roomsBackend: base}, Options{Actor: func(*http.Request) (string, error) { return roomOwner, nil }}) |
| 64 | if err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | recorder := httptest.NewRecorder() |
| 68 | app.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, "/rooms/general" , nil)) |
| 69 | if recorder.Code != http.StatusOK { |
| 70 | t.Fatalf("status = %d" , recorder.Code) |
| 71 | } |
| 72 | body := recorder.Body.String() |
| 73 | if !strings.Contains(body, `picture="https://cdn.example/general.png"` ) || !strings.Contains(body, `<img src="https://cdn.example/general.png"` ) { |
| 74 | t.Fatalf("validated room picture was not preferred in SSR: %s" , body) |
| 75 | } |
| 76 | } |