wrolpi/app/src/hooks/customHooks.test.js

281 lines
11 KiB
JavaScript
Raw Normal View History

import React from 'react';
import {act, renderHook} from '@testing-library/react';
ui: Common.js and thirteen more files off Semantic Phase 2, part 3. Baseline 63 -> 50. Common.js is the one that mattered: 51 files import it, so its Semantic usage was propping up the rest of the migration. Everything in it now comes from the library -- Paginator, the two search inputs, DirectorySearch, TabLinks, the toggles, the messages, APIButton, Breadcrumbs, SimpleAccordion, IframeViewer, CardPoster, FileIcon, LoadStatistic, the theme menu, and the hotspot icons. Also migrated: Placeholder, Apps, ArchivePreview, CbzViewer, AddToPlaylist, TaggedDeleteConfirmModal, the collection edit/tag forms, and four inventory components. New in the library, all in the gallery and tested: SearchBox Semantic's `<Search category>` with its grouped results shape kept, because four call sites already build it. A real combobox rather than a div that listens for clicks. Pagination Semantic's prop names; onPageChange gets the page number. TabBar the strip only -- routing stays with the caller. IconStack Semantic's IconGroup, named once for assistive technology. Table.HeaderCell sorted/onSort -- a real button carrying aria-sort. Button/IconButton translate Semantic's size names. That last one is defensive: unmigrated callers still pass size='tiny', and Mantine drops a size it does not recognise without a word, so the button quietly renders at the wrong size. Mapping here keeps the mixed state honest. Three real bugs found and fixed on the way, none of them cosmetic: * `fileSuffixIconName` returned fourteen icon names the map did not have, so opening a folder with a PDF, a Word document or an archive in it logged an error and drew a fallback glyph. All fourteen mapped, with a test that walks the mapper's own return values, and another asserting every mapped name resolves to a Tabler component that exists. * `Statistic` relied on Semantic's `Statistic.Group` for its spacing, so on the dashboard the load averages ran together into "1 MIN. LOAD5 MIN. LOAD15 MIN. LOAD". It carries its own spacing now. * Semantic's inverted stylesheet colors every descendant of an inverted segment, so Toggle labels inherited #dddddd -- white pixels in night mode, on the Settings page, which is exactly where someone adjusts their WROLPi in the dark. Restated from tokens. The baseline ratchet gained a third assertion: a migrated file may still lean on Semantic's CSS classes without importing the library, which the import check cannot see and which would collapse silently when Phase 4 deletes the stylesheet. There is one such bridge today and it is now declared.
2026-07-30 10:00:48 -06:00
import {render, screen} from '../test-utils';
import {usePages, useDriveTemperature, useDriveHealth} from './customHooks';
Warn in the navbar when a hard drive runs hot (#470) * Warn in the navbar when a hard drive runs hot. The navbar already warns about high CPU temperature; add the equivalent for drive temperature, since an overheating disk risks data loss and a user running an off-grid library won't be watching for it. - controller/lib/smart.py: build_smart_stats() returns the per-drive list plus warning thresholds (55C warn / 65C critical) so the frontend stays dumb. Reading SMART wakes a spun-down drive, so drives in standby are detected with `smartctl -n standby` (which works through the USB-SATA bridges where `hdparm -C` only reports "unknown") and NOT read; their last-known reading is reused instead. - controller/lib/status_worker.py: collect SMART on a slow 60s cadence (cached on app.state) rather than the 5s status loop, so the poll does not keep USB drives awake. The result flows into the same cached payload via a new collect_all_status(smart_stats=...) kwarg. - Frontend: useDriveTemperature() reports the hottest drive; Nav.js shows a yellow/red `hdd` icon with a popup, mirroring the CPU warning. Verified on hardware: smart_stats reports real temps and SMART runs once a minute, not every 5s. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Address Greptile review on HDD temperature warning. - _drive_is_asleep also treats SLEEP mode as asleep, not just STANDBY. smartctl -n standby exits "Device is in SLEEP mode, exit(2)" for the deeper power mode; matching only "STANDBY" meant a SLEEP drive was read and woken — the exact failure the check prevents. (TDD: added a failing SLEEP test, then fixed.) - useDriveTemperature uses ?? instead of || for the temperature fallback to express "fall back only on null/undefined". Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-03 18:46:29 -06:00
import {QueryContext, StatusContext} from '../contexts/contexts';
import {Paginator} from '../components/Common';
// Build a wrapper that provides a controlled QueryContext so usePages can read
// `o` and `l` from searchParams without a real router.
const makeWrapper = (initialParams = {}) => {
const params = new URLSearchParams(initialParams);
const Wrapper = ({children}) => {
const [searchParams, setSearchParams] = React.useState(params);
const updateQuery = (newParams) => {
const next = new URLSearchParams(searchParams.toString());
Object.entries(newParams).forEach(([k, v]) => {
if (v === null || v === undefined) {
next.delete(k);
} else {
next.set(k, String(v));
}
});
setSearchParams(next);
};
return (
<QueryContext.Provider value={{searchParams, updateQuery, getLocationStr: () => '/'}}>
{children}
</QueryContext.Provider>
);
};
return Wrapper;
};
// Mock Media so Paginator renders both mobile + tablet variants synchronously.
jest.mock('../contexts/contexts', () => {
const actual = jest.requireActual('../contexts/contexts');
return {
...actual,
Media: ({children}) => <>{children}</>,
};
});
describe('usePages.setTotal -> totalPages', () => {
// These cases describe the contract: how many pages should the Paginator
// render given a total result count and a per-page limit?
const cases = [
// [total, limit, expectedPages, label]
[0, 24, 1, 'no results -> 1 page'],
[1, 24, 1, 'fewer than one page'],
[23, 24, 1, 'just under one page'],
[24, 24, 1, 'exactly one page (BUG: reports 2)'],
[25, 24, 2, 'one full page plus one'],
[47, 24, 2, 'just under two pages'],
[48, 24, 2, 'exactly two pages (BUG: reports 3)'],
[49, 24, 3, 'just over two pages'],
[72, 24, 3, 'exactly three pages (BUG: reports 4)'],
[100, 24, 5, 'partial last page'],
[144, 24, 6, 'exactly six pages (BUG: reports 7)'],
];
test.each(cases)('total=%i limit=%i -> %i pages (%s)', (total, limit, expected) => {
const wrapper = makeWrapper({l: String(limit)});
const {result} = renderHook(() => usePages(limit), {wrapper});
act(() => {
result.current.setTotal(total);
});
expect(result.current.totalPages).toBe(expected);
});
test('the last reported page yields an offset within total (no empty trailing page)', () => {
// This is the user-visible symptom: clicking the last page link should
// produce an offset that the API can still satisfy. When totalPages is
// off-by-one, the last page maps to offset === total (or > total) and
// the API returns zero rows.
const total = 48;
const limit = 24;
const wrapper = makeWrapper({l: String(limit)});
const {result} = renderHook(() => usePages(limit), {wrapper});
act(() => {
result.current.setTotal(total);
});
act(() => {
result.current.setPage(result.current.totalPages);
});
// setPage stores (page - 1) * limit as `o` in the query.
const lastOffset = (result.current.totalPages - 1) * limit;
expect(lastOffset).toBeLessThan(total);
});
});
Warn in the navbar when a hard drive runs hot (#470) * Warn in the navbar when a hard drive runs hot. The navbar already warns about high CPU temperature; add the equivalent for drive temperature, since an overheating disk risks data loss and a user running an off-grid library won't be watching for it. - controller/lib/smart.py: build_smart_stats() returns the per-drive list plus warning thresholds (55C warn / 65C critical) so the frontend stays dumb. Reading SMART wakes a spun-down drive, so drives in standby are detected with `smartctl -n standby` (which works through the USB-SATA bridges where `hdparm -C` only reports "unknown") and NOT read; their last-known reading is reused instead. - controller/lib/status_worker.py: collect SMART on a slow 60s cadence (cached on app.state) rather than the 5s status loop, so the poll does not keep USB drives awake. The result flows into the same cached payload via a new collect_all_status(smart_stats=...) kwarg. - Frontend: useDriveTemperature() reports the hottest drive; Nav.js shows a yellow/red `hdd` icon with a popup, mirroring the CPU warning. Verified on hardware: smart_stats reports real temps and SMART runs once a minute, not every 5s. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * Address Greptile review on HDD temperature warning. - _drive_is_asleep also treats SLEEP mode as asleep, not just STANDBY. smartctl -n standby exits "Device is in SLEEP mode, exit(2)" for the deeper power mode; matching only "STANDBY" meant a SLEEP drive was read and woken — the exact failure the check prevents. (TDD: added a failing SLEEP test, then fixed.) - useDriveTemperature uses ?? instead of || for the temperature fallback to express "fall back only on null/undefined". Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-03 18:46:29 -06:00
describe('useDriveTemperature', () => {
const makeStatusWrapper = (status) => ({children}) => (
<StatusContext.Provider value={{status}}>
{children}
</StatusContext.Provider>
);
test('selects the hottest drive and reads thresholds from the payload', () => {
const status = {
smart_stats: {
drives: [
{device: 'sda', temperature: 51},
{device: 'sdb', temperature: 60},
],
high_temperature: 55,
critical_temperature: 65,
},
};
const {result} = renderHook(() => useDriveTemperature(),
{wrapper: makeStatusWrapper(status)});
expect(result.current.device).toBe('sdb');
expect(result.current.temperature).toBe(60);
expect(result.current.highTemperature).toBe(55);
expect(result.current.criticalTemperature).toBe(65);
});
test('ignores drives without a numeric temperature', () => {
const status = {
smart_stats: {
drives: [
{device: 'sda', temperature: null},
{device: 'sdb', temperature: 48},
],
high_temperature: 55,
critical_temperature: 65,
},
};
const {result} = renderHook(() => useDriveTemperature(),
{wrapper: makeStatusWrapper(status)});
expect(result.current.device).toBe('sdb');
expect(result.current.temperature).toBe(48);
});
test('defaults to no warning and default thresholds when smart_stats is absent', () => {
const {result} = renderHook(() => useDriveTemperature(),
{wrapper: makeStatusWrapper({})});
expect(result.current.device).toBeNull();
expect(result.current.temperature).toBe(0);
expect(result.current.highTemperature).toBe(55);
expect(result.current.criticalTemperature).toBe(65);
});
test('returns no drive when none report a temperature', () => {
const status = {
smart_stats: {
drives: [{device: 'sda', temperature: null}],
high_temperature: 55,
critical_temperature: 65,
},
};
const {result} = renderHook(() => useDriveTemperature(),
{wrapper: makeStatusWrapper(status)});
expect(result.current.device).toBeNull();
expect(result.current.temperature).toBe(0);
});
});
describe('useDriveHealth', () => {
const makeStatusWrapper = (status) => ({children}) => (
<StatusContext.Provider value={{status}}>
{children}
</StatusContext.Provider>
);
test('flags drives whose SMART assessment is FAIL', () => {
const status = {
smart_stats: {
drives: [
{device: 'sda', assessment: 'PASS'},
{device: 'sdb', assessment: 'FAIL'},
],
},
};
const {result} = renderHook(() => useDriveHealth(),
{wrapper: makeStatusWrapper(status)});
expect(result.current.failing).toBe(true);
expect(result.current.failingDevices).toEqual(['sdb']);
});
test('reports every failing drive', () => {
const status = {
smart_stats: {
drives: [
{device: 'sda', assessment: 'FAIL'},
{device: 'sdb', assessment: 'FAIL'},
],
},
};
const {result} = renderHook(() => useDriveHealth(),
{wrapper: makeStatusWrapper(status)});
expect(result.current.failingDevices).toEqual(['sda', 'sdb']);
});
test('does not warn when all drives PASS', () => {
const status = {
smart_stats: {drives: [{device: 'sda', assessment: 'PASS'}]},
};
const {result} = renderHook(() => useDriveHealth(),
{wrapper: makeStatusWrapper(status)});
expect(result.current.failing).toBe(false);
expect(result.current.failingDevices).toEqual([]);
});
test('ignores drives with an unknown (null) assessment', () => {
const status = {
smart_stats: {drives: [{device: 'sda', assessment: null}]},
};
const {result} = renderHook(() => useDriveHealth(),
{wrapper: makeStatusWrapper(status)});
expect(result.current.failing).toBe(false);
});
test('does not warn when smart_stats is absent', () => {
const {result} = renderHook(() => useDriveHealth(),
{wrapper: makeStatusWrapper({})});
expect(result.current.failing).toBe(false);
expect(result.current.failingDevices).toEqual([]);
});
test('surfaces WARN drives separately from FAIL', () => {
const status = {
smart_stats: {
drives: [
{device: 'sda', health: 'PASS', assessment: 'PASS'},
{device: 'sdb', health: 'WARN', assessment: 'PASS'},
{device: 'sdc', health: 'FAIL', assessment: 'FAIL'},
],
},
};
const {result} = renderHook(() => useDriveHealth(),
{wrapper: makeStatusWrapper(status)});
expect(result.current.failingDevices).toEqual(['sdc']);
expect(result.current.warning).toBe(true);
expect(result.current.warningDevices).toEqual(['sdb']);
});
test('prefers derived health over raw assessment', () => {
// A drive self-assessing PASS but with pending sectors is WARN.
const status = {
smart_stats: {drives: [{device: 'sda', health: 'WARN', assessment: 'PASS'}]},
};
const {result} = renderHook(() => useDriveHealth(),
{wrapper: makeStatusWrapper(status)});
expect(result.current.failing).toBe(false);
expect(result.current.warningDevices).toEqual(['sda']);
});
test('falls back to assessment when health is absent', () => {
const status = {
smart_stats: {drives: [{device: 'sda', assessment: 'FAIL'}]},
};
const {result} = renderHook(() => useDriveHealth(),
{wrapper: makeStatusWrapper(status)});
expect(result.current.failingDevices).toEqual(['sda']);
});
});
describe('Paginator rendering', () => {
test('renders exactly totalPages numbered page links (no phantom trailing link)', () => {
ui: the last of Semantic UI, in name and in dead CSS The library has been uninstalled for some time, but the codebase still read as though it were there: identifiers named after it, rules written against its class names, and about 180 comments explaining a rule in terms of a thing that is no longer installed. Renamed, keeping the vocabulary the app actually uses: semanticUIColorMap -> navColorHexMap, semanticColorNames -> paletteColorNames, semanticNames -> iconNameAliases, semanticSizes -> sizeAliases, and SemanticUIColors -> NavColors in wrolpi/schema.py, which is what validates nav_color on the way in. ColorToSemanticHexColor had no callers at all and is gone. The baseline ratchet has done its job -- the list reached zero, so the JSON is deleted and the guard keeps only the checks with teeth: no package, no import, no `className="ui "`, no toast library. Its Theme.tsx check went with the wrappers it was watching for. It also asserts its own walk found files, so a clean result cannot mean it scanned nothing. Deleted from App.css: every rule requiring `.ui`, `.inverted`, `.segment` or `.modal`, none of which any component has set since the stylesheet stopped loading, plus the form skeletons, ui-alerts and progress-text rules that nothing renders. Two of them were doing harm rather than nothing: the drag tints had a dark-theme variant under `.inverted` that stopped applying, leaving 15% of a fixed blue over a dark row, and now mix a token instead. Two selectors naming markup that no longer exists were live bugs. The file browser's filter had `.ui.input.file-browser-filter`, so it rendered at the browser default width on a bar laid out to give it 34em -- now qualified by its container, because `.wrolpi-action-input` sets `width: 100%` at the same specificity and is loaded later. And SearchBox takes named props rather than a rest spread, so the `id` DestinationForm hands it was dropped in silence and the label it renders pointed at nothing; the download forms' Destination field has had no associated label since the migration. "Semantic roles" survives in four places, where it means what it says. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-05 19:15:57 -06:00
// 48 results at 24 per page -> 2 pages. If totalPages is 3, the pagination
// will render an extra "3" link that fetches an empty page.
render(
<Paginator activePage={1} totalPages={2} onPageChange={() => {}}/>
);
ui: the last of Semantic UI, in name and in dead CSS The library has been uninstalled for some time, but the codebase still read as though it were there: identifiers named after it, rules written against its class names, and about 180 comments explaining a rule in terms of a thing that is no longer installed. Renamed, keeping the vocabulary the app actually uses: semanticUIColorMap -> navColorHexMap, semanticColorNames -> paletteColorNames, semanticNames -> iconNameAliases, semanticSizes -> sizeAliases, and SemanticUIColors -> NavColors in wrolpi/schema.py, which is what validates nav_color on the way in. ColorToSemanticHexColor had no callers at all and is gone. The baseline ratchet has done its job -- the list reached zero, so the JSON is deleted and the guard keeps only the checks with teeth: no package, no import, no `className="ui "`, no toast library. Its Theme.tsx check went with the wrappers it was watching for. It also asserts its own walk found files, so a clean result cannot mean it scanned nothing. Deleted from App.css: every rule requiring `.ui`, `.inverted`, `.segment` or `.modal`, none of which any component has set since the stylesheet stopped loading, plus the form skeletons, ui-alerts and progress-text rules that nothing renders. Two of them were doing harm rather than nothing: the drag tints had a dark-theme variant under `.inverted` that stopped applying, leaving 15% of a fixed blue over a dark row, and now mix a token instead. Two selectors naming markup that no longer exists were live bugs. The file browser's filter had `.ui.input.file-browser-filter`, so it rendered at the browser default width on a bar laid out to give it 34em -- now qualified by its container, because `.wrolpi-action-input` sets `width: 100%` at the same specificity and is loaded later. And SearchBox takes named props rather than a rest spread, so the `id` DestinationForm hands it was dropped in silence and the label it renders pointed at nothing; the download forms' Destination field has had no associated label since the migration. "Semantic roles" survives in four places, where it means what it says. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-05 19:15:57 -06:00
// Page numbers render as <a> elements carrying the page text.
// Tablet + mobile variants both render here because Media is mocked to
// pass children through, so each numeric page appears at least once.
expect(screen.queryAllByText('1').length).toBeGreaterThan(0);
expect(screen.queryAllByText('2').length).toBeGreaterThan(0);
expect(screen.queryByText('3')).toBeNull();
});
});