mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-21 05:32:53 +00:00
Matrix: avoid redundant self id retries
This commit is contained in:
43
extensions/matrix/src/matrix/direct-room.test.ts
Normal file
43
extensions/matrix/src/matrix/direct-room.test.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { inspectMatrixDirectRoomEvidence } from "./direct-room.js";
|
||||
import type { MatrixClient } from "./sdk.js";
|
||||
|
||||
function createClient(overrides: Partial<MatrixClient> = {}): MatrixClient {
|
||||
return {
|
||||
getUserId: vi.fn(async () => "@bot:example.org"),
|
||||
getJoinedRoomMembers: vi.fn(async () => ["@bot:example.org", "@alice:example.org"]),
|
||||
getRoomStateEvent: vi.fn(async () => ({})),
|
||||
...overrides,
|
||||
} as unknown as MatrixClient;
|
||||
}
|
||||
|
||||
describe("inspectMatrixDirectRoomEvidence", () => {
|
||||
it("does not retry getUserId when callers explicitly pass a missing self user", async () => {
|
||||
const getUserId = vi.fn(async () => "@bot:example.org");
|
||||
const client = createClient({ getUserId });
|
||||
|
||||
const result = await inspectMatrixDirectRoomEvidence({
|
||||
client,
|
||||
roomId: "!dm:example.org",
|
||||
remoteUserId: "@alice:example.org",
|
||||
selfUserId: null,
|
||||
});
|
||||
|
||||
expect(getUserId).not.toHaveBeenCalled();
|
||||
expect(result.strict).toBe(false);
|
||||
});
|
||||
|
||||
it("resolves selfUserId when callers leave it undefined", async () => {
|
||||
const getUserId = vi.fn(async () => "@bot:example.org");
|
||||
const client = createClient({ getUserId });
|
||||
|
||||
const result = await inspectMatrixDirectRoomEvidence({
|
||||
client,
|
||||
roomId: "!dm:example.org",
|
||||
remoteUserId: "@alice:example.org",
|
||||
});
|
||||
|
||||
expect(getUserId).toHaveBeenCalledTimes(1);
|
||||
expect(result.strict).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -75,8 +75,9 @@ export async function inspectMatrixDirectRoomEvidence(params: {
|
||||
selfUserId?: string | null;
|
||||
}): Promise<MatrixDirectRoomEvidence> {
|
||||
const selfUserId =
|
||||
trimMaybeString(params.selfUserId) ??
|
||||
trimMaybeString(await params.client.getUserId().catch(() => null));
|
||||
params.selfUserId !== undefined
|
||||
? trimMaybeString(params.selfUserId)
|
||||
: trimMaybeString(await params.client.getUserId().catch(() => null));
|
||||
const joinedMembers = await readJoinedMatrixMembers(params.client, params.roomId);
|
||||
const strict = isStrictDirectMembership({
|
||||
selfUserId,
|
||||
|
||||
Reference in New Issue
Block a user