diff --git a/src/commands/WatchUnwatchCommand.ts b/src/commands/WatchUnwatchCommand.ts index c9ee27e9..80f0db77 100644 --- a/src/commands/WatchUnwatchCommand.ts +++ b/src/commands/WatchUnwatchCommand.ts @@ -8,7 +8,12 @@ // https://github.com/matrix-org/mjolnir // -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> { - 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> { - 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(), + }) + ); } diff --git a/test/unit/commands/WatchUnwatchCommandTest.ts b/test/unit/commands/WatchUnwatchCommandTest.ts new file mode 100644 index 00000000..b4ca49aa --- /dev/null +++ b/test/unit/commands/WatchUnwatchCommandTest.ts @@ -0,0 +1,62 @@ +// SPDX-FileCopyrightText: 2024 Gnuxie +// +// 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({ + async watchList(propagation, room, _options) { + expect(room).toBe(policyRoom); + expect(propagation).toBe(PropagationType.Direct); + return Ok(undefined); + }, + }); + const roomResolver = createMock({ + 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); + }); +});