Skip to content

Commit 4e33d42

Browse files
committed
refactor(tests): test-hygiene fixes for Agent test files
- Replace Record<string, unknown> return types on TanStack mock chunk builders with inferred types via as const - Remove dynamic imports in converter-aisdk error tests — use top-level collectEventsIncludingErrors import instead - Remove redundant as RunAgentInput casts in converter-tanstack-input tests (createDefaultInput already returns the correct type) - Add comments explaining necessary casts in createAgent overloads (TypeScript limitation with discriminated union narrowing)
1 parent b344d08 commit 4e33d42

3 files changed

Lines changed: 37 additions & 36 deletions

File tree

packages/runtime/src/agent/__tests__/agent-test-helpers.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export { Agent, type AgentFactoryContext };
4343
*/
4444
export function createDefaultInput(
4545
overrides?: Partial<RunAgentInput>,
46-
): RunAgentInput {
46+
) {
4747
return {
4848
threadId: "test-thread",
4949
runId: "test-run",
@@ -61,31 +61,31 @@ export function createDefaultInput(
6161
// ---------------------------------------------------------------------------
6262

6363
/** TanStack text content chunk */
64-
export function tanstackTextChunk(delta: string): Record<string, unknown> {
65-
return { type: "TEXT_MESSAGE_CONTENT", delta };
64+
export function tanstackTextChunk(delta: string) {
65+
return { type: "TEXT_MESSAGE_CONTENT", delta } as const;
6666
}
6767

6868
/** TanStack tool call start chunk */
6969
export function tanstackToolCallStart(
7070
toolCallId: string,
7171
toolCallName: string,
72-
): Record<string, unknown> {
73-
return { type: "TOOL_CALL_START", toolCallId, toolCallName };
72+
) {
73+
return { type: "TOOL_CALL_START", toolCallId, toolCallName } as const;
7474
}
7575

7676
/** TanStack tool call args chunk */
7777
export function tanstackToolCallArgs(
7878
toolCallId: string,
7979
delta: string,
80-
): Record<string, unknown> {
81-
return { type: "TOOL_CALL_ARGS", toolCallId, delta };
80+
) {
81+
return { type: "TOOL_CALL_ARGS", toolCallId, delta } as const;
8282
}
8383

8484
/** TanStack tool call end chunk */
8585
export function tanstackToolCallEnd(
8686
toolCallId: string,
87-
): Record<string, unknown> {
88-
return { type: "TOOL_CALL_END", toolCallId };
87+
) {
88+
return { type: "TOOL_CALL_END", toolCallId } as const;
8989
}
9090

9191
// ---------------------------------------------------------------------------
@@ -97,7 +97,7 @@ export function tanstackToolCallEnd(
9797
*/
9898
export function mockTanStackStream(
9999
chunks: Record<string, unknown>[],
100-
): AsyncIterable<unknown> {
100+
) {
101101
return {
102102
[Symbol.asyncIterator]: async function* () {
103103
for (const chunk of chunks) {
@@ -155,6 +155,8 @@ export function createAgent(
155155
): Agent {
156156
switch (type) {
157157
case "aisdk": {
158+
// Cast needed: TypeScript's control-flow narrowing doesn't propagate
159+
// through overload signatures to narrow the union parameter type.
158160
const events = streamData as MockStreamEvent[];
159161
return new Agent({
160162
type: "aisdk",
@@ -168,13 +170,15 @@ export function createAgent(
168170
});
169171
}
170172
case "tanstack": {
173+
// Cast needed: same overload-narrowing limitation as above.
171174
const chunks = streamData as Record<string, unknown>[];
172175
return new Agent({
173176
type: "tanstack",
174177
factory: () => mockTanStackStream(chunks),
175178
});
176179
}
177180
case "custom": {
181+
// Cast needed: same overload-narrowing limitation as above.
178182
const events = streamData as BaseEvent[];
179183
return new Agent({
180184
type: "custom",
@@ -258,7 +262,7 @@ export function createMidStreamErrorAgent(
258262
type: EventType.TEXT_MESSAGE_CHUNK,
259263
role: "assistant",
260264
delta: "partial",
261-
} as BaseEvent;
265+
} as const as BaseEvent;
262266
throw new Error(errorMessage);
263267
},
264268
}),

packages/runtime/src/agent/__tests__/converter-aisdk.test.ts

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
createAgent,
55
createDefaultInput,
66
collectEvents,
7+
collectEventsIncludingErrors,
78
expectLifecycleWrapped,
89
expectEventSequence,
910
eventField,
@@ -593,10 +594,9 @@ describe("AI SDK Converter", () => {
593594
]);
594595
const input = createDefaultInput();
595596

596-
const { events, errored } =
597-
await (await import("./agent-test-helpers")).collectEventsIncludingErrors(
598-
agent.run(input),
599-
);
597+
const { events, errored } = await collectEventsIncludingErrors(
598+
agent.run(input),
599+
);
600600

601601
expect(errored).toBe(true);
602602
const errorEvents = events.filter(
@@ -618,10 +618,9 @@ describe("AI SDK Converter", () => {
618618
]);
619619
const input = createDefaultInput();
620620

621-
const { events, errored } =
622-
await (await import("./agent-test-helpers")).collectEventsIncludingErrors(
623-
agent.run(input),
624-
);
621+
const { events, errored } = await collectEventsIncludingErrors(
622+
agent.run(input),
623+
);
625624

626625
expect(errored).toBe(true);
627626
const errorEvents = events.filter(
@@ -638,10 +637,9 @@ describe("AI SDK Converter", () => {
638637
]);
639638
const input = createDefaultInput();
640639

641-
const { events, errored } =
642-
await (await import("./agent-test-helpers")).collectEventsIncludingErrors(
643-
agent.run(input),
644-
);
640+
const { events, errored } = await collectEventsIncludingErrors(
641+
agent.run(input),
642+
);
645643

646644
expect(errored).toBe(true);
647645
const errorEvents = events.filter(

packages/runtime/src/agent/__tests__/converter-tanstack-input.test.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { describe, it, expect } from "vitest";
2-
import type { RunAgentInput } from "@ag-ui/client";
32
import { convertInputToTanStackAI } from "../converters/tanstack";
43
import { createDefaultInput } from "./agent-test-helpers";
54

@@ -14,7 +13,7 @@ describe("convertInputToTanStackAI", () => {
1413
{ role: "system", content: "You are helpful" },
1514
{ role: "user", content: "Hello" },
1615
],
17-
}) as RunAgentInput;
16+
});
1817

1918
const { messages } = convertInputToTanStackAI(input);
2019

@@ -29,7 +28,7 @@ describe("convertInputToTanStackAI", () => {
2928
{ role: "developer", content: "Internal instruction" },
3029
{ role: "user", content: "Hi" },
3130
],
32-
}) as RunAgentInput;
31+
});
3332

3433
const { messages } = convertInputToTanStackAI(input);
3534

@@ -44,7 +43,7 @@ describe("convertInputToTanStackAI", () => {
4443
{ role: "developer", content: "Dev instruction" },
4544
{ role: "user", content: "Hello" },
4645
],
47-
}) as RunAgentInput;
46+
});
4847

4948
const { systemPrompts } = convertInputToTanStackAI(input);
5049

@@ -59,7 +58,7 @@ describe("convertInputToTanStackAI", () => {
5958
{ role: "assistant", content: "Answer 1" },
6059
{ role: "user", content: "Question 2" },
6160
],
62-
}) as RunAgentInput;
61+
});
6362

6463
const { messages } = convertInputToTanStackAI(input);
6564

@@ -92,7 +91,7 @@ describe("convertInputToTanStackAI", () => {
9291
],
9392
},
9493
],
95-
}) as RunAgentInput;
94+
});
9695

