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 { StreamChunk } from '@tanstack/ai'
21
22const sandbox = defineSandbox({
23 id: 'claude-code-e2e',
24 provider: localProcessSandbox(),
25 workspace: {
26 source: { type: 'none' },
27 instructions: 'Claude Code streaming smoke test workspace.',
28 },
29 lifecycle: { reuse: 'none', snapshot: 'none', destroyOnComplete: true },
30 fileEvents: false,
31})
32
33test.describe('claude-code harness (gated live smoke)', () => {
34 test.skip(
35 !process.env.CLAUDE_CODE_E2E,
36 'Set CLAUDE_CODE_E2E=1 (plus ANTHROPIC_API_KEY or a local Claude login) to run the Claude Code live smoke test',
37 )
38
39 test('streams a full harness turn with session id and stop finish', async () => {
40 test.setTimeout(180_000)
41
42 const chunks: Array<StreamChunk> = []
43 const stream = chat({
44 adapter: claudeCodeText('haiku', {
45 maxTurns: 2,
46 // Read-only smoke: the default permission policy denies anything
47 // that would prompt, and no tools are bridged.
48 disallowedTools: ['Bash', 'Write', 'Edit'],
49 }),
50 middleware: [withSandbox(sandbox)],
51 messages: [
52 {
53 role: 'user',
54 content: 'Reply with exactly the word: pong',
55 },
56 ],
57 })
58
59 for await (const chunk of stream) {
60 chunks.push(chunk)
61 }
62
63 const types = chunks.map((chunk) => chunk.type as string)
64 expect(types[0]).toBe('RUN_STARTED')
65
66 const sessionEvent = chunks.find(
67 (chunk) =>
68 chunk.type === 'CUSTOM' &&
69 (chunk as { name?: string }).name === 'claude-code.session-id',
70 )
71 expect(sessionEvent).toBeDefined()
72 expect(
73 (sessionEvent as { value: { sessionId: string } }).value.sessionId,
74 ).toMatch(/.+/)
75
76 const finished = chunks.find((chunk) => chunk.type === 'RUN_FINISHED')
77 expect(finished).toBeDefined()
78 expect(
79 finished?.type === 'RUN_FINISHED' &&
80 finished.metadata?.tanstack?.finishReason,
81 ).toBe('stop')
82
83 const text = chunks
84 .filter((chunk) => chunk.type === 'TEXT_MESSAGE_CONTENT')
85 .map((chunk) => (chunk as { delta?: string }).delta ?? '')
86 .join('')
87 expect(text.toLowerCase()).toContain('pong')
88 })
89
90 test('streams tool-call argument deltas', async () => {
91 test.setTimeout(180_000)
92
93 const filePath = 'AGENTS.md'
94 const chunks: Array<StreamChunk> = []
95 const stream = chat({
96 adapter: claudeCodeText('haiku', {
97 maxTurns: 2,
98 allowedTools: ['Read'],
99 disallowedTools: ['Bash', 'Write', 'Edit'],
100 }),
101 middleware: [withSandbox(sandbox)],
102 messages: [
103 {
104 role: 'user',
105 content: `Use the Read tool exactly once to read ${filePath}.`,
106 },
107 ],
108 })
109
110 for await (const chunk of stream) chunks.push(chunk)
111
112 const start = chunks.find(
113 (chunk) =>
114 chunk.type === 'TOOL_CALL_START' && chunk.toolCallName === 'Read',
115 )
116 expect(start).toBeDefined()
117
118 const args = chunks.filter(
119 (chunk) =>
120 chunk.type === 'TOOL_CALL_ARGS' &&
121 chunk.toolCallId === start?.toolCallId,
122 )
123 expect(args.length).toBeGreaterThan(0)
124 expect(args.every((chunk) => !('args' in chunk))).toBe(true)
125 expect(args.map((chunk) => chunk.delta).join('')).toContain(filePath)
126
127 const endIndex = chunks.findIndex(
128 (chunk) =>
129 chunk.type === 'TOOL_CALL_END' &&
130 chunk.toolCallId === start?.toolCallId,
131 )
132 expect(endIndex).toBeGreaterThan(chunks.indexOf(args[0]))
133 })
134})