diff --git a/frontend/src/lib/dataExport.js b/frontend/src/lib/dataExport.js new file mode 100644 index 0000000..6bcbcf2 --- /dev/null +++ b/frontend/src/lib/dataExport.js @@ -0,0 +1,23 @@ +// Pure helpers for exporting daily-parameter data as a date×subject CSV matrix. +// No React, no I/O — mirrors the crossSubjectChart.js pure-helper pattern. + +// Read a single parameter's raw value from one daily status. +// Returns null when the value is absent. Note: 0 and '' are returned as-is +// (real values); only genuinely-missing lookups become null. +export function extractValue(status, param) { + if (!status || !param) return null; + switch (param.kind) { + case 'total': + return status.analysis_summary?.total ?? null; + case 'success_rate': + return status.analysis_summary?.success_rate ?? null; + case 'category': + return status.analysis_summary?.counts?.[param.category] ?? null; + case 'builtin': + return status[param.statusKey] ?? null; + case 'custom': + return status.custom_fields?.[param.fieldId] ?? null; + default: + return null; + } +} diff --git a/frontend/tests/dataExport.test.js b/frontend/tests/dataExport.test.js new file mode 100644 index 0000000..02b3084 --- /dev/null +++ b/frontend/tests/dataExport.test.js @@ -0,0 +1,33 @@ +import { extractValue } 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(); + }); +});