(t *testing.T)
| 17 | ) |
| 18 | |
| 19 | func TestParseTokenUsageFile(t *testing.T) { |
| 20 | t.Run("valid single entry", func(t *testing.T) { |
| 21 | tmpDir := testutil.TempDir(t, "token-usage") |
| 22 | filePath := filepath.Join(tmpDir, "token-usage.jsonl") |
| 23 | |
| 24 | content := `{"timestamp":"2026-04-01T17:56:38.042Z","request_id":"abc-123","provider":"anthropic","model":"claude-sonnet-4-6","path":"/v1/messages","status":200,"streaming":true,"input_tokens":100,"output_tokens":200,"cache_read_tokens":5000,"cache_write_tokens":3000,"duration_ms":2500,"response_bytes":1500}` |
| 25 | require.NoError(t, os.WriteFile(filePath, []byte(content+"\n"), 0o644), "should write test file") |
| 26 | |
| 27 | summary, err := parseTokenUsageFile(filePath) |
| 28 | require.NoError(t, err, "should parse without error") |
| 29 | require.NotNil(t, summary, "should return non-nil summary") |
| 30 | |
| 31 | assert.Equal(t, 100, summary.TotalInputTokens, "input tokens") |
| 32 | assert.Equal(t, 200, summary.TotalOutputTokens, "output tokens") |
| 33 | assert.Equal(t, 5000, summary.TotalCacheReadTokens, "cache read tokens") |
| 34 | assert.Equal(t, 3000, summary.TotalCacheWriteTokens, "cache write tokens") |
| 35 | assert.Equal(t, 1, summary.TotalRequests, "total requests") |
| 36 | assert.Equal(t, 2500, summary.TotalDurationMs, "total duration ms") |
| 37 | assert.Equal(t, 1500, summary.TotalResponseBytes, "total response bytes") |
| 38 | |
| 39 | // Check by-model breakdown |
| 40 | require.Contains(t, summary.ByModel, "claude-sonnet-4-6", "should have model entry") |
| 41 | model := summary.ByModel["claude-sonnet-4-6"] |
| 42 | assert.Equal(t, "anthropic", model.Provider, "model provider") |
| 43 | assert.Equal(t, 100, model.InputTokens, "model input tokens") |
| 44 | assert.Equal(t, 200, model.OutputTokens, "model output tokens") |
| 45 | }) |
| 46 | |
| 47 | t.Run("multiple entries with multiple models", func(t *testing.T) { |
| 48 | tmpDir := testutil.TempDir(t, "token-usage") |
| 49 | filePath := filepath.Join(tmpDir, "token-usage.jsonl") |
| 50 | |
| 51 | content := `{"timestamp":"2026-04-01T17:56:38.042Z","request_id":"1","provider":"anthropic","model":"claude-sonnet-4-6","path":"/v1/messages","status":200,"streaming":true,"input_tokens":3,"output_tokens":414,"cache_read_tokens":14044,"cache_write_tokens":26035,"duration_ms":6383,"response_bytes":2843} |
| 52 | {"timestamp":"2026-04-01T17:57:00.000Z","request_id":"2","provider":"anthropic","model":"claude-sonnet-4-6","path":"/v1/messages","status":200,"streaming":true,"input_tokens":3,"output_tokens":450,"cache_read_tokens":40984,"cache_write_tokens":0,"duration_ms":4000,"response_bytes":3000} |
| 53 | {"timestamp":"2026-04-01T17:58:00.000Z","request_id":"3","provider":"anthropic","model":"claude-haiku-4-5","path":"/v1/messages","status":200,"streaming":false,"input_tokens":769,"output_tokens":86,"cache_read_tokens":0,"cache_write_tokens":0,"duration_ms":700,"response_bytes":500}` |
| 54 | require.NoError(t, os.WriteFile(filePath, []byte(content+"\n"), 0o644), "should write test file") |
| 55 | |
| 56 | summary, err := parseTokenUsageFile(filePath) |
| 57 | require.NoError(t, err, "should parse without error") |
| 58 | require.NotNil(t, summary, "should return non-nil summary") |
| 59 | |
| 60 | assert.Equal(t, 775, summary.TotalInputTokens, "total input tokens") |
| 61 | assert.Equal(t, 950, summary.TotalOutputTokens, "total output tokens") |
| 62 | assert.Equal(t, 55028, summary.TotalCacheReadTokens, "total cache read tokens") |
| 63 | assert.Equal(t, 26035, summary.TotalCacheWriteTokens, "total cache write tokens") |
| 64 | assert.Equal(t, 3, summary.TotalRequests, "total requests") |
| 65 | assert.Equal(t, 11083, summary.TotalDurationMs, "total duration ms") |
| 66 | |
| 67 | // Check by-model |
| 68 | require.Len(t, summary.ByModel, 2, "should have 2 models") |
| 69 | assert.Equal(t, 2, summary.ByModel["claude-sonnet-4-6"].Requests, "sonnet requests") |
| 70 | assert.Equal(t, 1, summary.ByModel["claude-haiku-4-5"].Requests, "haiku requests") |
| 71 | |
| 72 | assert.InDelta(t, 0.0, summary.CacheEfficiency, 0.001, "cache efficiency is not computed from raw token counts") |
| 73 | }) |
| 74 | |
| 75 | t.Run("extracts ambient context from first chronological invocation", func(t *testing.T) { |
| 76 | tmpDir := testutil.TempDir(t, "token-usage") |
nothing calls this directly
no test coverage detected