Skip to content

Commit d5e339b

Browse files
committed
tests for thread list pagination
1 parent b3baf5a commit d5e339b

File tree

1 file changed

+160
-1
lines changed

1 file changed

+160
-1
lines changed

spec/integ/matrix-client-event-timeline.spec.ts

Lines changed: 160 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,19 @@ limitations under the License.
1515
*/
1616

1717
import * as utils from "../test-utils/test-utils";
18-
import { ClientEvent, EventTimeline, Filter, IEvent, MatrixClient, MatrixEvent, Room } from "../../src/matrix";
18+
import {
19+
ClientEvent,
20+
Direction,
21+
EventTimeline,
22+
EventTimelineSet,
23+
Filter,
24+
IEvent,
25+
MatrixClient,
26+
MatrixEvent,
27+
Room,
28+
} from "../../src/matrix";
1929
import { logger } from "../../src/logger";
30+
import { encodeUri } from "../../src/utils";
2031
import { TestClient } from "../TestClient";
2132
import { FeatureSupport, Thread, THREAD_RELATION_TYPE } from "../../src/models/thread";
2233

@@ -925,6 +936,154 @@ describe("MatrixClient event timelines", function() {
925936
});
926937
});
927938

939+
describe("paginateEventTimeline for thread list timeline", function() {
940+
async function flushHttp<T>(promise: Promise<T>): Promise<T> {
941+
return Promise.all([promise, httpBackend.flushAllExpected()]).then(([result]) => result);
942+
}
943+
944+
describe("with server compatibility", function() {
945+
async function testPagination(timelineSet: EventTimelineSet, direction: Direction) {
946+
const RANDOM_TOKEN = "7280349c7bee430f91defe2a38a0a08c";
947+
function respondToThreads() {
948+
httpBackend.when("GET", encodeUri("/_matrix/client/r0/rooms/$roomId/threads", {
949+
$roomId: roomId,
950+
})).respond(200, {
951+
chunk: [THREAD_ROOT],
952+
state: [],
953+
next_batch: RANDOM_TOKEN,
954+
});
955+
}
956+
function respondToContext() {
957+
httpBackend.when("GET", encodeUri("/_matrix/client/r0/rooms/$roomId/context/$eventId", {
958+
$roomId: roomId,
959+
$eventId: THREAD_ROOT.event_id!,
960+
})).respond(200, {
961+
end: "",
962+
start: "",
963+
state: [],
964+
events_before: [],
965+
events_after: [],
966+
event: THREAD_ROOT,
967+
});
968+
}
969+
970+
respondToContext();
971+
await flushHttp(client.getEventTimeline(timelineSet, THREAD_ROOT.event_id!));
972+
respondToThreads();
973+
const timeline = await flushHttp(client.getLatestTimeline(timelineSet));
974+
expect(timeline).not.toBeNull();
975+
976+
respondToThreads();
977+
const success = await flushHttp(client.paginateEventTimeline(timeline!, {
978+
backwards: direction === Direction.Backward,
979+
}));
980+
expect(success).toBeTruthy();
981+
expect(timeline!.getEvents().length).toEqual(1);
982+
expect(timeline!.getEvents()[0].event).toEqual(THREAD_ROOT);
983+
expect(timeline!.getPaginationToken(direction)).toEqual(RANDOM_TOKEN);
984+
}
985+
986+
it("should allow you to paginate all threads backwards", async function() {
987+
// @ts-ignore
988+
client.clientOpts.experimentalThreadSupport = true;
989+
Thread.setServerSideSupport(FeatureSupport.Experimental);
990+
Thread.setServerSideListSupport(FeatureSupport.Stable);
991+
992+
const room = client.getRoom(roomId);
993+
const timelineSets = await (room?.createThreadsTimelineSets());
994+
expect(timelineSets).not.toBeNull();
995+
const [allThreads, myThreads] = timelineSets!;
996+
await testPagination(allThreads, Direction.Backward);
997+
await testPagination(myThreads, Direction.Backward);
998+
});
999+
1000+
it("should allow you to paginate all threads forwards", async function() {
1001+
// @ts-ignore
1002+
client.clientOpts.experimentalThreadSupport = true;
1003+
Thread.setServerSideSupport(FeatureSupport.Experimental);
1004+
Thread.setServerSideListSupport(FeatureSupport.Stable);
1005+
1006+
const room = client.getRoom(roomId);
1007+
const timelineSets = await (room?.createThreadsTimelineSets());
1008+
expect(timelineSets).not.toBeNull();
1009+
const [allThreads, myThreads] = timelineSets!;
1010+
1011+
await testPagination(allThreads, Direction.Forward);
1012+
await testPagination(myThreads, Direction.Forward);
1013+
});
1014+
});
1015+
1016+
describe("without server compatibility", function() {
1017+
async function testPagination(timelineSet: EventTimelineSet, direction: Direction) {
1018+
const RANDOM_TOKEN = "7280349c7bee430f91defe2a38a0a08c";
1019+
function respondToMessagesRequest() {
1020+
httpBackend.when("GET", encodeUri("/_matrix/client/r0/rooms/$roomId/messages", {
1021+
$roomId: roomId,
1022+
})).respond(200, {
1023+
chunk: [THREAD_ROOT],
1024+
state: [],
1025+
start: `${Direction.Forward}${RANDOM_TOKEN}2`,
1026+
end: `${Direction.Backward}${RANDOM_TOKEN}2`,
1027+
});
1028+
}
1029+
function respondToContext() {
1030+
httpBackend.when("GET", encodeUri("/_matrix/client/r0/rooms/$roomId/context/$eventId", {
1031+
$roomId: roomId,
1032+
$eventId: THREAD_ROOT.event_id!,
1033+
})).respond(200, {
1034+
end: `${Direction.Forward}${RANDOM_TOKEN}1`,
1035+
start: `${Direction.Backward}${RANDOM_TOKEN}1`,
1036+
state: [],
1037+
events_before: [],
1038+
events_after: [],
1039+
event: THREAD_ROOT,
1040+
});
1041+
}
1042+
1043+
respondToContext();
1044+
httpBackend.when("GET", "/sync").respond(200, INITIAL_SYNC_DATA);
1045+
await flushHttp(client.getEventTimeline(timelineSet, THREAD_ROOT.event_id!));
1046+
1047+
respondToMessagesRequest();
1048+
const timeline = await flushHttp(client.getLatestTimeline(timelineSet));
1049+
expect(timeline).not.toBeNull();
1050+
1051+
respondToMessagesRequest();
1052+
const success = await flushHttp(client.paginateEventTimeline(timeline!, {
1053+
backwards: direction === Direction.Backward,
1054+
}));
1055+
1056+
expect(success).toBeTruthy();
1057+
expect(timeline!.getEvents().length).toEqual(1);
1058+
expect(timeline!.getEvents()[0].event).toEqual(THREAD_ROOT);
1059+
expect(timeline!.getPaginationToken(direction)).toEqual(`${direction}${RANDOM_TOKEN}2`);
1060+
}
1061+
1062+
it("should allow you to paginate all threads", async function() {
1063+
// @ts-ignore
1064+
client.clientOpts.experimentalThreadSupport = true;
1065+
Thread.setServerSideSupport(FeatureSupport.Experimental);
1066+
Thread.setServerSideListSupport(FeatureSupport.None);
1067+
1068+
const room = client.getRoom(roomId);
1069+
1070+
httpBackend.when("POST", "/filter").respond(200, { filter_id: "fid" });
1071+
httpBackend.when("GET", "/sync").respond(200, INITIAL_SYNC_DATA);
1072+
httpBackend.when("POST", "/filter").respond(200, { filter_id: "fid" });
1073+
httpBackend.when("GET", "/sync").respond(200, INITIAL_SYNC_DATA);
1074+
1075+
const timelineSetsPromise = room?.createThreadsTimelineSets();
1076+
expect(timelineSetsPromise).not.toBeNull();
1077+
const timelineSets = await flushHttp(timelineSetsPromise!);
1078+
expect(timelineSets).not.toBeNull();
1079+
const [allThreads, myThreads] = timelineSets!;
1080+
1081+
await testPagination(allThreads, Direction.Backward);
1082+
await testPagination(myThreads, Direction.Backward);
1083+
});
1084+
});
1085+
});
1086+
9281087
describe("event timeline for sent events", function() {
9291088
const TXN_ID = "txn1";
9301089
const event = utils.mkMessage({

0 commit comments

Comments
 (0)