2022-12-20 08:01:14 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 Mikhail Aheichyk
|
|
|
|
Copyright 2022 Nordeck IT + Consulting GmbH.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/// <reference types="cypress" />
|
|
|
|
|
|
|
|
import { IWidget } from "matrix-widget-api/src/interfaces/IWidget";
|
|
|
|
|
2022-12-20 09:20:40 +00:00
|
|
|
import type { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
2023-01-12 09:49:24 +00:00
|
|
|
import { HomeserverInstance } from "../../plugins/utils/homeserver";
|
2022-12-20 08:01:14 +00:00
|
|
|
import { UserCredentials } from "../../support/login";
|
|
|
|
|
|
|
|
const DEMO_WIDGET_ID = "demo-widget-id";
|
|
|
|
const DEMO_WIDGET_NAME = "Demo Widget";
|
|
|
|
const DEMO_WIDGET_TYPE = "demo";
|
|
|
|
const ROOM_NAME = "Demo";
|
|
|
|
|
|
|
|
const DEMO_WIDGET_HTML = `
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<title>Demo Widget</title>
|
|
|
|
<script>
|
|
|
|
let sendEventCount = 0
|
|
|
|
window.onmessage = ev => {
|
|
|
|
if (ev.data.action === 'capabilities') {
|
|
|
|
window.parent.postMessage(Object.assign({
|
|
|
|
response: {
|
|
|
|
capabilities: [
|
|
|
|
"org.matrix.msc2762.timeline:*",
|
2023-01-13 11:53:06 +00:00
|
|
|
"org.matrix.msc2762.receive.state_event:m.room.topic",
|
2022-12-20 08:01:14 +00:00
|
|
|
"org.matrix.msc2762.send.event:net.widget_echo"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
}, ev.data), '*');
|
|
|
|
} else if (ev.data.action === 'send_event' && !ev.data.response) {
|
|
|
|
// wraps received event into 'net.widget_echo' and sends back
|
|
|
|
sendEventCount += 1
|
|
|
|
window.parent.postMessage({
|
|
|
|
api: "fromWidget",
|
|
|
|
widgetId: ev.data.widgetId,
|
|
|
|
requestId: 'widget-' + sendEventCount,
|
|
|
|
action: "send_event",
|
|
|
|
data: {
|
|
|
|
type: 'net.widget_echo',
|
|
|
|
content: ev.data.data // sets matrix event to the content returned
|
|
|
|
},
|
|
|
|
}, '*')
|
|
|
|
}
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<button id="demo">Demo</button>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
`;
|
|
|
|
|
|
|
|
function waitForRoom(win: Cypress.AUTWindow, roomId: string, predicate: (room: Room) => boolean): Promise<void> {
|
|
|
|
const matrixClient = win.mxMatrixClientPeg.get();
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const room = matrixClient.getRoom(roomId);
|
|
|
|
|
|
|
|
if (predicate(room)) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function onEvent(ev: MatrixEvent) {
|
|
|
|
if (ev.getRoomId() !== roomId) return;
|
|
|
|
|
|
|
|
if (predicate(room)) {
|
|
|
|
matrixClient.removeListener(win.matrixcs.ClientEvent.Event, onEvent);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
matrixClient.on(win.matrixcs.ClientEvent.Event, onEvent);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("Widget Events", () => {
|
2023-01-12 09:49:24 +00:00
|
|
|
let homeserver: HomeserverInstance;
|
2022-12-20 08:01:14 +00:00
|
|
|
let user: UserCredentials;
|
|
|
|
let bot: MatrixClient;
|
|
|
|
let demoWidgetUrl: string;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-01-12 09:49:24 +00:00
|
|
|
cy.startHomeserver("default").then((data) => {
|
|
|
|
homeserver = data;
|
2022-12-20 08:01:14 +00:00
|
|
|
|
2023-01-12 09:49:24 +00:00
|
|
|
cy.initTestUser(homeserver, "Mike").then((_user) => {
|
2022-12-20 08:01:14 +00:00
|
|
|
user = _user;
|
|
|
|
});
|
2023-01-12 09:49:24 +00:00
|
|
|
cy.getBot(homeserver, { displayName: "Bot", autoAcceptInvites: true }).then((_bot) => {
|
2022-12-20 08:01:14 +00:00
|
|
|
bot = _bot;
|
|
|
|
});
|
|
|
|
});
|
2022-12-20 09:20:40 +00:00
|
|
|
cy.serveHtmlFile(DEMO_WIDGET_HTML).then((url) => {
|
2022-12-20 08:01:14 +00:00
|
|
|
demoWidgetUrl = url;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2023-01-12 09:49:24 +00:00
|
|
|
cy.stopHomeserver(homeserver);
|
2022-12-20 08:01:14 +00:00
|
|
|
cy.stopWebServers();
|
|
|
|
});
|
|
|
|
|
2022-12-20 09:20:40 +00:00
|
|
|
it("should be updated if user is re-invited into the room with updated state event", () => {
|
2022-12-20 08:01:14 +00:00
|
|
|
cy.createRoom({
|
|
|
|
name: ROOM_NAME,
|
|
|
|
invite: [bot.getUserId()],
|
2022-12-20 09:20:40 +00:00
|
|
|
}).then((roomId) => {
|
2022-12-20 08:01:14 +00:00
|
|
|
// setup widget via state event
|
2022-12-20 09:20:40 +00:00
|
|
|
cy.getClient()
|
|
|
|
.then(async (matrixClient) => {
|
|
|
|
const content: IWidget = {
|
|
|
|
id: DEMO_WIDGET_ID,
|
|
|
|
creatorUserId: "somebody",
|
|
|
|
type: DEMO_WIDGET_TYPE,
|
|
|
|
name: DEMO_WIDGET_NAME,
|
|
|
|
url: demoWidgetUrl,
|
|
|
|
};
|
|
|
|
await matrixClient.sendStateEvent(roomId, "im.vector.modular.widgets", content, DEMO_WIDGET_ID);
|
|
|
|
})
|
|
|
|
.as("widgetEventSent");
|
2022-12-20 08:01:14 +00:00
|
|
|
|
|
|
|
// set initial layout
|
2022-12-20 09:20:40 +00:00
|
|
|
cy.getClient()
|
|
|
|
.then(async (matrixClient) => {
|
|
|
|
const content = {
|
|
|
|
widgets: {
|
|
|
|
[DEMO_WIDGET_ID]: {
|
|
|
|
container: "top",
|
|
|
|
index: 1,
|
|
|
|
width: 100,
|
|
|
|
height: 0,
|
|
|
|
},
|
2022-12-20 08:01:14 +00:00
|
|
|
},
|
2022-12-20 09:20:40 +00:00
|
|
|
};
|
|
|
|
await matrixClient.sendStateEvent(roomId, "io.element.widgets.layout", content, "");
|
|
|
|
})
|
|
|
|
.as("layoutEventSent");
|
2022-12-20 08:01:14 +00:00
|
|
|
|
|
|
|
// open the room
|
|
|
|
cy.viewRoomByName(ROOM_NAME);
|
|
|
|
|
|
|
|
// approve capabilities
|
2022-12-20 09:20:40 +00:00
|
|
|
cy.contains(".mx_WidgetCapabilitiesPromptDialog button", "Approve").click();
|
2022-12-20 08:01:14 +00:00
|
|
|
|
2022-12-20 09:20:40 +00:00
|
|
|
cy.all([cy.get<string>("@widgetEventSent"), cy.get<string>("@layoutEventSent")]).then(async () => {
|
2023-01-13 11:53:06 +00:00
|
|
|
// bot creates a new room with 'm.room.topic'
|
2022-12-20 08:01:14 +00:00
|
|
|
const { room_id: roomNew } = await bot.createRoom({
|
|
|
|
name: "New room",
|
|
|
|
initial_state: [
|
|
|
|
{
|
2023-01-13 11:53:06 +00:00
|
|
|
type: "m.room.topic",
|
2022-12-20 09:20:40 +00:00
|
|
|
state_key: "",
|
2022-12-20 08:01:14 +00:00
|
|
|
content: {
|
2023-01-13 11:53:06 +00:00
|
|
|
topic: "topic initial",
|
2022-12-20 08:01:14 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
await bot.invite(roomNew, user.userId);
|
|
|
|
|
2023-01-13 11:53:06 +00:00
|
|
|
// widget should receive 'm.room.topic' event after invite
|
2022-12-20 09:20:40 +00:00
|
|
|
cy.window().then(async (win) => {
|
2022-12-20 08:01:14 +00:00
|
|
|
await waitForRoom(win, roomId, (room) => {
|
|
|
|
const events = room.getLiveTimeline().getEvents();
|
2022-12-20 09:20:40 +00:00
|
|
|
return events.some(
|
|
|
|
(e) =>
|
|
|
|
e.getType() === "net.widget_echo" &&
|
2023-01-13 11:53:06 +00:00
|
|
|
e.getContent().type === "m.room.topic" &&
|
|
|
|
e.getContent().content.topic === "topic initial",
|
2022-12-20 09:20:40 +00:00
|
|
|
);
|
2022-12-20 08:01:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-01-13 11:53:06 +00:00
|
|
|
// update the topic
|
2022-12-20 09:20:40 +00:00
|
|
|
await bot.sendStateEvent(
|
|
|
|
roomNew,
|
2023-01-13 11:53:06 +00:00
|
|
|
"m.room.topic",
|
2022-12-20 09:20:40 +00:00
|
|
|
{
|
2023-01-13 11:53:06 +00:00
|
|
|
topic: "topic updated",
|
2022-12-20 09:20:40 +00:00
|
|
|
},
|
|
|
|
"",
|
|
|
|
);
|
2022-12-20 08:01:14 +00:00
|
|
|
|
2022-12-20 09:20:40 +00:00
|
|
|
await bot.invite(roomNew, user.userId, "something changed in the room");
|
2022-12-20 08:01:14 +00:00
|
|
|
|
2023-01-13 11:53:06 +00:00
|
|
|
// widget should receive updated 'net.room.topic' event after re-invite
|
2022-12-20 09:20:40 +00:00
|
|
|
cy.window().then(async (win) => {
|
2022-12-20 08:01:14 +00:00
|
|
|
await waitForRoom(win, roomId, (room) => {
|
|
|
|
const events = room.getLiveTimeline().getEvents();
|
2022-12-20 09:20:40 +00:00
|
|
|
return events.some(
|
|
|
|
(e) =>
|
|
|
|
e.getType() === "net.widget_echo" &&
|
2023-01-13 11:53:06 +00:00
|
|
|
e.getContent().type === "m.room.topic" &&
|
|
|
|
e.getContent().content.topic === "topic updated",
|
2022-12-20 09:20:40 +00:00
|
|
|
);
|
2022-12-20 08:01:14 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|