test: guard remaining mock call helpers

This commit is contained in:
Peter Steinberger
2026-05-11 19:07:13 +01:00
parent 4f02ce5242
commit d8f39ce2ee
9 changed files with 32 additions and 17 deletions

View File

@@ -254,7 +254,9 @@ function expectRecordFields(
record: unknown,
expected: Record<string, unknown>,
): Record<string, unknown> {
expect(record).toBeDefined();
if (!record || typeof record !== "object") {
throw new Error("Expected record");
}
const actual = record as Record<string, unknown>;
for (const [key, value] of Object.entries(expected)) {
expect(actual[key]).toEqual(value);
@@ -267,8 +269,10 @@ function streamContext(callIndex = 0): {
systemPrompt?: unknown;
} {
const call = streamSimpleMock.mock.calls[callIndex];
expect(call).toBeDefined();
return (call?.[1] ?? {}) as {
if (!call) {
throw new Error(`Expected streamSimple call at index ${callIndex}`);
}
return (call[1] ?? {}) as {
messages?: Array<Record<string, unknown>>;
systemPrompt?: unknown;
};

View File

@@ -85,7 +85,9 @@ function captureDiagnosticEvents(): {
function mockCallArg(mock: { mock: { calls: unknown[][] } }, index = 0): unknown {
const call = mock.mock.calls[index];
expect(call).toBeDefined();
if (!call) {
throw new Error(`Expected mock call at index ${index}`);
}
return call[0];
}

View File

@@ -55,8 +55,10 @@ function gatewayCalls(): GatewayCall[] {
function gatewayCall(method: string): GatewayCall {
const call = gatewayCalls().find(([candidate]) => candidate === method);
expect(call).toBeDefined();
return call as GatewayCall;
if (!call) {
throw new Error(`Expected gateway call for ${method}`);
}
return call;
}
function expectGatewayCallFields(
@@ -84,7 +86,9 @@ function expectRecordFields(
record: unknown,
expected: Record<string, unknown>,
): Record<string, unknown> {
expect(record).toBeDefined();
if (!record || typeof record !== "object") {
throw new Error("Expected record");
}
const actual = record as Record<string, unknown>;
for (const [key, value] of Object.entries(expected)) {
expect(actual[key]).toEqual(value);
@@ -104,8 +108,10 @@ function expectConfigMutationCall(params: {
}) {
expect(params.callGatewayTool.mock.calls.some(([method]) => method === "config.get")).toBe(true);
const call = params.callGatewayTool.mock.calls.find(([method]) => method === params.action);
expect(call).toBeDefined();
expectRecordFields(call?.[2], {
if (!call) {
throw new Error(`Expected gateway call for ${params.action}`);
}
expectRecordFields(call[2], {
raw: params.raw.trim(),
baseHash: "hash-1",
sessionKey: params.sessionKey,

View File

@@ -96,7 +96,9 @@ async function waitForAfterToolCall(hooks: {
expect(hooks.afterToolCall.mock.calls.length).toBe(1);
});
const call = hooks.afterToolCall.mock.calls[0];
expect(call).toBeDefined();
if (!call) {
throw new Error("Expected afterToolCall hook call");
}
return call as [Record<string, unknown>, Record<string, unknown>];
}

View File

@@ -661,7 +661,6 @@ describe("before_tool_call requireApproval handling", () => {
index: number,
): [event: Record<string, unknown>, context: Record<string, unknown>] {
const call = hookRunner.runBeforeToolCall.mock.calls[index] as unknown[] | undefined;
expect(call).toBeDefined();
if (!call) {
throw new Error(`missing before_tool_call hook call ${index + 1}`);
}

View File

@@ -86,7 +86,6 @@ function expectInputProvenance(
function getAgentCall(index = 0): AgentCallRequest {
const call = agentSpy.mock.calls[index]?.[0];
expect(call).toBeDefined();
if (!call) {
throw new Error(`Expected agent call at index ${index}`);
}

View File

@@ -186,7 +186,6 @@ describe("agent event handler", () => {
label: string,
) {
const call = mock.mock.calls[index];
expect(call).toBeDefined();
if (!call) {
throw new Error(`missing ${label} call ${index + 1}`);
}

View File

@@ -333,8 +333,10 @@ function expectMockCallFields(
function expectNthSystemEventFields(callIndex: number, expected: Record<string, unknown>): void {
const call = mocks.enqueueSystemEvent.mock.calls[callIndex];
expect(call).toBeDefined();
expectRecordFields(call?.[1], expected);
if (!call) {
throw new Error(`Expected enqueueSystemEvent call at index ${callIndex}`);
}
expectRecordFields(call[1], expected);
}
function expectContinuationDispatchFields(

View File

@@ -27,8 +27,10 @@ type HookEventRecord = Record<string, unknown> & {
function firstHookCall(mock: { mock: { calls: unknown[][] } }): [HookEventRecord, HookEventRecord] {
const call = mock.mock.calls[0];
expect(call).toBeDefined();
return [call?.[0] as HookEventRecord, call?.[1] as HookEventRecord];
if (!call) {
throw new Error("Expected hook call");
}
return [call[0] as HookEventRecord, call[1] as HookEventRecord];
}
function expectTranscriptResetEvent(params: {