mirror of
https://github.com/vector-im/element-web.git
synced 2024-11-15 20:54:59 +08:00
bb4f57583f
* Initial commit Signed-off-by: Ed Geraghty <ed@geraghty.family> * Remove commented code Signed-off-by: Ed Geraghty <ed@geraghty.family> * Change function to reflect it's proxy not native support Signed-off-by: Ed Geraghty <ed@geraghty.family> * Re-add check for servers with native support Signed-off-by: Ed Geraghty <ed@geraghty.family> * Add native support check back in Signed-off-by: Ed Geraghty <ed@geraghty.family> * Re-add endpoint health check function Signed-off-by: Ed Geraghty <ed@geraghty.family> * Use inbuilt `getWellKnown` function Signed-off-by: Ed Geraghty <ed@geraghty.family> * Change the error message to the correct function Signed-off-by: Ed Geraghty <ed@geraghty.family> * Stop storing the proxyurl in the settings for now Signed-off-by: Ed Geraghty <ed@geraghty.family> * Make the logger messages more useful Signed-off-by: Ed Geraghty <ed@geraghty.family> * Start moving the checking logic directly into the controller Signed-off-by: Ed Geraghty <ed@geraghty.family> * Add missing import Signed-off-by: Ed Geraghty <ed@geraghty.family> * Get the client rather than passing it in to the functions Signed-off-by: Ed Geraghty <ed@geraghty.family> * remove invalid `function` keyword Signed-off-by: Ed Geraghty <ed@geraghty.family> * Fix imports Signed-off-by: Ed Geraghty <ed@geraghty.family> * Our new functions are private We shouldn't(?) have to use these check in future elsewhere Signed-off-by: Ed Geraghty <ed@geraghty.family> * Change our proxy check function to return a boolean Signed-off-by: Ed Geraghty <ed@geraghty.family> * Make `nativeSlidingSyncSupport` also return boolean, add in health check Signed-off-by: Ed Geraghty <ed@geraghty.family> * Disable the sliding sync option if the server doesn't support Signed-off-by: Ed Geraghty <ed@geraghty.family> * Only enable the setting if it passes (again) Signed-off-by: Ed Geraghty <ed@geraghty.family> * Update our comments to better match what's going on Signed-off-by: Ed Geraghty <ed@geraghty.family> * Remove unused dialog Signed-off-by: Ed Geraghty <ed@geraghty.family> * Add a well-known check on start-up, if sliding sync has been enabled Signed-off-by: Ed Geraghty <ed@geraghty.family> * Check against the correct endpoint... Signed-off-by: Ed Geraghty <ed@geraghty.family> * Extract baseUrl as we'll reuse it Signed-off-by: Ed Geraghty <ed@geraghty.family> * Make the logs differentiate between the types of proxy Signed-off-by: Ed Geraghty <ed@geraghty.family> * Grab the client well-known directly for use Can't use the client object at this point, it hasn't read in the well-known Signed-off-by: Ed Geraghty <ed@geraghty.family> * Add myself to the copyright assignation I wrote the majority of this file... Signed-off-by: Ed Geraghty <ed@geraghty.family> * Only return `true` if it's actually there Signed-off-by: Ed Geraghty <ed@geraghty.family> * Correct the `proxySlidingSyncSupport` function comment to match the code Signed-off-by: Ed Geraghty <ed@geraghty.family> * Correct the `nativeSlidingSyncSupport`function comment to match the code Signed-off-by: Ed Geraghty <ed@geraghty.family> * Another comment/functionality paring Signed-off-by: Ed Geraghty <ed@geraghty.family> * Remove duplicated types from the doc Signed-off-by: Ed Geraghty <ed@geraghty.family> * Move await to the previous line Removes brackets, and corrects `wellKnown` from being a `Promise` Signed-off-by: Ed Geraghty <ed@geraghty.family> * use `waitForClientWellKnown` to avoid a race condition with the request Signed-off-by: Ed Geraghty <ed@geraghty.family> * Move getting the client out of the `if`, use `waitForClientWellKnown` Signed-off-by: Ed Geraghty <ed@geraghty.family> * Remove `beforeChange` override Signed-off-by: Ed Geraghty <ed@geraghty.family> * Move proxy setup logic into `SlidingSyncManager` Signed-off-by: Ed Geraghty <ed@geraghty.family> * Swap `configure` to private, we call it from `setup` which handles proxy Signed-off-by: Ed Geraghty <ed@geraghty.family> * Promises are always `true` TIL. Signed-off-by: Ed Geraghty <ed@geraghty.family> * use `timeoutSignal` Signed-off-by: Ed Geraghty <ed@geraghty.family> * Change message when there's no server support Signed-off-by: Ed Geraghty <ed@geraghty.family> * Refactor `slidingSyncHealthCheck` Signed-off-by: Ed Geraghty <ed@geraghty.family> * Refactor `nativeSlidingSyncSupport` with try/catch Signed-off-by: Ed Geraghty <ed@geraghty.family> * Change comment to hotlink Signed-off-by: Ed Geraghty <ed@geraghty.family> * Try and make the toggle disabled when there's no endpoint Signed-off-by: Ed Geraghty <ed@geraghty.family> * Move the if statement outside the refactored fn to avoid an await Signed-off-by: Ed Geraghty <ed@geraghty.family> * Revert "Swap `configure` to private, we call it from `setup` which handles proxy" This reverts commit c80a00b50c261becc9ad58e08d2a893d572d8426. * Remove unused import Signed-off-by: Ed Geraghty <ed@geraghty.family> * Further refactor `slidingSyncHealthCheck` `proxySlidingSyncSupport` already checks the client well-known is there Signed-off-by: Ed Geraghty <ed@geraghty.family> * Make `proxySlidingSyncSupport` log on success Signed-off-by: Ed Geraghty <ed@geraghty.family> * Clarify log message for proxy being up Signed-off-by: Ed Geraghty <ed@geraghty.family> * Move the logic into SlidingSyncManager All so we can set a static variable because the disabled check isn't asynchronous :) Signed-off-by: Ed Geraghty <ed@geraghty.family> * Obviously this isn't a return so don't overwrite with false! Signed-off-by: Ed Geraghty <ed@geraghty.family> * Remove outdated comment Signed-off-by: Ed Geraghty <ed@geraghty.family> * No need to pass in the client Signed-off-by: Ed Geraghty <ed@geraghty.family> * Activating SS should probably be info level logs Signed-off-by: Ed Geraghty <ed@geraghty.family> * If we've not enabled sliding sync, push the logs down a bit Signed-off-by: Ed Geraghty <ed@geraghty.family> * Update i18n error message Signed-off-by: Ed Geraghty <ed@geraghty.family> * Remove unused i18n strings Signed-off-by: Ed Geraghty <ed@geraghty.family> * Correct log message Signed-off-by: Ed Geraghty <ed@geraghty.family> * Prettier Signed-off-by: Ed Geraghty <ed@geraghty.family> * Remove many of the log messages Signed-off-by: Ed Geraghty <ed@geraghty.family> * Short out of `checkSupport` if it's `true` Signed-off-by: Ed Geraghty <ed@geraghty.family> * Add the endpoint back into the log when we're enabling it Signed-off-by: Ed Geraghty <ed@geraghty.family> * Note in the comment that `feature_sliding_sync_proxy_url` is legacy Signed-off-by: Ed Geraghty <ed@geraghty.family> * Expand the well-known liveness check log Signed-off-by: Ed Geraghty <ed@geraghty.family> * No need to stall the client waiting for sliding sync support * `AutoDiscovery.findClientConfig` throws if the baseUrl is blank * Fix `getProxyFromWellKnown` (?) * Add missing semicolon Sorry, linter! Signed-off-by: Ed Geraghty <ed@geraghty.family> * Pass our `MatrixClient` through instead of trying to grab it Signed-off-by: Ed Geraghty <ed@geraghty.family> * Add missing return in function comment Signed-off-by: Ed Geraghty <ed@geraghty.family> * Actually pass through our Client, not the Peg object Signed-off-by: Ed Geraghty <ed@geraghty.family> * Remove SonarCube smell complaint Signed-off-by: Ed Geraghty <ed@geraghty.family> * Neew to make our other two methods public to test Signed-off-by: Ed Geraghty <ed@geraghty.family> * First passing test Hurrah! Signed-off-by: Ed Geraghty <ed@geraghty.family> * Two more tests, this time on `checkSupport` Signed-off-by: Ed Geraghty <ed@geraghty.family> * Reset our `serverSupportsSlidingSync` between tests Signed-off-by: Ed Geraghty <ed@geraghty.family> * Check the static member is being set Signed-off-by: Ed Geraghty <ed@geraghty.family> * Move the static assignation down to the relevant tests Signed-off-by: Ed Geraghty <ed@geraghty.family> * Pull getProxyFromWellKnown mocking up Signed-off-by: Ed Geraghty <ed@geraghty.family> * Check we /haven't/ shorted out Signed-off-by: Ed Geraghty <ed@geraghty.family> * Move our spy up so we can reuse it Signed-off-by: Ed Geraghty <ed@geraghty.family> * Check spidering is being called Signed-off-by: Ed Geraghty <ed@geraghty.family> * Test the proxy is declared Signed-off-by: Ed Geraghty <ed@geraghty.family> * Test entered manually Signed-off-by: Ed Geraghty <ed@geraghty.family> * Sorry, linter * I guess these strings are wrong? * Replace any with string Co-authored-by: Michael Telatynski <7t3chguy@gmail.com> --------- Signed-off-by: Ed Geraghty <ed@geraghty.family> Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
286 lines
12 KiB
TypeScript
286 lines
12 KiB
TypeScript
/*
|
|
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.
|
|
*/
|
|
|
|
import { SlidingSync } from "matrix-js-sdk/src/sliding-sync";
|
|
import { mocked } from "jest-mock";
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { SlidingSyncManager } from "../src/SlidingSyncManager";
|
|
import { stubClient } from "./test-utils";
|
|
import SlidingSyncController from "../src/settings/controllers/SlidingSyncController";
|
|
import SettingsStore from "../src/settings/SettingsStore";
|
|
|
|
jest.mock("matrix-js-sdk/src/sliding-sync");
|
|
const MockSlidingSync = <jest.Mock<SlidingSync>>(<unknown>SlidingSync);
|
|
|
|
describe("SlidingSyncManager", () => {
|
|
let manager: SlidingSyncManager;
|
|
let slidingSync: SlidingSync;
|
|
let client: MatrixClient;
|
|
|
|
beforeEach(() => {
|
|
slidingSync = new MockSlidingSync();
|
|
manager = new SlidingSyncManager();
|
|
client = stubClient();
|
|
// by default the client has no rooms: stubClient magically makes rooms annoyingly.
|
|
mocked(client.getRoom).mockReturnValue(null);
|
|
manager.configure(client, "invalid");
|
|
manager.slidingSync = slidingSync;
|
|
});
|
|
|
|
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);
|
|
expect(slidingSync.modifyRoomSubscriptions).toHaveBeenCalledWith(new Set<string>([roomId]));
|
|
});
|
|
it("adds a custom subscription for a lazy-loadable room", async () => {
|
|
const roomId = "!lazy:id";
|
|
const room = new Room(roomId, client, client.getUserId()!);
|
|
room.getLiveTimeline().initialiseState([
|
|
new MatrixEvent({
|
|
type: "m.room.create",
|
|
state_key: "",
|
|
event_id: "$abc123",
|
|
sender: client.getUserId()!,
|
|
content: {
|
|
creator: client.getUserId()!,
|
|
},
|
|
}),
|
|
]);
|
|
mocked(client.getRoom).mockImplementation((r: string): Room | null => {
|
|
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);
|
|
expect(slidingSync.modifyRoomSubscriptions).toHaveBeenCalledWith(new Set<string>([roomId]));
|
|
// we aren't prescriptive about what the sub name is.
|
|
expect(slidingSync.useCustomSubscription).toHaveBeenCalledWith(roomId, expect.anything());
|
|
});
|
|
});
|
|
|
|
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"],
|
|
});
|
|
expect(slidingSync.setList).toHaveBeenCalledWith(
|
|
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"],
|
|
});
|
|
expect(slidingSync.setList).toHaveBeenCalledWith(
|
|
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]],
|
|
});
|
|
expect(slidingSync.setList).not.toHaveBeenCalled();
|
|
expect(slidingSync.setListRanges).toHaveBeenCalledWith(listKey, [[0, 52]]);
|
|
});
|
|
|
|
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"],
|
|
});
|
|
expect(slidingSync.setList).not.toHaveBeenCalled();
|
|
expect(slidingSync.setListRanges).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("startSpidering", () => {
|
|
it("requests in batchSizes", async () => {
|
|
const gapMs = 1;
|
|
const batchSize = 10;
|
|
mocked(slidingSync.setList).mockResolvedValue("yep");
|
|
mocked(slidingSync.setListRanges).mockResolvedValue("yep");
|
|
mocked(slidingSync.getListData).mockImplementation((key) => {
|
|
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 = [
|
|
[10, 19],
|
|
[20, 29],
|
|
[30, 39],
|
|
[40, 49],
|
|
[50, 59],
|
|
[60, 69],
|
|
];
|
|
expect(slidingSync.getListData).toHaveBeenCalledTimes(wantWindows.length);
|
|
expect(slidingSync.setList).toHaveBeenCalledTimes(1);
|
|
expect(slidingSync.setListRanges).toHaveBeenCalledTimes(wantWindows.length - 1);
|
|
wantWindows.forEach((range, i) => {
|
|
if (i === 0) {
|
|
// eslint-disable-next-line jest/no-conditional-expect
|
|
expect(slidingSync.setList).toHaveBeenCalledWith(
|
|
SlidingSyncManager.ListSearch,
|
|
// eslint-disable-next-line jest/no-conditional-expect
|
|
expect.objectContaining({
|
|
ranges: [[0, batchSize - 1], range],
|
|
}),
|
|
);
|
|
return;
|
|
}
|
|
expect(slidingSync.setListRanges).toHaveBeenCalledWith(SlidingSyncManager.ListSearch, [
|
|
[0, batchSize - 1],
|
|
range,
|
|
]);
|
|
});
|
|
});
|
|
it("handles accounts with zero rooms", async () => {
|
|
const gapMs = 1;
|
|
const batchSize = 10;
|
|
mocked(slidingSync.setList).mockResolvedValue("yep");
|
|
mocked(slidingSync.getListData).mockImplementation((key) => {
|
|
return {
|
|
joinedCount: 0,
|
|
roomIndexToRoomId: {},
|
|
};
|
|
});
|
|
await manager.startSpidering(batchSize, gapMs);
|
|
expect(slidingSync.getListData).toHaveBeenCalledTimes(1);
|
|
expect(slidingSync.setList).toHaveBeenCalledTimes(1);
|
|
expect(slidingSync.setList).toHaveBeenCalledWith(
|
|
SlidingSyncManager.ListSearch,
|
|
expect.objectContaining({
|
|
ranges: [
|
|
[0, batchSize - 1],
|
|
[batchSize, batchSize + batchSize - 1],
|
|
],
|
|
}),
|
|
);
|
|
});
|
|
it("continues even when setList rejects", async () => {
|
|
const gapMs = 1;
|
|
const batchSize = 10;
|
|
mocked(slidingSync.setList).mockRejectedValue("narp");
|
|
mocked(slidingSync.getListData).mockImplementation((key) => {
|
|
return {
|
|
joinedCount: 0,
|
|
roomIndexToRoomId: {},
|
|
};
|
|
});
|
|
await manager.startSpidering(batchSize, gapMs);
|
|
expect(slidingSync.getListData).toHaveBeenCalledTimes(1);
|
|
expect(slidingSync.setList).toHaveBeenCalledTimes(1);
|
|
expect(slidingSync.setList).toHaveBeenCalledWith(
|
|
SlidingSyncManager.ListSearch,
|
|
expect.objectContaining({
|
|
ranges: [
|
|
[0, batchSize - 1],
|
|
[batchSize, batchSize + batchSize - 1],
|
|
],
|
|
}),
|
|
);
|
|
});
|
|
});
|
|
describe("checkSupport", () => {
|
|
beforeEach(() => {
|
|
SlidingSyncController.serverSupportsSlidingSync = false;
|
|
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy");
|
|
});
|
|
it("shorts out if the server has 'native' sliding sync support", async () => {
|
|
jest.spyOn(manager, "nativeSlidingSyncSupport").mockResolvedValue(true);
|
|
expect(SlidingSyncController.serverSupportsSlidingSync).toBeFalsy();
|
|
await manager.checkSupport(client);
|
|
expect(manager.getProxyFromWellKnown).not.toHaveBeenCalled(); // We return earlier
|
|
expect(SlidingSyncController.serverSupportsSlidingSync).toBeTruthy();
|
|
});
|
|
it("tries to find a sliding sync proxy url from the client well-known if there's no 'native' support", async () => {
|
|
jest.spyOn(manager, "nativeSlidingSyncSupport").mockResolvedValue(false);
|
|
expect(SlidingSyncController.serverSupportsSlidingSync).toBeFalsy();
|
|
await manager.checkSupport(client);
|
|
expect(manager.getProxyFromWellKnown).toHaveBeenCalled();
|
|
expect(SlidingSyncController.serverSupportsSlidingSync).toBeTruthy();
|
|
});
|
|
});
|
|
describe("setup", () => {
|
|
beforeEach(() => {
|
|
jest.spyOn(manager, "configure");
|
|
jest.spyOn(manager, "startSpidering");
|
|
});
|
|
it("uses the baseUrl as a proxy if no proxy is set in the client well-known and the server has no native support", async () => {
|
|
await manager.setup(client);
|
|
expect(manager.configure).toHaveBeenCalled();
|
|
expect(manager.configure).toHaveBeenCalledWith(client, client.baseUrl);
|
|
expect(manager.startSpidering).toHaveBeenCalled();
|
|
});
|
|
it("uses the proxy declared in the client well-known", async () => {
|
|
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy");
|
|
await manager.setup(client);
|
|
expect(manager.configure).toHaveBeenCalled();
|
|
expect(manager.configure).toHaveBeenCalledWith(client, "proxy");
|
|
expect(manager.startSpidering).toHaveBeenCalled();
|
|
});
|
|
it("uses the legacy `feature_sliding_sync_proxy_url` if it was set", async () => {
|
|
jest.spyOn(manager, "getProxyFromWellKnown").mockResolvedValue("proxy");
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((name: string) => {
|
|
if (name === "feature_sliding_sync_proxy_url") return "legacy-proxy";
|
|
});
|
|
await manager.setup(client);
|
|
expect(manager.configure).toHaveBeenCalled();
|
|
expect(manager.configure).toHaveBeenCalledWith(client, "legacy-proxy");
|
|
expect(manager.startSpidering).toHaveBeenCalled();
|
|
});
|
|
});
|
|
});
|