import { extractValue } from '../src/lib/dataExport'; import { listExportableParams } from '../src/lib/dataExport'; const status = { vitals: 'ok', custom_fields: { 'f-weight': 250 }, analysis_summary: { total: 12, success_rate: 0.83, counts: { Success: 10, Failure: 2 } }, }; describe('extractValue', () => { it('reads a builtin field via statusKey', () => { expect(extractValue(status, { kind: 'builtin', statusKey: 'vitals' })).toBe('ok'); }); it('reads a custom field via fieldId', () => { expect(extractValue(status, { kind: 'custom', fieldId: 'f-weight' })).toBe(250); }); it('reads session-metric total', () => { expect(extractValue(status, { kind: 'total' })).toBe(12); }); it('reads session-metric success_rate as a raw 0-1 fraction', () => { expect(extractValue(status, { kind: 'success_rate' })).toBe(0.83); }); it('reads a dynamic count category', () => { expect(extractValue(status, { kind: 'category', category: 'Failure' })).toBe(2); }); it('returns null when analysis_summary is absent', () => { expect(extractValue({ custom_fields: {} }, { kind: 'total' })).toBeNull(); expect(extractValue({}, { kind: 'category', category: 'Success' })).toBeNull(); }); it('returns null for a missing custom field', () => { expect(extractValue({ custom_fields: {} }, { kind: 'custom', fieldId: 'nope' })).toBeNull(); expect(extractValue({}, { kind: 'builtin', statusKey: 'notes' })).toBeNull(); }); }); const dailyTemplate = [ { fieldId: 'b-vitals', key: 'vitals', label: 'Vitals', builtin: true, active: true }, { fieldId: 'c-weight', key: 'weight', label: 'Weight (g)', builtin: false, active: false }, ]; const statuses = [ { analysis_summary: { counts: { Success: 1, Failure: 0 } } }, { analysis_summary: { counts: { Success: 2, Other: 1 } } }, { analysis_summary: null }, ]; describe('listExportableParams', () => { const params = listExportableParams(dailyTemplate, statuses); it('starts with the two fixed session metrics', () => { expect(params.slice(0, 2)).toEqual([ expect.objectContaining({ id: '__total__', label: 'Total attempts', group: 'metrics', kind: 'total' }), expect.objectContaining({ id: '__success_rate__', label: 'Success rate', group: 'metrics', kind: 'success_rate' }), ]); }); it('adds one metrics entry per dynamic count category, sorted', () => { const cats = params.filter((p) => p.kind === 'category').map((p) => p.label); expect(cats).toEqual(['Failure', 'Other', 'Success']); }); it('includes every template field (incl. inactive) in the daily group', () => { const daily = params.filter((p) => p.group === 'daily'); expect(daily).toEqual([ expect.objectContaining({ label: 'Vitals', kind: 'builtin', statusKey: 'vitals' }), expect.objectContaining({ label: 'Weight (g)', kind: 'custom', fieldId: 'c-weight' }), ]); }); it('gives every param a unique id', () => { const ids = params.map((p) => p.id); expect(new Set(ids).size).toBe(ids.length); }); it('tolerates empty/missing inputs', () => { expect(listExportableParams(undefined, undefined).length).toBe(2); // just the two fixed metrics }); });