281 lines
13 KiB
JavaScript
281 lines
13 KiB
JavaScript
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
|
|
});
|
|
});
|
|
|
|
import { buildMatrix } from '../src/lib/dataExport';
|
|
|
|
const gAnimals = [
|
|
{ id: 'a2', animal_name: 'Beta', animal_id_string: 'R-002', subject_info: { grp: 'Drug' } },
|
|
{ id: 'a1', animal_name: 'Alpha', animal_id_string: 'R-001', subject_info: { grp: 'Control' } },
|
|
];
|
|
const gStatuses = [
|
|
{ animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 5, success_rate: 0.5 } },
|
|
{ animal_id: 'a2', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 9, success_rate: 0.9 } },
|
|
{ animal_id: 'a1', date: '2026-07-02T00:00:00.000Z', analysis_summary: { total: 7, success_rate: 0.7 } },
|
|
];
|
|
const ctx = { statuses: gStatuses, animals: gAnimals, dailyTemplate: [] };
|
|
const totalMember = { id: '__total__', label: 'Total attempts', param: { kind: 'total' }, group: 'metrics' };
|
|
|
|
describe('buildMatrix', () => {
|
|
it('default preset: rows=date, cols=subject, pinned=parameter (legacy layout)', () => {
|
|
const m = buildMatrix({ rowDim: 'date', colDim: 'subject', pinnedMember: totalMember, groupField: '__none__' }, ctx);
|
|
expect(m.corner).toBe('Date');
|
|
expect(m.context).toEqual({ label: 'Parameter', value: 'Total attempts' });
|
|
expect(m.columns.map((c) => c.label)).toEqual(['Alpha', 'Beta']);
|
|
expect(m.rows.map((r) => r.member.label)).toEqual(['2026-07-01', '2026-07-02']);
|
|
expect(m.rows[0].values).toEqual([5, 9]);
|
|
expect(m.rows[1].values).toEqual([7, '']); // Beta blank on 07-02
|
|
expect(m.groupAxis).toBeNull();
|
|
});
|
|
|
|
it('drops blank date rows but keeps all subject columns', () => {
|
|
const statuses = [
|
|
{ animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 0 } }, // 0 is a value
|
|
{ animal_id: 'a1', date: '2026-07-03T00:00:00.000Z', analysis_summary: null }, // no value
|
|
];
|
|
const m = buildMatrix({ rowDim: 'date', colDim: 'subject', pinnedMember: totalMember, groupField: '__none__' },
|
|
{ statuses, animals: gAnimals, dailyTemplate: [] });
|
|
expect(m.rows.map((r) => r.member.label)).toEqual(['2026-07-01']);
|
|
expect(m.columns.length).toBe(2);
|
|
expect(m.rows[0].values).toEqual([0, '']);
|
|
});
|
|
|
|
it('subject as columns sets groupAxis=col and clusters subjects', () => {
|
|
const m = buildMatrix({ rowDim: 'date', colDim: 'subject', pinnedMember: totalMember, groupField: 'grp' }, ctx);
|
|
expect(m.groupAxis).toBe('col');
|
|
expect(m.columns.map((c) => [c.group, c.label])).toEqual([['Control', 'Alpha'], ['Drug', 'Beta']]);
|
|
});
|
|
|
|
it('subject as rows sets groupAxis=row; pinned date; cols=parameter', () => {
|
|
const dateMember = { id: '2026-07-01', label: '2026-07-01', dateKey: '2026-07-01' };
|
|
const m = buildMatrix({ rowDim: 'subject', colDim: 'parameter', pinnedMember: dateMember, groupField: 'grp' },
|
|
{ statuses: gStatuses, animals: gAnimals, dailyTemplate: [] });
|
|
expect(m.corner).toBe('Subject');
|
|
expect(m.context).toEqual({ label: 'Date', value: '2026-07-01' });
|
|
expect(m.groupAxis).toBe('row');
|
|
expect(m.rows.map((r) => [r.member.group, r.member.label])).toEqual([['Control', 'Alpha'], ['Drug', 'Beta']]);
|
|
// columns are parameters (Total attempts, Success rate); Alpha on 07-01: total 5, rate 0.5
|
|
const totalCol = m.columns.findIndex((c) => c.label === 'Total attempts');
|
|
expect(m.rows[0].values[totalCol]).toBe(5);
|
|
});
|
|
|
|
it('drops blank parameter columns (params always dropped when empty)', () => {
|
|
// dailyTemplate adds a custom field with no data anywhere -> its column drops
|
|
const dailyTemplate = [{ fieldId: 'c-x', key: 'x', label: 'X', builtin: false, active: true }];
|
|
const dateMember = { id: '2026-07-01', label: '2026-07-01', dateKey: '2026-07-01' };
|
|
const m = buildMatrix({ rowDim: 'subject', colDim: 'parameter', pinnedMember: dateMember, groupField: '__none__' },
|
|
{ statuses: gStatuses, animals: gAnimals, dailyTemplate });
|
|
expect(m.columns.map((c) => c.label)).not.toContain('X');
|
|
});
|
|
});
|
|
|
|
import { toCSV, csvFilename } from '../src/lib/dataExport';
|
|
|
|
describe('toCSV', () => {
|
|
it('writes a header row of Date + column headers, then data rows', () => {
|
|
const matrix = {
|
|
columns: [{ id: 'a1', header: 'Alpha' }, { id: 'a2', header: 'Beta' }],
|
|
rows: [{ date: '2026-07-01', values: [5, ''] }, { date: '2026-07-02', values: ['', 9] }],
|
|
};
|
|
expect(toCSV(matrix)).toBe('Date,Alpha,Beta\n2026-07-01,5,\n2026-07-02,,9');
|
|
});
|
|
it('escapes commas, quotes, and newlines per RFC 4180', () => {
|
|
const matrix = {
|
|
columns: [{ id: 'a1', header: 'Note, field' }],
|
|
rows: [{ date: '2026-07-01', values: ['he said "hi"'] }, { date: '2026-07-02', values: ['line1\nline2'] }],
|
|
};
|
|
expect(toCSV(matrix)).toBe(
|
|
'Date,"Note, field"\n2026-07-01,"he said ""hi"""\n2026-07-02,"line1\nline2"',
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('csvFilename', () => {
|
|
it('slugifies the experiment title and parameter label', () => {
|
|
expect(csvFilename('My Study #1', 'Success rate')).toBe('My-Study-1-Success-rate.csv');
|
|
});
|
|
it('falls back to "export" when both slugs are empty', () => {
|
|
expect(csvFilename('', '')).toBe('export.csv');
|
|
});
|
|
});
|
|
|
|
import {
|
|
EXPORT_DIMENSIONS, dimLabel, otherDimension, subjectGroupValue,
|
|
listDateMembers, buildStatusIndex,
|
|
} from '../src/lib/dataExport';
|
|
|
|
describe('dimension primitives', () => {
|
|
it('exposes the three dimensions with labels', () => {
|
|
expect(EXPORT_DIMENSIONS.map((d) => d.dim)).toEqual(['date', 'subject', 'parameter']);
|
|
expect(dimLabel('date')).toBe('Date');
|
|
expect(dimLabel('subject')).toBe('Subject');
|
|
expect(dimLabel('parameter')).toBe('Parameter');
|
|
});
|
|
it('otherDimension returns the leftover', () => {
|
|
expect(otherDimension('date', 'subject')).toBe('parameter');
|
|
expect(otherDimension('parameter', 'date')).toBe('subject');
|
|
});
|
|
});
|
|
|
|
describe('subjectGroupValue', () => {
|
|
const animal = { animal_name: 'Alpha', animal_id_string: 'R-001', subject_info: { sex: 'M', cohort: '' } };
|
|
it('returns null when no group field', () => {
|
|
expect(subjectGroupValue(animal, '__none__')).toBeNull();
|
|
expect(subjectGroupValue(animal, '')).toBeNull();
|
|
});
|
|
it('resolves name / id / subject_info fields', () => {
|
|
expect(subjectGroupValue(animal, '__name__')).toBe('Alpha');
|
|
expect(subjectGroupValue(animal, '__id__')).toBe('R-001');
|
|
expect(subjectGroupValue(animal, 'sex')).toBe('M');
|
|
});
|
|
it('renders missing or empty values as an em dash', () => {
|
|
expect(subjectGroupValue(animal, 'cohort')).toBe('—');
|
|
expect(subjectGroupValue({}, 'sex')).toBe('—');
|
|
});
|
|
});
|
|
|
|
describe('listDateMembers', () => {
|
|
it('returns sorted unique dateKeys with id/label', () => {
|
|
const statuses = [
|
|
{ date: '2026-07-02T00:00:00.000Z' },
|
|
{ date: '2026-07-01T12:00:00.000Z' },
|
|
{ date: '2026-07-02T09:00:00.000Z' },
|
|
];
|
|
expect(listDateMembers(statuses)).toEqual([
|
|
{ id: '2026-07-01', label: '2026-07-01', dateKey: '2026-07-01' },
|
|
{ id: '2026-07-02', label: '2026-07-02', dateKey: '2026-07-02' },
|
|
]);
|
|
});
|
|
it('tolerates empty input', () => {
|
|
expect(listDateMembers(undefined)).toEqual([]);
|
|
});
|
|
});
|
|
|
|
describe('buildStatusIndex', () => {
|
|
it('indexes by dateKey then animalId, first status wins', () => {
|
|
const statuses = [
|
|
{ animal_id: 'a1', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 7 } },
|
|
{ animal_id: 'a1', date: '2026-07-01T06:00:00.000Z', analysis_summary: { total: 99 } },
|
|
{ animal_id: 'a2', date: '2026-07-01T00:00:00.000Z', analysis_summary: { total: 3 } },
|
|
];
|
|
const idx = buildStatusIndex(statuses);
|
|
expect(idx.get('2026-07-01').get('a1').analysis_summary.total).toBe(7);
|
|
expect(idx.get('2026-07-01').get('a2').analysis_summary.total).toBe(3);
|
|
});
|
|
});
|
|
|
|
import { listSubjectMembers, listParameterMembers, listDimensionMembers } from '../src/lib/dataExport';
|
|
|
|
describe('listSubjectMembers', () => {
|
|
const animals = [
|
|
{ id: 'a2', animal_name: 'Beta', animal_id_string: 'R-002', subject_info: { grp: 'Drug' } },
|
|
{ id: 'a1', animal_name: 'Alpha', animal_id_string: 'R-001', subject_info: { grp: 'Control' } },
|
|
{ id: 'a3', animal_name: 'Gamma', animal_id_string: 'R-003', subject_info: { grp: 'Control' } },
|
|
];
|
|
it('orders by name and carries group=null when no group field', () => {
|
|
const m = listSubjectMembers(animals, '__none__');
|
|
expect(m.map((s) => s.label)).toEqual(['Alpha', 'Beta', 'Gamma']);
|
|
expect(m.every((s) => s.group === null)).toBe(true);
|
|
expect(m[0]).toMatchObject({ id: 'a1', animalId: 'a1' });
|
|
});
|
|
it('clusters by group value then name when grouping is on', () => {
|
|
const m = listSubjectMembers(animals, 'grp');
|
|
expect(m.map((s) => [s.group, s.label])).toEqual([
|
|
['Control', 'Alpha'], ['Control', 'Gamma'], ['Drug', 'Beta'],
|
|
]);
|
|
});
|
|
it('disambiguates duplicate names with the id string', () => {
|
|
const dup = [
|
|
{ id: 'a1', animal_name: 'Rat', animal_id_string: 'R-001' },
|
|
{ id: 'a2', animal_name: 'Rat', animal_id_string: 'R-002' },
|
|
];
|
|
expect(listSubjectMembers(dup, '__none__').map((s) => s.label)).toEqual(['Rat (R-001)', 'Rat (R-002)']);
|
|
});
|
|
});
|
|
|
|
describe('listParameterMembers / listDimensionMembers', () => {
|
|
const dailyTemplate = [{ fieldId: 'b-vitals', key: 'vitals', label: 'Vitals', builtin: true, active: true }];
|
|
const statuses = [{ date: '2026-07-01T00:00:00.000Z', animal_id: 'a1', analysis_summary: { total: 1 } }];
|
|
const animals = [{ id: 'a1', animal_name: 'Alpha', animal_id_string: 'R-001' }];
|
|
it('wraps params with id/label/param/group', () => {
|
|
const m = listParameterMembers(dailyTemplate, statuses);
|
|
expect(m[0]).toMatchObject({ id: '__total__', label: 'Total attempts', group: 'metrics' });
|
|
expect(m[0].param).toMatchObject({ kind: 'total' });
|
|
});
|
|
it('dispatches by dimension', () => {
|
|
const ctx = { statuses, animals, dailyTemplate, groupField: '__none__' };
|
|
expect(listDimensionMembers('date', ctx).map((d) => d.id)).toEqual(['2026-07-01']);
|
|
expect(listDimensionMembers('subject', ctx).map((d) => d.id)).toEqual(['a1']);
|
|
expect(listDimensionMembers('parameter', ctx)[0].id).toBe('__total__');
|
|
});
|
|
});
|