package traefikllmgateway import ( "errors" "fmt" "net/http/httptest" "strings" "testing" ) // assertAnthropicBlockSequence checks start/delta/stop nesting: a delta must // target the one open block, blocks never overlap, indexes run 0..n-1. // It returns the joined delta payload per block index. func assertAnthropicBlockSequence(t *testing.T, events []anthEvent) map[int]string { t.Helper() joined := map[int]string{} open := -1 next := 0 for i, e := range events { idx, _ := e.data["index"].(float64) switch e.name { case "content_block_start": if open != -1 { t.Fatalf("event %d: start %d while block %d open", i, int(idx), open) } if int(idx) != next { t.Fatalf("event %d: start index %d, want %d", i, int(idx), next) } open = int(idx) next++ case "content_block_delta": if int(idx) != open { t.Fatalf("event %d: delta for block %d while open block is %d", i, int(idx), open) } d := sub(e.data, "delta") for _, k := range []string{"partial_json", "text"} { if v, ok := d[k].(string); ok { joined[open] += v } } case "content_block_stop": if int(idx) != open { t.Fatalf("event %d: stop %d while open block is %d", i, int(idx), open) } open = -1 case "message_delta": if open != -1 { t.Fatalf("event %d: message_delta with block %d open", i, open) } } } return joined } func toolFrame(index int, id, name, args string) string { return fmt.Sprintf(`{"id":"c1","choices":[{"delta":{"tool_calls":[{"index":%d,"id":%q,"function":{"name":%q,"arguments":%q}}]}}]}`, index, id, name, args) } func textFrame(text string) string { return fmt.Sprintf(`{"id":"c1","choices":[{"delta":{"content":%q}}]}`, text) } func blockStarts(evs []anthEvent) []map[string]any { var out []map[string]any for _, e := range evs { if e.name == "content_block_start" { out = append(out, sub(e.data, "content_block")) } } return out } func TestAnthropicStreamWriter_InterleavedToolArgumentsStayOnOpenBlock(t *testing.T) { _, evs := runStreamWriter(t, usage{}, toolFrame(0, "call_a", "alpha", ""), toolFrame(1, "call_b", "beta", ""), toolFrame(0, "", "", `{"a":`), toolFrame(1, "", "", `{"b":2}`), toolFrame(0, "", "", `1}`), ) joined := assertAnthropicBlockSequence(t, evs) starts := blockStarts(evs) if len(starts) != 2 || starts[0]["name"] != "alpha" || starts[1]["name"] != "beta" { t.Fatalf("starts = %v", starts) } if joined[0] != `{"a":1}` || joined[1] != `{"b":2}` { t.Fatalf("joined = %v", joined) } } func TestAnthropicStreamWriter_TextAfterToolCallThenMoreArguments(t *testing.T) { _, evs := runStreamWriter(t, usage{}, toolFrame(0, "call_a", "alpha", `{"a":`), textFrame("hello "), textFrame("world"), toolFrame(0, "", "", `1}`), ) joined := assertAnthropicBlockSequence(t, evs) starts := blockStarts(evs) if len(starts) != 2 || starts[0]["type"] != "tool_use" || starts[1]["type"] != "text" { t.Fatalf("starts = %v", starts) } if joined[0] != `{"a":1}` || joined[1] != "hello world" { t.Fatalf("joined = %v", joined) } } func TestAnthropicStreamWriter_SequentialToolCallsKeepAllArguments(t *testing.T) { _, evs := runStreamWriter(t, usage{}, toolFrame(0, "call_a", "alpha", `{"a":1}`), toolFrame(1, "call_b", "beta", `{"b":2}`), ) joined := assertAnthropicBlockSequence(t, evs) starts := blockStarts(evs) if len(starts) != 2 || starts[0]["name"] != "alpha" || starts[1]["name"] != "beta" { t.Fatalf("starts = %v", starts) } if joined[0] != `{"a":1}` || joined[1] != `{"b":2}` { t.Fatalf("joined = %v", joined) } } func TestAnthropicStreamWriter_SingleToolCallStreamsLive(t *testing.T) { rec := httptest.NewRecorder() sw := newAnthropicStreamWriter(rec, "my-alias") for _, f := range []string{toolFrame(0, "call_a", "alpha", ""), toolFrame(0, "", "", `{"a":1}`)} { if _, err := sw.Write(oaiFrame(f)); err != nil { t.Fatal(err) } } if !strings.Contains(rec.Body.String(), `partial_json`) { t.Fatalf("no delta written before finish: %s", rec.Body.String()) } } func TestAnthropicStreamWriter_QueuedBlocksOverCapFail(t *testing.T) { rec := httptest.NewRecorder() sw := newAnthropicStreamWriter(rec, "my-alias") sw.queueCap = 8 if _, err := sw.Write(oaiFrame(toolFrame(0, "call_a", "alpha", ""))); err != nil { t.Fatal(err) } _, err := sw.Write(oaiFrame(textFrame("this text is longer than the cap"))) if !errors.Is(err, errUpstream) { t.Fatalf("err = %v, want errUpstream", err) } }