feat: session file registry with drag-and-drop on day view

Adds a FileDropZone at the bottom of the day view where users can
drop all session files at once. Filenames are regex-matched to
subjects using their animal_id_string (word-boundary aware, so
"2852" won't match "28520"). Only metadata is recorded — files
are never uploaded.

Backend: new session_files table (id, daily_status_id, filename,
file_size BIGINT, file_type, last_modified, notes). Routes:
  POST /api/daily-statuses/:id/files  (batch create)
  GET  /api/daily-statuses/:id/files
  DELETE /api/session-files/:id

Frontend: FileDropZone shows per-subject match groups, unmatched
files, and a warning for subjects with no match. After saving,
registered files are displayed with delete capability.

test-files/: generate_test_files.sh creates 4 dummy files per
subject (2×bin, 2×csv) with correct naming for drag-and-drop
testing. Pre-generated for 2026-04-23 with IDs 2852/3076/3077/3078.

Tests: 19 tests covering drag state, regex matching, save payload,
partial-overlap prevention, multi-subject batching, and delete.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Experiments DB Dev
2026-04-23 13:12:45 -04:00
parent dc46af8d31
commit daf36908ef
25 changed files with 1008 additions and 0 deletions
+7
View File
@@ -69,6 +69,13 @@ export const systemApi = {
info: () => api.get('/info').then((r) => r.data),
};
// ── Session Files ─────────────────────────────────────────────────────────────
export const sessionFilesApi = {
list: (statusId) => api.get(`/daily-statuses/${statusId}/files`).then((r) => r.data),
create: (statusId, files) => api.post(`/daily-statuses/${statusId}/files`, files).then((r) => r.data),
delete: (id) => api.delete(`/session-files/${id}`),
};
// ── Analyses ──────────────────────────────────────────────────────────────────
export const analysesApi = {
listForStatus: (statusId) => api.get(`/daily-statuses/${statusId}/analyses`).then((r) => r.data),
+385
View File
@@ -0,0 +1,385 @@
import React, { useCallback, useEffect, useRef, useState } from 'react';
import { sessionFilesApi } from '../api/client';
// ── Helpers ───────────────────────────────────────────────────────────────────
function escapeRegex(str) {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
/**
* Build a regex that matches an animal's identifier in a filename.
* Matches only when the ID is not surrounded by other alphanumeric chars,
* so "2852" matches "rat_2852_session" but not "28520" or "X2852Y".
*/
function buildAnimalRegex(animal) {
const ids = [];
if (animal.animal_id_string) ids.push(escapeRegex(animal.animal_id_string));
if (animal.animal_name) ids.push(escapeRegex(animal.animal_name));
if (ids.length === 0) return null;
return new RegExp(`(?<![a-zA-Z0-9])(${ids.join('|')})(?![a-zA-Z0-9])`, 'i');
}
function classifyFile(filename) {
const ext = filename.split('.').pop().toLowerCase();
const MAP = { csv: 'CSV', tsv: 'TSV', h5: 'HDF5', hdf5: 'HDF5', nwb: 'NWB', bin: 'Binary', dat: 'Binary', raw: 'Binary', npy: 'NumPy', npz: 'NumPy', mat: 'MATLAB', pkl: 'Pickle' };
return MAP[ext] ?? 'Binary';
}
function fmtBytes(n) {
const num = typeof n === 'bigint' ? Number(n) : Number(n);
if (num >= 1e9) return `${(num / 1e9).toFixed(1)} GB`;
if (num >= 1e6) return `${(num / 1e6).toFixed(1)} MB`;
if (num >= 1e3) return `${(num / 1e3).toFixed(1)} KB`;
return `${num} B`;
}
/** Match an array of File objects against animals. Returns { matched: Map<animalId, File[]>, unmatched: File[] } */
function matchFilesToAnimals(files, animals) {
const patterns = animals.map((a) => ({ animal: a, re: buildAnimalRegex(a) })).filter((x) => x.re);
const matched = new Map(animals.map((a) => [a.id, []]));
const unmatched = [];
for (const file of files) {
const hit = patterns.find(({ re }) => re.test(file.name));
if (hit) {
matched.get(hit.animal.id).push(file);
} else {
unmatched.push(file);
}
}
return { matched, unmatched };
}
// ── Sub-components ────────────────────────────────────────────────────────────
function FileRow({ file, onRemove }) {
return (
<div className="flex items-center justify-between py-1 gap-2 text-xs">
<span className="font-mono text-gray-700 truncate min-w-0 flex-1">{file.name}</span>
<span className="text-gray-400 shrink-0">{fmtBytes(file.size)}</span>
<span className="text-gray-400 shrink-0 w-12 text-right">{classifyFile(file.name)}</span>
{onRemove && (
<button type="button" onClick={onRemove}
className="text-red-300 hover:text-red-500 transition-colors shrink-0 ml-1"></button>
)}
</div>
);
}
function SavedFileRow({ file, onDelete, deleting }) {
const size = typeof file.file_size === 'string' ? BigInt(file.file_size) : file.file_size;
return (
<div className="flex items-center justify-between py-1 gap-2 text-xs">
<span className="font-mono text-gray-600 truncate min-w-0 flex-1">{file.filename}</span>
<span className="text-gray-400 shrink-0">{fmtBytes(size)}</span>
<span className="text-gray-400 shrink-0 w-12 text-right">{file.file_type ?? '?'}</span>
<button type="button" onClick={onDelete} disabled={deleting}
className="text-red-300 hover:text-red-500 transition-colors shrink-0 ml-1 disabled:opacity-40"></button>
</div>
);
}
// ── Main component ────────────────────────────────────────────────────────────
/**
* FileDropZone
*
* Props:
* rows — array of { animal, status } from ExperimentDayView
* (only rows with a status are shown; status.id is required to save files)
* date — YYYY-MM-DD string, shown in naming hint
*/
export default function FileDropZone({ rows, date }) {
const [isDragging, setIsDragging] = useState(false);
const [dropped, setDropped] = useState(null); // { matched: Map, unmatched: File[] } | null
const [saving, setSaving] = useState(false);
const [saveError, setSaveError] = useState(null);
const [savedByStatus, setSavedByStatus] = useState({}); // statusId → SavedFile[]
const [loadingIds, setLoadingIds] = useState(new Set());
const [deletingId, setDeletingId] = useState(null);
const dragCounter = useRef(0);
const statusRows = rows.filter((r) => r.status);
const animals = statusRows.map((r) => r.animal);
// Load existing saved files for all statuses in this day view
useEffect(() => {
if (statusRows.length === 0) return;
const ids = statusRows.map((r) => r.status.id);
setLoadingIds(new Set(ids));
Promise.all(
ids.map((sid) => sessionFilesApi.list(sid).then((files) => ({ sid, files })))
).then((results) => {
const map = {};
results.forEach(({ sid, files }) => { map[sid] = files; });
setSavedByStatus(map);
setLoadingIds(new Set());
}).catch(() => setLoadingIds(new Set()));
}, [statusRows.map((r) => r.status?.id).join(',')]); // eslint-disable-line react-hooks/exhaustive-deps
// ── Drag handlers ──────────────────────────────────────────────────────────
const onDragEnter = useCallback((e) => {
e.preventDefault();
dragCounter.current += 1;
if (dragCounter.current === 1) setIsDragging(true);
}, []);
const onDragLeave = useCallback((e) => {
e.preventDefault();
dragCounter.current -= 1;
if (dragCounter.current === 0) setIsDragging(false);
}, []);
const onDragOver = useCallback((e) => { e.preventDefault(); }, []);
const onDrop = useCallback((e) => {
e.preventDefault();
dragCounter.current = 0;
setIsDragging(false);
const files = Array.from(e.dataTransfer.files);
if (files.length === 0) return;
setDropped(matchFilesToAnimals(files, animals));
setSaveError(null);
}, [animals]);
function removeMatchedFile(animalId, fileIdx) {
setDropped((prev) => {
const next = new Map(prev.matched);
const arr = [...next.get(animalId)];
arr.splice(fileIdx, 1);
next.set(animalId, arr);
return { matched: next, unmatched: prev.unmatched };
});
}
function removeUnmatched(idx) {
setDropped((prev) => {
const u = [...prev.unmatched];
u.splice(idx, 1);
return { matched: prev.matched, unmatched: u };
});
}
// ── Save ───────────────────────────────────────────────────────────────────
async function save() {
if (!dropped) return;
setSaving(true);
setSaveError(null);
try {
const results = await Promise.all(
statusRows.map(async ({ animal, status }) => {
const files = dropped.matched.get(animal.id) ?? [];
if (files.length === 0) return { statusId: status.id, saved: [] };
const payload = files.map((f) => ({
filename: f.name,
file_size: f.size,
file_type: classifyFile(f.name),
last_modified: f.lastModified ?? null,
}));
const saved = await sessionFilesApi.create(status.id, payload);
return { statusId: status.id, saved };
})
);
setSavedByStatus((prev) => {
const next = { ...prev };
results.forEach(({ statusId, saved }) => {
next[statusId] = [...(next[statusId] ?? []), ...saved];
});
return next;
});
setDropped(null);
} catch (err) {
setSaveError(err.message ?? 'Failed to save file metadata');
} finally {
setSaving(false);
}
}
// ── Delete ─────────────────────────────────────────────────────────────────
async function deleteFile(fileId, statusId) {
setDeletingId(fileId);
try {
await sessionFilesApi.delete(fileId);
setSavedByStatus((prev) => ({
...prev,
[statusId]: prev[statusId].filter((f) => f.id !== fileId),
}));
} catch {
// silently ignore — UI stays consistent, user can retry
} finally {
setDeletingId(null);
}
}
// ── Derived counts ─────────────────────────────────────────────────────────
const totalDropped = dropped ? [...dropped.matched.values()].reduce((n, a) => n + a.length, 0) : 0;
const totalUnmatched = dropped?.unmatched.length ?? 0;
const totalSaved = Object.values(savedByStatus).reduce((n, a) => n + a.length, 0);
const hasSaved = totalSaved > 0;
const hasMatchedDropped = totalDropped > 0;
// Hint string for naming convention
const sampleId = animals[0]?.animal_id_string || animals[0]?.animal_name || '<id>';
const namingHint = `${date}_${sampleId}_session1.csv`;
return (
<div className="mt-8">
<h3 className="text-sm font-semibold text-gray-700 mb-3">Session File Registry</h3>
<p className="text-xs text-gray-400 mb-4">
Drop all session files here. Filenames are matched to subjects by ID
only metadata (name, size) is recorded; files are never uploaded.
<br />
Naming convention: <span className="font-mono text-gray-500">{namingHint}</span>
</p>
{/* Drop zone */}
<div
onDragEnter={onDragEnter}
onDragLeave={onDragLeave}
onDragOver={onDragOver}
onDrop={onDrop}
data-testid="drop-zone"
className={[
'border-2 border-dashed rounded-xl px-6 py-10 text-center transition-all cursor-default select-none',
isDragging
? 'border-blue-400 bg-blue-50'
: 'border-gray-200 bg-gray-50 hover:border-gray-300',
].join(' ')}
>
{isDragging ? (
<p className="text-blue-600 font-medium text-sm">Release to register files</p>
) : (
<>
<p className="text-gray-400 text-sm">Drag &amp; drop session files here</p>
<p className="text-gray-300 text-xs mt-1">All subjects at once · files are not uploaded</p>
</>
)}
</div>
{/* Dropped files preview */}
{dropped && (
<div className="mt-4 space-y-4" data-testid="drop-preview">
{/* Per-subject matched files */}
{statusRows.map(({ animal, status }) => {
const files = dropped.matched.get(animal.id) ?? [];
if (files.length === 0) return null;
return (
<div key={animal.id} className="bg-white border border-gray-200 rounded-xl p-4">
<div className="flex items-center justify-between mb-2">
<span className="text-sm font-semibold text-gray-800">
{animal.animal_name}
{animal.animal_id_string && (
<span className="ml-1.5 text-xs font-normal text-gray-400">{animal.animal_id_string}</span>
)}
</span>
<span className="text-xs text-gray-400">{files.length} file{files.length !== 1 ? 's' : ''}</span>
</div>
<div className="divide-y divide-gray-50">
{files.map((f, i) => (
<FileRow key={f.name + i} file={f} onRemove={() => removeMatchedFile(animal.id, i)} />
))}
</div>
</div>
);
})}
{/* Subjects with no matched files */}
{statusRows.some(({ animal }) => (dropped.matched.get(animal.id) ?? []).length === 0) && (
<div className="bg-amber-50 border border-amber-100 rounded-xl p-4">
<p className="text-xs font-semibold text-amber-700 mb-1">No files matched for:</p>
{statusRows
.filter(({ animal }) => (dropped.matched.get(animal.id) ?? []).length === 0)
.map(({ animal }) => (
<span key={animal.id} className="inline-block text-xs text-amber-600 bg-amber-100 rounded px-2 py-0.5 mr-1 mb-1 font-mono">
{animal.animal_id_string || animal.animal_name}
</span>
))}
</div>
)}
{/* Unmatched files */}
{dropped.unmatched.length > 0 && (
<div className="bg-gray-50 border border-dashed border-gray-200 rounded-xl p-4">
<p className="text-xs font-semibold text-gray-500 mb-2">
Unmatched ({dropped.unmatched.length}) no subject ID found in filename
</p>
<div className="divide-y divide-gray-100">
{dropped.unmatched.map((f, i) => (
<FileRow key={f.name + i} file={f} onRemove={() => removeUnmatched(i)} />
))}
</div>
</div>
)}
{saveError && (
<p className="text-xs text-red-500 px-1">{saveError}</p>
)}
{/* Action row */}
<div className="flex items-center gap-3 pt-1">
{hasMatchedDropped && (
<button
type="button"
onClick={save}
disabled={saving}
data-testid="save-btn"
className="px-4 py-2 rounded-lg text-sm font-medium bg-blue-600 text-white hover:bg-blue-700 disabled:opacity-50 transition-colors"
>
{saving ? 'Saving…' : `Register ${totalDropped} file${totalDropped !== 1 ? 's' : ''}`}
</button>
)}
<button
type="button"
onClick={() => { setDropped(null); setSaveError(null); }}
className="text-sm text-gray-400 hover:text-gray-600"
>
Clear
</button>
</div>
</div>
)}
{/* Saved files */}
{hasSaved && (
<div className="mt-6 space-y-3" data-testid="saved-files">
<p className="text-xs font-semibold text-gray-500 uppercase tracking-wide">
Registered files ({totalSaved})
</p>
{statusRows.map(({ animal, status }) => {
const files = savedByStatus[status.id] ?? [];
if (files.length === 0) return null;
return (
<div key={animal.id} className="bg-white border border-gray-200 rounded-xl p-4">
<div className="flex items-center justify-between mb-2">
<span className="text-sm font-semibold text-gray-800">
{animal.animal_name}
{animal.animal_id_string && (
<span className="ml-1.5 text-xs font-normal text-gray-400">{animal.animal_id_string}</span>
)}
</span>
<span className="text-xs text-gray-400">{files.length} file{files.length !== 1 ? 's' : ''}</span>
</div>
<div className="divide-y divide-gray-50">
{files.map((f) => (
<SavedFileRow
key={f.id}
file={f}
onDelete={() => deleteFile(f.id, status.id)}
deleting={deletingId === f.id}
/>
))}
</div>
</div>
);
})}
</div>
)}
</div>
);
}
+4
View File
@@ -10,6 +10,7 @@ import Button from '../components/ui/Button';
import Modal from '../components/ui/Modal';
import DailyStatusForm from '../components/DailyStatusForm';
import Alert from '../components/ui/Alert';
import FileDropZone from '../components/FileDropZone';
const BUILTIN_KEYS_SET = new Set(['experiment_description', 'vitals', 'treatment', 'notes']);
@@ -367,6 +368,9 @@ export default function ExperimentDayView() {
</div>
)}
{/* Session file drop zone */}
<FileDropZone rows={rows} date={date} />
{/* Edit modal (full row) */}
{editStatus && (
<Modal
+301
View File
@@ -0,0 +1,301 @@
import React from 'react';
import { render, screen, fireEvent, waitFor, act } from '@testing-library/react';
import FileDropZone from '../src/components/FileDropZone';
import { sessionFilesApi } from '../src/api/client';
jest.mock('../src/api/client', () => ({
sessionFilesApi: {
list: jest.fn(),
create: jest.fn(),
delete: jest.fn(),
},
}));
// ── Fixtures ───────────────────────────────────────────────────────────────────
const DATE = '2026-04-23';
const ANIMALS = [
{ id: 'a1', animal_name: 'Rat 2852', animal_id_string: '2852' },
{ id: 'a2', animal_name: 'Rat 3076', animal_id_string: '3076' },
{ id: 'a3', animal_name: 'Rat 3077', animal_id_string: '3077' },
];
const STATUS_A1 = { id: 's-a1', animal_id: 'a1', date: `${DATE}T00:00:00.000Z` };
const STATUS_A2 = { id: 's-a2', animal_id: 'a2', date: `${DATE}T00:00:00.000Z` };
const STATUS_A3 = { id: 's-a3', animal_id: 'a3', date: `${DATE}T00:00:00.000Z` };
const ROWS = [
{ animal: ANIMALS[0], status: STATUS_A1 },
{ animal: ANIMALS[1], status: STATUS_A2 },
{ animal: ANIMALS[2], status: STATUS_A3 },
];
function makeFile(name, size = 1024, type = '') {
return new File(['x'], name, { type, lastModified: Date.now() });
}
function makeSavedFile(id, statusId, filename, fileSize = '10240') {
return { id, daily_status_id: statusId, filename, file_size: fileSize, file_type: 'CSV', last_modified: null, created_at: new Date().toISOString() };
}
function dropFiles(files) {
const zone = screen.getByTestId('drop-zone');
fireEvent.dragEnter(zone, { dataTransfer: { files } });
fireEvent.dragOver(zone, { dataTransfer: { files } });
fireEvent.drop(zone, { dataTransfer: { files } });
}
beforeEach(() => {
jest.clearAllMocks();
sessionFilesApi.list.mockResolvedValue([]);
sessionFilesApi.create.mockResolvedValue([]);
sessionFilesApi.delete.mockResolvedValue();
});
// ── Rendering ──────────────────────────────────────────────────────────────────
describe('rendering', () => {
it('renders the drop zone', () => {
render(<FileDropZone rows={ROWS} date={DATE} />);
expect(screen.getByTestId('drop-zone')).toBeInTheDocument();
expect(screen.getByText(/drag.*drop session files/i)).toBeInTheDocument();
});
it('shows naming convention hint using first animal id', () => {
render(<FileDropZone rows={ROWS} date={DATE} />);
expect(screen.getByText(/2026-04-23_2852/)).toBeInTheDocument();
});
it('loads saved files on mount for each status', async () => {
sessionFilesApi.list.mockResolvedValue([]);
render(<FileDropZone rows={ROWS} date={DATE} />);
await waitFor(() => {
expect(sessionFilesApi.list).toHaveBeenCalledWith('s-a1');
expect(sessionFilesApi.list).toHaveBeenCalledWith('s-a2');
expect(sessionFilesApi.list).toHaveBeenCalledWith('s-a3');
});
});
it('shows saved files loaded from the API', async () => {
sessionFilesApi.list.mockImplementation((id) =>
id === 's-a1'
? Promise.resolve([makeSavedFile('f1', 's-a1', `${DATE}_2852_trials.csv`)])
: Promise.resolve([])
);
render(<FileDropZone rows={ROWS} date={DATE} />);
await waitFor(() =>
expect(screen.getByText(`${DATE}_2852_trials.csv`)).toBeInTheDocument()
);
expect(screen.getByTestId('saved-files')).toBeInTheDocument();
});
});
// ── Drag state ─────────────────────────────────────────────────────────────────
describe('drag interaction', () => {
it('highlights drop zone on dragenter', () => {
render(<FileDropZone rows={ROWS} date={DATE} />);
const zone = screen.getByTestId('drop-zone');
fireEvent.dragEnter(zone, { dataTransfer: { files: [] } });
expect(zone.className).toMatch(/border-blue-400/);
expect(screen.getByText(/release to register/i)).toBeInTheDocument();
});
it('removes highlight on dragleave', () => {
render(<FileDropZone rows={ROWS} date={DATE} />);
const zone = screen.getByTestId('drop-zone');
fireEvent.dragEnter(zone, { dataTransfer: { files: [] } });
fireEvent.dragLeave(zone, { dataTransfer: { files: [] } });
expect(zone.className).not.toMatch(/border-blue-400/);
});
});
// ── File matching ──────────────────────────────────────────────────────────────
describe('filename-to-subject matching', () => {
it('matches files containing the animal_id_string to the correct subject', async () => {
render(<FileDropZone rows={ROWS} date={DATE} />);
dropFiles([
makeFile(`${DATE}_2852_ephys_raw.bin`, 10_000_000_000),
makeFile(`${DATE}_3076_trials.csv`, 10_000_000),
]);
await waitFor(() => screen.getByTestId('drop-preview'));
expect(screen.getByText(/Rat 2852/)).toBeInTheDocument();
expect(screen.getByText(/Rat 3076/)).toBeInTheDocument();
expect(screen.getByText(`${DATE}_2852_ephys_raw.bin`)).toBeInTheDocument();
expect(screen.getByText(`${DATE}_3076_trials.csv`)).toBeInTheDocument();
});
it('shows unmatched section for files with no subject ID in name', async () => {
render(<FileDropZone rows={ROWS} date={DATE} />);
dropFiles([makeFile('unknown_file.csv', 1024)]);
await waitFor(() => screen.getByTestId('drop-preview'));
expect(screen.getByText(/unmatched/i)).toBeInTheDocument();
expect(screen.getByText('unknown_file.csv')).toBeInTheDocument();
});
it('does not match a partial numeric overlap (2852 should not match 28520)', async () => {
render(<FileDropZone rows={ROWS} date={DATE} />);
dropFiles([makeFile('session_28520_data.csv', 1024)]);
await waitFor(() => screen.getByTestId('drop-preview'));
expect(screen.getByText(/unmatched/i)).toBeInTheDocument();
});
it('shows warning for subjects with no matched files', async () => {
render(<FileDropZone rows={ROWS} date={DATE} />);
// Only drop file for 2852 — 3076 and 3077 get no match
dropFiles([makeFile(`${DATE}_2852_trials.csv`, 1024)]);
await waitFor(() => screen.getByTestId('drop-preview'));
expect(screen.getByText(/no files matched/i)).toBeInTheDocument();
expect(screen.getByText('3076')).toBeInTheDocument();
expect(screen.getByText('3077')).toBeInTheDocument();
});
it('matches all four files per subject correctly', async () => {
render(<FileDropZone rows={[{ animal: ANIMALS[0], status: STATUS_A1 }]} date={DATE} />);
dropFiles([
makeFile(`${DATE}_2852_ephys_raw.bin`, 10_000_000_000),
makeFile(`${DATE}_2852_kilosort.h5`, 10_000_000_000),
makeFile(`${DATE}_2852_trials.csv`, 10_000_000),
makeFile(`${DATE}_2852_behavior_log.csv`, 10_000_000),
]);
await waitFor(() => screen.getByTestId('drop-preview'));
// Save button label is the authoritative "all 4 matched" signal
expect(screen.getByTestId('save-btn')).toHaveTextContent('Register 4 files');
});
it('handles multiple subjects with 4 files each simultaneously', async () => {
render(<FileDropZone rows={ROWS} date={DATE} />);
dropFiles([
makeFile(`${DATE}_2852_ephys_raw.bin`, 10_000_000_000),
makeFile(`${DATE}_2852_kilosort.h5`, 10_000_000_000),
makeFile(`${DATE}_2852_trials.csv`, 10_000_000),
makeFile(`${DATE}_2852_behavior_log.csv`, 10_000_000),
makeFile(`${DATE}_3076_ephys_raw.bin`, 10_000_000_000),
makeFile(`${DATE}_3076_kilosort.h5`, 10_000_000_000),
makeFile(`${DATE}_3076_trials.csv`, 10_000_000),
makeFile(`${DATE}_3076_behavior_log.csv`, 10_000_000),
]);
await waitFor(() => screen.getByTestId('drop-preview'));
const saveBtn = screen.getByTestId('save-btn');
expect(saveBtn.textContent).toMatch(/8 files/);
});
});
// ── Remove before saving ───────────────────────────────────────────────────────
describe('remove files before saving', () => {
it('removes a matched file when ✕ is clicked', async () => {
render(<FileDropZone rows={ROWS} date={DATE} />);
dropFiles([makeFile(`${DATE}_2852_trials.csv`, 1024)]);
await waitFor(() => screen.getByText(`${DATE}_2852_trials.csv`));
fireEvent.click(screen.getAllByText('✕')[0]);
await waitFor(() =>
expect(screen.queryByText(`${DATE}_2852_trials.csv`)).not.toBeInTheDocument()
);
});
});
// ── Saving ─────────────────────────────────────────────────────────────────────
describe('saving metadata', () => {
it('calls sessionFilesApi.create with correct payload for each subject', async () => {
const file = makeFile(`${DATE}_2852_trials.csv`); // size = 1 byte (content 'x')
const savedFile = makeSavedFile('f1', 's-a1', file.name, String(file.size));
sessionFilesApi.create.mockResolvedValue([savedFile]);
render(<FileDropZone rows={ROWS} date={DATE} />);
dropFiles([file]);
await waitFor(() => screen.getByTestId('save-btn'));
fireEvent.click(screen.getByTestId('save-btn'));
await waitFor(() =>
expect(sessionFilesApi.create).toHaveBeenCalledWith(
's-a1',
expect.arrayContaining([
expect.objectContaining({
filename: `${DATE}_2852_trials.csv`,
file_size: file.size,
file_type: 'CSV',
}),
])
)
);
});
it('does not call create for subjects with no matched files', async () => {
sessionFilesApi.create.mockResolvedValue([]);
render(<FileDropZone rows={ROWS} date={DATE} />);
// Only 2852 gets a file
dropFiles([makeFile(`${DATE}_2852_trials.csv`, 1024)]);
await waitFor(() => screen.getByTestId('save-btn'));
fireEvent.click(screen.getByTestId('save-btn'));
await waitFor(() => expect(sessionFilesApi.create).toHaveBeenCalledTimes(1));
expect(sessionFilesApi.create).toHaveBeenCalledWith('s-a1', expect.any(Array));
});
it('stores correct file_type classification from extension', async () => {
sessionFilesApi.create.mockResolvedValue([]);
render(<FileDropZone rows={[{ animal: ANIMALS[0], status: STATUS_A1 }]} date={DATE} />);
dropFiles([
makeFile(`${DATE}_2852_ephys.bin`, 10_000_000_000),
makeFile(`${DATE}_2852_spikes.h5`, 10_000_000_000),
makeFile(`${DATE}_2852_trials.csv`, 10_000_000),
makeFile(`${DATE}_2852_log.csv`, 10_000_000),
]);
await waitFor(() => screen.getByTestId('save-btn'));
fireEvent.click(screen.getByTestId('save-btn'));
await waitFor(() => expect(sessionFilesApi.create).toHaveBeenCalled());
const [, files] = sessionFilesApi.create.mock.calls[0];
expect(files.find((f) => f.filename.endsWith('.bin')).file_type).toBe('Binary');
expect(files.find((f) => f.filename.endsWith('.h5')).file_type).toBe('HDF5');
expect(files.filter((f) => f.filename.endsWith('.csv'))).toHaveLength(2);
files.filter((f) => f.filename.endsWith('.csv')).forEach((f) => {
expect(f.file_type).toBe('CSV');
});
});
it('clears the drop preview after a successful save', async () => {
sessionFilesApi.create.mockResolvedValue([makeSavedFile('f1', 's-a1', `${DATE}_2852_trials.csv`)]);
render(<FileDropZone rows={ROWS} date={DATE} />);
dropFiles([makeFile(`${DATE}_2852_trials.csv`, 1024)]);
await waitFor(() => screen.getByTestId('save-btn'));
fireEvent.click(screen.getByTestId('save-btn'));
await waitFor(() =>
expect(screen.queryByTestId('drop-preview')).not.toBeInTheDocument()
);
});
it('shows an error message when create fails', async () => {
sessionFilesApi.create.mockRejectedValue({ message: 'Server error' });
render(<FileDropZone rows={ROWS} date={DATE} />);
dropFiles([makeFile(`${DATE}_2852_trials.csv`, 1024)]);
await waitFor(() => screen.getByTestId('save-btn'));
fireEvent.click(screen.getByTestId('save-btn'));
await waitFor(() => screen.getByText(/server error/i));
});
});
// ── Deleting saved files ───────────────────────────────────────────────────────
describe('deleting saved files', () => {
it('removes a saved file when its ✕ is clicked', async () => {
sessionFilesApi.list.mockImplementation((id) =>
id === 's-a1'
? Promise.resolve([makeSavedFile('f1', 's-a1', `${DATE}_2852_trials.csv`)])
: Promise.resolve([])
);
render(<FileDropZone rows={ROWS} date={DATE} />);
await waitFor(() => screen.getByText(`${DATE}_2852_trials.csv`));
fireEvent.click(screen.getAllByText('✕')[0]);
await waitFor(() =>
expect(sessionFilesApi.delete).toHaveBeenCalledWith('f1')
);
await waitFor(() =>
expect(screen.queryByText(`${DATE}_2852_trials.csv`)).not.toBeInTheDocument()
);
});
});