Open History to see the last commit
1/**
2 * Shared test doubles for the in-sandbox Claude Code adapter's real-sandbox
3 * tests (`text-adapter.test.ts`, `translate-determinism.test.ts`,
4 * `attach.test.ts`). Kept here, not inlined per-file, per the repo's
5 * test-hygiene rule on reusable test utilities.
6 */
7import {
8 SandboxCapability,
9 provideSandboxDurability,
10} from '@tanstack/ai-sandbox'
11import { InMemoryRunStore, memoryStream } from '@tanstack/ai'
12import type { InternalLogger } from '@tanstack/ai/adapter-internals'
13import type { CapabilityContext } from '@tanstack/ai'
14import type { SandboxHandle, SandboxRunDurability } from '@tanstack/ai-sandbox'
15
16export const noopLogger = {
17 request: () => {},
18 provider: () => {},
19 errors: () => {},
20 agentLoop: () => {},
21 warnings: () => {},
22 debug: () => {},
23} as unknown as InternalLogger
24
25/**
26 * Build a resolved `SandboxRunDurability` payload for a test, without going
27 * through `withSandbox`/`resolveSandboxDurability` (both belong to
28 * `@tanstack/ai-sandbox` and are not exported — a harness adapter only ever
29 * consumes the ALREADY-RESOLVED capability, never re-derives it). `runs` is a
30 * fresh `InMemoryRunStore` unless the caller supplies one (e.g. to assert on
31 * its contents afterwards); `adapter` is a fresh `memoryStream` keyed by the
32 * test's own `runId` unless the caller wants a specific fixture.
33 */
34export function fakeDurability(
35 runId: string,
36 overrides: Partial<SandboxRunDurability> = {},
37): SandboxRunDurability {
38 return {
39 runs: new InMemoryRunStore(),
40 adapter: memoryStream(new Request(`https://x/run?runId=${runId}`)),
41 journalDir: '/tmp/tanstack-runs',
42 attach: false,
43 detachOnDisconnect: true,
44 ...overrides,
45 }
46}
47
48/**
49 * Build a capability context that hands the adapter the given sandbox handle,
50 * and — when `durability` is supplied — the resolved `SandboxDurabilityCapability`
51 * a real `withSandbox(sandbox, { runs, durability })` would have provided.
52 * Omitting it entirely (not passing `durability: undefined`) matches how
53 * `withSandbox` behaves when an app never wires `runs`/`durability`.
54 */
55export function capabilityContextWith(
56 handle: SandboxHandle,
57 durability?: SandboxRunDurability,
58): CapabilityContext {
59 const [, provideSandbox] = SandboxCapability
60 const ctx = {
61 capabilities: { markProvided: () => {}, has: () => true },
62 } as unknown as CapabilityContext
63 provideSandbox(ctx, handle)
64 if (durability !== undefined) provideSandboxDurability(ctx, durability)
65 return ctx
66}
67
68// `@tanstack/ai-sandbox`'s journal reader picks a "follow" strategy
69// (`tail -c +N -f journal | base64`, piped through a spawned `SpawnHandle`)
70// whenever `capabilities.backgroundProcesses && capabilities.killableProcesses`
71// — true for `local-process`. On this host that pipeline's downstream
72// `base64` fully buffers its stdout (it isn't a tty and `tail -f` never
73// closes its input), so nothing is ever flushed to the reader for a payload
74// under one buffer's worth — the read hangs forever even though the journal
75// file itself is complete and correct. The bounded "poll" strategy
76// (`tail -c +N journal | base64`, no `-f`) has no such problem: each poll is a
77// one-shot process that flushes its buffer on exit. `journalReadStrategy`
78// decides purely from `handle.capabilities`, so reporting
79// `killableProcesses: false` on the handle we hand the adapter steers it onto
80// the poll path without touching `@tanstack/ai-sandbox` or the adapter itself
81// — every other operation (fs, process.exec, destroy) still goes through the
82// real local-process sandbox unchanged.
83/**
84 * Force the bounded-poll read strategy, and record every command the adapter
85 * spawns/execs so the journal wiring can be asserted from the command itself.
86 *
87 * Recording the command is the only durable way to prove journal options
88 * reached `spawnNdjson`: the journal is deleted once the run reaches its
89 * `{"__exit":N}` sentinel, so by the time a test can look, a correctly
90 * journaled run and an unjournaled one both leave no file behind.
91 */
92export function pollStrategyHandle(handle: SandboxHandle): {
93 handle: SandboxHandle
94 spawned: Array<string>
95} {
96 const spawned: Array<string> = []
97 return {
98 spawned,
99 handle: {
100 ...handle,
101 capabilities: { ...handle.capabilities, killableProcesses: false },
102 process: {
103 ...handle.process,
104 spawn: (command, options) => {
105 spawned.push(command)
106 return handle.process.spawn(command, options)
107 },
108 exec: (command, options) => {
109 spawned.push(command)
110 return handle.process.exec(command, options)
111 },
112 },
113 },
114 }
115}