Files
insights/products/mcp_analytics/frontend/mcpAnalyticsToolQualityLogic.test.ts
zeekay 1b367e2b62 chore: rebase the tree onto upstream master
The tree was a debranded snapshot of upstream taken around 2026-02-20, and
upstream has moved about five months since. This replaces it with upstream
master, mechanically re-derived: bin/debrand does the rename, and the
pipeline and squashed migrations are restored from the shipped tree.

Not a merge. The previous import replaced the tree wholesale in a single
commit, so there is no shared content history to merge against. Reproducing
the rename on the new tree is what actually converges.

What survives: .hanzo/workflows, the Dockerfiles and bin/docker* entrypoints,
compose files, LLM.md, and insights/migrations. The squashed initial
migration is load-bearing — the live database records it by name, so
upstream's ~918 individual migrations cannot come back.

Nine products are dropped as unreferenced. Twenty-one more that we do not run
are kept anyway because the core app still imports them; unpicking those is a
refactor and does not belong in a rebase.

This base does not build yet, and the reasons are not in the rename:
upstream now wants Python 3.13.13 against our 3.12.12 and Django 5.2 against
our 4.2, and the dependency manifests still name upstream packages that we
republish under our own names. Those are decisions, not renames.
2026-08-01 15:44:40 -07:00

193 lines
7.8 KiB
TypeScript

