TestChatStreamsContent: content deltas merge into one final string.
(t *testing.T)
| 39 | |
| 40 | // TestChatStreamsContent: content deltas merge into one final string. |
| 41 | func TestChatStreamsContent(t *testing.T) { |
| 42 | var gotAuth, gotBody string |
| 43 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 44 | gotAuth = r.Header.Get("Authorization") |
| 45 | b, _ := io.ReadAll(r.Body) |
| 46 | gotBody = string(b) |
| 47 | sseOK(w, []string{ |
| 48 | `{"choices":[{"delta":{"content":"Hel"}}]}`, |
| 49 | `{"choices":[{"delta":{"content":"lo"}}],"usage":{"completion_tokens":7}}`, |
| 50 | }) |
| 51 | })) |
| 52 | defer srv.Close() |
| 53 | |
| 54 | c := New(srv.URL, "test-model", "sk-xyz") |
| 55 | events := collect(c.Chat(context.Background(), |
| 56 | []chmctx.Message{{Role: chmctx.RoleUser, Content: "hi"}}, nil)) |
| 57 | |
| 58 | if gotAuth != "Bearer sk-xyz" { |
| 59 | t.Fatalf("auth header missing: %q", gotAuth) |
| 60 | } |
| 61 | if !strings.Contains(gotBody, `"model":"test-model"`) { |
| 62 | t.Fatalf("model missing from request: %s", gotBody) |
| 63 | } |
| 64 | if !strings.Contains(gotBody, `"reasoning_effort":"medium"`) { |
| 65 | t.Fatalf("reasoning_effort must default to 'medium' (decode is the serialised critical path): %s", gotBody) |
| 66 | } |
| 67 | |
| 68 | var content strings.Builder |
| 69 | var sawDone bool |
| 70 | for _, e := range events { |
| 71 | switch e.Kind { |
| 72 | case EventContent: |
| 73 | content.WriteString(e.Content) |
| 74 | case EventDone: |
| 75 | sawDone = true |
| 76 | if e.Final == nil || e.Final.Content != "Hello" { |
| 77 | t.Errorf("final content wrong: %+v", e.Final) |
| 78 | } |
| 79 | if e.Tokens != 7 { |
| 80 | t.Errorf("tokens = %d, want 7", e.Tokens) |
| 81 | } |
| 82 | if !e.Budget.Set || e.Budget.Remaining != 0.73 { |
| 83 | t.Errorf("budget not propagated: %+v", e.Budget) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | if content.String() != "Hello" { |
| 88 | t.Fatalf("content = %q", content.String()) |
| 89 | } |
| 90 | if !sawDone { |
| 91 | t.Fatal("no done event") |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // TestChatToolCall: tool_calls in a delta emit EventToolCall and ride along in |
| 96 | // EventDone.Final.ToolCalls so the next turn can replay the assistant message. |