test: remove redundant matrix conversions

This commit is contained in:
Peter Steinberger
2026-04-10 21:34:37 +01:00
parent 71c4900051
commit b0a39f4112
2 changed files with 41 additions and 55 deletions

View File

@@ -13,10 +13,18 @@ type VerificationSummaryListener = (summary: MatrixVerificationSummary) => void;
function getSentNoticeBody(sendMessage: ReturnType<typeof vi.fn>, index = 0): string {
const calls = sendMessage.mock.calls as unknown[][];
const payload = (calls[index]?.[1] ?? {}) as { body?: string };
return getSentNoticeBodyFromCall(calls[index] ?? []);
}
function getSentNoticeBodyFromCall(call: unknown[]): string {
const payload = (call[1] ?? {}) as { body?: string };
return payload.body ?? "";
}
function getSentNoticeBodies(sendMessage: ReturnType<typeof vi.fn>): string[] {
return (sendMessage.mock.calls as unknown[][]).map(getSentNoticeBodyFromCall);
}
function createHarness(params?: {
cfg?: CoreConfig;
accountId?: string;
@@ -532,9 +540,7 @@ describe("registerMatrixMonitorEvents verification routing", () => {
});
await vi.waitFor(() => {
const bodies = (sendMessage.mock.calls as unknown[][]).map((call) =>
String((call[1] as { body?: string } | undefined)?.body ?? ""),
);
const bodies = getSentNoticeBodies(sendMessage);
expect(bodies.some((body) => body.includes("SAS emoji:"))).toBe(true);
expect(bodies.some((body) => body.includes("SAS decimal: 6158 1986 3513"))).toBe(true);
});
@@ -595,9 +601,7 @@ describe("registerMatrixMonitorEvents verification routing", () => {
});
await vi.waitFor(() => {
const bodies = (sendMessage.mock.calls as unknown[][]).map((call) =>
String((call[1] as { body?: string } | undefined)?.body ?? ""),
);
const bodies = getSentNoticeBodies(sendMessage);
expect(bodies.some((body) => body.includes("SAS decimal: 2468 1357 9753"))).toBe(true);
});
});
@@ -739,9 +743,7 @@ describe("registerMatrixMonitorEvents verification routing", () => {
});
await vi.waitFor(() => {
const bodies = (sendMessage.mock.calls as unknown[][]).map((call) =>
String((call[1] as { body?: string } | undefined)?.body ?? ""),
);
const bodies = getSentNoticeBodies(sendMessage);
expect(bodies.some((body) => body.includes("SAS decimal: 1111 2222 3333"))).toBe(true);
});
});
@@ -812,9 +814,7 @@ describe("registerMatrixMonitorEvents verification routing", () => {
});
await vi.waitFor(() => {
const bodies = (sendMessage.mock.calls as unknown[][]).map((call) =>
String((call[1] as { body?: string } | undefined)?.body ?? ""),
);
const bodies = getSentNoticeBodies(sendMessage);
expect(bodies.some((body) => body.includes("Matrix verification started with"))).toBe(true);
});
@@ -844,16 +844,12 @@ describe("registerMatrixMonitorEvents verification routing", () => {
});
await vi.waitFor(() => {
const bodies = (sendMessage.mock.calls as unknown[][]).map((call) =>
String((call[1] as { body?: string } | undefined)?.body ?? ""),
);
const bodies = getSentNoticeBodies(sendMessage);
expect(bodies.some((body) => body.includes("SAS decimal: 2468 1357 9753"))).toBe(true);
});
const calls = sendMessage.mock.calls as unknown[][];
const sasCall = calls.find((call) =>
String((call[1] as { body?: string } | undefined)?.body ?? "").includes(
"SAS decimal: 2468 1357 9753",
),
getSentNoticeBodyFromCall(call).includes("SAS decimal: 2468 1357 9753"),
);
expect((sasCall?.[0] ?? "") as string).toBe("!dm-active:example.org");
});
@@ -910,9 +906,7 @@ describe("registerMatrixMonitorEvents verification routing", () => {
await vi.advanceTimersByTimeAsync(500);
await vi.waitFor(() => {
const bodies = (sendMessage.mock.calls as unknown[][]).map((call) =>
String((call[1] as { body?: string } | undefined)?.body ?? ""),
);
const bodies = getSentNoticeBodies(sendMessage);
expect(bodies.some((body) => body.includes("SAS emoji:"))).toBe(true);
});
} finally {
@@ -1112,9 +1106,9 @@ describe("registerMatrixMonitorEvents verification routing", () => {
expect(listVerifications).toHaveBeenCalledTimes(2);
});
const sasBodies = sendMessage.mock.calls
.map((call) => String(((call as unknown[])[1] as { body?: string } | undefined)?.body ?? ""))
.filter((body) => body.includes("SAS emoji:"));
const sasBodies = getSentNoticeBodies(sendMessage).filter((body) =>
body.includes("SAS emoji:"),
);
expect(sasBodies).toHaveLength(1);
});
@@ -1172,14 +1166,10 @@ describe("registerMatrixMonitorEvents verification routing", () => {
});
await vi.waitFor(() => {
const bodies = (sendMessage.mock.calls as unknown[][]).map((call) =>
String((call[1] as { body?: string } | undefined)?.body ?? ""),
);
const bodies = getSentNoticeBodies(sendMessage);
expect(bodies.some((body) => body.includes("SAS decimal: 6158 1986 3513"))).toBe(true);
});
const bodies = (sendMessage.mock.calls as unknown[][]).map((call) =>
String((call[1] as { body?: string } | undefined)?.body ?? ""),
);
const bodies = getSentNoticeBodies(sendMessage);
expect(bodies.some((body) => body.includes("SAS decimal: 1111 2222 3333"))).toBe(false);
});
@@ -1221,9 +1211,7 @@ describe("registerMatrixMonitorEvents verification routing", () => {
});
await vi.waitFor(() => {
const bodies = (sendMessage.mock.calls as unknown[][]).map((call) =>
String((call[1] as { body?: string } | undefined)?.body ?? ""),
);
const bodies = getSentNoticeBodies(sendMessage);
expect(bodies.some((body) => body.includes("SAS decimal: 6158 1986 3513"))).toBe(true);
});
});
@@ -1284,14 +1272,10 @@ describe("registerMatrixMonitorEvents verification routing", () => {
});
await vi.waitFor(() => {
const bodies = (sendMessage.mock.calls as unknown[][]).map((call) =>
String((call[1] as { body?: string } | undefined)?.body ?? ""),
);
const bodies = getSentNoticeBodies(sendMessage);
expect(bodies.some((body) => body.includes("SAS decimal: 6158 1986 3513"))).toBe(true);
});
const bodies = (sendMessage.mock.calls as unknown[][]).map((call) =>
String((call[1] as { body?: string } | undefined)?.body ?? ""),
);
const bodies = getSentNoticeBodies(sendMessage);
expect(bodies.some((body) => body.includes("SAS decimal: 1111 2222 3333"))).toBe(false);
});

