From 7e6c0edef53341c9dcfa50bb29476c19c13c1185 Mon Sep 17 00:00:00 2001 From: MTRNord Date: Tue, 24 Oct 2023 12:15:12 +0200 Subject: [PATCH] Fix event handler for queryAdmin Test --- .../commands/queryAdminDetailsTest.ts | 96 +++++++++---------- 1 file changed, 46 insertions(+), 50 deletions(-) diff --git a/test/integration/commands/queryAdminDetailsTest.ts b/test/integration/commands/queryAdminDetailsTest.ts index 0049c35e..9b06c450 100644 --- a/test/integration/commands/queryAdminDetailsTest.ts +++ b/test/integration/commands/queryAdminDetailsTest.ts @@ -1,89 +1,85 @@ import { strict as assert } from "assert"; -import { newTestUser } from "../clientHelper"; -import { getMessagesByUserIn } from "../../../src/utils"; +import { newTestUser, noticeListener } from "../clientHelper"; describe("Test: The queryAdmin command", function () { // If a test has a timeout while awaitng on a promise then we never get given control back. afterEach(function () { this.moderator?.stop(); }); it('Mjölnir can query and display the query results for a complete json.', async function () { - let moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } }); + const moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } }); this.moderator = moderator; await moderator.joinRoom(this.config.managementRoom); - moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text.', body: `!mjolnir queryAdmin http://localhost:8081` }); + // listener for getting the event reply + const reply = new Promise((resolve, reject) => { + moderator.on('room.message', noticeListener(this.mjolnir.managementRoomId, (event) => { + resolve(event); + })) + }); - const draupnir = this.config.RUNTIME.client! - let draupnirUserId = await draupnir.getUserId(); + await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text.', body: `!mjolnir queryAdmin http://localhost:8081` }); - // Check if draupnir replied - await getMessagesByUserIn(moderator, draupnirUserId, this.mjolnir.managementRoomId, 1000, function (events) { - events.map(e => { - if (e.type === 'm.room.message') { - assert.equal(e.content.body, "", `Draupnir did not parse the json as expected: ${e.content.body}.`) - } - }) - }); + + const reply_event = await reply; + + assert.equal(reply_event.content.body, "", `Draupnir did not parse the json as expected: ${reply_event.content.body}.`) }) it('Mjölnir can query and display the query results for a partial contacts-only json.', async function () { - let moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } }); + const moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } }); this.moderator = moderator; await moderator.joinRoom(this.config.managementRoom); - moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text.', body: `!mjolnir queryAdmin http://localhost:7072` }); + // listener for getting the event reply + const reply = new Promise((resolve, reject) => { + moderator.on('room.message', noticeListener(this.mjolnir.managementRoomId, (event) => { + resolve(event); + })) + }); - const draupnir = this.config.RUNTIME.client! - let draupnirUserId = await draupnir.getUserId(); + await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text.', body: `!mjolnir queryAdmin http://localhost:7072` }); - // Check if draupnir replied - await getMessagesByUserIn(moderator, draupnirUserId, this.mjolnir.managementRoomId, 1000, function (events) { - events.map(e => { - if (e.type === 'm.room.message') { - assert.equal(e.content.body, "", `Draupnir did not parse the json as expected: ${e.content.body}.`) - } - }) - }); + const reply_event = await reply; + + assert.equal(reply_event.content.body, "", `Draupnir did not parse the json as expected: ${reply_event.content.body}.`) }) it('Mjölnir can query and display the query results for a partial support_page-only json.', async function () { - let moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } }); + const moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } }); this.moderator = moderator; await moderator.joinRoom(this.config.managementRoom); - moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text.', body: `!mjolnir queryAdmin http://localhost:7071` }); + // listener for getting the event reply + const reply = new Promise((resolve, reject) => { + moderator.on('room.message', noticeListener(this.mjolnir.managementRoomId, (event) => { + resolve(event); + })) + }); - const draupnir = this.config.RUNTIME.client! - let draupnirUserId = await draupnir.getUserId(); + await moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text.', body: `!mjolnir queryAdmin http://localhost:7071` }); - // Check if draupnir replied - await getMessagesByUserIn(moderator, draupnirUserId, this.mjolnir.managementRoomId, 1000, function (events) { - events.map(e => { - if (e.type === 'm.room.message') { - assert.equal(e.content.body, "", `Draupnir did not parse the json as expected: ${e.content.body}.`) - } - }) - }); + const reply_event = await reply; + + assert.equal(reply_event.content.body, "", `Draupnir did not parse the json as expected: ${reply_event.content.body}.`) }) it('Mjölnir can query and display an error for a non well-formed json.', async function () { - let moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } }); + const moderator = await newTestUser(this.config.homeserverUrl, { name: { contains: "moderator" } }); this.moderator = moderator; await moderator.joinRoom(this.config.managementRoom); - moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text.', body: `!mjolnir queryAdmin http://localhost:7070` }); + // listener for getting the event reply + const reply = new Promise((resolve, reject) => { + moderator.on('room.message', noticeListener(this.mjolnir.managementRoomId, (event) => { + resolve(event); + })) + }); - const draupnir = this.config.RUNTIME.client! - let draupnirUserId = await draupnir.getUserId(); + moderator.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text.', body: `!mjolnir queryAdmin http://localhost:7070` }); - // Check if draupnir replied - await getMessagesByUserIn(moderator, draupnirUserId, this.mjolnir.managementRoomId, 1000, function (events) { - events.map(e => { - if (e.type === 'm.room.message') { - assert.equal(e.content.body, "", `Draupnir did not parse the json as expected: ${e.content.body}.`) - } - }) - }); + const reply_event = await reply; + + assert.equal(reply_event.content.body, "", `Draupnir did not parse the json as expected: ${reply_event.content.body}.`) }) });