Give mjolnir a clean history in each test

This commit is contained in:
gnuxie 2021-09-23 16:12:08 +01:00
parent 68aa717826
commit ff216f4918
3 changed files with 32 additions and 17 deletions

View File

@ -1,19 +1,27 @@
import config from "../../src/config";
import { Mjolnir } from "../../src/Mjolnir";
import { makeMjolnir } from "./mjolnirSetupUtils";
import { makeMjolnir, teardownManagementRoom } from "./mjolnirSetupUtils";
export async function mochaGlobalSetup() {
console.log("Starting mjolnir.");
try {
this.bot = await makeMjolnir()
// do not block on this!
this.bot.start();
} catch (e) {
console.trace(e);
throw e;
}
}
export async function mochaGlobalTeardown() {
this.bot.stop();
console.log('stopping mjolnir');
}
// when mjolnir starts it clobbers the config, which is cached between runs,
// by resolving the alias and setting it to a roomid.
export const mochaHooks = {
beforeEach: [
async function() {
this.managementRoomAlias = config.managementRoom
this.mjolnir = await makeMjolnir()
this.mjolnir.start()
}
],
afterEach: [
async function() {
console.log("stopping mjolnir");
await this.mjolnir.stop();
// unclobber mjolnir's dirty work, i thought the config was being cached
// and was global, but that might have just been supersitiion, needs confirming.
let managementRoomId = config.managementRoom;
config.managementRoom = this.managementRoomAlias;
// remove alias from management room and leave it.
await teardownManagementRoom(this.mjolnir.client, managementRoomId, this.managementRoomAlias);
}
]
};

View File

@ -12,6 +12,7 @@ describe("help command", () => {
})
it('Mjolnir responded to !mjolnir help', async function() {
this.timeout(30000);
console.log(`management room ${config.managementRoom}`);
// send a messgage
await client.joinRoom(config.managementRoom);
// listener for getting the event reply
@ -23,6 +24,7 @@ describe("help command", () => {
}
}))});
// check we get one back
console.log(config);
await client.sendMessage(config.managementRoom, {msgtype: "m.text", body: "!mjolnir help"})
await reply
})

View File

@ -75,4 +75,9 @@ export async function makeMjolnir() {
const client = await pantalaimon.createClientWithCredentials(config.pantalaimon.username, config.pantalaimon.password);
await ensureManagementRoomExists(client);
return await setupMjolnir(client, config);
}
export async function teardownManagementRoom(client: MatrixClient, roomId: string, alias: string) {
await client.deleteRoomAlias(alias);
await client.leaveRoom(roomId);
}