test: improved test null safety and type assertions
- Added explicit existence checks for objects prior to property access across various test suites. - Replaced optional chaining with non-null assertions to satisfy TypeScript strictness requirements in test assertions.
This commit is contained in:
@@ -382,12 +382,17 @@ describe("Anthropic request fingerprint alignment", () => {
|
||||
// cache window (the Continue. pad is appended after it) but must not get
|
||||
// a breakpoint — Anthropic rejects cache_control on thinking blocks.
|
||||
const assistant = payload.messages?.find(message => message.role === "assistant");
|
||||
expect(Array.isArray(assistant?.content)).toBe(true);
|
||||
for (const block of assistant?.content as Array<{ type: string; cache_control?: unknown }>) {
|
||||
expect(assistant).toBeDefined();
|
||||
const assistantContent = assistant?.content;
|
||||
expect(Array.isArray(assistantContent)).toBe(true);
|
||||
for (const block of (assistantContent ?? []) as Array<{ type: string; cache_control?: unknown }>) {
|
||||
expect(block.cache_control).toBeUndefined();
|
||||
}
|
||||
const last = payload.messages?.at(-1);
|
||||
expect((last?.content as Array<{ cache_control?: unknown }>)[0]?.cache_control).toBeDefined();
|
||||
expect(last).toBeDefined();
|
||||
const lastContent = last?.content;
|
||||
expect(Array.isArray(lastContent)).toBe(true);
|
||||
expect((lastContent as Array<{ cache_control?: unknown }>)[0]?.cache_control).toBeDefined();
|
||||
});
|
||||
|
||||
it("adds effort and mid-conversation betas to API-key requests that use those features", async () => {
|
||||
|
||||
@@ -665,12 +665,14 @@ describe("openai-codex Responses Lite and client metadata wire format", () => {
|
||||
}).result();
|
||||
|
||||
expect(result.stopReason).toBe("stop");
|
||||
expect(captured?.headers.get("x-openai-internal-codex-responses-lite")).toBe("true");
|
||||
expect(captured?.headers.get("version")).toBe("0.144.1");
|
||||
expect(captured?.body.reasoning).toEqual({ context: "all_turns" });
|
||||
expect(captured?.body.instructions).toBeUndefined();
|
||||
expect(captured?.body.tools).toBeUndefined();
|
||||
expect((captured?.body.input as Array<Record<string, unknown>>)[0]?.type).toBe("additional_tools");
|
||||
expect(captured).toBeDefined();
|
||||
expect(captured!.headers.get("x-openai-internal-codex-responses-lite")).toBe("true");
|
||||
expect(captured!.headers.get("version")).toBe("0.144.1");
|
||||
const body = captured!.body;
|
||||
expect(body.reasoning).toEqual({ context: "all_turns" });
|
||||
expect(body.instructions).toBeUndefined();
|
||||
expect(body.tools).toBeUndefined();
|
||||
expect((body.input as Array<Record<string, unknown>>)[0]?.type).toBe("additional_tools");
|
||||
});
|
||||
|
||||
it("omits the lite marker while retaining canonical client_metadata", async () => {
|
||||
|
||||
@@ -1181,9 +1181,10 @@ describe("OpenAI responses history payload", () => {
|
||||
),
|
||||
).toBe(false);
|
||||
expect(reasoningItem?.encrypted_content).toBe("enc_opaque");
|
||||
expect(functionCallItem?.call_id).toBe(expectedCallId);
|
||||
expect(functionCallItem).toBeDefined();
|
||||
expect(functionCallItem!.call_id).toBe(expectedCallId);
|
||||
expect(functionCallOutputItem?.call_id).toBe(expectedCallId);
|
||||
expect((functionCallItem?.call_id as string).length).toBeLessThanOrEqual(64);
|
||||
expect((functionCallItem!.call_id as string).length).toBeLessThanOrEqual(64);
|
||||
expect(containsAssistantOutputText(payload.input, "Sanitized assistant answer")).toBe(true);
|
||||
expect(replayHistoryItems[0]?.id).toBe(opaqueReasoningId);
|
||||
expect(replayHistoryItems[1]?.id).toBe(opaqueMessageId);
|
||||
|
||||
@@ -396,8 +396,9 @@ describe("openai-responses stateful chaining", () => {
|
||||
|
||||
expect(sentRequests).toHaveLength(2);
|
||||
expect(sentRequests[0]?.store).toBe(false);
|
||||
expect(sentRequests[1]?.store).toBe(false);
|
||||
expect(sentRequests[1]?.previous_response_id).toBeUndefined();
|
||||
expect((sentRequests[1]?.input as unknown[]).length).toBeGreaterThan(1);
|
||||
expect(sentRequests[1]).toBeDefined();
|
||||
expect(sentRequests[1]!.store).toBe(false);
|
||||
expect(sentRequests[1]!.previous_response_id).toBeUndefined();
|
||||
expect((sentRequests[1]!.input as unknown[]).length).toBeGreaterThan(1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -54,9 +54,10 @@ describe("armPreResponseTimeout", () => {
|
||||
const caller = new AbortController();
|
||||
const { signal, clear } = armPreResponseTimeout(caller.signal, 60_000);
|
||||
clear();
|
||||
expect(signal?.aborted).toBe(false);
|
||||
expect(signal).toBeDefined();
|
||||
expect(signal!.aborted).toBe(false);
|
||||
caller.abort(new Error("caller cancelled"));
|
||||
expect(signal?.aborted).toBe(true);
|
||||
expect((signal?.reason as Error).message).toBe("caller cancelled");
|
||||
expect(signal!.aborted).toBe(true);
|
||||
expect((signal!.reason as Error).message).toBe("caller cancelled");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -114,7 +114,8 @@ describe("streamSimple resolver auth retry", () => {
|
||||
{ lastChance: false, hasError: false },
|
||||
{ lastChance: false, hasError: true },
|
||||
]);
|
||||
expect((contexts[1]?.error as { status?: number }).status).toBe(401);
|
||||
expect(contexts[1]).toBeDefined();
|
||||
expect((contexts[1]!.error as { status?: number }).status).toBe(401);
|
||||
});
|
||||
|
||||
it("buffers the start event and retries on a 401 error event before content", async () => {
|
||||
@@ -503,7 +504,8 @@ describe("streamSimple resolver auth retry", () => {
|
||||
expect(retryContexts.map(ctx => ({ lastChance: ctx.lastChance, hasError: ctx.error !== undefined }))).toEqual([
|
||||
{ lastChance: true, hasError: true },
|
||||
]);
|
||||
expect((retryContexts[0]?.error as Error).message).toContain("Resource exhausted");
|
||||
expect(retryContexts[0]).toBeDefined();
|
||||
expect((retryContexts[0]!.error as Error).message).toContain("Resource exhausted");
|
||||
});
|
||||
|
||||
it("surfaces the original error when the resolver declines every retry", async () => {
|
||||
|
||||
@@ -61,8 +61,10 @@ describe("loadCustomTools per-session binding (#2190 review fix)", () => {
|
||||
expect(parentResult.tools).toHaveLength(1);
|
||||
expect(subagentResult.tools).toHaveLength(1);
|
||||
|
||||
const parentApi = (parentResult.tools[0]?.tool as unknown as { __boundApi: CustomToolAPI }).__boundApi;
|
||||
const subagentApi = (subagentResult.tools[0]?.tool as unknown as { __boundApi: CustomToolAPI }).__boundApi;
|
||||
expect(parentResult.tools[0]).toBeDefined();
|
||||
expect(subagentResult.tools[0]).toBeDefined();
|
||||
const parentApi = (parentResult.tools[0]!.tool as unknown as { __boundApi: CustomToolAPI }).__boundApi;
|
||||
const subagentApi = (subagentResult.tools[0]!.tool as unknown as { __boundApi: CustomToolAPI }).__boundApi;
|
||||
|
||||
expect(parentApi.cwd).toBe("/tmp/parent-cwd");
|
||||
expect(subagentApi.cwd).toBe("/tmp/subagent-cwd");
|
||||
@@ -82,8 +84,10 @@ describe("loadCustomTools per-session binding (#2190 review fix)", () => {
|
||||
subagentLog.push(`subagent:${action.label}`),
|
||||
);
|
||||
|
||||
const parentApi = (parentResult.tools[0]?.tool as unknown as { __boundApi: CustomToolAPI }).__boundApi;
|
||||
const subagentApi = (subagentResult.tools[0]?.tool as unknown as { __boundApi: CustomToolAPI }).__boundApi;
|
||||
expect(parentResult.tools[0]).toBeDefined();
|
||||
expect(subagentResult.tools[0]).toBeDefined();
|
||||
const parentApi = (parentResult.tools[0]!.tool as unknown as { __boundApi: CustomToolAPI }).__boundApi;
|
||||
const subagentApi = (subagentResult.tools[0]!.tool as unknown as { __boundApi: CustomToolAPI }).__boundApi;
|
||||
|
||||
// Cast: the test fixture exposes the runtime API verbatim.
|
||||
parentApi.pushPendingAction({
|
||||
|
||||
@@ -54,7 +54,8 @@ describe("convertToLlm compaction summary", () => {
|
||||
{ role: "compactionSummary", summary: "plain summary", tokensBefore: 1000, timestamp: Date.now() },
|
||||
];
|
||||
const converted = convertToLlm(messages);
|
||||
expect((converted[0]?.content as unknown[]).length).toBe(1);
|
||||
expect(converted[0]).toBeDefined();
|
||||
expect((converted[0]!.content as unknown[]).length).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -267,9 +267,10 @@ describe("browser tab-supervisor — cmux tab close mid-run (#4499)", () => {
|
||||
// the composed `runSignal` in `runInTabWithSnapshot` transitions
|
||||
// to aborted. Without this line, the reviewer's failure mode
|
||||
// stands: the run body keeps executing until its own timeout.
|
||||
expect(capturedCloseAc?.signal.aborted).toBe(true);
|
||||
expect(capturedCloseAc?.signal.reason).toBeInstanceOf(Error);
|
||||
expect((capturedCloseAc?.signal.reason as Error).message).toMatch(/Tab "docfinal" was closed/);
|
||||
expect(capturedCloseAc).toBeDefined();
|
||||
expect(capturedCloseAc!.signal.aborted).toBe(true);
|
||||
expect(capturedCloseAc!.signal.reason).toBeInstanceOf(Error);
|
||||
expect((capturedCloseAc!.signal.reason as Error).message).toMatch(/Tab "docfinal" was closed/);
|
||||
|
||||
// Caller-facing contract: `runInTab` rejects with the tab-close
|
||||
// error immediately, not after the run's 60_000ms timeout.
|
||||
|
||||
@@ -735,7 +735,8 @@ describe("IRC", () => {
|
||||
expect(promptSpy).toHaveBeenCalledTimes(1);
|
||||
// The idle wake routes through #wakeForIrc, which batches records into one prompt —
|
||||
// even a lone incoming message is delivered as a one-element array.
|
||||
const prompted = (promptSpy.mock.calls[0]?.[0] as unknown as CustomMessage[])[0];
|
||||
expect(promptSpy.mock.calls[0]).toBeDefined();
|
||||
const prompted = (promptSpy.mock.calls[0]![0] as unknown as CustomMessage[])[0];
|
||||
expect(prompted).toMatchObject({ role: "custom", customType: "irc:incoming" });
|
||||
expect(prompted.details).toMatchObject({ id: "msg-1", from: "0-Peer", message: "wake up" });
|
||||
|
||||
|
||||
@@ -356,9 +356,11 @@ describe("Perplexity OAuth request shape", () => {
|
||||
|
||||
// The consumer ask endpoint has no system slot; prepending the prompt makes
|
||||
// the model refuse ("I don't have web-search tools in this turn").
|
||||
expect(body?.query_str).toBe("quic vs tcp");
|
||||
expect((body?.params as Record<string, unknown>).query_str).toBe("quic vs tcp");
|
||||
expect((body?.params as Record<string, unknown>).model_preference).toBe("experimental");
|
||||
expect(body).toBeDefined();
|
||||
expect(body!.query_str).toBe("quic vs tcp");
|
||||
const params = body!.params as Record<string, unknown>;
|
||||
expect(params.query_str).toBe("quic vs tcp");
|
||||
expect(params.model_preference).toBe("experimental");
|
||||
// The ask endpoint authenticates via the next-auth session cookie; a bearer
|
||||
// header is ignored and silently downgrades to the anonymous `turbo` model.
|
||||
expect(headers?.get("cookie")).toBe("__Secure-next-auth.session-token=test-oauth-token");
|
||||
|
||||
Reference in New Issue
Block a user