feat(ST-d9sl8egw): lib/insights/token-history.ts — sprint-historiek, dag-data & PBI-aggregaat (#115)
Drie functies via prisma.$queryRaw: getSprintTokenHistory (per-sprint aggregaat), getDayTokenData (dag-totalen met guard op lege sprintId), getPbiTokenAggregates (per-PBI met guard). Tests voor alle drie. Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
a2c8bd41af
commit
d819d29b04
2 changed files with 250 additions and 0 deletions
74
__tests__/lib/insights/token-history.test.ts
Normal file
74
__tests__/lib/insights/token-history.test.ts
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
|
||||
const { mockQueryRaw } = vi.hoisted(() => ({ mockQueryRaw: vi.fn() }))
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
prisma: { $queryRaw: mockQueryRaw },
|
||||
}))
|
||||
|
||||
import {
|
||||
getSprintTokenHistory,
|
||||
getDayTokenData,
|
||||
getPbiTokenAggregates,
|
||||
} from '@/lib/insights/token-history'
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('getSprintTokenHistory', () => {
|
||||
it('returns mapped sprint rows', async () => {
|
||||
mockQueryRaw.mockResolvedValueOnce([
|
||||
{ sprint_id: 'sp-1', sprint_goal: 'Goal A', total_tokens: BigInt(5000), total_cost: 0.1, job_count: BigInt(2) },
|
||||
])
|
||||
const rows = await getSprintTokenHistory('user-1')
|
||||
expect(rows).toHaveLength(1)
|
||||
expect(rows[0].sprintId).toBe('sp-1')
|
||||
expect(rows[0].totalTokens).toBe(5000)
|
||||
expect(rows[0].totalCostUsd).toBe(0.1)
|
||||
expect(rows[0].jobCount).toBe(2)
|
||||
})
|
||||
|
||||
it('returns zero cost when total_cost is null', async () => {
|
||||
mockQueryRaw.mockResolvedValueOnce([
|
||||
{ sprint_id: 'sp-2', sprint_goal: 'Goal B', total_tokens: BigInt(0), total_cost: null, job_count: BigInt(0) },
|
||||
])
|
||||
const rows = await getSprintTokenHistory('user-1')
|
||||
expect(rows[0].totalCostUsd).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getDayTokenData', () => {
|
||||
it('returns empty array for empty sprintId', async () => {
|
||||
const rows = await getDayTokenData('user-1', '')
|
||||
expect(rows).toHaveLength(0)
|
||||
expect(mockQueryRaw).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('maps day rows with ISO date string', async () => {
|
||||
mockQueryRaw.mockResolvedValueOnce([
|
||||
{ day: new Date('2026-05-01T00:00:00Z'), total_tokens: BigInt(2000), total_cost: 0.05 },
|
||||
])
|
||||
const rows = await getDayTokenData('user-1', 'sprint-1')
|
||||
expect(rows).toHaveLength(1)
|
||||
expect(rows[0].day).toBe('2026-05-01')
|
||||
expect(rows[0].totalTokens).toBe(2000)
|
||||
})
|
||||
})
|
||||
|
||||
describe('getPbiTokenAggregates', () => {
|
||||
it('returns empty array for empty sprintId', async () => {
|
||||
const rows = await getPbiTokenAggregates('user-1', '')
|
||||
expect(rows).toHaveLength(0)
|
||||
expect(mockQueryRaw).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('maps pbi rows', async () => {
|
||||
mockQueryRaw.mockResolvedValueOnce([
|
||||
{ pbi_id: 'pbi-1', pbi_code: 'M1', pbi_title: 'First PBI', total_tokens: BigInt(3000), total_cost: 0.08 },
|
||||
])
|
||||
const rows = await getPbiTokenAggregates('user-1', 'sprint-1')
|
||||
expect(rows[0].pbiCode).toBe('M1')
|
||||
expect(rows[0].totalTokens).toBe(3000)
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue