Skip to content

Commit 6d82489

Browse files
committed
test: add reusable fixtures for E2E testing with real AI SDK pipeline
Add two shared test fixtures that enable E2E tests through the real AI SDK streamText pipeline without mocking: - test/fixture/anthropic.ts: fake Anthropic HTTP server with SSE response helpers (toolResponse, textResponse, waitRequest, deferred) - test/fixture/prompt-layers.ts: full SessionPrompt Effect layer stack with real LLM.defaultLayer and no-op stubs for MCP/LSP/FileTime These fixtures reduce boilerplate for tests that need to exercise the complete prompt pipeline (tool registration, resolveTools, processor event handling) against a fake provider. Closes anomalyco#20206
1 parent 2cc738f commit 6d82489

2 files changed

Lines changed: 261 additions & 0 deletions

File tree

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/**
2+
* Fake Anthropic HTTP server for E2E tests that need real AI SDK streamText.
3+
*
4+
* Usage:
5+
* import { server, waitRequest, toolResponse, textResponse, deferred } from "../fixture/anthropic"
6+
*
7+
* beforeAll(() => server.start())
8+
* beforeEach(() => server.reset())
9+
* afterAll(() => server.stop())
10+
*
11+
* // In test:
12+
* waitRequest("/messages", toolResponse("toolu_01", "my_tool", { key: "value" }))
13+
* waitRequest("/messages", textResponse("Done"))
14+
*/
15+
16+
export type Capture = {
17+
url: URL
18+
headers: Headers
19+
body: Record<string, unknown>
20+
}
21+
22+
type Entry = {
23+
path: string
24+
response: Response | ((req: Request, capture: Capture) => Response)
25+
resolve: (value: Capture) => void
26+
}
27+
28+
const state = {
29+
instance: null as ReturnType<typeof Bun.serve> | null,
30+
queue: [] as Entry[],
31+
}
32+
33+
export function deferred<T>() {
34+
const result = {} as { promise: Promise<T>; resolve: (value: T) => void }
35+
result.promise = new Promise((resolve) => {
36+
result.resolve = resolve
37+
})
38+
return result
39+
}
40+
41+
export const server = {
42+
start() {
43+
state.instance = Bun.serve({
44+
port: 0,
45+
async fetch(req) {
46+
const next = state.queue.shift()
47+
if (!next) return new Response("unexpected request", { status: 500 })
48+
const url = new URL(req.url)
49+
const body = (await req.json()) as Record<string, unknown>
50+
next.resolve({ url, headers: req.headers, body })
51+
if (!url.pathname.endsWith(next.path)) return new Response("not found", { status: 404 })
52+
return typeof next.response === "function"
53+
? next.response(req, { url, headers: req.headers, body })
54+
: next.response
55+
},
56+
})
57+
},
58+
stop() {
59+
state.instance?.stop()
60+
},
61+
reset() {
62+
state.queue.length = 0
63+
},
64+
get origin() {
65+
if (!state.instance) throw new Error("server.start() must be called before accessing origin")
66+
return state.instance.url.origin
67+
},
68+
}
69+
70+
export function waitRequest(pathname: string, response: Response | ((req: Request, capture: Capture) => Response)) {
71+
const pending = deferred<Capture>()
72+
state.queue.push({ path: pathname, response, resolve: pending.resolve })
73+
return pending.promise
74+
}
75+
76+
// --- SSE helpers ---
77+
78+
export function sse(chunks: unknown[]) {
79+
const payload = chunks.map((c) => `data: ${JSON.stringify(c)}`).join("\n\n") + "\n\n"
80+
const bytes = new TextEncoder().encode(payload)
81+
return new Response(
82+
new ReadableStream({
83+
start(controller) {
84+
controller.enqueue(bytes)
85+
controller.close()
86+
},
87+
}),
88+
{ status: 200, headers: { "Content-Type": "text/event-stream" } },
89+
)
90+
}
91+
92+
export function toolResponse(id: string, name: string, input: Record<string, unknown>) {
93+
return sse([
94+
{
95+
type: "message_start",
96+
message: {
97+
id: "msg-1",
98+
model: "claude-3-5-sonnet-20241022",
99+
role: "assistant",
100+
usage: { input_tokens: 10, cache_creation_input_tokens: null, cache_read_input_tokens: null },
101+
},
102+
},
103+
{ type: "content_block_start", index: 0, content_block: { type: "tool_use", id, name } },
104+
{ type: "content_block_delta", index: 0, delta: { type: "input_json_delta", partial_json: JSON.stringify(input) } },
105+
{ type: "content_block_stop", index: 0 },
106+
{
107+
type: "message_delta",
108+
delta: { stop_reason: "tool_use" },
109+
usage: { output_tokens: 20, cache_creation_input_tokens: null, cache_read_input_tokens: null },
110+
},
111+
{ type: "message_stop" },
112+
])
113+
}
114+
115+
export function textResponse(msg: string) {
116+
return sse([
117+
{
118+
type: "message_start",
119+
message: {
120+
id: "msg-2",
121+
model: "claude-3-5-sonnet-20241022",
122+
role: "assistant",
123+
usage: { input_tokens: 10, cache_creation_input_tokens: null, cache_read_input_tokens: null },
124+
},
125+
},
126+
{ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } },
127+
{ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: msg } },
128+
{ type: "content_block_stop", index: 0 },
129+
{
130+
type: "message_delta",
131+
delta: { stop_reason: "end_turn" },
132+
usage: { output_tokens: 5, cache_creation_input_tokens: null, cache_read_input_tokens: null },
133+
},
134+
{ type: "message_stop" },
135+
])
136+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/**
2+
* Full SessionPrompt Effect layer stack with real LLM (not TestLLM).
3+
*
4+
* Use this when tests need to exercise the complete prompt pipeline
5+
* (resolveTools, tool execution, processor event handling) through
6+
* the real AI SDK streamText against a fake HTTP server.
7+
*
8+
* Service stubs (MCP, LSP, FileTime) are no-ops — they satisfy the
9+
* layer graph without requiring real servers.
10+
*
11+
* Usage:
12+
* import { env } from "../fixture/prompt-layers"
13+
* const it = testEffect(env)
14+
*/
15+
16+
import { NodeFileSystem } from "@effect/platform-node"
17+
import { Effect, Layer } from "effect"
18+
import { Agent as AgentSvc } from "../../src/agent/agent"
19+
import { Bus } from "../../src/bus"
20+
import { Command } from "../../src/command"
21+
import { Config } from "../../src/config/config"
22+
import { FileTime } from "../../src/file/time"
23+
import { AppFileSystem } from "../../src/filesystem"
24+
import { LSP } from "../../src/lsp"
25+
import { MCP } from "../../src/mcp"
26+
import { Permission } from "../../src/permission"
27+
import { Plugin } from "../../src/plugin"
28+
import { Session } from "../../src/session"
29+
import { LLM } from "../../src/session/llm"
30+
import { SessionCompaction } from "../../src/session/compaction"
31+
import { SessionProcessor } from "../../src/session/processor"
32+
import { SessionPrompt } from "../../src/session/prompt"
33+
import { SessionStatus } from "../../src/session/status"
34+
import { Snapshot } from "../../src/snapshot"
35+
import { ToolRegistry } from "../../src/tool/registry"
36+
import { Truncate } from "../../src/tool/truncate"
37+
import * as CrossSpawnSpawner from "../../src/effect/cross-spawn-spawner"
38+
39+
// --- Service stubs ---
40+
41+
const mcp = Layer.succeed(
42+
MCP.Service,
43+
MCP.Service.of({
44+
status: () => Effect.succeed({}),
45+
clients: () => Effect.succeed({}),
46+
tools: () => Effect.succeed({}),
47+
prompts: () => Effect.succeed({}),
48+
resources: () => Effect.succeed({}),
49+
add: () => Effect.succeed({ status: { status: "disabled" as const } }),
50+
connect: () => Effect.void,
51+
disconnect: () => Effect.void,
52+
getPrompt: () => Effect.succeed(undefined),
53+
readResource: () => Effect.succeed(undefined),
54+
startAuth: () => Effect.die("unexpected MCP auth"),
55+
authenticate: () => Effect.die("unexpected MCP auth"),
56+
finishAuth: () => Effect.die("unexpected MCP auth"),
57+
removeAuth: () => Effect.void,
58+
supportsOAuth: () => Effect.succeed(false),
59+
hasStoredTokens: () => Effect.succeed(false),
60+
getAuthStatus: () => Effect.succeed("not_authenticated" as const),
61+
}),
62+
)
63+
64+
const lsp = Layer.succeed(
65+
LSP.Service,
66+
LSP.Service.of({
67+
init: () => Effect.void,
68+
status: () => Effect.succeed([]),
69+
hasClients: () => Effect.succeed(false),
70+
touchFile: () => Effect.void,
71+
diagnostics: () => Effect.succeed({}),
72+
hover: () => Effect.succeed(undefined),
73+
definition: () => Effect.succeed([]),
74+
references: () => Effect.succeed([]),
75+
implementation: () => Effect.succeed([]),
76+
documentSymbol: () => Effect.succeed([]),
77+
workspaceSymbol: () => Effect.succeed([]),
78+
prepareCallHierarchy: () => Effect.succeed([]),
79+
incomingCalls: () => Effect.succeed([]),
80+
outgoingCalls: () => Effect.succeed([]),
81+
}),
82+
)
83+
84+
const filetime = Layer.succeed(
85+
FileTime.Service,
86+
FileTime.Service.of({
87+
read: () => Effect.void,
88+
get: () => Effect.succeed(undefined),
89+
assert: () => Effect.void,
90+
withLock: (_filepath, fn) => Effect.promise(fn),
91+
}),
92+
)
93+
94+
// --- Layer composition ---
95+
96+
const status = SessionStatus.layer.pipe(Layer.provideMerge(Bus.layer))
97+
const infra = Layer.mergeAll(NodeFileSystem.layer, CrossSpawnSpawner.defaultLayer)
98+
const deps = Layer.mergeAll(
99+
Session.defaultLayer,
100+
Snapshot.defaultLayer,
101+
AgentSvc.defaultLayer,
102+
Command.defaultLayer,
103+
Permission.layer,
104+
Plugin.defaultLayer,
105+
Config.defaultLayer,
106+
filetime,
107+
lsp,
108+
mcp,
109+
AppFileSystem.defaultLayer,
110+
status,
111+
LLM.defaultLayer,
112+
).pipe(Layer.provideMerge(infra))
113+
const registry = ToolRegistry.layer.pipe(Layer.provideMerge(deps))
114+
const trunc = Truncate.layer.pipe(Layer.provideMerge(deps))
115+
const proc = SessionProcessor.layer.pipe(Layer.provideMerge(deps))
116+
const compact = SessionCompaction.layer.pipe(Layer.provideMerge(proc), Layer.provideMerge(deps))
117+
118+
/** Full SessionPrompt layer with real LLM and no-op stubs for MCP/LSP/FileTime */
119+
export const env = SessionPrompt.layer.pipe(
120+
Layer.provideMerge(compact),
121+
Layer.provideMerge(proc),
122+
Layer.provideMerge(registry),
123+
Layer.provideMerge(trunc),
124+
Layer.provideMerge(deps),
125+
)

0 commit comments

Comments
 (0)