2022-05-04 11:29:13 +02:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector Ltd
|
|
|
|
Copyright 2019, 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.
|
|
|
|
*/
|
2023-08-15 10:43:15 +02:00
|
|
|
|
|
|
|
import { EventEmitter } from "events";
|
2024-03-12 15:52:54 +01:00
|
|
|
import { Room, RoomMember, EventType, MatrixEvent, KnownMembership } from "matrix-js-sdk/src/matrix";
|
2022-05-04 11:29:13 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
|
2023-03-06 12:45:37 +01:00
|
|
|
import { PermalinkParts } from "../../../src/utils/permalinks/PermalinkConstructor";
|
2022-05-04 11:29:13 +02:00
|
|
|
import {
|
|
|
|
makeRoomPermalink,
|
|
|
|
makeUserPermalink,
|
|
|
|
parsePermalink,
|
|
|
|
RoomPermalinkCreator,
|
|
|
|
} from "../../../src/utils/permalinks/Permalinks";
|
2022-12-12 12:24:14 +01:00
|
|
|
import { getMockClientWithEventEmitter } from "../../test-utils";
|
2022-05-04 11:29:13 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("Permalinks", function () {
|
|
|
|
const userId = "@test:example.com";
|
2022-05-04 11:29:13 +02:00
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
getUserId: jest.fn().mockReturnValue(userId),
|
|
|
|
getRoom: jest.fn(),
|
|
|
|
});
|
|
|
|
mockClient.credentials = { userId };
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
const makeMemberWithPL = (roomId: Room["roomId"], userId: string, powerLevel: number): RoomMember => {
|
2022-05-04 11:29:13 +02:00
|
|
|
const member = new RoomMember(roomId, userId);
|
|
|
|
member.powerLevel = powerLevel;
|
|
|
|
return member;
|
|
|
|
};
|
|
|
|
|
|
|
|
function mockRoom(
|
2022-12-12 12:24:14 +01:00
|
|
|
roomId: Room["roomId"],
|
|
|
|
members: RoomMember[],
|
|
|
|
serverACLContent?: { deny?: string[]; allow?: string[] },
|
2022-05-04 11:29:13 +02:00
|
|
|
): Room {
|
2024-03-12 15:52:54 +01:00
|
|
|
members.forEach((m) => (m.membership = KnownMembership.Join));
|
2023-02-13 12:39:16 +01:00
|
|
|
const powerLevelsUsers = members.reduce<Record<string, number>>((pl, member) => {
|
2022-05-04 11:29:13 +02:00
|
|
|
if (Number.isFinite(member.powerLevel)) {
|
|
|
|
pl[member.userId] = member.powerLevel;
|
|
|
|
}
|
|
|
|
return pl;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
const room = new Room(roomId, mockClient, userId);
|
|
|
|
|
|
|
|
const powerLevels = new MatrixEvent({
|
|
|
|
type: EventType.RoomPowerLevels,
|
|
|
|
room_id: roomId,
|
2022-12-12 12:24:14 +01:00
|
|
|
state_key: "",
|
2022-05-04 11:29:13 +02:00
|
|
|
content: {
|
2022-12-12 12:24:14 +01:00
|
|
|
users: powerLevelsUsers,
|
|
|
|
users_default: 0,
|
2022-05-04 11:29:13 +02:00
|
|
|
},
|
|
|
|
});
|
2022-12-12 12:24:14 +01:00
|
|
|
const serverACL = serverACLContent
|
|
|
|
? new MatrixEvent({
|
|
|
|
type: EventType.RoomServerAcl,
|
|
|
|
room_id: roomId,
|
|
|
|
state_key: "",
|
|
|
|
content: serverACLContent,
|
|
|
|
})
|
|
|
|
: undefined;
|
2022-05-04 11:29:13 +02:00
|
|
|
const stateEvents = serverACL ? [powerLevels, serverACL] : [powerLevels];
|
|
|
|
room.currentState.setStateEvents(stateEvents);
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(room, "getCanonicalAlias").mockReturnValue(null);
|
|
|
|
jest.spyOn(room, "getJoinedMembers").mockReturnValue(members);
|
2023-02-13 18:01:43 +01:00
|
|
|
jest.spyOn(room, "getMember").mockImplementation((userId) => members.find((m) => m.userId === userId) || null);
|
2022-05-04 11:29:13 +02:00
|
|
|
|
|
|
|
return room;
|
|
|
|
}
|
2022-12-12 12:24:14 +01:00
|
|
|
beforeEach(function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
jest.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2022-12-12 12:24:14 +01:00
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockRestore();
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2023-08-15 10:43:15 +02:00
|
|
|
it("should not clean up listeners even if start was called multiple times", () => {
|
|
|
|
const room = mockRoom("!fake:example.org", []);
|
|
|
|
const getListenerCount = (emitter: EventEmitter) =>
|
|
|
|
emitter
|
|
|
|
.eventNames()
|
|
|
|
.map((e) => emitter.listenerCount(e))
|
|
|
|
.reduce((a, b) => a + b, 0);
|
|
|
|
const listenerCountBefore = getListenerCount(room.currentState);
|
|
|
|
|
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.start();
|
|
|
|
creator.start();
|
|
|
|
creator.start();
|
|
|
|
creator.start();
|
|
|
|
expect(getListenerCount(room.currentState)).toBeGreaterThan(listenerCountBefore);
|
|
|
|
|
|
|
|
creator.stop();
|
|
|
|
expect(getListenerCount(room.currentState)).toBe(listenerCountBefore);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should pick no candidate servers when the room has no members", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const room = mockRoom("!fake:example.org", []);
|
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator.serverCandidates).toBeTruthy();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates!.length).toBe(0);
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should gracefully handle invalid MXIDs", () => {
|
2022-09-16 10:03:17 +02:00
|
|
|
const roomId = "!fake:example.org";
|
|
|
|
const alice50 = makeMemberWithPL(roomId, "@alice:pl_50:org", 50);
|
|
|
|
const room = mockRoom(roomId, [alice50]);
|
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator.serverCandidates).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should pick a candidate server for the highest power level user in the room", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!fake:example.org";
|
|
|
|
const alice50 = makeMemberWithPL(roomId, "@alice:pl_50", 50);
|
|
|
|
const alice75 = makeMemberWithPL(roomId, "@alice:pl_75", 75);
|
|
|
|
const alice95 = makeMemberWithPL(roomId, "@alice:pl_95", 95);
|
2022-12-12 12:24:14 +01:00
|
|
|
const room = mockRoom("!fake:example.org", [alice50, alice75, alice95]);
|
2022-05-04 11:29:13 +02:00
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator.serverCandidates).toBeTruthy();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates!.length).toBe(3);
|
|
|
|
expect(creator.serverCandidates![0]).toBe("pl_95");
|
2022-05-04 11:29:13 +02:00
|
|
|
// we don't check the 2nd and 3rd servers because that is done by the next test
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should change candidate server when highest power level user leaves the room", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!fake:example.org";
|
|
|
|
const member95 = makeMemberWithPL(roomId, "@alice:pl_95", 95);
|
|
|
|
|
|
|
|
const room = mockRoom(roomId, [
|
|
|
|
makeMemberWithPL(roomId, "@alice:pl_50", 50),
|
|
|
|
makeMemberWithPL(roomId, "@alice:pl_75", 75),
|
|
|
|
member95,
|
|
|
|
]);
|
|
|
|
const creator = new RoomPermalinkCreator(room, null);
|
|
|
|
creator.load();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates![0]).toBe("pl_95");
|
2024-03-12 15:52:54 +01:00
|
|
|
member95.membership = KnownMembership.Leave;
|
2022-05-04 11:29:13 +02:00
|
|
|
// @ts-ignore illegal private property
|
|
|
|
creator.onRoomStateUpdate();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates![0]).toBe("pl_75");
|
2024-03-12 15:52:54 +01:00
|
|
|
member95.membership = KnownMembership.Join;
|
2022-05-04 11:29:13 +02:00
|
|
|
// @ts-ignore illegal private property
|
|
|
|
creator.onRoomStateUpdate();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates![0]).toBe("pl_95");
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should pick candidate servers based on user population", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!fake:example.org";
|
|
|
|
const room = mockRoom(roomId, [
|
|
|
|
makeMemberWithPL(roomId, "@alice:first", 0),
|
|
|
|
makeMemberWithPL(roomId, "@bob:first", 0),
|
|
|
|
makeMemberWithPL(roomId, "@charlie:first", 0),
|
|
|
|
makeMemberWithPL(roomId, "@alice:second", 0),
|
|
|
|
makeMemberWithPL(roomId, "@bob:second", 0),
|
|
|
|
makeMemberWithPL(roomId, "@charlie:third", 0),
|
|
|
|
]);
|
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator.serverCandidates).toBeTruthy();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates!.length).toBe(3);
|
|
|
|
expect(creator.serverCandidates![0]).toBe("first");
|
|
|
|
expect(creator.serverCandidates![1]).toBe("second");
|
|
|
|
expect(creator.serverCandidates![2]).toBe("third");
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should pick prefer candidate servers with higher power levels", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!fake:example.org";
|
|
|
|
const room = mockRoom(roomId, [
|
|
|
|
makeMemberWithPL(roomId, "@alice:first", 100),
|
|
|
|
makeMemberWithPL(roomId, "@alice:second", 0),
|
|
|
|
makeMemberWithPL(roomId, "@bob:second", 0),
|
|
|
|
makeMemberWithPL(roomId, "@charlie:third", 0),
|
|
|
|
]);
|
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates!.length).toBe(3);
|
|
|
|
expect(creator.serverCandidates![0]).toBe("first");
|
|
|
|
expect(creator.serverCandidates![1]).toBe("second");
|
|
|
|
expect(creator.serverCandidates![2]).toBe("third");
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should pick a maximum of 3 candidate servers", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!fake:example.org";
|
|
|
|
const room = mockRoom(roomId, [
|
|
|
|
makeMemberWithPL(roomId, "@alice:alpha", 100),
|
|
|
|
makeMemberWithPL(roomId, "@alice:bravo", 0),
|
|
|
|
makeMemberWithPL(roomId, "@alice:charlie", 0),
|
|
|
|
makeMemberWithPL(roomId, "@alice:delta", 0),
|
|
|
|
makeMemberWithPL(roomId, "@alice:echo", 0),
|
|
|
|
]);
|
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator.serverCandidates).toBeTruthy();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates!.length).toBe(3);
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should not consider IPv4 hosts", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!fake:example.org";
|
2022-12-12 12:24:14 +01:00
|
|
|
const room = mockRoom(roomId, [makeMemberWithPL(roomId, "@alice:127.0.0.1", 100)]);
|
2022-05-04 11:29:13 +02:00
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator.serverCandidates).toBeTruthy();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates!.length).toBe(0);
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should not consider IPv6 hosts", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!fake:example.org";
|
2022-12-12 12:24:14 +01:00
|
|
|
const room = mockRoom(roomId, [makeMemberWithPL(roomId, "@alice:[::1]", 100)]);
|
2022-05-04 11:29:13 +02:00
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator.serverCandidates).toBeTruthy();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates!.length).toBe(0);
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should not consider IPv4 hostnames with ports", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!fake:example.org";
|
2022-12-12 12:24:14 +01:00
|
|
|
const room = mockRoom(roomId, [makeMemberWithPL(roomId, "@alice:127.0.0.1:8448", 100)]);
|
2022-05-04 11:29:13 +02:00
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator.serverCandidates).toBeTruthy();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates!.length).toBe(0);
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should not consider IPv6 hostnames with ports", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!fake:example.org";
|
2022-12-12 12:24:14 +01:00
|
|
|
const room = mockRoom(roomId, [makeMemberWithPL(roomId, "@alice:[::1]:8448", 100)]);
|
2022-05-04 11:29:13 +02:00
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator.serverCandidates).toBeTruthy();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates!.length).toBe(0);
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should work with hostnames with ports", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!fake:example.org";
|
2022-12-12 12:24:14 +01:00
|
|
|
const room = mockRoom(roomId, [makeMemberWithPL(roomId, "@alice:example.org:8448", 100)]);
|
2022-05-04 11:29:13 +02:00
|
|
|
|
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator.serverCandidates).toBeTruthy();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates!.length).toBe(1);
|
|
|
|
expect(creator.serverCandidates![0]).toBe("example.org:8448");
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should not consider servers explicitly denied by ACLs", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!fake:example.org";
|
2022-12-12 12:24:14 +01:00
|
|
|
const room = mockRoom(
|
|
|
|
roomId,
|
|
|
|
[
|
|
|
|
makeMemberWithPL(roomId, "@alice:evilcorp.com", 100),
|
|
|
|
makeMemberWithPL(roomId, "@bob:chat.evilcorp.com", 0),
|
|
|
|
],
|
|
|
|
{
|
|
|
|
deny: ["evilcorp.com", "*.evilcorp.com"],
|
|
|
|
allow: ["*"],
|
|
|
|
},
|
|
|
|
);
|
2022-05-04 11:29:13 +02:00
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator.serverCandidates).toBeTruthy();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates!.length).toBe(0);
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should not consider servers not allowed by ACLs", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!fake:example.org";
|
2022-12-12 12:24:14 +01:00
|
|
|
const room = mockRoom(
|
|
|
|
roomId,
|
|
|
|
[
|
|
|
|
makeMemberWithPL(roomId, "@alice:evilcorp.com", 100),
|
|
|
|
makeMemberWithPL(roomId, "@bob:chat.evilcorp.com", 0),
|
|
|
|
],
|
|
|
|
{
|
|
|
|
deny: [],
|
|
|
|
allow: [], // implies "ban everyone"
|
|
|
|
},
|
|
|
|
);
|
2022-05-04 11:29:13 +02:00
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator.serverCandidates).toBeTruthy();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates!.length).toBe(0);
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should consider servers not explicitly banned by ACLs", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!fake:example.org";
|
2022-12-12 12:24:14 +01:00
|
|
|
const room = mockRoom(
|
|
|
|
roomId,
|
|
|
|
[
|
|
|
|
makeMemberWithPL(roomId, "@alice:evilcorp.com", 100),
|
|
|
|
makeMemberWithPL(roomId, "@bob:chat.evilcorp.com", 0),
|
|
|
|
],
|
|
|
|
{
|
|
|
|
deny: ["*.evilcorp.com"], // evilcorp.com is still good though
|
|
|
|
allow: ["*"],
|
|
|
|
},
|
|
|
|
);
|
2022-05-04 11:29:13 +02:00
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator.serverCandidates).toBeTruthy();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates!.length).toBe(1);
|
|
|
|
expect(creator.serverCandidates![0]).toEqual("evilcorp.com");
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should consider servers not disallowed by ACLs", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!fake:example.org";
|
2022-12-12 12:24:14 +01:00
|
|
|
const room = mockRoom(
|
|
|
|
"!fake:example.org",
|
|
|
|
[
|
|
|
|
makeMemberWithPL(roomId, "@alice:evilcorp.com", 100),
|
|
|
|
makeMemberWithPL(roomId, "@bob:chat.evilcorp.com", 0),
|
|
|
|
],
|
|
|
|
{
|
|
|
|
deny: [],
|
|
|
|
allow: ["evilcorp.com"], // implies "ban everyone else"
|
|
|
|
},
|
|
|
|
);
|
2022-05-04 11:29:13 +02:00
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
expect(creator.serverCandidates).toBeTruthy();
|
2023-02-24 16:28:40 +01:00
|
|
|
expect(creator.serverCandidates!.length).toBe(1);
|
|
|
|
expect(creator.serverCandidates![0]).toEqual("evilcorp.com");
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should generate an event permalink for room IDs with no candidate servers", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const room = mockRoom("!somewhere:example.org", []);
|
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
const result = creator.forEvent("$something:example.com");
|
|
|
|
expect(result).toBe("https://matrix.to/#/!somewhere:example.org/$something:example.com");
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should generate an event permalink for room IDs with some candidate servers", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const roomId = "!somewhere:example.org";
|
|
|
|
const room = mockRoom(roomId, [
|
|
|
|
makeMemberWithPL(roomId, "@alice:first", 100),
|
|
|
|
makeMemberWithPL(roomId, "@bob:second", 0),
|
|
|
|
]);
|
|
|
|
const creator = new RoomPermalinkCreator(room);
|
|
|
|
creator.load();
|
|
|
|
const result = creator.forEvent("$something:example.com");
|
|
|
|
expect(result).toBe("https://matrix.to/#/!somewhere:example.org/$something:example.com?via=first&via=second");
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should generate a room permalink for room IDs with some candidate servers", function () {
|
|
|
|
mockClient.getRoom.mockImplementation((roomId: Room["roomId"]) => {
|
2022-05-04 11:29:13 +02:00
|
|
|
return mockRoom(roomId, [
|
|
|
|
makeMemberWithPL(roomId, "@alice:first", 100),
|
|
|
|
makeMemberWithPL(roomId, "@bob:second", 0),
|
|
|
|
]);
|
|
|
|
});
|
2023-05-23 17:24:12 +02:00
|
|
|
const result = makeRoomPermalink(mockClient, "!somewhere:example.org");
|
2022-05-04 11:29:13 +02:00
|
|
|
expect(result).toBe("https://matrix.to/#/!somewhere:example.org?via=first&via=second");
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should generate a room permalink for room aliases with no candidate servers", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
mockClient.getRoom.mockReturnValue(null);
|
2023-05-23 17:24:12 +02:00
|
|
|
const result = makeRoomPermalink(mockClient, "#somewhere:example.org");
|
2022-05-04 11:29:13 +02:00
|
|
|
expect(result).toBe("https://matrix.to/#/#somewhere:example.org");
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should generate a room permalink for room aliases without candidate servers", function () {
|
|
|
|
mockClient.getRoom.mockImplementation((roomId: Room["roomId"]) => {
|
2022-05-04 11:29:13 +02:00
|
|
|
return mockRoom(roomId, [
|
|
|
|
makeMemberWithPL(roomId, "@alice:first", 100),
|
|
|
|
makeMemberWithPL(roomId, "@bob:second", 0),
|
|
|
|
]);
|
|
|
|
});
|
2023-05-23 17:24:12 +02:00
|
|
|
const result = makeRoomPermalink(mockClient, "#somewhere:example.org");
|
2022-05-04 11:29:13 +02:00
|
|
|
expect(result).toBe("https://matrix.to/#/#somewhere:example.org");
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should generate a user permalink", function () {
|
2022-05-04 11:29:13 +02:00
|
|
|
const result = makeUserPermalink("@someone:example.org");
|
|
|
|
expect(result).toBe("https://matrix.to/#/@someone:example.org");
|
|
|
|
});
|
|
|
|
|
2023-03-06 12:45:37 +01:00
|
|
|
describe("parsePermalink", () => {
|
|
|
|
it("should correctly parse room permalinks with a via argument", () => {
|
|
|
|
const result = parsePermalink("https://matrix.to/#/!room_id:server?via=some.org");
|
|
|
|
expect(result?.roomIdOrAlias).toBe("!room_id:server");
|
|
|
|
expect(result?.viaServers).toEqual(["some.org"]);
|
|
|
|
});
|
2022-05-04 11:29:13 +02:00
|
|
|
|
2023-03-06 12:45:37 +01:00
|
|
|
it("should correctly parse room permalink via arguments", () => {
|
|
|
|
const result = parsePermalink("https://matrix.to/#/!room_id:server?via=foo.bar&via=bar.foo");
|
|
|
|
expect(result?.roomIdOrAlias).toBe("!room_id:server");
|
|
|
|
expect(result?.viaServers).toEqual(["foo.bar", "bar.foo"]);
|
|
|
|
});
|
2022-05-04 11:29:13 +02:00
|
|
|
|
2023-03-06 12:45:37 +01:00
|
|
|
it("should correctly parse event permalink via arguments", () => {
|
|
|
|
const result = parsePermalink(
|
|
|
|
"https://matrix.to/#/!room_id:server/$event_id/some_thing_here/foobar" + "?via=m1.org&via=m2.org",
|
|
|
|
);
|
|
|
|
expect(result?.eventId).toBe("$event_id/some_thing_here/foobar");
|
|
|
|
expect(result?.roomIdOrAlias).toBe("!room_id:server");
|
|
|
|
expect(result?.viaServers).toEqual(["m1.org", "m2.org"]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should correctly parse permalinks with http protocol", () => {
|
|
|
|
expect(parsePermalink("http://matrix.to/#/@user:example.com")).toEqual(
|
|
|
|
new PermalinkParts(null, null, "@user:example.com", null),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should correctly parse permalinks without protocol", () => {
|
|
|
|
expect(parsePermalink("matrix.to/#/@user:example.com")).toEqual(
|
|
|
|
new PermalinkParts(null, null, "@user:example.com", null),
|
|
|
|
);
|
|
|
|
});
|
2022-05-04 11:29:13 +02:00
|
|
|
});
|
|
|
|
});
|