test: guard remaining truthiness assertions

This commit is contained in:
Peter Steinberger
2026-05-11 20:45:44 +01:00
parent 23f8c38ba5
commit 63af3bcfdb
12 changed files with 80 additions and 39 deletions

View File

@@ -459,8 +459,8 @@ describe("chrome.ts internal", () => {
expect(spawnOptions.env?.HTTPS_PROXY).toBeUndefined();
expect(spawnOptions.env?.NO_PROXY).toBeUndefined();
if (process.platform === "linux") {
expect(spawnOptions.env?.XDG_CONFIG_HOME).toBeTruthy();
expect(spawnOptions.env?.XDG_CACHE_HOME).toBeTruthy();
expect(spawnOptions.env?.XDG_CONFIG_HOME).toEqual(expect.any(String));
expect(spawnOptions.env?.XDG_CACHE_HOME).toEqual(expect.any(String));
}
// Cleanup.
running.proc.kill?.("SIGTERM");

View File

@@ -110,7 +110,6 @@ describe("qa channel transport", () => {
const message = await transport.capabilities.readNormalizedMessage({
messageId: inbound.id,
});
expect(message).toBeTruthy();
if (!message) {
throw new Error("expected normalized QA message");
}

View File

@@ -1626,12 +1626,14 @@ Second paragraph should still reach the agent after Slack's preview cutoff.`;
opts: { source: "message" },
});
expect(prepared).toBeTruthy();
expect(prepared!.ctxPayload.WasMentioned).toBe(true);
expect(prepared!.ctxPayload.ExplicitlyMentionedBot).toBe(false);
expect(prepared!.ctxPayload.MentionedUserIds).toEqual(["UOTHER"]);
expect(prepared!.ctxPayload.ImplicitMentionKinds).toEqual(["reply_to_bot"]);
expect(prepared!.ctxPayload.MentionSource).toBe("implicit_thread");
if (!prepared) {
throw new Error("expected prepared Slack message");
}
expect(prepared.ctxPayload.WasMentioned).toBe(true);
expect(prepared.ctxPayload.ExplicitlyMentionedBot).toBe(false);
expect(prepared.ctxPayload.MentionedUserIds).toEqual(["UOTHER"]);
expect(prepared.ctxPayload.ImplicitMentionKinds).toEqual(["reply_to_bot"]);
expect(prepared.ctxPayload.MentionSource).toBe("implicit_thread");
});
it("marks authorized implicit thread control-command wakes as command bypass source", async () => {
@@ -1670,10 +1672,12 @@ Second paragraph should still reach the agent after Slack's preview cutoff.`;
opts: { source: "message" },
});
expect(prepared).toBeTruthy();
expect(prepared!.ctxPayload.WasMentioned).toBe(true);
expect(prepared!.ctxPayload.ImplicitMentionKinds).toEqual(["reply_to_bot"]);
expect(prepared!.ctxPayload.MentionSource).toBe("command_bypass");
if (!prepared) {
throw new Error("expected prepared Slack message");
}
expect(prepared.ctxPayload.WasMentioned).toBe(true);
expect(prepared.ctxPayload.ImplicitMentionKinds).toEqual(["reply_to_bot"]);
expect(prepared.ctxPayload.MentionSource).toBe("command_bypass");
});
it("keeps an implicit-conversation root and its Slack thread follow-up on one parent session in `requireMention: false` channels (#78505)", async () => {

View File

@@ -1797,7 +1797,10 @@ describe("dispatchTelegramMessage draft streaming", () => {
const replies = deliverReplies.mock.calls[0]?.[0]?.replies as
| Array<{ text?: string }>
| undefined;
expect(replies?.[0]?.text?.trim()).toBeTruthy();
const replyText = replies?.[0]?.text?.trim();
if (!replyText) {
throw new Error("expected non-empty Telegram reply text");
}
expect(replies?.[0]?.text).not.toBe("NO_REPLY");
});

View File

@@ -570,7 +570,6 @@ describe("startTelegramWebhook", () => {
const setWebhookCall = requireMockCall(setWebhookSpy, 0, "setWebhook");
expect(typeof setWebhookCall[0]).toBe("string");
const options = requireRecord(setWebhookCall[1], "setWebhook options");
expect(options.certificate).toBeTruthy();
const certificate = options.certificate as
| { path?: string; fileData?: string; filename?: string }
| undefined;

View File

@@ -149,7 +149,9 @@ function hasApprovalRelay(agent: AcpGatewayAgent, approvalId: string): boolean {
}
function requireRecord(value: unknown): Record<string, unknown> {
expect(value).toBeTruthy();
if (!value) {
throw new Error("expected record");
}
expect(typeof value).toBe("object");
expect(Array.isArray(value)).toBe(false);
return value as Record<string, unknown>;

View File

@@ -175,7 +175,9 @@ function readEncodedRequestFromCommand(command: string): Record<string, unknown>
}
function requireRecord(value: unknown): Record<string, unknown> {
expect(value).toBeTruthy();
if (!value) {
throw new Error("expected record");
}
expect(typeof value).toBe("object");
expect(Array.isArray(value)).toBe(false);
return value as Record<string, unknown>;
@@ -191,10 +193,12 @@ function execCallRecord(
index = 0,
): { defaults: Record<string, unknown>; params: Record<string, unknown> } {
const call = execCalls[index];
expect(call).toBeTruthy();
if (!call) {
throw new Error(`expected exec call at index ${index}`);
}
return {
defaults: requireRecord(call?.defaults),
params: requireRecord(call?.params),
defaults: requireRecord(call.defaults),
params: requireRecord(call.params),
};
}

View File

@@ -50,7 +50,9 @@ vi.mock("../runtime.js", () => ({
}));
function requireRecord(value: unknown): Record<string, unknown> {
expect(value).toBeTruthy();
if (!value) {
throw new Error("expected record");
}
expect(typeof value).toBe("object");
expect(Array.isArray(value)).toBe(false);
return value as Record<string, unknown>;

View File

@@ -248,8 +248,10 @@ type ValidationMessage = {
function findValidationMessage(messages: ValidationMessage[], path: string): ValidationMessage {
const message = messages.find((entry) => entry.path === path);
expect(message).toBeTruthy();
return message ?? {};
if (!message) {
throw new Error(`expected validation message for ${path}`);
}
return message;
}
function expectAllowedValuesInclude(message: ValidationMessage, values: string[]): void {

View File

@@ -1446,7 +1446,9 @@ describe("OpenAI-compatible HTTP API (e2e)", () => {
| string
| undefined) === "get_weather",
);
expect(withIdentity).toBeTruthy();
if (!withIdentity) {
throw new Error("expected tool call delta with identity");
}
const argsJoined = toolCallDeltaRecords
.filter((record) => record.index === 0)
.map(
@@ -1460,7 +1462,9 @@ describe("OpenAI-compatible HTTP API (e2e)", () => {
const finishChunk = toolCallChunks
.flatMap((chunk) => (chunk.choices as Array<Record<string, unknown>> | undefined) ?? [])
.find((choice) => choice.finish_reason === "tool_calls");
expect(finishChunk).toBeTruthy();
if (!finishChunk) {
throw new Error("expected tool_calls finish chunk");
}
}
{
@@ -1499,9 +1503,11 @@ describe("OpenAI-compatible HTTP API (e2e)", () => {
.filter((d) => d !== "[DONE]")
.map((d) => JSON.parse(d) as Record<string, unknown>);
const usageChunk = jsonChunks.find((chunk) => "usage" in chunk);
expect(usageChunk).toBeTruthy();
expect(usageChunk?.choices).toEqual([]);
expect(usageChunk?.usage).toEqual({
if (!usageChunk) {
throw new Error("expected streamed usage chunk");
}
expect(usageChunk.choices).toEqual([]);
expect(usageChunk.usage).toEqual({
prompt_tokens: 12,
completion_tokens: 3,
total_tokens: 15,
@@ -1566,7 +1572,9 @@ describe("OpenAI-compatible HTTP API (e2e)", () => {
const finishChunk = lateToolCallChunks
.flatMap((chunk) => (chunk.choices as Array<Record<string, unknown>> | undefined) ?? [])
.find((choice) => choice.finish_reason === "tool_calls");
expect(finishChunk).toBeTruthy();
if (!finishChunk) {
throw new Error("expected late tool_calls finish chunk");
}
const anyToolCalls = lateToolCallChunks.some((chunk) => {
const choice = ((chunk.choices as Array<Record<string, unknown>> | undefined) ?? [])[0];
const delta = (choice?.delta as Record<string, unknown> | undefined) ?? {};
@@ -1608,7 +1616,9 @@ describe("OpenAI-compatible HTTP API (e2e)", () => {
((chunk.error as { type?: unknown }).type ?? "") === "invalid_request_error" &&
((chunk.error as { message?: unknown }).message ?? "") === "invalid tool configuration",
);
expect(protocolError).toBeTruthy();
if (!protocolError) {
throw new Error("expected invalid tool configuration protocol error");
}
const stopChoice = toolConflictChunks
.flatMap((c) => (c.choices as Array<Record<string, unknown>> | undefined) ?? [])
.find((choice) => choice.finish_reason === "stop");

View File

@@ -493,10 +493,17 @@ describe("node.invoke approval bypass", () => {
idempotencyKey: crypto.randomUUID(),
});
expect(invoke.ok).toBe(true);
await vi.waitFor(() => expect(lastInvokeParams).toBeTruthy(), {
timeout: 5_000,
interval: 50,
});
await vi.waitFor(
() => {
if (!lastInvokeParams) {
throw new Error("expected forwarded invoke params");
}
},
{
timeout: 5_000,
interval: 50,
},
);
const forwardedParams = requireRecord(lastInvokeParams, "forwarded invoke params");
expect(forwardedParams["approved"]).toBe(true);
expect(forwardedParams["approvalDecision"]).toBe("allow-once");
@@ -576,10 +583,17 @@ describe("node.invoke approval bypass", () => {
idempotencyKey: crypto.randomUUID(),
});
expect(invoke.ok).toBe(true);
await vi.waitFor(() => expect(lastInvokeParams).toBeTruthy(), {
timeout: 5_000,
interval: 50,
});
await vi.waitFor(
() => {
if (!lastInvokeParams) {
throw new Error("expected forwarded invoke params");
}
},
{
timeout: 5_000,
interval: 50,
},
);
const forwardedParams = requireRecord(lastInvokeParams, "forwarded invoke params");
expect(forwardedParams["approved"]).toBe(true);
expect(forwardedParams["approvalDecision"]).toBe("allow-once");

View File

@@ -196,7 +196,9 @@ test("sessions.reset closes ACP runtime handles for ACP sessions", async () => {
}
| undefined;
expect(closeSessionParams?.allowBackendUnavailable).toBe(true);
expect(closeSessionParams?.cfg).toBeTruthy();
if (!closeSessionParams?.cfg) {
throw new Error("expected closeSession config");
}
expect(closeSessionParams?.discardPersistentState).toBe(true);
expect(closeSessionParams?.requireAcpSession).toBe(false);
expect(closeSessionParams?.reason).toBe("session-reset");