Skip to content

Commit dd94073

Browse files
committed
feat(sdk): add mockChatAgent test harness with locals DI
Drives a chat.agent definition through real turns offline — send messages, actions, and stop signals; inspect captured chunks; assert on hook order. Pre-seed dependencies via setupLocals so hooks read test instances (DB clients, stubs) via locals.get() instead of leaking through untrusted clientData. Adds ai-chat reference tests exercising the harness across basic flow, onValidateMessages, hydrateMessages, and actions.
1 parent ccf3381 commit dd94073

File tree

12 files changed

+1099
-4
lines changed

12 files changed

+1099
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
"@trigger.dev/core": patch
4+
---
5+
6+
Add `setupLocals` option to `mockChatAgent` for dependency injection in tests. Pre-seed `locals` (database clients, service stubs) before the agent's `run()` starts, so hooks read the test instance via `locals.get()` without leaking through untrusted `clientData`. Also exposes `drivers.locals.set()` on `runInMockTaskContext`.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
"@trigger.dev/core": patch
4+
---
5+
6+
Add `mockChatAgent` test harness at `@trigger.dev/sdk/ai/test` for unit-testing `chat.agent` definitions offline. Drives a real agent's turn loop without network or task runtime: send messages, actions, and stop signals via driver methods, inspect captured output chunks, and verify hooks fire. Pairs with `MockLanguageModelV3` from `ai/test` for model mocking.
7+
8+
Also adds `TestRunMetadataManager` to `@trigger.dev/core/v3/test` (in-memory metadata manager used by the harness), and exposes an `onWrite` hook on `TestRealtimeStreamsManager` so harnesses can react to stream writes without polling.

packages/trigger-sdk/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
".": "./src/v3/index.ts",
2626
"./v3": "./src/v3/index.ts",
2727
"./ai": "./src/v3/ai.ts",
28+
"./ai/test": "./src/v3/test/index.ts",
2829
"./chat": "./src/v3/chat.ts",
2930
"./chat/react": "./src/v3/chat-react.ts"
3031
},
@@ -40,6 +41,9 @@
4041
"ai": [
4142
"dist/commonjs/v3/ai.d.ts"
4243
],
44+
"ai/test": [
45+
"dist/commonjs/v3/test/index.d.ts"
46+
],
4347
"chat": [
4448
"dist/commonjs/v3/chat.d.ts"
4549
],
@@ -72,6 +76,7 @@
7276
"ws": "^8.11.0"
7377
},
7478
"devDependencies": {
79+
"@ai-sdk/provider": "3.0.8",
7580
"@arethetypeswrong/cli": "^0.15.4",
7681
"@types/debug": "^4.1.7",
7782
"@types/react": "^19.2.14",
@@ -137,6 +142,17 @@
137142
"default": "./dist/commonjs/v3/ai.js"
138143
}
139144
},
145+
"./ai/test": {
146+
"import": {
147+
"@triggerdotdev/source": "./src/v3/test/index.ts",
148+
"types": "./dist/esm/v3/test/index.d.ts",
149+
"default": "./dist/esm/v3/test/index.js"
150+
},
151+
"require": {
152+
"types": "./dist/commonjs/v3/test/index.d.ts",
153+
"default": "./dist/commonjs/v3/test/index.js"
154+
}
155+
},
140156
"./chat": {
141157
"import": {
142158
"@triggerdotdev/source": "./src/v3/chat.ts",
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Importing this module installs an in-memory resource catalog so that
2+
// chat.agent() calls (which run at import time) register their task
3+
// functions where the test harness can find them.
4+
//
5+
// Users should import `@trigger.dev/sdk/ai/test` BEFORE their agent
6+
// modules so the registration side-effect runs first.
7+
import "./setup-catalog.js";
8+
9+
export {
10+
mockChatAgent,
11+
type MockChatAgentOptions,
12+
type MockChatAgentHarness,
13+
type MockChatAgentTurn,
14+
} from "./mock-chat-agent.js";
15+
16+
// Re-export the lower-level task context harness so consumers can build
17+
// their own test helpers without adding a separate `@trigger.dev/core`
18+
// dependency to their reference projects.
19+
export {
20+
runInMockTaskContext,
21+
type MockTaskContextDrivers,
22+
type MockTaskContextOptions,
23+
} from "@trigger.dev/core/v3/test";

0 commit comments

Comments
 (0)