View File

@@ -95,7 +95,9 @@ class FakeMatrixEvent extends EventEmitter {
}
}
type MatrixJsClientStub = EventEmitter & {
type MatrixJsClientStub = {
emit: (eventName: string | symbol, ...args: unknown[]) => boolean;
on: (eventName: string | symbol, listener: (...args: unknown[]) => void) => MatrixJsClientStub;
startClient: ReturnType<typeof vi.fn>;
stopClient: ReturnType<typeof vi.fn>;
initRustCrypto: ReturnType<typeof vi.fn>;
@@ -126,7 +128,7 @@ type MatrixJsClientStub = EventEmitter & {
};
function createMatrixJsClientStub(): MatrixJsClientStub {
const client = new EventEmitter() as MatrixJsClientStub;
const client = new EventEmitter() as unknown as MatrixJsClientStub;
client.startClient = vi.fn(async () => {
queueMicrotask(() => {
client.emit("sync", "PREPARED", null, undefined);
@@ -1977,10 +1979,10 @@ describe("MatrixClient crypto bootstrapping", () => {
encryption: true,
});
vi.spyOn(client, "doRequest").mockImplementation(async (method, endpoint) => {
if (method === "GET" && String(endpoint).includes("/room_keys/version")) {
if (method === "GET" && endpoint.includes("/room_keys/version")) {
return { version: "21868" };
}
if (method === "DELETE" && String(endpoint).includes("/room_keys/version/21868")) {
if (method === "DELETE" && endpoint.includes("/room_keys/version/21868")) {
return {};
}
return {};
@@ -2031,10 +2033,10 @@ describe("MatrixClient crypto bootstrapping", () => {
encryption: true,
});
vi.spyOn(client, "doRequest").mockImplementation(async (method, endpoint) => {
if (method === "GET" && String(endpoint).includes("/room_keys/version")) {
if (method === "GET" && endpoint.includes("/room_keys/version")) {
return { version: "22245" };
}
if (method === "DELETE" && String(endpoint).includes("/room_keys/version/22245")) {
if (method === "DELETE" && endpoint.includes("/room_keys/version/22245")) {
return {};
}
return {};
@@ -2071,10 +2073,10 @@ describe("MatrixClient crypto bootstrapping", () => {
encryption: true,
});
vi.spyOn(client, "doRequest").mockImplementation(async (method, endpoint) => {
if (method === "GET" && String(endpoint).includes("/room_keys/version")) {
if (method === "GET" && endpoint.includes("/room_keys/version")) {
return { version: "21868" };
}
if (method === "DELETE" && String(endpoint).includes("/room_keys/version/21868")) {
if (method === "DELETE" && endpoint.includes("/room_keys/version/21868")) {
return {};
}
return {};
@@ -2129,10 +2131,10 @@ describe("MatrixClient crypto bootstrapping", () => {
encryption: true,
});
vi.spyOn(client, "doRequest").mockImplementation(async (method, endpoint) => {
if (method === "GET" && String(endpoint).includes("/room_keys/version")) {
if (method === "GET" && endpoint.includes("/room_keys/version")) {
return { version: "21999" };
}
if (method === "DELETE" && String(endpoint).includes("/room_keys/version/21999")) {
if (method === "DELETE" && endpoint.includes("/room_keys/version/21999")) {
return {};
}
return {};
@@ -2189,7 +2191,7 @@ describe("MatrixClient crypto bootstrapping", () => {
encryption: true,
});
const doRequest = vi.spyOn(client, "doRequest").mockImplementation(async (method, endpoint) => {
if (method === "GET" && String(endpoint).includes("/room_keys/version")) {
if (method === "GET" && endpoint.includes("/room_keys/version")) {
return {};
}
return {};
@@ -2246,10 +2248,10 @@ describe("MatrixClient crypto bootstrapping", () => {
encryption: true,
});
vi.spyOn(client, "doRequest").mockImplementation(async (method, endpoint) => {
if (method === "GET" && String(endpoint).includes("/room_keys/version")) {
if (method === "GET" && endpoint.includes("/room_keys/version")) {
return { version: "22000" };
}
if (method === "DELETE" && String(endpoint).includes("/room_keys/version/22000")) {
if (method === "DELETE" && endpoint.includes("/room_keys/version/22000")) {
return {};
}
return {};
@@ -2431,7 +2433,7 @@ describe("MatrixClient crypto bootstrapping", () => {
});
let backupChecks = 0;
vi.spyOn(client, "doRequest").mockImplementation(async (_method, endpoint) => {
if (String(endpoint).includes("/room_keys/version")) {
if (endpoint.includes("/room_keys/version")) {
backupChecks += 1;
return backupChecks >= 2 ? { version: "7" } : {};
}
@@ -2488,7 +2490,7 @@ describe("MatrixClient crypto bootstrapping", () => {
published: true,
});
vi.spyOn(client, "doRequest").mockImplementation(async (_method, endpoint) => {
if (String(endpoint).includes("/room_keys/version")) {
if (endpoint.includes("/room_keys/version")) {
return { version: "9" };
}
return {};