import { describe, expect, it } from 'vitest'; import type { Tx } from './types.js'; import { dashboard } from './analytics.js'; // Minimal in-memory tx double exercising the dashboard() aggregator. // We only stub the calls dashboard() actually makes; other Prisma methods remain unimplemented. function makeTx(args: { partCount: number; stateRows: { state: string; count: number; totalPrice: number }[]; parts: { id: string; state: string; binId: string | null; createdAt: Date; partModelId: string; }[]; openFms: number; eolPartModels: { id: string; mpn: string; eolDate: Date | null; manufacturerId: string; manufacturer: { name: string }; }[]; bins: { id: string; name: string; room: { name: string; site: { name: string } } }[]; }): Tx { const tx = { part: { count: async () => args.partCount, groupBy: async () => args.stateRows.map((s) => ({ state: s.state, _count: { _all: s.count }, _sum: { price: s.totalPrice }, })), findMany: async () => args.parts, }, fm: { count: async () => args.openFms, }, partModel: { findMany: async () => args.eolPartModels, }, bin: { findMany: async () => args.bins, }, }; return tx as unknown as Tx; } const now = new Date('2026-04-16T00:00:00.000Z'); const daysAgo = (n: number) => new Date(now.getTime() - n * 24 * 60 * 60 * 1000); describe('analytics.dashboard', () => { it('aggregates totals, state counts and open FMs', async () => { const tx = makeTx({ partCount: 5, stateRows: [ { state: 'SPARE', count: 3, totalPrice: 1500 }, { state: 'DEPLOYED', count: 2, totalPrice: 8000 }, ], parts: [], openFms: 4, eolPartModels: [], bins: [], }); const r = await dashboard(tx); expect(r.totalParts).toBe(5); expect(r.openFms).toBe(4); expect(r.byState).toEqual([ { state: 'SPARE', count: 3, totalPrice: 1500 }, { state: 'DEPLOYED', count: 2, totalPrice: 8000 }, ]); }); it('buckets parts by age correctly', async () => { const tx = makeTx({ partCount: 4, stateRows: [], parts: [ { id: 'p1', state: 'SPARE', binId: null, createdAt: daysAgo(10), partModelId: 'pm' }, { id: 'p2', state: 'SPARE', binId: null, createdAt: daysAgo(60), partModelId: 'pm' }, { id: 'p3', state: 'SPARE', binId: null, createdAt: daysAgo(400), partModelId: 'pm' }, { id: 'p4', state: 'SPARE', binId: null, createdAt: daysAgo(900), partModelId: 'pm' }, ], openFms: 0, eolPartModels: [], bins: [], }); const r = await dashboard(tx); const byLabel = Object.fromEntries(r.ageBuckets.map((b) => [b.label, b.count])); expect(byLabel['0–30d']).toBe(1); expect(byLabel['31–90d']).toBe(1); expect(byLabel['1–2y']).toBe(1); expect(byLabel['2y+']).toBe(1); // totals should match expect(r.ageBuckets.reduce((s, b) => s + b.count, 0)).toBe(4); }); it('ranks top bins and labels them site/room/bin', async () => { const tx = makeTx({ partCount: 4, stateRows: [], parts: [ { id: '1', state: 'SPARE', binId: 'b1', createdAt: daysAgo(1), partModelId: 'pm' }, { id: '2', state: 'SPARE', binId: 'b1', createdAt: daysAgo(1), partModelId: 'pm' }, { id: '3', state: 'SPARE', binId: 'b2', createdAt: daysAgo(1), partModelId: 'pm' }, { id: '4', state: 'SPARE', binId: null, createdAt: daysAgo(1), partModelId: 'pm' }, ], openFms: 0, eolPartModels: [], bins: [ { id: 'b1', name: 'A1', room: { name: 'Lab', site: { name: 'HQ' } } }, { id: 'b2', name: 'B2', room: { name: 'Lab', site: { name: 'HQ' } } }, ], }); const r = await dashboard(tx); expect(r.topBins).toEqual([ { binId: 'b1', label: 'HQ / Lab / A1', count: 2 }, { binId: 'b2', label: 'HQ / Lab / B2', count: 1 }, ]); }); it('flags part models whose EOL has passed and have deployed parts', async () => { const tx = makeTx({ partCount: 3, stateRows: [], parts: [ { id: '1', state: 'DEPLOYED', binId: null, createdAt: daysAgo(1), partModelId: 'pm1' }, { id: '2', state: 'DEPLOYED', binId: null, createdAt: daysAgo(1), partModelId: 'pm1' }, { id: '3', state: 'DEPLOYED', binId: null, createdAt: daysAgo(1), partModelId: 'pm2' }, ], openFms: 0, eolPartModels: [ { id: 'pm1', mpn: 'ACM-100', eolDate: daysAgo(30), manufacturerId: 'm1', manufacturer: { name: 'Acme' }, }, { id: 'pm2', mpn: 'BET-200', eolDate: daysAgo(10), manufacturerId: 'm2', manufacturer: { name: 'Beta' }, }, { id: 'pm3', mpn: 'GAM-300', eolDate: daysAgo(5), manufacturerId: 'm3', manufacturer: { name: 'Gamma' }, }, ], bins: [], }); const r = await dashboard(tx); expect(r.deployedPastEol.map((m) => m.mpn)).toEqual(['ACM-100', 'BET-200']); expect(r.deployedPastEol[0]).toMatchObject({ partModelId: 'pm1', manufacturerName: 'Acme', deployedCount: 2, }); expect(r.deployedPastEol[1]).toMatchObject({ partModelId: 'pm2', manufacturerName: 'Beta', deployedCount: 1, }); }); });