9796
const { messages } = convertInputToTanStackAI(input);
9897

@@ -114,7 +113,7 @@ describe("convertInputToTanStackAI", () => {
114113
toolCallId: "tc-1",
115114
},
116115
],
117-
}) as RunAgentInput;
116+
});
118117

119118
const { messages } = convertInputToTanStackAI(input);
120119

@@ -134,7 +133,7 @@ describe("convertInputToTanStackAI", () => {
134133
{ description: "User preferences", value: "Dark mode enabled" },
135134
{ description: "Current page", value: "/dashboard" },
136135
],
137-
}) as RunAgentInput;
136+
});
138137

139138
const { systemPrompts } = convertInputToTanStackAI(input);
140139

@@ -145,7 +144,7 @@ describe("convertInputToTanStackAI", () => {
145144
});
146145

147146
it("does not add context when context array is empty", () => {
148-
const input = createDefaultInput({ context: [] }) as RunAgentInput;
147+
const input = createDefaultInput({ context: [] });
149148

150149
const { systemPrompts } = convertInputToTanStackAI(input);
151150

@@ -160,7 +159,7 @@ describe("convertInputToTanStackAI", () => {
160159
it("serializes non-empty state into systemPrompts", () => {
161160
const input = createDefaultInput({
162161
state: { count: 42, items: ["a", "b"] },
163-
}) as RunAgentInput;
162+
});
164163

165164
const { systemPrompts } = convertInputToTanStackAI(input);
166165

@@ -172,7 +171,7 @@ describe("convertInputToTanStackAI", () => {
172171
});
173172

174173
it("does not add state when state is empty object", () => {
175-
const input = createDefaultInput({ state: {} }) as RunAgentInput;
174+
const input = createDefaultInput({ state: {} });
176175

177176
const { systemPrompts } = convertInputToTanStackAI(input);
178177

@@ -182,7 +181,7 @@ describe("convertInputToTanStackAI", () => {
182181
});
183182

184183
it("does not add state when state is null", () => {
185-
const input = createDefaultInput({ state: null }) as RunAgentInput;
184+
const input = createDefaultInput({ state: null });
186185

187186
const { systemPrompts } = convertInputToTanStackAI(input);
188187

@@ -197,7 +196,7 @@ describe("convertInputToTanStackAI", () => {
197196
// -------------------------------------------------------------------------
198197
describe("empty input", () => {
199198
it("returns empty messages and systemPrompts for default input", () => {
200-
const input = createDefaultInput() as RunAgentInput;
199+
const input = createDefaultInput();
201200

202201
const { messages, systemPrompts } = convertInputToTanStackAI(input);
203202

0 commit comments

Comments
 (0)