mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-18 20:44:47 +00:00
test: guard auto reply null helpers
This commit is contained in:
@@ -283,8 +283,6 @@ function createMockReplyOperation(): {
|
||||
}
|
||||
|
||||
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
||||
expect(typeof value).toBe("object");
|
||||
expect(value).not.toBeNull();
|
||||
if (typeof value !== "object" || value === null) {
|
||||
throw new Error(`${label} was not an object`);
|
||||
}
|
||||
|
||||
@@ -16,8 +16,9 @@ const baseParams = {
|
||||
};
|
||||
|
||||
function expectFields(value: unknown, expected: Record<string, unknown>): void {
|
||||
expect(value).toBeTypeOf("object");
|
||||
expect(value).not.toBeNull();
|
||||
if (!value || typeof value !== "object") {
|
||||
throw new Error("expected fields object");
|
||||
}
|
||||
const record = value as Record<string, unknown>;
|
||||
for (const [key, expectedValue] of Object.entries(expected)) {
|
||||
expect(record[key], key).toEqual(expectedValue);
|
||||
|
||||
@@ -41,8 +41,6 @@ function countMatching<T>(items: readonly T[], predicate: (item: T) => boolean):
|
||||
}
|
||||
|
||||
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
||||
expect(value).toBeTypeOf("object");
|
||||
expect(value).not.toBeNull();
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
throw new Error(`expected ${label} to be an object`);
|
||||
}
|
||||
|
||||
@@ -26,8 +26,6 @@ vi.mock("../../globals.js", () => ({
|
||||
}));
|
||||
|
||||
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
||||
expect(value).toBeTypeOf("object");
|
||||
expect(value).not.toBeNull();
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
throw new Error(`expected ${label} to be an object`);
|
||||
}
|
||||
|
||||
@@ -34,14 +34,16 @@ function mockCall(mock: unknown, index = 0): Array<unknown> {
|
||||
|
||||
function mockFirstObjectArg(mock: unknown): Record<string, unknown> {
|
||||
const [arg] = mockCall(mock);
|
||||
expect(arg).toBeTypeOf("object");
|
||||
expect(arg).not.toBeNull();
|
||||
if (!arg || typeof arg !== "object") {
|
||||
throw new Error("expected first mock argument object");
|
||||
}
|
||||
return arg as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function expectObjectFields(value: unknown, expected: Record<string, unknown>): void {
|
||||
expect(value).toBeTypeOf("object");
|
||||
expect(value).not.toBeNull();
|
||||
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);
|
||||
@@ -217,8 +219,9 @@ describe("handleBtwCommand", () => {
|
||||
|
||||
const sessionAgentArgs = mockFirstObjectArg(resolveSessionAgentIdMock);
|
||||
expect(sessionAgentArgs.sessionKey).toBe("agent:worker-1:whatsapp:direct:12345");
|
||||
expect(sessionAgentArgs.config).toBeTypeOf("object");
|
||||
expect(sessionAgentArgs.config).not.toBeNull();
|
||||
if (!sessionAgentArgs.config || typeof sessionAgentArgs.config !== "object") {
|
||||
throw new Error("expected session agent config");
|
||||
}
|
||||
expect(String(mockFirstObjectArg(runBtwSideQuestionMock).agentDir)).toContain(
|
||||
"/agents/worker-1/agent",
|
||||
);
|
||||
@@ -245,11 +248,13 @@ describe("handleBtwCommand", () => {
|
||||
|
||||
const canonicalAgentArgs = mockFirstObjectArg(resolveSessionAgentIdMock);
|
||||
expect(canonicalAgentArgs.sessionKey).toBe("agent:worker-1:whatsapp:direct:12345");
|
||||
expect(canonicalAgentArgs.config).toBeTypeOf("object");
|
||||
expect(canonicalAgentArgs.config).not.toBeNull();
|
||||
if (!canonicalAgentArgs.config || typeof canonicalAgentArgs.config !== "object") {
|
||||
throw new Error("expected canonical agent config");
|
||||
}
|
||||
const resolveDirCall = mockCall(resolveAgentDirMock);
|
||||
expect(resolveDirCall[0]).toBeTypeOf("object");
|
||||
expect(resolveDirCall[0]).not.toBeNull();
|
||||
if (!resolveDirCall[0] || typeof resolveDirCall[0] !== "object") {
|
||||
throw new Error("expected resolveAgentDir config");
|
||||
}
|
||||
expect(resolveDirCall[1]).toBe("worker-1");
|
||||
expect(mockFirstObjectArg(runBtwSideQuestionMock).agentDir).toBe("/tmp/worker-1-agent");
|
||||
expect(result).toEqual({
|
||||
|
||||
@@ -76,14 +76,16 @@ function mockCall(mock: unknown, index = 0): Array<unknown> {
|
||||
|
||||
function mockFirstObjectArg(mock: unknown): Record<string, unknown> {
|
||||
const [arg] = mockCall(mock);
|
||||
expect(arg).toBeTypeOf("object");
|
||||
expect(arg).not.toBeNull();
|
||||
if (!arg || typeof arg !== "object") {
|
||||
throw new Error("expected first mock argument object");
|
||||
}
|
||||
return arg as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function expectObjectFields(value: unknown, expected: Record<string, unknown>): void {
|
||||
expect(value).toBeTypeOf("object");
|
||||
expect(value).not.toBeNull();
|
||||
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);
|
||||
|
||||
@@ -107,8 +107,9 @@ type MockCalls = {
|
||||
};
|
||||
|
||||
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
||||
expect(typeof value, label).toBe("object");
|
||||
expect(value, label).not.toBeNull();
|
||||
if (!value || typeof value !== "object") {
|
||||
throw new Error(`expected ${label}`);
|
||||
}
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
|
||||
@@ -115,8 +115,9 @@ function mockCall(mock: unknown, index = 0): Array<unknown> {
|
||||
}
|
||||
|
||||
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
||||
expect(value, label).toBeTypeOf("object");
|
||||
expect(value, label).not.toBeNull();
|
||||
if (!value || typeof value !== "object") {
|
||||
throw new Error(`expected ${label}`);
|
||||
}
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
@@ -229,8 +230,9 @@ describe("handleCommands reset hooks", () => {
|
||||
mockCall(resetMocks.resetConfiguredBindingTargetInPlace)[0],
|
||||
"reset args",
|
||||
);
|
||||
expect(resetArgs.cfg).toBeTypeOf("object");
|
||||
expect(resetArgs.cfg).not.toBeNull();
|
||||
if (!resetArgs.cfg || typeof resetArgs.cfg !== "object") {
|
||||
throw new Error("expected reset config");
|
||||
}
|
||||
expectObjectFields(resetArgs, {
|
||||
sessionKey: "agent:claude:acp:binding:discord:default:9373ab192b2317f4",
|
||||
reason: "reset",
|
||||
|
||||
@@ -187,8 +187,9 @@ type MockTtsReply = Awaited<ReturnType<typeof ttsMocks.maybeApplyTtsToPayload>>;
|
||||
type MockCallSource = { mock: { calls: Array<Array<unknown>> } };
|
||||
|
||||
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
||||
expect(value, label).toBeTypeOf("object");
|
||||
expect(value, label).not.toBeNull();
|
||||
if (!value || typeof value !== "object") {
|
||||
throw new Error(`expected ${label}`);
|
||||
}
|
||||
return value as Record<string, unknown>;
|
||||
}
|
||||
|
||||
|
||||
@@ -119,8 +119,9 @@ function mockCallInput(mock: { mock: { calls: unknown[][] } }, index = 0): Recor
|
||||
throw new Error(`Expected mock call ${index}`);
|
||||
}
|
||||
const input = call[0];
|
||||
expect(typeof input).toBe("object");
|
||||
expect(input).not.toBeNull();
|
||||
if (!input || typeof input !== "object") {
|
||||
throw new Error(`expected mock input ${index}`);
|
||||
}
|
||||
return input as Record<string, unknown>;
|
||||
}
|
||||
|
||||
|
||||
@@ -144,8 +144,6 @@ async function runInlineStatusAction(storePath?: string) {
|
||||
}
|
||||
|
||||
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
||||
expect(value).toBeTypeOf("object");
|
||||
expect(value).not.toBeNull();
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
throw new Error(`expected ${label} to be an object`);
|
||||
}
|
||||
|
||||
@@ -112,8 +112,9 @@ function lastDelivery() {
|
||||
throw new Error("Expected outbound delivery call");
|
||||
}
|
||||
const delivery = call[0];
|
||||
expect(typeof delivery).toBe("object");
|
||||
expect(delivery).not.toBeNull();
|
||||
if (!delivery || typeof delivery !== "object") {
|
||||
throw new Error("expected outbound delivery");
|
||||
}
|
||||
return delivery as Record<string, unknown>;
|
||||
}
|
||||
|
||||
@@ -128,8 +129,9 @@ function lastDeliveryPayload(index = 0): Record<string, unknown> {
|
||||
const payloads = lastDelivery().payloads;
|
||||
expect(Array.isArray(payloads)).toBe(true);
|
||||
const payload = (payloads as unknown[])[index];
|
||||
expect(typeof payload).toBe("object");
|
||||
expect(payload).not.toBeNull();
|
||||
if (!payload || typeof payload !== "object") {
|
||||
throw new Error(`expected delivery payload ${index}`);
|
||||
}
|
||||
return payload as Record<string, unknown>;
|
||||
}
|
||||
|
||||
|
||||
@@ -136,8 +136,9 @@ async function initStoredSessionState(params: {
|
||||
}
|
||||
|
||||
function expectFields(value: unknown, expected: Record<string, unknown>): void {
|
||||
expect(value).toBeTypeOf("object");
|
||||
expect(value).not.toBeNull();
|
||||
if (!value || typeof value !== "object") {
|
||||
throw new Error("expected fields object");
|
||||
}
|
||||
const record = value as Record<string, unknown>;
|
||||
for (const [key, expectedValue] of Object.entries(expected)) {
|
||||
expect(record[key], key).toEqual(expectedValue);
|
||||
|
||||
@@ -40,8 +40,9 @@ async function expectPathMissing(targetPath: string): Promise<void> {
|
||||
if (statError === undefined) {
|
||||
throw new Error(`Expected ${targetPath} to be missing`);
|
||||
}
|
||||
expect(typeof statError).toBe("object");
|
||||
expect(statError).not.toBeNull();
|
||||
if (!statError || typeof statError !== "object") {
|
||||
throw new Error("expected stat error object");
|
||||
}
|
||||
expect((statError as NodeJS.ErrnoException).code).toBe("ENOENT");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user