| 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 typedRoomsBackend struct{ fakeBackend } |
| 15 | |
| 16 | func (typedRoomsBackend) Query(context.Context, string, []json.RawMessage, string) (any, error) { |
| 17 | panic("typed room reads must not use Backend.Query" ) |
| 18 | } |
| 19 | |
| 20 | func (typedRoomsBackend) ListRooms(context.Context, string, string, int) (RoomList, error) { |
| 21 | return RoomList{Rooms: []RoomSummary{{ID: "build" , Name: "Build" , Access: "open" }}}, nil |
| 22 | } |
| 23 | |
| 24 | func (typedRoomsBackend) ReadRoom(context.Context, string, string, string, int) (RoomPage, error) { |
| 25 | return RoomPage{Room: RoomSummary{ID: "build" , Name: "Build" }}, nil |
| 26 | } |
| 27 | |
| 28 | func (typedRoomsBackend) ReadThread(context.Context, string, string, string, string, int) (RoomPage, error) { |
| 29 | return RoomPage{Room: RoomSummary{ID: "build" , Name: "Build" }}, nil |
| 30 | } |
| 31 | |
| 32 | func TestTypedRoomsReaderAvoidsLegacyQuery(t *testing.T) { |
| 33 | backend := typedRoomsBackend{fakeBackend: fakeBackend{policy: policy.Defaults(strings.Repeat("a" , 64))}} |
| 34 | app, err := New(&backend, Options{}) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | for _, path := range []string{"/rooms/build" } { |
| 39 | response := httptest.NewRecorder() |
| 40 | app.ServeHTTP(response, httptest.NewRequest(http.MethodGet, path, nil)) |
| 41 | if response.Code != http.StatusOK { |
| 42 | t.Fatalf("%s: status %d" , path, response.Code) |
| 43 | } |
| 44 | if !strings.Contains(response.Body.String(), "Build" ) { |
| 45 | t.Fatalf("%s: typed room result missing from page" , path) |
| 46 | } |
| 47 | } |
| 48 | } |