import { expectLogic } from 'kea-test-utils'
import api from 'lib/api'
import { initKeaTests } from '~/test/init'
import { formatMsAsSeconds } from './dashboard/formatters'
import { type DailyToolStat, buildDailyChartData, mcpAnalyticsToolQualityLogic } from './mcpAnalyticsToolQualityLogic'
jest.mock('lib/api')
jest.mock('~/queries/query', () => ({
insightsqlQuery: jest.fn().mockResolvedValue({ results: [] }),
}))
const mockApi = api as jest.Mocked<typeof api>
function dailyStat(overrides: Partial<DailyToolStat> & { day: string }): DailyToolStat {
return { calls: 0, errors: 0, p50: 0, p95: 0, p99: 0, ...overrides }
}
describe('mcpAnalyticsToolQualityLogic', () => {
describe('buildDailyChartData', () => {
it('projects rows onto the bucket keys, zero/NaN-filling gaps', () => {
const data = buildDailyChartData(
[
dailyStat({ day: '2026-06-05', calls: 100, errors: 10, p50: 200, p95: 900, p99: 2100 }),
dailyStat({ day: '2026-06-07', calls: 50, errors: 0, p50: 150, p95: 800, p99: 1500 }),
],
['2026-06-05 00:00:00', '2026-06-06 00:00:00', '2026-06-07 00:00:00']
)
expect(data.labels).toEqual(['2026-06-05 00:00:00', '2026-06-06 00:00:00', '2026-06-07 00:00:00'])
expect(data.calls).toEqual([100, 0, 50])
expect(data.errors).toEqual([10, 0, 0])
// Gap buckets get NaN (skipped by the chart), not a dip to zero.
expect(data.successRate[0]).toBeCloseTo(90)
expect(data.successRate[1]).toBeNaN()
expect(data.p99).toEqual([2100, NaN, 1500])
})
it('returns empty series for empty bucket keys', () => {
expect(buildDailyChartData([], []).labels).toEqual([])
})
// Sub-day windows bucket by hour: rows keyed to an hour must line up with hourly keys, so the
// "12 hours collapses to a single point" bug can't come back.
it('lines up hourly rows with hourly bucket keys', () => {
const data = buildDailyChartData(
[dailyStat({ day: '2026-06-07 10:00:00', calls: 12, errors: 3, p50: 80, p95: 200, p99: 400 })],
['2026-06-07 09:00:00', '2026-06-07 10:00:00', '2026-06-07 11:00:00']
)
expect(data.calls).toEqual([0, 12, 0])
expect(data.errors).toEqual([0, 3, 0])
expect(data.successRate[1]).toBeCloseTo(75)
})
})
describe('formatMsAsSeconds', () => {
it.each([
[0, '0'],
[50, '50ms'],
[500, '0.5s'],
[1000, '1s'],
[99, '99ms'],
[100, '0.1s'],
[1500, '1.5s'],
[2000, '2s'],
[NaN, '—'],
[Infinity, '—'],
])('formats %s ms as %s', (input, expected) => {
expect(formatMsAsSeconds(input)).toBe(expected)
})
})
describe('incompleteTail', () => {
beforeEach(() => {
jest.clearAllMocks()
initKeaTests()
jest.spyOn(mockApi, 'query').mockResolvedValue({ results: [] })
})
// An open-ended relative window always ends in the bucket that is still collecting, and a
// window that closed in the past never does, so this holds whenever the suite runs.
it.each([
['an open-ended window', '-7d', null, true],
['a window that already closed', '2026-06-01', '2026-06-10', false],
])('is %s: %s', async (_label, dateFrom, dateTo, expected) => {
const logic = mcpAnalyticsToolQualityLogic()
logic.mount()
await expectLogic(logic, () => {
logic.actions.setDateFilter(dateFrom, dateTo)
}).toFinishAllListeners()
expect(logic.values.incompleteTail).toBe(expected)
})
})
describe('date range and tool filters', () => {
beforeEach(() => {
jest.clearAllMocks()
initKeaTests()
jest.spyOn(mockApi, 'query').mockResolvedValue({ results: [] })
})
function queryCallsSince(callIndex: number): Record<string, any>[] {
return mockApi.query.mock.calls.slice(callIndex).map((call) => call[0] as any)
}
it('reloads the tool rows, daily stats and category counts with the new date range when the date filter changes', async () => {
const logic = mcpAnalyticsToolQualityLogic()
logic.mount()
await expectLogic(logic).toFinishAllListeners()
const callsBefore = mockApi.query.mock.calls.length
await expectLogic(logic, () => {
logic.actions.setDateFilter('-30d', null)
}).toFinishAllListeners()
const newCalls = queryCallsSince(callsBefore)
expect(newCalls.length).toBe(3) // tool rows + daily stats + category counts
// The scope-share headline must track the same window as the rest of the tab.
expect(newCalls.map((call) => call.dateRange)).toEqual([
{ date_from: '-30d', date_to: null },
{ date_from: '-30d', date_to: null },
{ date_from: '-30d', date_to: null },
])
})
it('reloads daily stats scoped to the tool when one is selected, passing it as a typed param', async () => {
const logic = mcpAnalyticsToolQualityLogic()
logic.mount()
await expectLogic(logic).toFinishAllListeners()
const callsBefore = mockApi.query.mock.calls.length
await expectLogic(logic, () => {
logic.actions.setSelectedTool("evil'); DROP TABLE events; --")
}).toFinishAllListeners()
const newCalls = queryCallsSince(callsBefore)
expect(newCalls.length).toBe(1) // daily stats only
// The tool rides as a typed toolName param (the runner binds it as a constant), never as raw SQL.
expect(newCalls[0].toolName).toBe("evil'); DROP TABLE events; --")
expect(newCalls[0].query).toBeUndefined()
})
// Tool-quality row item as returned by the runner.
const toolRowResult = (tool: string): Record<string, unknown> => ({
tool,
total_calls: 1,
errors: 0,
error_rate_pct: 0,
p50_duration_ms: 0,
p95_duration_ms: 0,
p99_duration_ms: 0,
users: 0,
sessions: 0,
first_seen: '',
last_seen: '',
})
it('clears the selected tool when a reload no longer includes it', async () => {
mockApi.query.mockResolvedValue({ results: [toolRowResult('tool_a')] })
const logic = mcpAnalyticsToolQualityLogic()
logic.mount()
await expectLogic(logic).toFinishAllListeners()
logic.actions.setSelectedTool('tool_a')
await expectLogic(logic).toFinishAllListeners()
expect(logic.values.selectedTool).toBe('tool_a')
mockApi.query.mockResolvedValue({ results: [toolRowResult('tool_b')] })
await expectLogic(logic, () => {
logic.actions.setSelectedCategories(['some-category'])
}).toFinishAllListeners()
expect(logic.values.selectedTool).toBeNull()
})
it('keeps the selected tool when a reload still includes it', async () => {
mockApi.query.mockResolvedValue({ results: [toolRowResult('tool_a'), toolRowResult('tool_b')] })
const logic = mcpAnalyticsToolQualityLogic()
logic.mount()
await expectLogic(logic).toFinishAllListeners()
logic.actions.setSelectedTool('tool_a')
await expectLogic(logic, () => {
logic.actions.setSelectedCategories(['some-category'])
}).toFinishAllListeners()
expect(logic.values.selectedTool).toBe('tool_a')
})
})
})