test: guard matrix helper assertions

This commit is contained in:
Peter Steinberger
2026-05-11 19:44:37 +01:00
parent 4c7bffc3a7
commit cd9f0b97f8
5 changed files with 22 additions and 21 deletions

View File

@@ -22,9 +22,9 @@ type MatrixPendingPluginApprovalView = Extract<
const MATRIX_APPROVAL_METADATA_KEY = "com.openclaw.approval";
function expectRecordFields(value: unknown, expected: Record<string, unknown>) {
expect(value).toBeDefined();
expect(typeof value).toBe("object");
expect(value).not.toBeNull();
if (!value || typeof value !== "object") {
throw new Error("Expected record");
}
const actual = value as Record<string, unknown>;
for (const [key, expectedValue] of Object.entries(expected)) {
expect(actual[key]).toEqual(expectedValue);

View File

@@ -52,7 +52,6 @@ describe("matrix channel message adapter", () => {
it("backs declared durable-final capabilities with runtime outbound proofs", async () => {
const adapter = matrixPlugin.message;
expect(adapter).toBeDefined();
if (!adapter?.send?.text || !adapter.send.media) {
throw new Error("Expected Matrix message adapter send capabilities.");
}

View File

@@ -71,7 +71,6 @@ function expectSavedCredentials(
accountId: string,
) {
const call = mock.mock.calls[0] as unknown[] | undefined;
expect(call).toBeDefined();
if (!call) {
throw new Error("missing save credentials call");
}
@@ -82,7 +81,6 @@ function expectSavedCredentials(
function expectMatrixLoginCall(fields: Record<string, unknown>) {
const call = matrixDoRequestMock.mock.calls[0] as unknown[] | undefined;
expect(call).toBeDefined();
if (!call) {
throw new Error("missing Matrix login call");
}

View File

@@ -140,15 +140,22 @@ function requireArray(value: unknown, label: string): Array<unknown> {
function mockCalls(mock: unknown, label: string): Array<Array<unknown>> {
const mockState = (mock as { mock?: { calls?: Array<Array<unknown>> } }).mock;
expect(mockState, `${label}.mock`).toBeDefined();
expect(Array.isArray(mockState?.calls), `${label}.mock.calls`).toBe(true);
return mockState?.calls ?? [];
if (!mockState) {
throw new Error(`${label}.mock was missing`);
}
const calls = mockState.calls;
if (!Array.isArray(calls)) {
throw new Error(`${label}.mock.calls was not an array`);
}
return calls;
}
function callArg(mock: unknown, callIndex: number, argIndex: number, label: string) {
const call = mockCalls(mock, label).at(callIndex);
expect(call, label).toBeDefined();
return call?.[argIndex];
if (!call) {
throw new Error(`${label} call ${callIndex} was missing`);
}
return call[argIndex];
}
function lastCallArg(mock: unknown, argIndex: number, label: string) {
@@ -182,8 +189,10 @@ function expectRuntimeErrorContaining(mock: unknown, text: string) {
function findMockCall(mock: unknown, label: string, predicate: (call: Array<unknown>) => boolean) {
const call = mockCalls(mock, label).find(predicate);
expect(call, label).toBeDefined();
return call as Array<unknown>;
if (!call) {
throw new Error(`${label} was missing`);
}
return call;
}
function expectMatrixEdit(roomId: string, eventId: string, body: string) {
@@ -193,7 +202,7 @@ function expectMatrixEdit(roomId: string, eventId: string, body: string) {
([room, editedEventId, editedBody]) =>
room === roomId && editedEventId === eventId && editedBody === body,
);
expect(call[3], "edit options").toBeDefined();
requireRecord(call[3], "edit options");
}
function expectFinalizedPreviewEdit(eventId: string, text: string) {
@@ -730,7 +739,7 @@ describe("matrix monitor handler pairing account scope", () => {
);
expect(callArg(hasControlCommand, 0, 0, "control command")).toBe("/new");
expect(callArg(hasControlCommand, 0, 1, "control command")).toBeDefined();
requireRecord(callArg(hasControlCommand, 0, 1, "control command"), "control command context");
expect(recordInboundSession).not.toHaveBeenCalled();
expect(finalizeInboundContext).not.toHaveBeenCalled();
});
@@ -756,7 +765,7 @@ describe("matrix monitor handler pairing account scope", () => {
);
expect(callArg(hasControlCommand, 0, 0, "control command")).toBe("/new");
expect(callArg(hasControlCommand, 0, 1, "control command")).toBeDefined();
requireRecord(callArg(hasControlCommand, 0, 1, "control command"), "control command context");
const context = requireRecord(
callArg(finalizeInboundContext, 0, 0, "finalized context"),
"finalized context",
@@ -1083,7 +1092,6 @@ describe("matrix monitor handler pairing account scope", () => {
);
expect(callArg(sendNotice, 0, 0, "send notice")).toBe("!dm:example.org");
expect(callArg(sendNotice, 0, 1, "send notice")).toBeDefined();
expectNoticeSent(sendNotice);
await handler(
@@ -1138,7 +1146,6 @@ describe("matrix monitor handler pairing account scope", () => {
);
expect(callArg(sendNotice, 0, 0, "send notice")).toBe("!dm:example.org");
expect(callArg(sendNotice, 0, 1, "send notice")).toBeDefined();
expectNoticeSent(sendNotice);
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
@@ -1191,7 +1198,6 @@ describe("matrix monitor handler pairing account scope", () => {
);
expect(callArg(sendNotice, 0, 0, "send notice")).toBe("!dm:example.org");
expect(callArg(sendNotice, 0, 1, "send notice")).toBeDefined();
expectNoticeSent(sendNotice);
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });
@@ -1234,7 +1240,6 @@ describe("matrix monitor handler pairing account scope", () => {
);
expect(callArg(sendNotice, 0, 0, "send notice")).toBe("!dm:example.org");
expect(callArg(sendNotice, 0, 1, "send notice")).toBeDefined();
expectNoticeSent(sendNotice);
} finally {
fs.rmSync(tempDir, { recursive: true, force: true });

View File

@@ -114,7 +114,6 @@ function requireBindCallWithTarget(targetSessionKey: string) {
const record = params as { targetSessionKey?: string };
return record.targetSessionKey === targetSessionKey;
});
expect(call).toBeDefined();
if (!call) {
throw new Error(`missing bind call for ${targetSessionKey}`);
}