TestChatReadsUsageTokens: tokens come from `usage.completion_tokens` (and prompt_tokens rides along for the debug-log calibration), not content length; we trust what the backend reports.
(t *testing.T)
| 393 | // prompt_tokens rides along for the debug-log calibration), not content |
| 394 | // length; we trust what the backend reports. |
| 395 | func TestChatReadsUsageTokens(t *testing.T) { |
| 396 | srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { |
| 397 | sseOK(w, []string{ |
| 398 | `{"choices":[{"delta":{"content":"` + strings.Repeat("x", 100) + `"}}],"usage":{"completion_tokens":7,"prompt_tokens":42}}`, |
| 399 | }) |
| 400 | })) |
| 401 | defer srv.Close() |
| 402 | var tokens, promptTokens int |
| 403 | for _, e := range collect(New(srv.URL, "m", "").Chat(context.Background(), nil, nil)) { |
| 404 | if e.Kind == EventDone { |
| 405 | tokens = e.Tokens |
| 406 | promptTokens = e.PromptTokens |
| 407 | } |
| 408 | } |
| 409 | if tokens != 7 { |
| 410 | t.Fatalf("expected tokens=7 from usage block, got %d", tokens) |
| 411 | } |
| 412 | if promptTokens != 42 { |
| 413 | t.Fatalf("expected prompt_tokens=42 from usage block, got %d", promptTokens) |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // TestSendEventUnblocksOnCancel pins sendEvent's anti-wedge invariant: once the |
| 418 | // parent context is cancelled, a send to an undrained channel must abort via the |