Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

claude-code.spec.ts

72 lines2.4 KB

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 type { StreamChunk } from '@tanstack/ai'
19​
20test.describe('claude-code harness (gated live smoke)', () => {
21 test.skip(
22 !process.env.CLAUDE_CODE_E2E,
23 'Set CLAUDE_CODE_E2E=1 (plus ANTHROPIC_API_KEY or a local Claude login) to run the Claude Code live smoke test',
24 )
25​
26 test('streams a full harness turn with session id and stop finish', async () => {
27 test.setTimeout(180_000)
28​
29 const chunks: Array<StreamChunk> = []
30 const stream = chat({
31 adapter: claudeCodeText('haiku', {
32 maxTurns: 2,
33 // Read-only smoke: the default permission policy denies anything
34 // that would prompt, and no tools are bridged.
35 disallowedTools: ['Bash', 'Write', 'Edit'],
36 }),
37 messages: [
38 {
39 role: 'user',
40 content: 'Reply with exactly the word: pong',
41 },
42 ],
43 })
44​
45 for await (const chunk of stream) {
46 chunks.push(chunk)
47 }
48​
49 const types = chunks.map((chunk) => chunk.type as string)
50 expect(types[0]).toBe('RUN_STARTED')
51​
52 const sessionEvent = chunks.find(
53 (chunk) =>
54 chunk.type === 'CUSTOM' &&
55 (chunk as { name?: string }).name === 'claude-code.session-id',
56 )
57 expect(sessionEvent).toBeDefined()
58 expect(
59 (sessionEvent as { value: { sessionId: string } }).value.sessionId,
60 ).toMatch(/.+/)
61​
62 const finished = chunks.find((chunk) => chunk.type === 'RUN_FINISHED')
63 expect(finished).toBeDefined()
64 expect((finished as { finishReason?: string }).finishReason).toBe('stop')
65​
66 const text = chunks
67 .filter((chunk) => chunk.type === 'TEXT_MESSAGE_CONTENT')
68 .map((chunk) => (chunk as { delta?: string }).delta ?? '')
69 .join('')
70 expect(text.toLowerCase()).toContain('pong')
71 })
72})