Skip to content

Commit a495bd8

Browse files
committed
test(claude): activate the budgeted raw-frame parser with unspaced fields (#1170)
The offset-based parser inside responsesSseToAnthropicSse was covered only indirectly. This drives it head-on: the same six Responses frames in spaced and unspaced form, asserting identical event names, identical translated text, and identical budget accounting. The budget assertion compares the two paths rather than asserting zero. This translator leaves a small residue at stream end on the spaced path too (51 bytes for this fixture), so zero would be asserting something that was never true. Equality is the contract the offset arithmetic must satisfy. Confirmed to fail when the offset fix is reverted.
1 parent 9c54e4e commit a495bd8

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

‎tests/claude-outbound.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ function dataOnlySse(data: Record<string, unknown>): string {
4646
return `data: ${JSON.stringify(data)}\n\n`;
4747
}
4848

49+
/** Unspaced counterparts of `sse` / `dataOnlySse` — `data:{...}` is as valid as `data: {...}` (#1170). */
50+
function unspacedSse(name: string, data: Record<string, unknown>): string {
51+
return `event:${name}\ndata:${JSON.stringify(data)}\n\n`;
52+
}
53+
4954
const DONE_SSE = "data: [DONE]\n\n";
5055

5156
function streamFrom(text: string): ReadableStream<Uint8Array> {
@@ -149,6 +154,48 @@ describe("claude outbound SSE", () => {
149154
expect(budget.snapshot().currentBytes).toBe(0);
150155
});
151156

157+
test("#1170: the budgeted raw-frame parser accepts unspaced event/data fields and still balances the budget", async () => {
158+
// This drives the offset-based parser inside responsesSseToAnthropicSse, which reserves and
159+
// releases translator budget by offset rather than by slicing each line. A spaced-only prefix
160+
// check dropped every frame here, producing an empty translation.
161+
const frames = [
162+
{ name: "response.created", data: { response: { id: "resp_1" } } },
163+
{ name: "response.output_item.added", data: { output_index: 0, item: { type: "message", id: "msg_1", role: "assistant" } } },
164+
{ name: "response.content_part.added", data: { item_id: "msg_1", output_index: 0, content_index: 0, part: { type: "output_text" } } },
165+
{ name: "response.output_text.delta", data: { item_id: "msg_1", output_index: 0, content_index: 0, delta: "unspaced" } },
166+
{ name: "response.output_item.done", data: { output_index: 0, item: { type: "message", id: "msg_1" } } },
167+
{ name: "response.completed", data: { response: { status: "completed", usage: { input_tokens: 5, output_tokens: 2 } } } },
168+
];
169+
170+
const spacedBudget = createTestTranslatorBudget();
171+
const spaced = await collectEvents(responsesSseToAnthropicSse(
172+
streamFrom(frames.map(f => sse(f.name, f.data)).join("")),
173+
"claude-ocx-test",
174+
{ translatorBudget: spacedBudget },
175+
));
176+
177+
const unspacedBudget = createTestTranslatorBudget();
178+
const unspaced = await collectEvents(responsesSseToAnthropicSse(
179+
streamFrom(frames.map(f => unspacedSse(f.name, f.data)).join("")),
180+
"claude-ocx-test",
181+
{ translatorBudget: unspacedBudget },
182+
));
183+
184+
const textOf = (events: { name: string; data: Record<string, any> }[]) => events
185+
.filter(e => e.name === "content_block_delta")
186+
.map(e => e.data?.delta?.text ?? "")
187+
.join("");
188+
189+
expect(textOf(spaced)).toBe("unspaced");
190+
expect(unspaced.map(e => e.name)).toEqual(spaced.map(e => e.name));
191+
expect(textOf(unspaced)).toBe(textOf(spaced));
192+
// The offset arithmetic must not change accounting: the unspaced path retains exactly what
193+
// the spaced path retains. (Both leave a small non-zero residue at stream end; that is
194+
// pre-existing behavior of this translator, not something this fix introduces — asserting
195+
// equality is the contract that matters here.)
196+
expect(unspacedBudget.snapshot().currentBytes).toBe(spacedBudget.snapshot().currentBytes);
197+
});
198+
152199
test("text + thinking + tool call + completed w/ usage -> exact Anthropic sequence", async () => {
153200
const upstream = [
154201
sse("response.created", { response: { id: "resp_1", status: "in_progress" } }),

0 commit comments

Comments
 (0)