2021-05-08 23:51:51 +00:00
|
|
|
/*
|
2021-06-06 01:41:28 +00:00
|
|
|
Copyright 2021 Robin Townsend <robin@robin.town>
|
2021-05-08 23:51:51 +00:00
|
|
|
|
|
|
|
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 React from "react";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { act } from "react-dom/test-utils";
|
2022-05-03 08:22:38 +00:00
|
|
|
import { MatrixEvent, EventType } from "matrix-js-sdk/src/matrix";
|
2022-05-10 16:21:20 +00:00
|
|
|
import { LocationAssetType, M_ASSET, M_LOCATION, M_TIMESTAMP } from "matrix-js-sdk/src/@types/location";
|
2023-01-13 17:02:33 +00:00
|
|
|
import { M_TEXT } from "matrix-js-sdk/src/@types/extensible_events";
|
2022-11-18 09:16:11 +00:00
|
|
|
import { fireEvent, getByTestId, render, RenderResult, screen } from "@testing-library/react";
|
|
|
|
import userEvent from "@testing-library/user-event";
|
2021-05-08 23:51:51 +00:00
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-05-03 08:22:38 +00:00
|
|
|
import ForwardDialog from "../../../../src/components/views/dialogs/ForwardDialog";
|
2021-05-08 23:51:51 +00:00
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { RoomPermalinkCreator } from "../../../../src/utils/permalinks/Permalinks";
|
2022-05-03 08:22:38 +00:00
|
|
|
import {
|
|
|
|
getMockClientWithEventEmitter,
|
2022-06-17 13:27:08 +00:00
|
|
|
makeBeaconEvent,
|
2022-05-10 16:21:20 +00:00
|
|
|
makeLegacyLocationEvent,
|
|
|
|
makeLocationEvent,
|
2022-05-03 08:22:38 +00:00
|
|
|
mkEvent,
|
|
|
|
mkMessage,
|
|
|
|
mkStubRoom,
|
2022-11-18 09:16:11 +00:00
|
|
|
mockPlatformPeg,
|
2022-05-03 08:22:38 +00:00
|
|
|
} from "../../../test-utils";
|
2022-05-10 16:21:20 +00:00
|
|
|
import { TILE_SERVER_WK_KEY } from "../../../../src/utils/WellKnownUtils";
|
2021-05-08 23:51:51 +00:00
|
|
|
|
|
|
|
describe("ForwardDialog", () => {
|
2021-05-10 04:54:00 +00:00
|
|
|
const sourceRoom = "!111111111111111111:example.org";
|
2022-05-03 08:22:38 +00:00
|
|
|
const aliceId = "@alice:example.org";
|
|
|
|
const defaultMessage = mkMessage({
|
2021-05-10 04:54:00 +00:00
|
|
|
room: sourceRoom,
|
2022-05-03 08:22:38 +00:00
|
|
|
user: aliceId,
|
2021-05-08 23:51:51 +00:00
|
|
|
msg: "Hello world!",
|
|
|
|
event: true,
|
|
|
|
});
|
2022-05-03 08:22:38 +00:00
|
|
|
const accountDataEvent = new MatrixEvent({
|
|
|
|
type: EventType.Direct,
|
|
|
|
sender: aliceId,
|
|
|
|
content: {},
|
|
|
|
});
|
|
|
|
const mockClient = getMockClientWithEventEmitter({
|
|
|
|
getUserId: jest.fn().mockReturnValue(aliceId),
|
|
|
|
isGuest: jest.fn().mockReturnValue(false),
|
|
|
|
getVisibleRooms: jest.fn().mockReturnValue([]),
|
|
|
|
getRoom: jest.fn(),
|
|
|
|
getAccountData: jest.fn().mockReturnValue(accountDataEvent),
|
|
|
|
getPushActionsForEvent: jest.fn(),
|
2022-12-12 11:24:14 +00:00
|
|
|
mxcUrlToHttp: jest.fn().mockReturnValue(""),
|
2022-05-03 08:22:38 +00:00
|
|
|
isRoomEncrypted: jest.fn().mockReturnValue(false),
|
|
|
|
getProfileInfo: jest.fn().mockResolvedValue({
|
2022-12-12 11:24:14 +00:00
|
|
|
displayname: "Alice",
|
2022-05-03 08:22:38 +00:00
|
|
|
}),
|
|
|
|
decryptEventIfNeeded: jest.fn(),
|
|
|
|
sendEvent: jest.fn(),
|
2022-05-10 16:21:20 +00:00
|
|
|
getClientWellKnown: jest.fn().mockReturnValue({
|
2022-12-12 11:24:14 +00:00
|
|
|
[TILE_SERVER_WK_KEY.name]: { map_style_url: "maps.com" },
|
2022-05-10 16:21:20 +00:00
|
|
|
}),
|
2022-05-03 08:22:38 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
const defaultRooms = ["a", "A", "b"].map((name) => mkStubRoom(name, name, mockClient));
|
2021-05-08 23:51:51 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
const mountForwardDialog = (message = defaultMessage, rooms = defaultRooms) => {
|
2022-05-03 08:22:38 +00:00
|
|
|
mockClient.getVisibleRooms.mockReturnValue(rooms);
|
2022-12-12 11:24:14 +00:00
|
|
|
mockClient.getRoom.mockImplementation((roomId) => rooms.find((room) => room.roomId === roomId));
|
2021-05-08 23:51:51 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
const wrapper: RenderResult = render(
|
|
|
|
<ForwardDialog
|
|
|
|
matrixClient={mockClient}
|
|
|
|
event={message}
|
|
|
|
permalinkCreator={new RoomPermalinkCreator(undefined, sourceRoom)}
|
|
|
|
onFinished={jest.fn()}
|
|
|
|
/>,
|
|
|
|
);
|
2021-05-10 04:54:00 +00:00
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
DMRoomMap.makeShared();
|
2022-05-03 08:22:38 +00:00
|
|
|
jest.clearAllMocks();
|
|
|
|
mockClient.getUserId.mockReturnValue("@bob:example.org");
|
|
|
|
mockClient.sendEvent.mockReset();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockRestore();
|
2021-05-08 23:51:51 +00:00
|
|
|
});
|
|
|
|
|
2021-05-10 04:54:00 +00:00
|
|
|
it("shows a preview with us as the sender", async () => {
|
2022-11-18 09:16:11 +00:00
|
|
|
const { container } = mountForwardDialog();
|
2021-05-10 04:54:00 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
expect(screen.queryByText("Hello world!")).toBeInTheDocument();
|
2021-05-08 23:51:51 +00:00
|
|
|
|
|
|
|
// We would just test SenderProfile for the user ID, but it's stubbed
|
2022-11-18 09:16:11 +00:00
|
|
|
const previewAvatar = container.querySelector(".mx_EventTile_avatar .mx_BaseAvatar_image");
|
|
|
|
expect(previewAvatar?.getAttribute("title")).toBe("@bob:example.org");
|
2021-05-08 23:51:51 +00:00
|
|
|
});
|
|
|
|
|
2021-05-10 04:54:00 +00:00
|
|
|
it("filters the rooms", async () => {
|
2022-11-18 09:16:11 +00:00
|
|
|
const { container } = mountForwardDialog();
|
2021-05-10 04:54:00 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
expect(container.querySelectorAll(".mx_ForwardList_entry")).toHaveLength(3);
|
2021-05-08 23:51:51 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
const searchInput = getByTestId(container, "searchbox-input");
|
|
|
|
act(() => userEvent.type(searchInput, "a"));
|
2021-05-08 23:51:51 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
expect(container.querySelectorAll(".mx_ForwardList_entry")).toHaveLength(3);
|
2021-05-08 23:51:51 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("tracks message sending progress across multiple rooms", async () => {
|
2022-11-18 09:16:11 +00:00
|
|
|
mockPlatformPeg();
|
|
|
|
const { container } = mountForwardDialog();
|
2021-05-10 04:54:00 +00:00
|
|
|
|
2021-05-08 23:51:51 +00:00
|
|
|
// Make sendEvent require manual resolution so we can see the sending state
|
|
|
|
let finishSend;
|
|
|
|
let cancelSend;
|
2022-12-12 11:24:14 +00:00
|
|
|
mockClient.sendEvent.mockImplementation(
|
|
|
|
<T extends {}>() =>
|
|
|
|
new Promise<T>((resolve, reject) => {
|
|
|
|
finishSend = resolve;
|
|
|
|
cancelSend = reject;
|
|
|
|
}),
|
|
|
|
);
|
2021-05-08 23:51:51 +00:00
|
|
|
|
2022-03-03 12:54:20 +00:00
|
|
|
let firstButton;
|
|
|
|
let secondButton;
|
|
|
|
const update = () => {
|
2022-11-18 09:16:11 +00:00
|
|
|
[firstButton, secondButton] = container.querySelectorAll(".mx_ForwardList_sendButton");
|
2022-03-03 12:54:20 +00:00
|
|
|
};
|
|
|
|
update();
|
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
expect(firstButton.className).toContain("mx_ForwardList_canSend");
|
2021-05-08 23:51:51 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
act(() => {
|
|
|
|
fireEvent.click(firstButton);
|
|
|
|
});
|
2022-03-03 12:54:20 +00:00
|
|
|
update();
|
2022-11-18 09:16:11 +00:00
|
|
|
expect(firstButton.className).toContain("mx_ForwardList_sending");
|
2021-05-08 23:51:51 +00:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
cancelSend();
|
|
|
|
// Wait one tick for the button to realize the send failed
|
2022-12-12 11:24:14 +00:00
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
2021-05-08 23:51:51 +00:00
|
|
|
});
|
2022-03-03 12:54:20 +00:00
|
|
|
update();
|
2022-11-18 09:16:11 +00:00
|
|
|
expect(firstButton.className).toContain("mx_ForwardList_sendFailed");
|
2021-05-08 23:51:51 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
expect(secondButton.className).toContain("mx_ForwardList_canSend");
|
2021-05-08 23:51:51 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
act(() => {
|
|
|
|
fireEvent.click(secondButton);
|
|
|
|
});
|
2022-03-03 12:54:20 +00:00
|
|
|
update();
|
2022-11-18 09:16:11 +00:00
|
|
|
expect(secondButton.className).toContain("mx_ForwardList_sending");
|
2021-05-08 23:51:51 +00:00
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
finishSend();
|
|
|
|
// Wait one tick for the button to realize the send succeeded
|
2022-12-12 11:24:14 +00:00
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
2021-05-08 23:51:51 +00:00
|
|
|
});
|
2022-03-03 12:54:20 +00:00
|
|
|
update();
|
2022-11-18 09:16:11 +00:00
|
|
|
expect(secondButton.className).toContain("mx_ForwardList_sent");
|
2021-05-08 23:51:51 +00:00
|
|
|
});
|
2021-05-10 04:54:00 +00:00
|
|
|
|
|
|
|
it("can render replies", async () => {
|
2022-05-03 08:22:38 +00:00
|
|
|
const replyMessage = mkEvent({
|
2021-05-10 04:54:00 +00:00
|
|
|
type: "m.room.message",
|
|
|
|
room: "!111111111111111111:example.org",
|
|
|
|
user: "@alice:example.org",
|
|
|
|
content: {
|
2021-05-10 05:16:37 +00:00
|
|
|
"msgtype": "m.text",
|
|
|
|
"body": "> <@bob:example.org> Hi Alice!\n\nHi Bob!",
|
2021-05-10 04:54:00 +00:00
|
|
|
"m.relates_to": {
|
|
|
|
"m.in_reply_to": {
|
|
|
|
event_id: "$2222222222222222222222222222222222222222222",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
event: true,
|
|
|
|
});
|
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
mountForwardDialog(replyMessage);
|
|
|
|
|
|
|
|
expect(screen.queryByText("Hi Alice!", { exact: false })).toBeInTheDocument();
|
2021-05-10 04:54:00 +00:00
|
|
|
});
|
2021-05-10 05:07:42 +00:00
|
|
|
|
|
|
|
it("disables buttons for rooms without send permissions", async () => {
|
2022-05-03 08:22:38 +00:00
|
|
|
const readOnlyRoom = mkStubRoom("a", "a", mockClient);
|
2021-05-10 05:07:42 +00:00
|
|
|
readOnlyRoom.maySendMessage = jest.fn().mockReturnValue(false);
|
2022-05-03 08:22:38 +00:00
|
|
|
const rooms = [readOnlyRoom, mkStubRoom("b", "b", mockClient)];
|
2021-05-10 05:07:42 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
const { container } = mountForwardDialog(undefined, rooms);
|
|
|
|
|
|
|
|
const [firstButton, secondButton] = container.querySelectorAll<HTMLButtonElement>(".mx_ForwardList_sendButton");
|
2021-05-10 05:07:42 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
expect(firstButton.getAttribute("aria-disabled")).toBeTruthy();
|
|
|
|
expect(secondButton.getAttribute("aria-disabled")).toBeFalsy();
|
2021-05-10 05:07:42 +00:00
|
|
|
});
|
2022-05-10 16:21:20 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("Location events", () => {
|
2022-05-10 16:21:20 +00:00
|
|
|
// 14.03.2022 16:15
|
|
|
|
const now = 1647270879403;
|
|
|
|
const roomId = "a";
|
|
|
|
const geoUri = "geo:51.5076,-0.1276";
|
|
|
|
const legacyLocationEvent = makeLegacyLocationEvent(geoUri);
|
|
|
|
const modernLocationEvent = makeLocationEvent(geoUri);
|
|
|
|
const pinDropLocationEvent = makeLocationEvent(geoUri, LocationAssetType.Pin);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
// legacy events will default timestamp to Date.now()
|
|
|
|
// mock a stable now for easy assertion
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(Date, "now").mockReturnValue(now);
|
2022-05-10 16:21:20 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
2022-12-12 11:24:14 +00:00
|
|
|
jest.spyOn(Date, "now").mockRestore();
|
2022-05-10 16:21:20 +00:00
|
|
|
});
|
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
const sendToFirstRoom = (container: HTMLElement): void =>
|
2022-05-10 16:21:20 +00:00
|
|
|
act(() => {
|
2022-11-18 09:16:11 +00:00
|
|
|
const sendToFirstRoomButton = container.querySelector(".mx_ForwardList_sendButton");
|
|
|
|
fireEvent.click(sendToFirstRoomButton!);
|
2022-05-10 16:21:20 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("converts legacy location events to pin drop shares", async () => {
|
2022-11-18 09:16:11 +00:00
|
|
|
const { container } = mountForwardDialog(legacyLocationEvent);
|
2022-05-10 16:21:20 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
expect(container.querySelector(".mx_MLocationBody")).toBeTruthy();
|
|
|
|
sendToFirstRoom(container);
|
2022-05-10 16:21:20 +00:00
|
|
|
|
|
|
|
// text and description from original event are removed
|
|
|
|
// text gets new default message from event values
|
|
|
|
// timestamp is defaulted to now
|
|
|
|
const text = `Location ${geoUri} at ${new Date(now).toISOString()}`;
|
|
|
|
const expectedStrippedContent = {
|
|
|
|
...modernLocationEvent.getContent(),
|
|
|
|
body: text,
|
2023-01-13 17:02:33 +00:00
|
|
|
[M_TEXT.name]: text,
|
2022-05-10 16:21:20 +00:00
|
|
|
[M_TIMESTAMP.name]: now,
|
|
|
|
[M_ASSET.name]: { type: LocationAssetType.Pin },
|
|
|
|
[M_LOCATION.name]: {
|
|
|
|
uri: geoUri,
|
|
|
|
description: undefined,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
roomId,
|
|
|
|
legacyLocationEvent.getType(),
|
|
|
|
expectedStrippedContent,
|
2022-05-10 16:21:20 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("removes personal information from static self location shares", async () => {
|
2022-11-18 09:16:11 +00:00
|
|
|
const { container } = mountForwardDialog(modernLocationEvent);
|
2022-05-10 16:21:20 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
expect(container.querySelector(".mx_MLocationBody")).toBeTruthy();
|
|
|
|
sendToFirstRoom(container);
|
2022-05-10 16:21:20 +00:00
|
|
|
|
|
|
|
const timestamp = M_TIMESTAMP.findIn<number>(modernLocationEvent.getContent());
|
|
|
|
// text and description from original event are removed
|
|
|
|
// text gets new default message from event values
|
|
|
|
const text = `Location ${geoUri} at ${new Date(timestamp).toISOString()}`;
|
|
|
|
const expectedStrippedContent = {
|
|
|
|
...modernLocationEvent.getContent(),
|
|
|
|
body: text,
|
2023-01-13 17:02:33 +00:00
|
|
|
[M_TEXT.name]: text,
|
2022-05-10 16:21:20 +00:00
|
|
|
[M_ASSET.name]: { type: LocationAssetType.Pin },
|
|
|
|
[M_LOCATION.name]: {
|
|
|
|
uri: geoUri,
|
|
|
|
description: undefined,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
roomId,
|
|
|
|
modernLocationEvent.getType(),
|
|
|
|
expectedStrippedContent,
|
2022-05-10 16:21:20 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("forwards beacon location as a pin drop event", async () => {
|
2022-06-17 13:27:08 +00:00
|
|
|
const timestamp = 123456;
|
2022-12-12 11:24:14 +00:00
|
|
|
const beaconEvent = makeBeaconEvent("@alice:server.org", { geoUri, timestamp });
|
2022-06-17 13:27:08 +00:00
|
|
|
const text = `Location ${geoUri} at ${new Date(timestamp).toISOString()}`;
|
|
|
|
const expectedContent = {
|
|
|
|
msgtype: "m.location",
|
|
|
|
body: text,
|
2023-01-13 17:02:33 +00:00
|
|
|
[M_TEXT.name]: text,
|
2022-06-17 13:27:08 +00:00
|
|
|
[M_ASSET.name]: { type: LocationAssetType.Pin },
|
|
|
|
[M_LOCATION.name]: {
|
|
|
|
uri: geoUri,
|
|
|
|
description: undefined,
|
|
|
|
},
|
|
|
|
geo_uri: geoUri,
|
|
|
|
[M_TIMESTAMP.name]: timestamp,
|
|
|
|
};
|
2022-11-18 09:16:11 +00:00
|
|
|
const { container } = mountForwardDialog(beaconEvent);
|
2022-06-17 13:27:08 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
expect(container.querySelector(".mx_MLocationBody")).toBeTruthy();
|
2022-06-17 13:27:08 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
sendToFirstRoom(container);
|
2022-06-17 13:27:08 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(roomId, EventType.RoomMessage, expectedContent);
|
2022-06-17 13:27:08 +00:00
|
|
|
});
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
it("forwards pin drop event", async () => {
|
2022-11-18 09:16:11 +00:00
|
|
|
const { container } = mountForwardDialog(pinDropLocationEvent);
|
2022-05-10 16:21:20 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
expect(container.querySelector(".mx_MLocationBody")).toBeTruthy();
|
2022-05-10 16:21:20 +00:00
|
|
|
|
2022-11-18 09:16:11 +00:00
|
|
|
sendToFirstRoom(container);
|
2022-05-10 16:21:20 +00:00
|
|
|
|
|
|
|
expect(mockClient.sendEvent).toHaveBeenCalledWith(
|
2022-12-12 11:24:14 +00:00
|
|
|
roomId,
|
|
|
|
pinDropLocationEvent.getType(),
|
|
|
|
pinDropLocationEvent.getContent(),
|
2022-05-10 16:21:20 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2021-05-08 23:51:51 +00:00
|
|
|
});
|