MCPcopy Index your code
hub / github.com/codehamr/codehamr / TestChatReadsUsageTokens

Function TestChatReadsUsageTokens

internal/llm/llm_test.go:395–415  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

393// prompt_tokens rides along for the debug-log calibration), not content
394// length; we trust what the backend reports.
395func 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

Callers

nothing calls this directly

Calls 4

sseOKFunction · 0.85
collectFunction · 0.85
ChatMethod · 0.80
NewFunction · 0.70

Tested by

no test coverage detected