unit test Draupnir watch/unwatch commands.

This commit is contained in:
gnuxie committed 2024-09-09 13:28:16 +01:00
1 parent 804bad5e37
commit 3df3e0eccb
2 files changed
+90 -12

No files matched your search

+28 -12
View File
@@ -8,7 +8,12 @@
// https://github.com/matrix-org/mjolnir
// </text>
import { PropagationType, isError } from "matrix-protection-suite";
import {
PolicyListConfig,
PropagationType,
RoomResolver,
isError,
} from "matrix-protection-suite";
import {
MatrixRoomReferencePresentationSchema,
describeCommand,
@@ -16,7 +21,15 @@ import {
} from "@the-draupnir-project/interface-manager";
import { Result } from "@gnuxie/typescript-result";
import { Draupnir } from "../Draupnir";
import { DraupnirInterfaceAdaptor } from "./DraupnirCommandPrerequisites";
import {
DraupnirContextToCommandContextTranslator,
DraupnirInterfaceAdaptor,
} from "./DraupnirCommandPrerequisites";
export type DraupnirWatchUnwatchCommandContext = {
issuerManager: PolicyListConfig;
roomResolver: RoomResolver;
};
export const DraupnirWatchPolicyRoomCommand = describeCommand({
summary:
@@ -26,19 +39,17 @@ export const DraupnirWatchPolicyRoomCommand = describeCommand({
acceptor: MatrixRoomReferencePresentationSchema,
}),
async executor(
draupnir: Draupnir,
{ issuerManager, roomResolver }: DraupnirWatchUnwatchCommandContext,
_info,
_keywords,
_rest,
policyRoomReference
): Promise<Result<void>> {
const policyRoom = await draupnir.clientPlatform
.toRoomResolver()
.resolveRoom(policyRoomReference);
const policyRoom = await roomResolver.resolveRoom(policyRoomReference);
if (isError(policyRoom)) {
return policyRoom;
}
return await draupnir.protectedRoomsSet.issuerManager.watchList(
return await issuerManager.watchList(
PropagationType.Direct,
policyRoom.ok,
{}
@@ -54,19 +65,17 @@ export const DraupnirUnwatchPolicyRoomCommand = describeCommand({
acceptor: MatrixRoomReferencePresentationSchema,
}),
async executor(
draupnir: Draupnir,
{ issuerManager, roomResolver }: DraupnirWatchUnwatchCommandContext,
_info,
_keywords,
_rest,
policyRoomReference
): Promise<Result<void>> {
const policyRoom = await draupnir.clientPlatform
.toRoomResolver()
.resolveRoom(policyRoomReference);
const policyRoom = await roomResolver.resolveRoom(policyRoomReference);
if (isError(policyRoom)) {
return policyRoom;
}
return await draupnir.protectedRoomsSet.issuerManager.unwatchList(
return await issuerManager.unwatchList(
PropagationType.Direct,
policyRoom.ok
);
@@ -80,4 +89,11 @@ for (const command of [
DraupnirInterfaceAdaptor.describeRenderer(command, {
isAlwaysSupposedToUseDefaultRenderer: true,
});
DraupnirContextToCommandContextTranslator.registerTranslation(
command,
(draupnir: Draupnir) => ({
issuerManager: draupnir.protectedRoomsSet.issuerManager,
roomResolver: draupnir.clientPlatform.toRoomResolver(),
})
);
}
@@ -0,0 +1,62 @@
// SPDX-FileCopyrightText: 2024 Gnuxie <Gnuxie@protonmail.com>
//
// SPDX-License-Identifier: AFL-3.0
import {
MatrixRoomID,
MatrixRoomReference,
StringRoomID,
} from "@the-draupnir-project/matrix-basic-types";
import expect from "expect";
import {
Ok,
PolicyListConfig,
PropagationType,
RoomResolver,
isOk,
} from "matrix-protection-suite";
import { createMock } from "ts-auto-mock";
import {
DraupnirUnwatchPolicyRoomCommand,
DraupnirWatchPolicyRoomCommand,
} from "../../../src/commands/WatchUnwatchCommand";
import { CommandExecutorHelper } from "@the-draupnir-project/interface-manager";
describe("Test the WatchUnwatchCommmands", function () {
const policyRoom = MatrixRoomReference.fromRoomID(
"!room:example.com" as StringRoomID
);
const issuerManager = createMock<PolicyListConfig>({
async watchList(propagation, room, _options) {
expect(room).toBe(policyRoom);
expect(propagation).toBe(PropagationType.Direct);
return Ok(undefined);
},
});
const roomResolver = createMock<RoomResolver>({
async resolveRoom(roomReference) {
if (roomReference instanceof MatrixRoomID) {
return Ok(roomReference);
}
throw new TypeError(`We don't really expect to resolve anything`);
},
});
it("DraupnirWatchCommand", async function () {
const result = await CommandExecutorHelper.execute(
DraupnirWatchPolicyRoomCommand,
{ issuerManager, roomResolver },
{},
policyRoom
);
expect(isOk(result)).toBe(true);
});
it("DraupnirUnwatchCommand", async function () {
const result = await CommandExecutorHelper.execute(
DraupnirUnwatchPolicyRoomCommand,
{ issuerManager, roomResolver },
{},
policyRoom
);
expect(isOk(result)).toBe(true);
});
});