|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + type AgentAnalyticsRaw, |
| 4 | + buildAgentAnalyticsQueries, |
| 5 | + EMPTY_AGENT_ANALYTICS, |
| 6 | + type HogQLGrid, |
| 7 | + shapeAgentAnalytics, |
| 8 | +} from "./agent-analytics"; |
| 9 | + |
| 10 | +const grid = (results: unknown[][]): HogQLGrid => ({ results, columns: [] }); |
| 11 | + |
| 12 | +// A 14-day daily series where every day is identical, so prior(7) === recent(7) |
| 13 | +// → zero deltas. Columns: [day, cost, sessions, errors, generations]. |
| 14 | +function flatDaily(): unknown[][] { |
| 15 | + return Array.from({ length: 14 }, (_, i) => [ |
| 16 | + `2026-06-${String(i + 1).padStart(2, "0")}T00:00:00`, |
| 17 | + 2, // cost |
| 18 | + 5, // sessions |
| 19 | + 1, // errors |
| 20 | + 10, // generations |
| 21 | + ]); |
| 22 | +} |
| 23 | + |
| 24 | +describe("buildAgentAnalyticsQueries", () => { |
| 25 | + it("scopes to agent-platform origin only when no application id", () => { |
| 26 | + const q = buildAgentAnalyticsQueries(); |
| 27 | + expect(q.kpi).toContain("$ai_origin = 'agent_platform_runner'"); |
| 28 | + expect(q.kpi).not.toContain("$agent_application_id ="); |
| 29 | + expect(q.kpi).toContain("event = '$ai_generation'"); |
| 30 | + expect(q.toolErrors).toContain("event = '$ai_span'"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("narrows to a single application id when given", () => { |
| 34 | + const id = "11111111-2222-3333-4444-555566667777"; |
| 35 | + const q = buildAgentAnalyticsQueries(id); |
| 36 | + expect(q.kpi).toContain(`properties.$agent_application_id = '${id}'`); |
| 37 | + expect(q.byModel).toContain(`properties.$agent_application_id = '${id}'`); |
| 38 | + }); |
| 39 | + |
| 40 | + it("rejects a non-uuid application id", () => { |
| 41 | + expect(() => buildAgentAnalyticsQueries("app-uuid-123")).toThrow( |
| 42 | + /must be a UUID/, |
| 43 | + ); |
| 44 | + }); |
| 45 | +}); |
| 46 | + |
| 47 | +describe("shapeAgentAnalytics", () => { |
| 48 | + it("returns an empty board for empty grids", () => { |
| 49 | + const out = shapeAgentAnalytics({}); |
| 50 | + expect(out.empty).toBe(true); |
| 51 | + expect(out.kpis).toEqual(EMPTY_AGENT_ANALYTICS.kpis); |
| 52 | + expect(out.byAgent).toEqual([]); |
| 53 | + expect(out.deltas).toEqual({ |
| 54 | + spend: null, |
| 55 | + sessions: null, |
| 56 | + failureRatePoints: null, |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it("derives KPIs incl. failure rate from generations", () => { |
| 61 | + const raw: Partial<AgentAnalyticsRaw> = { |
| 62 | + // cost, sessions, errors, generations, p95 |
| 63 | + kpi: grid([[12.5, 8, 3, 12, 4.2]]), |
| 64 | + }; |
| 65 | + const out = shapeAgentAnalytics(raw); |
| 66 | + expect(out.kpis.spendUsd).toBe(12.5); |
| 67 | + expect(out.kpis.sessions).toBe(8); |
| 68 | + expect(out.kpis.failureRate).toBeCloseTo(3 / 12); |
| 69 | + expect(out.kpis.p95LatencyS).toBe(4.2); |
| 70 | + expect(out.empty).toBe(false); |
| 71 | + }); |
| 72 | + |
| 73 | + it("coerces numeric strings (HogQL returns decimals as strings)", () => { |
| 74 | + const out = shapeAgentAnalytics({ |
| 75 | + kpi: grid([["1.50", "4", "0", "4", "2"]]), |
| 76 | + }); |
| 77 | + expect(out.kpis.spendUsd).toBe(1.5); |
| 78 | + expect(out.kpis.sessions).toBe(4); |
| 79 | + expect(out.kpis.failureRate).toBe(0); |
| 80 | + }); |
| 81 | + |
| 82 | + it("builds a 14-day daily series with zero deltas for a flat trend", () => { |
| 83 | + const out = shapeAgentAnalytics({ daily: grid(flatDaily()) }); |
| 84 | + expect(out.daily.labels).toHaveLength(14); |
| 85 | + expect(out.daily.spend).toHaveLength(14); |
| 86 | + expect(out.daily.failureRate.every((r) => r === 0.1)).toBe(true); |
| 87 | + // prior 7 === recent 7 → 0% change, and failure-rate delta is 0pp. |
| 88 | + expect(out.deltas.spend).toBe(0); |
| 89 | + expect(out.deltas.sessions).toBe(0); |
| 90 | + expect(out.deltas.failureRatePoints).toBe(0); |
| 91 | + }); |
| 92 | + |
| 93 | + it("computes a positive spend delta when recent exceeds prior", () => { |
| 94 | + // 7 days at cost 1, then 7 days at cost 3 → +200%. |
| 95 | + const days = Array.from({ length: 14 }, (_, i) => [ |
| 96 | + `2026-06-${String(i + 1).padStart(2, "0")}T00:00:00`, |
| 97 | + i < 7 ? 1 : 3, |
| 98 | + 1, |
| 99 | + 0, |
| 100 | + 1, |
| 101 | + ]); |
| 102 | + const out = shapeAgentAnalytics({ daily: grid(days) }); |
| 103 | + expect(out.deltas.spend).toBeCloseTo(200); |
| 104 | + }); |
| 105 | + |
| 106 | + it("maps per-agent rows and resolves names via the id→name map", () => { |
| 107 | + const raw: Partial<AgentAnalyticsRaw> = { |
| 108 | + // agent_id, sessions, generations, cost, tokens, errors, p95 |
| 109 | + perAgent: grid([ |
| 110 | + ["11111111-2222-3333-4444-555566667777", 5, 10, 4, 2000, 2, 1.5], |
| 111 | + ["aaaa", 1, 4, 0.5, 100, 0, 0.2], |
| 112 | + ]), |
| 113 | + }; |
| 114 | + const names = new Map([ |
| 115 | + ["11111111-2222-3333-4444-555566667777", "Support Bot"], |
| 116 | + ]); |
| 117 | + const out = shapeAgentAnalytics(raw, names); |
| 118 | + expect(out.byAgent[0]).toMatchObject({ |
| 119 | + name: "Support Bot", |
| 120 | + sessions: 5, |
| 121 | + spendUsd: 4, |
| 122 | + tokens: 2000, |
| 123 | + p95LatencyS: 1.5, |
| 124 | + }); |
| 125 | + expect(out.byAgent[0].failureRate).toBeCloseTo(2 / 10); |
| 126 | + // Unknown id falls back to a short id. |
| 127 | + expect(out.byAgent[1].name).toBe("aaaa"); |
| 128 | + }); |
| 129 | + |
| 130 | + it("maps model spend and tool error rates", () => { |
| 131 | + const out = shapeAgentAnalytics({ |
| 132 | + byModel: grid([["claude-opus-4-8", 9.99, 42]]), |
| 133 | + toolErrors: grid([ |
| 134 | + ["search", 20, 4], |
| 135 | + ["fetch", 5, 0], |
| 136 | + ]), |
| 137 | + }); |
| 138 | + expect(out.byModel[0]).toEqual({ |
| 139 | + model: "claude-opus-4-8", |
| 140 | + spendUsd: 9.99, |
| 141 | + calls: 42, |
| 142 | + }); |
| 143 | + expect(out.toolErrors[0].errorRate).toBeCloseTo(4 / 20); |
| 144 | + expect(out.toolErrors[1].errorRate).toBe(0); |
| 145 | + }); |
| 146 | + |
| 147 | + it("ignores non-array rows defensively", () => { |
| 148 | + const out = shapeAgentAnalytics({ |
| 149 | + kpi: grid([[1, 1, 0, 1, 1]]), |
| 150 | + perAgent: { |
| 151 | + results: [null, "oops"] as unknown as unknown[][], |
| 152 | + columns: [], |
| 153 | + }, |
| 154 | + }); |
| 155 | + expect(out.byAgent).toEqual([]); |
| 156 | + expect(out.empty).toBe(false); |
| 157 | + }); |
| 158 | +}); |
0 commit comments