fix(templates-screen): exclude placeholder blocks, warn on midnight crossing
GET /api/blocks emits a synthesized negative-id "(none)" row per empty block group; the block picker was including it as a selectable option. Filter to b.id > 0, mirroring the backend's real block ids. Also mirror ReplaceTemplateItemsHandler's new end-of-day validation on the client: flag an item whose start time + block duration crosses midnight and disable Save, same as the existing overlap-warning path. Adds coverage: block picker excludes negative-id rows; validate() flags a midnight-crossing item.
This commit is contained in:
@@ -152,6 +152,44 @@ describe('TemplatesScreen', () => {
|
||||
expect(sent.items).toEqual([{ blockId: 10, startTime: '06:00:00' }]);
|
||||
});
|
||||
|
||||
it('excludes the synthesized negative-id "(none)" placeholder row from the block picker', async () => {
|
||||
window.history.pushState({}, '', '/app/templates/4');
|
||||
mockApi({
|
||||
items: [],
|
||||
onRequest: (url, method) => {
|
||||
if (url === '/api/blocks' && method === 'GET') {
|
||||
return jsonResponse([
|
||||
...blocks,
|
||||
{ id: -1, groupId: 1, groupName: 'Morning Blocks', name: '(none)', minutes: 0, stopScheduling: 'AfterDurationEnd' }
|
||||
]);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
});
|
||||
render(<TemplatesScreen />);
|
||||
|
||||
await waitFor(() => expect(screen.getByDisplayValue('Weekdays')).toBeInTheDocument());
|
||||
|
||||
fireEvent.change(screen.getByLabelText('Block group'), { target: { value: '1' } });
|
||||
|
||||
const blockSelect = screen.getByLabelText('Block') as HTMLSelectElement;
|
||||
const optionValues = Array.from(blockSelect.options).map((o) => o.value);
|
||||
expect(optionValues).toEqual(['', '10']);
|
||||
});
|
||||
|
||||
it('shows a midnight-crossing warning and blocks save when an item crosses midnight', async () => {
|
||||
window.history.pushState({}, '', '/app/templates/4');
|
||||
mockApi({
|
||||
items: [{ blockId: 10, blockName: 'Cartoons', blockMinutes: 60, startTime: '23:30:00' }]
|
||||
});
|
||||
render(<TemplatesScreen />);
|
||||
|
||||
await waitFor(() => expect(screen.getByDisplayValue('Weekdays')).toBeInTheDocument());
|
||||
|
||||
expect(await screen.findByText(/crosses midnight/)).toBeInTheDocument();
|
||||
expect(screen.getByRole('button', { name: /Save template/ })).toBeDisabled();
|
||||
});
|
||||
|
||||
it('shows an overlap warning and blocks save when two items intersect', async () => {
|
||||
window.history.pushState({}, '', '/app/templates/4');
|
||||
mockApi({
|
||||
|
||||
@@ -96,12 +96,26 @@ function findOverlap(items: DraftItem[]): null | string {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Mirrors ReplaceTemplateItemsHandler's end-of-day check: an item's end time (start + block
|
||||
// duration) must not cross midnight. Exactly 24:00 is allowed; anything past it is rejected.
|
||||
function findMidnightCrossing(items: DraftItem[]): null | string {
|
||||
for (const item of items) {
|
||||
const start = timeToMinutes(item.startTime);
|
||||
const end = start + item.blockMinutes;
|
||||
if (end > 24 * 60) {
|
||||
return `"${item.blockName}" (${minutesToClock(start)}-${minutesToClock(end)}) crosses midnight, which is not supported`;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function validate(draft: Draft): null | string {
|
||||
if (!draft.name.trim()) {
|
||||
return 'Template name is required';
|
||||
}
|
||||
|
||||
return findOverlap(draft.items);
|
||||
return findMidnightCrossing(draft.items) ?? findOverlap(draft.items);
|
||||
}
|
||||
|
||||
type Mode = { id: number; kind: 'edit' } | { kind: 'list' };
|
||||
@@ -575,7 +589,9 @@ function TemplateEditor({ templateId }: { templateId: number }) {
|
||||
[...blockGroups].sort((a, b) => a.name.localeCompare(b.name)).map((g) => ({ label: g.name, value: String(g.id) }))
|
||||
);
|
||||
const blocksInGroup = addGroupId
|
||||
? blocks.filter((b) => b.groupId === Number(addGroupId)).sort((a, b) => a.name.localeCompare(b.name))
|
||||
? blocks
|
||||
.filter((b) => b.groupId === Number(addGroupId) && b.id > 0)
|
||||
.sort((a, b) => a.name.localeCompare(b.name))
|
||||
: [];
|
||||
const blockOptions = [{ label: 'Select a block…', value: '' }].concat(
|
||||
blocksInGroup.map((b) => ({ label: b.name, value: String(b.id) }))
|
||||
|
||||
Reference in New Issue
Block a user