2022-11-01 10:27:03 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import { SlidingSync } from "matrix-js-sdk/src/sliding-sync";
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2022-11-01 10:27:03 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import { SlidingSyncManager } from "../src/SlidingSyncManager";
|
|
|
|
import { stubClient } from "./test-utils";
|
2022-11-01 10:27:03 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.mock("matrix-js-sdk/src/sliding-sync");
|
|
|
|
const MockSlidingSync = <jest.Mock<SlidingSync>>(<unknown>SlidingSync);
|
2022-11-01 10:27:03 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("SlidingSyncManager", () => {
|
2022-11-01 10:27:03 +00:00
|
|
|
let manager: SlidingSyncManager;
|
|
|
|
let slidingSync: SlidingSync;
|
2022-11-18 19:05:00 +00:00
|
|
|
let client: MatrixClient;
|
2022-11-01 10:27:03 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
slidingSync = new MockSlidingSync();
|
|
|
|
manager = new SlidingSyncManager();
|
2022-11-18 19:05:00 +00:00
|
|
|
client = stubClient();
|
|
|
|
// by default the client has no rooms: stubClient magically makes rooms annoyingly.
|
|
|
|
mocked(client.getRoom).mockReturnValue(null);
|
|
|
|
manager.configure(client, "invalid");
|
2022-11-01 10:27:03 +00:00
|
|
|
manager.slidingSync = slidingSync;
|
|
|
|
});
|
|
|
|
|
2022-11-18 19:05:00 +00:00
|
|
|
describe("setRoomVisible", () => {
|
|
|
|
it("adds a subscription for the room", async () => {
|
|
|
|
const roomId = "!room:id";
|
|
|
|
const subs = new Set<string>();
|
|
|
|
mocked(slidingSync.getRoomSubscriptions).mockReturnValue(subs);
|
|
|
|
mocked(slidingSync.modifyRoomSubscriptions).mockResolvedValue("yep");
|
|
|
|
await manager.setRoomVisible(roomId, true);
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(slidingSync.modifyRoomSubscriptions).toHaveBeenCalledWith(new Set<string>([roomId]));
|
2022-11-18 19:05:00 +00:00
|
|
|
});
|
|
|
|
it("adds a custom subscription for a lazy-loadable room", async () => {
|
|
|
|
const roomId = "!lazy:id";
|
2023-02-15 13:36:22 +00:00
|
|
|
const room = new Room(roomId, client, client.getUserId()!);
|
2022-11-18 19:05:00 +00:00
|
|
|
room.getLiveTimeline().initialiseState([
|
|
|
|
new MatrixEvent({
|
|
|
|
type: "m.room.create",
|
|
|
|
state_key: "",
|
|
|
|
event_id: "$abc123",
|
2023-02-15 13:36:22 +00:00
|
|
|
sender: client.getUserId()!,
|
2022-11-18 19:05:00 +00:00
|
|
|
content: {
|
2023-02-16 09:38:44 +00:00
|
|
|
creator: client.getUserId()!,
|
2022-11-18 19:05:00 +00:00
|
|
|
},
|
|
|
|
}),
|
|
|
|
]);
|
2023-02-15 13:36:22 +00:00
|
|
|
mocked(client.getRoom).mockImplementation((r: string): Room | null => {
|
2022-11-18 19:05:00 +00:00
|
|
|
if (roomId === r) {
|
|
|
|
return room;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
const subs = new Set<string>();
|
|
|
|
mocked(slidingSync.getRoomSubscriptions).mockReturnValue(subs);
|
|
|
|
mocked(slidingSync.modifyRoomSubscriptions).mockResolvedValue("yep");
|
|
|
|
await manager.setRoomVisible(roomId, true);
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(slidingSync.modifyRoomSubscriptions).toHaveBeenCalledWith(new Set<string>([roomId]));
|
2022-11-18 19:05:00 +00:00
|
|
|
// we aren't prescriptive about what the sub name is.
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(slidingSync.useCustomSubscription).toHaveBeenCalledWith(roomId, expect.anything());
|
2022-11-18 19:05:00 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-01-19 15:02:48 +00:00
|
|
|
describe("ensureListRegistered", () => {
|
|
|
|
it("creates a new list based on the key", async () => {
|
|
|
|
const listKey = "key";
|
|
|
|
mocked(slidingSync.getListParams).mockReturnValue(null);
|
|
|
|
mocked(slidingSync.setList).mockResolvedValue("yep");
|
|
|
|
await manager.ensureListRegistered(listKey, {
|
|
|
|
sort: ["by_recency"],
|
|
|
|
});
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(slidingSync.setList).toHaveBeenCalledWith(
|
2023-01-19 15:02:48 +00:00
|
|
|
listKey,
|
|
|
|
expect.objectContaining({
|
|
|
|
sort: ["by_recency"],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates an existing list based on the key", async () => {
|
|
|
|
const listKey = "key";
|
|
|
|
mocked(slidingSync.getListParams).mockReturnValue({
|
|
|
|
ranges: [[0, 42]],
|
|
|
|
});
|
|
|
|
mocked(slidingSync.setList).mockResolvedValue("yep");
|
|
|
|
await manager.ensureListRegistered(listKey, {
|
|
|
|
sort: ["by_recency"],
|
|
|
|
});
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(slidingSync.setList).toHaveBeenCalledWith(
|
2023-01-19 15:02:48 +00:00
|
|
|
listKey,
|
|
|
|
expect.objectContaining({
|
|
|
|
sort: ["by_recency"],
|
|
|
|
ranges: [[0, 42]],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("updates ranges on an existing list based on the key if there's no other changes", async () => {
|
|
|
|
const listKey = "key";
|
|
|
|
mocked(slidingSync.getListParams).mockReturnValue({
|
|
|
|
ranges: [[0, 42]],
|
|
|
|
});
|
|
|
|
mocked(slidingSync.setList).mockResolvedValue("yep");
|
|
|
|
await manager.ensureListRegistered(listKey, {
|
|
|
|
ranges: [[0, 52]],
|
|
|
|
});
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(slidingSync.setList).not.toHaveBeenCalled();
|
|
|
|
expect(slidingSync.setListRanges).toHaveBeenCalledWith(listKey, [[0, 52]]);
|
2023-01-19 15:02:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("no-ops for idential changes", async () => {
|
|
|
|
const listKey = "key";
|
|
|
|
mocked(slidingSync.getListParams).mockReturnValue({
|
|
|
|
ranges: [[0, 42]],
|
|
|
|
sort: ["by_recency"],
|
|
|
|
});
|
|
|
|
mocked(slidingSync.setList).mockResolvedValue("yep");
|
|
|
|
await manager.ensureListRegistered(listKey, {
|
|
|
|
ranges: [[0, 42]],
|
|
|
|
sort: ["by_recency"],
|
|
|
|
});
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(slidingSync.setList).not.toHaveBeenCalled();
|
|
|
|
expect(slidingSync.setListRanges).not.toHaveBeenCalled();
|
2023-01-19 15:02:48 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-01 10:27:03 +00:00
|
|
|
describe("startSpidering", () => {
|
|
|
|
it("requests in batchSizes", async () => {
|
|
|
|
const gapMs = 1;
|
|
|
|
const batchSize = 10;
|
|
|
|
mocked(slidingSync.setList).mockResolvedValue("yep");
|
|
|
|
mocked(slidingSync.setListRanges).mockResolvedValue("yep");
|
2023-01-19 11:15:08 +00:00
|
|
|
mocked(slidingSync.getListData).mockImplementation((key) => {
|
2022-11-01 10:27:03 +00:00
|
|
|
return {
|
|
|
|
joinedCount: 64,
|
|
|
|
roomIndexToRoomId: {},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
await manager.startSpidering(batchSize, gapMs);
|
|
|
|
// we expect calls for 10,19 -> 20,29 -> 30,39 -> 40,49 -> 50,59 -> 60,69
|
|
|
|
const wantWindows = [
|
2022-12-12 11:24:14 +00:00
|
|
|
[10, 19],
|
|
|
|
[20, 29],
|
|
|
|
[30, 39],
|
|
|
|
[40, 49],
|
|
|
|
[50, 59],
|
|
|
|
[60, 69],
|
2022-11-01 10:27:03 +00:00
|
|
|
];
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(slidingSync.getListData).toHaveBeenCalledTimes(wantWindows.length);
|
|
|
|
expect(slidingSync.setList).toHaveBeenCalledTimes(1);
|
|
|
|
expect(slidingSync.setListRanges).toHaveBeenCalledTimes(wantWindows.length - 1);
|
2022-11-01 10:27:03 +00:00
|
|
|
wantWindows.forEach((range, i) => {
|
|
|
|
if (i === 0) {
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(slidingSync.setList).toHaveBeenCalledWith(
|
2023-01-19 11:15:08 +00:00
|
|
|
SlidingSyncManager.ListSearch,
|
2022-11-01 10:27:03 +00:00
|
|
|
expect.objectContaining({
|
2022-12-12 11:24:14 +00:00
|
|
|
ranges: [[0, batchSize - 1], range],
|
2022-11-01 10:27:03 +00:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(slidingSync.setListRanges).toHaveBeenCalledWith(SlidingSyncManager.ListSearch, [
|
2023-01-19 12:49:20 +00:00
|
|
|
[0, batchSize - 1],
|
|
|
|
range,
|
|
|
|
]);
|
2022-11-01 10:27:03 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
it("handles accounts with zero rooms", async () => {
|
|
|
|
const gapMs = 1;
|
|
|
|
const batchSize = 10;
|
|
|
|
mocked(slidingSync.setList).mockResolvedValue("yep");
|
2023-01-19 11:15:08 +00:00
|
|
|
mocked(slidingSync.getListData).mockImplementation((key) => {
|
2022-11-01 10:27:03 +00:00
|
|
|
return {
|
|
|
|
joinedCount: 0,
|
|
|
|
roomIndexToRoomId: {},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
await manager.startSpidering(batchSize, gapMs);
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(slidingSync.getListData).toHaveBeenCalledTimes(1);
|
|
|
|
expect(slidingSync.setList).toHaveBeenCalledTimes(1);
|
|
|
|
expect(slidingSync.setList).toHaveBeenCalledWith(
|
2023-01-19 11:15:08 +00:00
|
|
|
SlidingSyncManager.ListSearch,
|
2022-11-01 10:27:03 +00:00
|
|
|
expect.objectContaining({
|
2022-12-12 11:24:14 +00:00
|
|
|
ranges: [
|
|
|
|
[0, batchSize - 1],
|
|
|
|
[batchSize, batchSize + batchSize - 1],
|
|
|
|
],
|
2022-11-01 10:27:03 +00:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it("continues even when setList rejects", async () => {
|
|
|
|
const gapMs = 1;
|
|
|
|
const batchSize = 10;
|
|
|
|
mocked(slidingSync.setList).mockRejectedValue("narp");
|
2023-01-19 11:15:08 +00:00
|
|
|
mocked(slidingSync.getListData).mockImplementation((key) => {
|
2022-11-01 10:27:03 +00:00
|
|
|
return {
|
|
|
|
joinedCount: 0,
|
|
|
|
roomIndexToRoomId: {},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
await manager.startSpidering(batchSize, gapMs);
|
2023-03-01 15:23:35 +00:00
|
|
|
expect(slidingSync.getListData).toHaveBeenCalledTimes(1);
|
|
|
|
expect(slidingSync.setList).toHaveBeenCalledTimes(1);
|
|
|
|
expect(slidingSync.setList).toHaveBeenCalledWith(
|
2023-01-19 11:15:08 +00:00
|
|
|
SlidingSyncManager.ListSearch,
|
2022-11-01 10:27:03 +00:00
|
|
|
expect.objectContaining({
|
2022-12-12 11:24:14 +00:00
|
|
|
ranges: [
|
|
|
|
[0, batchSize - 1],
|
|
|
|
[batchSize, batchSize + batchSize - 1],
|
|
|
|
],
|
2022-11-01 10:27:03 +00:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|