Open History to see the last commit
1/**
2 * Gated live smoke test for the Claude Code harness adapter.
3 *
4 * The standard e2e matrix mocks providers with aimock via per-test
5 * `X-Test-Id` header isolation. Claude Code spawns its bundled runtime as a
6 * subprocess, so that isolation can't be injected — this adapter is excluded
7 * from the matrix and covered here instead, gated behind CLAUDE_CODE_E2E.
8 *
9 * Run with:
10 * CLAUDE_CODE_E2E=1 ANTHROPIC_API_KEY=sk-... \
11 * pnpm --filter @tanstack/ai-e2e test:e2e -- --grep "claude-code"
12 *
13 * (A local `claude login` works in place of ANTHROPIC_API_KEY.)
14 */
15import { expect, test } from '@playwright/test'
16import { chat } from '@tanstack/ai'
17import { claudeCodeText } from '@tanstack/ai-claude-code'
18import { defineSandbox, withSandbox } from '@tanstack/ai-sandbox'
19import { localProcessSandbox } from '@tanstack/ai-sandbox-local-process'
20import type {
21 StreamChunk,
22 ToolCallArgsEvent,
23 ToolCallEndEvent,
24 ToolCallStartEvent,
25} from '@tanstack/ai'
26
27const isToolCallStart = (chunk: StreamChunk): chunk is ToolCallStartEvent =>
28 chunk.type === 'TOOL_CALL_START'
29const isToolCallArgs = (chunk: StreamChunk): chunk is ToolCallArgsEvent =>
30 chunk.type === 'TOOL_CALL_ARGS'
31const isToolCallEnd = (chunk: StreamChunk): chunk is ToolCallEndEvent =>
32 chunk.type === 'TOOL_CALL_END'
33
34const sandbox = defineSandbox({
35 id: 'claude-code-e2e',
36 provider: localProcessSandbox(),
37 workspace: {
38 source: { type: 'none' },
39 instructions: 'Claude Code streaming smoke test workspace.',
40 },
41 lifecycle: { reuse: 'none', snapshot: 'none', destroyOnComplete: true },
42 fileEvents: false,
43})
44
45test.describe('claude-code harness (gated live smoke)', () => {
46 test.skip(
47 !process.env.CLAUDE_CODE_E2E,
48 'Set CLAUDE_CODE_E2E=1 (plus ANTHROPIC_API_KEY or a local Claude login) to run the Claude Code live smoke test',
49 )
50
51 test('streams a full harness turn with session id and stop finish', async () => {
52 test.setTimeout(180_000)
53
54 const chunks: Array<StreamChunk> = []
55 const stream = chat({
56 adapter: claudeCodeText('haiku', {
57 maxTurns: 2,
58 // Read-only smoke: the default permission policy denies anything
59 // that would prompt, and no tools are bridged.
60 disallowedTools: ['Bash', 'Write', 'Edit'],
61 }),
62 middleware: [withSandbox(sandbox)],
63 threadId: 'thread-claude-code-pong',
64 messages: [
65 {
66 role: 'user',
67 content: 'Reply with exactly the word: pong',
68 },
69 ],
70 })
71
72 for await (const chunk of stream) {
73 chunks.push(chunk)
74 }
75
76 const types = chunks.map((chunk) => chunk.type as string)
77 expect(types[0]).toBe('RUN_STARTED')
78
79 const sessionEvent = chunks.find(
80 (chunk) =>
81 chunk.type === 'CUSTOM' &&
82 (chunk as { name?: string }).name === 'claude-code.session-id',
83 )
84 expect(sessionEvent).toBeDefined()
85 expect(
86 (sessionEvent as { value: { sessionId: string } }).value.sessionId,
87 ).toMatch(/.+/)
88
89 const finished = chunks.find((chunk) => chunk.type === 'RUN_FINISHED')
90 expect(finished).toBeDefined()
91 expect(
92 finished?.type === 'RUN_FINISHED' &&
93 finished.metadata?.tanstack?.finishReason,
94 ).toBe('stop')
95
96 const text = chunks
97 .filter((chunk) => chunk.type === 'TEXT_MESSAGE_CONTENT')
98 .map((chunk) => (chunk as { delta?: string }).delta ?? '')
99 .join('')
100 expect(text.toLowerCase()).toContain('pong')
101 })
102
103 test('streams tool-call argument deltas', async () => {
104 test.setTimeout(180_000)
105
106 const filePath = 'AGENTS.md'
107 const chunks: Array<StreamChunk> = []
108 const stream = chat({
109 adapter: claudeCodeText('haiku', {
110 maxTurns: 2,
111 allowedTools: ['Read'],
112 disallowedTools: ['Bash', 'Write', 'Edit'],
113 }),
114 middleware: [withSandbox(sandbox)],
115 threadId: 'thread-claude-code-read',
116 messages: [
117 {
118 role: 'user',
119 content: `Use the Read tool exactly once to read ${filePath}.`,
120 },
121 ],
122 })
123
124 for await (const chunk of stream) chunks.push(chunk)
125
126 const start = chunks
127 .filter(isToolCallStart)
128 .find((chunk) => chunk.toolCallName === 'Read')
129 expect(start).toBeDefined()
130
131 const args = chunks
132 .filter(isToolCallArgs)
133 .filter((chunk) => chunk.toolCallId === start?.toolCallId)
134 expect(args.length).toBeGreaterThan(0)
135 expect(args.every((chunk) => !('args' in chunk))).toBe(true)
136 expect(args.map((chunk) => chunk.delta).join('')).toContain(filePath)
137
138 const endIndex = chunks.findIndex(
139 (chunk) => isToolCallEnd(chunk) && chunk.toolCallId === start?.toolCallId,
140 )
141 expect(endIndex).toBeGreaterThan(chunks.indexOf(args[0]))
142 })
143})