test(sdk): unit tests for ChatHandle (usage aggregation, permission, lifecycle)#13
Merged
Merged
Conversation
19 tests covering the parts of ChatHandle that are easy to get subtly
wrong without a live harness:
Usage aggregation
- input/output tokens always SUM across snapshots
- cache creation + cache read tokens SUM
- cost cumulative semantic → MAX
- cost delta semantic → SUM
- undefined semantic defaults to cumulative (safe)
- mixed cumulative + delta in one turn → prefer cumulative (no double-count)
- no cost snapshots → costUsd is undefined
- getUsage() consistent mid-stream and after drain
Dual iteration interface
- for-await yields events in order
- await handle drains to ChatResult
- result() is memoized (no re-iteration)
- drain() throws if stream closes without ca_session_ended
Permission callback
- onPermissionRequest called with exact event args; decision POSTed
- default decision is allow when no hook provided
- custom deny decision is forwarded
Lifecycle
- onComplete fires after ca_session_ended (once)
- onComplete NOT fired when stream never ends
- sdk_message payloads collected into result.messages in order
- cancel() POSTs /cancel for the right session
All assertions use vi.fn mocks — no network, no harness, no subprocess.
Suite runs in <25ms. Full SDK suite: 73 passing, 8 skipped (offline),
0 failures.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Backfills unit coverage for
packages/sdk/src/chat-handle.ts— currently the largest untested SDK file (245 LOC) and the one with the subtlest hidden invariant (cumulative-vs-delta cost-semantic aggregation).What's covered (19 tests)
costSemantic(cumulative → MAX, delta → SUM, mixed → prefer cumulative, undefined → safe default). Also coversgetUsage()consistency mid-stream vs after drain.for awaityields raw events;await handledrains to ChatResult;result()is memoized;drain()throws if the stream closes withoutca_session_ended.onPermissionRequestis called with exact event args, decision is POSTed to the harness, default decision isallow, customdenyis forwarded.onCompletefires once after end; doesn't fire on broken streams;sdk_messagepayloads collected in order;cancel()POSTs the right URL.Style
Pure mocks (
vi.fn, an async generator for the event stream, a fakefetchImpl). No subprocess, no network. Whole file runs in ~22ms.Verification
(The 8 skipped tests are the live-API substrate matrix — gated on
ANTHROPIC_API_KEY. Unchanged from main.)