mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-12 18:12:56 +00:00
test(gateway): share record assertion helpers
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
nodeHandlers,
|
||||
waitForNodeReconnect,
|
||||
} from "./nodes.js";
|
||||
import { expectRecordFields, requireRecord } from "../test-helpers.assertions.js";
|
||||
|
||||
type MockNodeCommandPolicyParams = {
|
||||
command: string;
|
||||
@@ -94,27 +95,6 @@ type TestNodeSession = {
|
||||
platform?: string;
|
||||
};
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
||||
expect(isRecord(value), `${label} must be an object`).toBe(true);
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function expectRecordFields(
|
||||
value: unknown,
|
||||
label: string,
|
||||
expected: Record<string, unknown>,
|
||||
): Record<string, unknown> {
|
||||
const record = requireRecord(value, label);
|
||||
for (const [key, expectedValue] of Object.entries(expected)) {
|
||||
expect(record[key], `${label}.${key}`).toEqual(expectedValue);
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
function requireString(value: unknown, label: string): string {
|
||||
expect(typeof value, `${label} must be a string`).toBe("string");
|
||||
return value as string;
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
sendTalkTranscriptionRelayAudio,
|
||||
stopTalkTranscriptionRelaySession,
|
||||
} from "./talk-transcription-relay.js";
|
||||
import { expectRecordFields, isRecord, requireRecord } from "./test-helpers.assertions.js";
|
||||
|
||||
type BroadcastEvent = { event: string; payload: unknown; connIds: string[] };
|
||||
|
||||
@@ -63,27 +64,6 @@ async function createStartedRelaySession(
|
||||
return { provider, events, session };
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
||||
expect(isRecord(value), `${label} must be an object`).toBe(true);
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function expectRecordFields(
|
||||
value: unknown,
|
||||
label: string,
|
||||
expected: Record<string, unknown>,
|
||||
): Record<string, unknown> {
|
||||
const record = requireRecord(value, label);
|
||||
for (const [key, expectedValue] of Object.entries(expected)) {
|
||||
expect(record[key], `${label}.${key}`).toEqual(expectedValue);
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
function findPayloadByType(events: BroadcastEvent[], type: string): Record<string, unknown> {
|
||||
const event = events.find((candidate) => {
|
||||
const payload = candidate.payload;
|
||||
|
||||
22
src/gateway/test-helpers.assertions.ts
Normal file
22
src/gateway/test-helpers.assertions.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { expect } from "vitest";
|
||||
|
||||
export function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null && !Array.isArray(value);
|
||||
}
|
||||
|
||||
export function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
||||
expect(isRecord(value), `${label} must be an object`).toBe(true);
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
export function expectRecordFields(
|
||||
value: unknown,
|
||||
label: string,
|
||||
expected: Record<string, unknown>,
|
||||
): Record<string, unknown> {
|
||||
const record = requireRecord(value, label);
|
||||
for (const [key, expectedValue] of Object.entries(expected)) {
|
||||
expect(record[key], `${label}.${key}`).toEqual(expectedValue);
|
||||
}
|
||||
return record;
|
||||
}
|
||||
Reference in New Issue
Block a user