Skip to content

Commit a5afdbe

Browse files
authored
Unit tests for the ban and unban commands. (#542)
* Unit test Ban and UnbanCommands. Fixes #441 will follow up with the-draupnir-project/interface-manager#7. Part of the-draupnir-project/planning#22. * Update to @the-draupnir-project/[email protected]. * Rest parameters can only give one argument from prompts. * Update to @the-draupnir-project/[email protected].
1 parent 986afb7 commit a5afdbe

File tree

10 files changed

+496
-69
lines changed

10 files changed

+496
-69
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"@sentry/node": "^7.17.2",
5757
"@sentry/tracing": "^7.17.2",
5858
"@sinclair/typebox": "0.32.34",
59-
"@the-draupnir-project/interface-manager": "2.0.0",
59+
"@the-draupnir-project/interface-manager": "2.2.0",
6060
"@the-draupnir-project/matrix-basic-types": "^0.1.1",
6161
"await-lock": "^2.2.2",
6262
"better-sqlite3": "^9.4.3",

src/commands/Ban.tsx

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,21 @@
88
// https://github.com/matrix-org/mjolnir
99
// </text>
1010

11-
import { Draupnir } from "../Draupnir";
1211
import {
1312
ActionResult,
1413
PolicyRoomEditor,
1514
PolicyRuleType,
1615
isError,
1716
Ok,
17+
PolicyRoomManager,
18+
RoomResolver,
19+
PolicyListConfig,
1820
} from "matrix-protection-suite";
19-
import { resolveRoomReferenceSafe } from "matrix-protection-suite-for-matrix-bot-sdk";
2021
import { findPolicyRoomIDFromShortcode } from "./CreateBanListCommand";
2122
import {
2223
MatrixRoomReference,
2324
MatrixUserID,
25+
StringUserID,
2426
} from "@the-draupnir-project/matrix-basic-types";
2527
import {
2628
BasicInvocationInformation,
@@ -32,22 +34,31 @@ import {
3234
tuple,
3335
union,
3436
} from "@the-draupnir-project/interface-manager";
35-
import { DraupnirInterfaceAdaptor } from "./DraupnirCommandPrerequisites";
37+
import {
38+
DraupnirContextToCommandContextTranslator,
39+
DraupnirInterfaceAdaptor,
40+
} from "./DraupnirCommandPrerequisites";
3641

3742
export async function findPolicyRoomEditorFromRoomReference(
38-
draupnir: Draupnir,
43+
roomResolver: RoomResolver,
44+
policyRoomManager: PolicyRoomManager,
3945
policyRoomReference: MatrixRoomReference
4046
): Promise<ActionResult<PolicyRoomEditor>> {
41-
const policyRoomID = await resolveRoomReferenceSafe(
42-
draupnir.client,
43-
policyRoomReference
44-
);
47+
const policyRoomID = await roomResolver.resolveRoom(policyRoomReference);
4548
if (isError(policyRoomID)) {
4649
return policyRoomID;
4750
}
48-
return await draupnir.policyRoomManager.getPolicyRoomEditor(policyRoomID.ok);
51+
return await policyRoomManager.getPolicyRoomEditor(policyRoomID.ok);
4952
}
5053

54+
export type DraupnirBanCommandContext = {
55+
policyRoomManager: PolicyRoomManager;
56+
issuerManager: PolicyListConfig;
57+
defaultReasons: string[];
58+
roomResolver: RoomResolver;
59+
clientUserID: StringUserID;
60+
};
61+
5162
export const DraupnirBanCommand = describeCommand({
5263
summary: "Bans an entity from the policy list.",
5364
parameters: tuple(
@@ -67,13 +78,13 @@ export const DraupnirBanCommand = describeCommand({
6778
MatrixRoomReferencePresentationSchema,
6879
StringPresentationType
6980
),
70-
prompt: async function (draupnir: Draupnir) {
81+
prompt: async function ({
82+
policyRoomManager,
83+
clientUserID,
84+
}: DraupnirBanCommandContext) {
7185
return Ok({
72-
suggestions: draupnir.policyRoomManager
73-
.getEditablePolicyRoomIDs(
74-
draupnir.clientUserID,
75-
PolicyRuleType.User
76-
)
86+
suggestions: policyRoomManager
87+
.getEditablePolicyRoomIDs(clientUserID, PolicyRuleType.User)
7788
.map((room) => MatrixRoomIDPresentationType.wrap(room)),
7889
});
7990
},
@@ -83,16 +94,21 @@ export const DraupnirBanCommand = describeCommand({
8394
name: "reason",
8495
description: "The reason for the ban.",
8596
acceptor: StringPresentationType,
86-
prompt: async function (draupnir: Draupnir) {
97+
prompt: async function ({ defaultReasons }: DraupnirBanCommandContext) {
8798
return Ok({
88-
suggestions: draupnir.config.commands.ban.defaultReasons.map(
89-
(reason) => [StringPresentationType.wrap(reason)]
99+
suggestions: defaultReasons.map((reason) =>
100+
StringPresentationType.wrap(reason)
90101
),
91102
});
92103
},
93104
},
94105
async executor(
95-
draupnir: Draupnir,
106+
{
107+
issuerManager,
108+
policyRoomManager,
109+
roomResolver,
110+
clientUserID,
111+
}: DraupnirBanCommandContext,
96112
_info: BasicInvocationInformation,
97113
_keywords,
98114
reasonParts,
@@ -101,13 +117,19 @@ export const DraupnirBanCommand = describeCommand({
101117
): Promise<ActionResult<string>> {
102118
const policyRoomReference =
103119
typeof policyRoomDesignator === "string"
104-
? await findPolicyRoomIDFromShortcode(draupnir, policyRoomDesignator)
120+
? await findPolicyRoomIDFromShortcode(
121+
issuerManager,
122+
policyRoomManager,
123+
clientUserID,
124+
policyRoomDesignator
125+
)
105126
: Ok(policyRoomDesignator);
106127
if (isError(policyRoomReference)) {
107128
return policyRoomReference;
108129
}
109130
const policyListEditorResult = await findPolicyRoomEditorFromRoomReference(
110-
draupnir,
131+
roomResolver,
132+
policyRoomManager,
111133
policyRoomReference.ok
112134
);
113135
if (isError(policyListEditorResult)) {
@@ -128,10 +150,7 @@ export const DraupnirBanCommand = describeCommand({
128150
reason
129151
);
130152
} else {
131-
const resolvedRoomReference = await resolveRoomReferenceSafe(
132-
draupnir.client,
133-
entity
134-
);
153+
const resolvedRoomReference = await roomResolver.resolveRoom(entity);
135154
if (isError(resolvedRoomReference)) {
136155
return resolvedRoomReference;
137156
}
@@ -144,6 +163,19 @@ export const DraupnirBanCommand = describeCommand({
144163
},
145164
});
146165

166+
DraupnirContextToCommandContextTranslator.registerTranslation(
167+
DraupnirBanCommand,
168+
function (draupnir) {
169+
return {
170+
policyRoomManager: draupnir.policyRoomManager,
171+
issuerManager: draupnir.protectedRoomsSet.issuerManager,
172+
defaultReasons: draupnir.config.commands.ban.defaultReasons,
173+
roomResolver: draupnir.clientPlatform.toRoomResolver(),
174+
clientUserID: draupnir.clientUserID,
175+
};
176+
}
177+
);
178+
147179
DraupnirInterfaceAdaptor.describeRenderer(DraupnirBanCommand, {
148180
isAlwaysSupposedToUseDefaultRenderer: true,
149181
});

src/commands/CreateBanListCommand.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ import {
1212
ActionError,
1313
ActionResult,
1414
Ok,
15+
PolicyListConfig,
16+
PolicyRoomManager,
1517
PolicyRuleType,
1618
PropagationType,
1719
isError,
1820
} from "matrix-protection-suite";
1921
import { listInfo } from "./StatusCommand";
2022
import { Draupnir } from "../Draupnir";
21-
import { MatrixRoomID } from "@the-draupnir-project/matrix-basic-types";
23+
import {
24+
MatrixRoomID,
25+
StringUserID,
26+
} from "@the-draupnir-project/matrix-basic-types";
2227
import {
2328
BasicInvocationInformation,
2429
ParsedKeywords,
@@ -84,10 +89,12 @@ DraupnirInterfaceAdaptor.describeRenderer(DraupnirListCreateCommand, {
8489
});
8590

8691
export async function findPolicyRoomIDFromShortcode(
87-
draupnir: Draupnir,
92+
issuerManager: PolicyListConfig,
93+
policyRoomManager: PolicyRoomManager,
94+
editingClientUserID: StringUserID,
8895
shortcode: string
8996
): Promise<ActionResult<MatrixRoomID>> {
90-
const info = await listInfo(draupnir);
97+
const info = await listInfo(issuerManager, policyRoomManager);
9198
const matchingRevisions = info.filter(
9299
(list) => list.revision.shortcode === shortcode
93100
);
@@ -99,7 +106,7 @@ export async function findPolicyRoomIDFromShortcode(
99106
return Ok(matchingRevisions[0].revision.room);
100107
} else {
101108
const remainingRevisions = matchingRevisions.filter((revision) =>
102-
revision.revision.isAbleToEdit(draupnir.clientUserID, PolicyRuleType.User)
109+
revision.revision.isAbleToEdit(editingClientUserID, PolicyRuleType.User)
103110
);
104111
if (
105112
remainingRevisions.length !== 1 ||

src/commands/Rules.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,10 @@ export const DraupnirListRulesCommand = describeCommand({
9090
summary: "Lists the rules currently in use by Draupnir.",
9191
parameters: [],
9292
async executor(draupnir: Draupnir): Promise<Result<ListMatches[]>> {
93-
const infoResult = await listInfo(draupnir);
93+
const infoResult = await listInfo(
94+
draupnir.protectedRoomsSet.issuerManager,
95+
draupnir.policyRoomManager
96+
);
9497
return Ok(
9598
infoResult.map((policyRoom) => ({
9699
room: policyRoom.revision.room,
@@ -124,7 +127,10 @@ export const DraupnirRulesMatchingCommand = describeCommand({
124127
_rest,
125128
entity
126129
): Promise<Result<ListMatches[]>> {
127-
const policyRooms = await listInfo(draupnir);
130+
const policyRooms = await listInfo(
131+
draupnir.protectedRoomsSet.issuerManager,
132+
draupnir.policyRoomManager
133+
);
128134
return Ok(
129135
policyRooms.map((policyRoom) => {
130136
return {

src/commands/StatusCommand.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import { DOCUMENTATION_URL, PACKAGE_JSON, SOFTWARE_VERSION } from "../config";
1212
import {
1313
ActionResult,
1414
Ok,
15+
PolicyListConfig,
16+
PolicyRoomManager,
1517
PolicyRoomRevision,
1618
PolicyRoomWatchProfile,
1719
PolicyRuleType,
@@ -47,12 +49,14 @@ export interface StatusInfo {
4749
documentationURL: string;
4850
}
4951

50-
export async function listInfo(draupnir: Draupnir): Promise<ListInfo[]> {
51-
const watchedListProfiles =
52-
draupnir.protectedRoomsSet.issuerManager.allWatchedLists;
52+
export async function listInfo(
53+
issuerManager: PolicyListConfig,
54+
policyRoomManager: PolicyRoomManager
55+
): Promise<ListInfo[]> {
56+
const watchedListProfiles = issuerManager.allWatchedLists;
5357
const issuerResults = await Promise.all(
5458
watchedListProfiles.map((profile) =>
55-
draupnir.policyRoomManager.getPolicyRoomRevisionIssuer(profile.room)
59+
policyRoomManager.getPolicyRoomRevisionIssuer(profile.room)
5660
)
5761
);
5862
return issuerResults.map((result) => {
@@ -89,7 +93,10 @@ DraupnirInterfaceAdaptor.describeRenderer(DraupnirStatusCommand, {
8993
export async function draupnirStatusInfo(
9094
draupnir: Draupnir
9195
): Promise<StatusInfo> {
92-
const watchedListInfo = await listInfo(draupnir);
96+
const watchedListInfo = await listInfo(
97+
draupnir.protectedRoomsSet.issuerManager,
98+
draupnir.policyRoomManager
99+
);
93100
const protectedWatchedLists = watchedListInfo.filter((info) =>
94101
draupnir.protectedRoomsSet.isProtectedRoom(
95102
info.revision.room.toRoomIDOrAlias()

0 commit comments

Comments
 (0)