mirror of
https://github.com/openclaw/openclaw.git
synced 2026-08-05 08:01:37 +00:00
31 lines
1.1 KiB
TypeScript
31 lines
1.1 KiB
TypeScript
// Assertion helpers for inspecting Vitest mock call payloads.
|
|
import { expect } from "vitest";
|
|
|
|
/** Returns a mock call with a useful failure when the call is missing. */
|
|
export function mockCall(mock: unknown, index = 0): Array<unknown> {
|
|
const calls = (mock as { mock?: { calls?: Array<Array<unknown>> } }).mock?.calls ?? [];
|
|
const call = calls.at(index);
|
|
if (!call) {
|
|
throw new Error(`Expected mock call ${index + 1}`);
|
|
}
|
|
return call;
|
|
}
|
|
|
|
export function mockFirstObjectArg(mock: unknown): Record<string, unknown> {
|
|
const [arg] = mockCall(mock);
|
|
if (!arg || typeof arg !== "object") {
|
|
throw new Error("expected first mock argument object");
|
|
}
|
|
return arg as Record<string, unknown>;
|
|
}
|
|
|
|
export function expectObjectFields(value: unknown, expected: Record<string, unknown>): void {
|
|
if (!value || typeof value !== "object") {
|
|
throw new Error("expected object fields");
|
|
}
|
|
const record = value as Record<string, unknown>;
|
|
for (const [key, expectedValue] of Object.entries(expected)) {
|
|
expect(record[key], key).toEqual(expectedValue);
|
|
}
|
|
}
|