mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-18 17:44:45 +00:00
test: guard agents helper assertions
This commit is contained in:
@@ -141,11 +141,13 @@ test("exec emits bounded process diagnostics without command text", async () =>
|
||||
const event = events.find(
|
||||
(item): item is DiagnosticExecProcessCompletedEvent => item.type === "exec.process.completed",
|
||||
);
|
||||
expect(event).toBeDefined();
|
||||
expect(event?.type).toBe("exec.process.completed");
|
||||
expect(event?.target).toBe("host");
|
||||
expect(event?.mode).toBe("child");
|
||||
expect(event?.outcome).toBe("completed");
|
||||
if (!event) {
|
||||
throw new Error("Expected exec process completed event");
|
||||
}
|
||||
expect(event.type).toBe("exec.process.completed");
|
||||
expect(event.target).toBe("host");
|
||||
expect(event.mode).toBe("child");
|
||||
expect(event.outcome).toBe("completed");
|
||||
expect(typeof event?.durationMs).toBe("number");
|
||||
expect(event?.commandLength).toBe(command.length);
|
||||
expect(event?.exitCode).toBe(0);
|
||||
|
||||
@@ -280,8 +280,10 @@ function streamContext(callIndex = 0): {
|
||||
|
||||
function contextMessages(context: unknown): Array<Record<string, unknown>> {
|
||||
const messages = (context as { messages?: Array<Record<string, unknown>> }).messages;
|
||||
expect(messages).toBeDefined();
|
||||
return messages ?? [];
|
||||
if (!messages) {
|
||||
throw new Error("Expected BTW context messages");
|
||||
}
|
||||
return messages;
|
||||
}
|
||||
|
||||
function expectTextBlockContains(block: unknown, text: string): void {
|
||||
@@ -752,7 +754,9 @@ describe("runBtwSideQuestion", () => {
|
||||
`<btw_side_question>\n${MATH_QUESTION}\n</btw_side_question>`,
|
||||
),
|
||||
);
|
||||
expect(sideQuestionMessage).toBeDefined();
|
||||
if (!sideQuestionMessage) {
|
||||
throw new Error("Expected BTW side question message");
|
||||
}
|
||||
});
|
||||
|
||||
it("uses the in-flight prompt as background only when there is no prior transcript context", async () => {
|
||||
@@ -792,7 +796,9 @@ describe("runBtwSideQuestion", () => {
|
||||
"Ignore any unfinished task in the conversation while answering it.",
|
||||
),
|
||||
);
|
||||
expect(sideQuestionMessage).toBeDefined();
|
||||
if (!sideQuestionMessage) {
|
||||
throw new Error("Expected isolated side question message");
|
||||
}
|
||||
});
|
||||
|
||||
it("branches away from an unresolved trailing user turn before building BTW context", async () => {
|
||||
|
||||
@@ -157,7 +157,9 @@ function expectRecordWithFields(
|
||||
const matching = records.find((record) =>
|
||||
Object.entries(expected).every(([key, value]) => record[key] === value),
|
||||
);
|
||||
expect(matching, `expected matching record for ${JSON.stringify(expected)}`).toBeDefined();
|
||||
if (!matching) {
|
||||
throw new Error(`Expected matching record for ${JSON.stringify(expected)}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function expectProbeFailureFallsBack({
|
||||
|
||||
@@ -66,7 +66,9 @@ function expectGatewayCallFields(
|
||||
expectedParams: Record<string, unknown>,
|
||||
): Record<string, unknown> {
|
||||
const [, , params] = gatewayCall(method);
|
||||
expect(params).toBeDefined();
|
||||
if (params === undefined) {
|
||||
throw new Error(`Expected gateway call params for ${method}`);
|
||||
}
|
||||
const record = params as Record<string, unknown>;
|
||||
for (const [key, value] of Object.entries(expectedParams)) {
|
||||
expect(record[key]).toEqual(value);
|
||||
|
||||
@@ -381,13 +381,17 @@ describe("compactEmbeddedPiSessionDirect hooks", () => {
|
||||
([provider, modelId]) => provider === "openai" && modelId === "gpt-primary",
|
||||
);
|
||||
expect(primaryCall[2]).toBeTypeOf("string");
|
||||
expect(primaryCall[3]).toBeDefined();
|
||||
if (primaryCall[3] === undefined) {
|
||||
throw new Error("Expected primary resolve-model options");
|
||||
}
|
||||
const fallbackCall = findMockCall(
|
||||
resolveModelMock,
|
||||
([provider, modelId]) => provider === "anthropic" && modelId === "claude-fallback",
|
||||
);
|
||||
expect(fallbackCall[2]).toBeTypeOf("string");
|
||||
expect(fallbackCall[3]).toBeDefined();
|
||||
if (fallbackCall[3] === undefined) {
|
||||
throw new Error("Expected fallback resolve-model options");
|
||||
}
|
||||
});
|
||||
|
||||
it("keeps compaction fallback selection ephemeral", async () => {
|
||||
@@ -442,13 +446,17 @@ describe("compactEmbeddedPiSessionDirect hooks", () => {
|
||||
([provider, modelId]) => provider === "openai" && modelId === "gpt-primary",
|
||||
);
|
||||
expect(primaryCall[2]).toBeTypeOf("string");
|
||||
expect(primaryCall[3]).toBeDefined();
|
||||
if (primaryCall[3] === undefined) {
|
||||
throw new Error("Expected primary resolve-model options");
|
||||
}
|
||||
const fallbackCall = findMockCall(
|
||||
resolveModelMock,
|
||||
([provider, modelId]) => provider === "anthropic" && modelId === "claude-fallback",
|
||||
);
|
||||
expect(fallbackCall[2]).toBeTypeOf("string");
|
||||
expect(fallbackCall[3]).toBeDefined();
|
||||
if (fallbackCall[3] === undefined) {
|
||||
throw new Error("Expected fallback resolve-model options");
|
||||
}
|
||||
expect(config).toEqual(configBefore);
|
||||
});
|
||||
|
||||
@@ -490,7 +498,9 @@ describe("compactEmbeddedPiSessionDirect hooks", () => {
|
||||
expect(mockCallArg(resolveModelMock)).toBe("azure");
|
||||
expect(mockCallArg(resolveModelMock, 0, 1)).toBe("compact-primary");
|
||||
expect(mockCallArg(resolveModelMock, 0, 2)).toBeTypeOf("string");
|
||||
expect(mockCallArg(resolveModelMock, 0, 3)).toBeDefined();
|
||||
if (mockCallArg(resolveModelMock, 0, 3) === undefined) {
|
||||
throw new Error("Expected resolve-model options");
|
||||
}
|
||||
});
|
||||
|
||||
it("preserves compaction failure status and code metadata", async () => {
|
||||
@@ -1266,7 +1276,9 @@ describe("compactEmbeddedPiSession hooks (ownsCompaction engine)", () => {
|
||||
expect(mockCallArg(resolveModelMock)).toBe("anthropic");
|
||||
expect(mockCallArg(resolveModelMock, 0, 1)).toBe("claude-opus-4-6");
|
||||
expect(mockCallArg(resolveModelMock, 0, 2)).toBeTypeOf("string");
|
||||
expect(mockCallArg(resolveModelMock, 0, 3)).toBeDefined();
|
||||
if (mockCallArg(resolveModelMock, 0, 3) === undefined) {
|
||||
throw new Error("Expected resolve-model options");
|
||||
}
|
||||
const compactArg = mockCallArg(contextEngineCompactMock) as {
|
||||
runtimeContext?: Record<string, unknown>;
|
||||
};
|
||||
@@ -1299,7 +1311,9 @@ describe("compactEmbeddedPiSession hooks (ownsCompaction engine)", () => {
|
||||
|
||||
expect(result.ok).toBe(true);
|
||||
const harnessArg = mockCallArg(maybeCompactAgentHarnessSessionMock) as Record<string, unknown>;
|
||||
expect(harnessArg.contextEngine).toBeDefined();
|
||||
if (harnessArg.contextEngine === undefined) {
|
||||
throw new Error("Expected compact harness context engine");
|
||||
}
|
||||
expect(harnessArg.contextTokenBudget).toBeTypeOf("number");
|
||||
expectRecordFields(harnessArg.contextEngineRuntimeContext, {
|
||||
sessionKey: TEST_SESSION_KEY,
|
||||
|
||||
@@ -78,7 +78,6 @@ describe("resolveModelAsync skipPiDiscovery runtime hooks", () => {
|
||||
});
|
||||
|
||||
expect(result.error).toBeUndefined();
|
||||
expect(result.model).toBeDefined();
|
||||
if (!result.model) {
|
||||
throw new Error("Expected resolved model");
|
||||
}
|
||||
|
||||
@@ -66,7 +66,6 @@ describe("createHostWorkspaceEditTool host access mapping", () => {
|
||||
// By resolving silently the subsequent readFile call surfaces the real
|
||||
// "Path escapes workspace root" / "outside-workspace" error instead.
|
||||
const operations = mocks.operations;
|
||||
expect(operations).toBeDefined();
|
||||
if (!operations) {
|
||||
throw new Error("Expected workspace edit operations to be registered.");
|
||||
}
|
||||
|
||||
@@ -89,9 +89,11 @@ describe("provider local service", () => {
|
||||
|
||||
const lease = await ensureModelProviderLocalService(model);
|
||||
|
||||
expect(lease).toBeDefined();
|
||||
if (!lease) {
|
||||
throw new Error("Expected provider local service lease");
|
||||
}
|
||||
expect((await fetch(healthUrl)).ok).toBe(true);
|
||||
lease?.release();
|
||||
lease.release();
|
||||
await waitForProbeFailure(healthUrl);
|
||||
});
|
||||
|
||||
@@ -121,7 +123,9 @@ describe("provider local service", () => {
|
||||
"X-Tenant": "acme",
|
||||
});
|
||||
|
||||
expect(lease).toBeDefined();
|
||||
if (!lease) {
|
||||
throw new Error("Expected provider local service lease");
|
||||
}
|
||||
expect((await fetch(healthUrl)).status).toBe(401);
|
||||
expect(
|
||||
(
|
||||
@@ -283,9 +287,11 @@ describe("provider local service", () => {
|
||||
expect((await fetch(healthUrl)).status).toBe(503);
|
||||
|
||||
const secondLease = await ensureModelProviderLocalService(model);
|
||||
expect(secondLease).toBeDefined();
|
||||
if (!secondLease) {
|
||||
throw new Error("Expected restarted provider local service lease");
|
||||
}
|
||||
expect((await fetch(healthUrl)).ok).toBe(true);
|
||||
secondLease?.release();
|
||||
secondLease.release();
|
||||
|
||||
const starts = (await fs.readFile(startsPath, "utf8")).trim().split("\n");
|
||||
expect(starts).toHaveLength(2);
|
||||
|
||||
@@ -121,7 +121,6 @@ describe("subagent spawn depth + child limits", () => {
|
||||
expect(accepted.childSessionKey).toMatch(/^agent:main:subagent:/);
|
||||
|
||||
const childSession = persistedStore?.[accepted.childSessionKey];
|
||||
expect(childSession).toBeDefined();
|
||||
if (!childSession) {
|
||||
throw new Error("Expected persisted child session");
|
||||
}
|
||||
|
||||
@@ -1005,11 +1005,13 @@ describe("message tool schema scoping", () => {
|
||||
});
|
||||
|
||||
const context = seenContexts.find((item) => item.phase === "describeMessageTool");
|
||||
expect(context).toBeDefined();
|
||||
expect(context?.currentChannelProvider).toBe("discord");
|
||||
expect(context?.currentChannelId).toBe("channel:123");
|
||||
expect(context?.currentThreadTs).toBe("thread-456");
|
||||
expect(context?.currentMessageId).toBe("msg-789");
|
||||
if (!context) {
|
||||
throw new Error("Expected describeMessageTool discovery context");
|
||||
}
|
||||
expect(context.currentChannelProvider).toBe("discord");
|
||||
expect(context.currentChannelId).toBe("channel:123");
|
||||
expect(context.currentThreadTs).toBe("thread-456");
|
||||
expect(context.currentMessageId).toBe("msg-789");
|
||||
expect(context?.accountId).toBe("ops");
|
||||
expect(context?.sessionKey).toBe("agent:alpha:main");
|
||||
expect(context?.sessionId).toBe("session-123");
|
||||
|
||||
@@ -36,7 +36,9 @@ function expectReplyInstructionContains(text: string) {
|
||||
const event = getDeliveredInternalEvents().find(
|
||||
(item) => typeof item.replyInstruction === "string" && item.replyInstruction.includes(text),
|
||||
);
|
||||
expect(event).toBeDefined();
|
||||
if (!event) {
|
||||
throw new Error(`Expected reply instruction containing ${text}`);
|
||||
}
|
||||
}
|
||||
|
||||
describe("music generate background helpers", () => {
|
||||
|
||||
@@ -148,14 +148,18 @@ function mockSavedVideoResult(fileName = "out.mp4") {
|
||||
}
|
||||
|
||||
function resultDetails(result: { details?: unknown }): Record<string, unknown> {
|
||||
expect(result.details).toBeDefined();
|
||||
if (result.details === undefined) {
|
||||
throw new Error("Expected video generation result details");
|
||||
}
|
||||
expect(typeof result.details).toBe("object");
|
||||
return result.details as Record<string, unknown>;
|
||||
}
|
||||
|
||||
function firstMockCallArg(mock: { mock: { calls: unknown[][] } }): unknown {
|
||||
const firstCall = mock.mock.calls[0];
|
||||
expect(firstCall).toBeDefined();
|
||||
if (!firstCall) {
|
||||
throw new Error("Expected first mock call");
|
||||
}
|
||||
return firstCall[0];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user