mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-30 03:46:29 -05:00
app: status and settings contexts say whether their first poll landed (#734)
Both contexts start as {} and nothing distinguished that from a
server that reported nothing, so for up to one poll interval after a
page load consumers rendered the negative case as fact: the Settings
form appeared with blank values and an "undefined/config/wrolpi.yaml"
path (the ready check `settings ? true : undefined` was always true),
the Downloads toggle was clickable and read "Downloading Disabled",
the upgrade panel said "up to date, vundefined", and the five hardware
toggles on the Controller page claimed "not supported on this server".
Invariants now enforced and tested:
* StatusContext.loaded is true once a poll returned a real status;
the API being down does not count. SettingsContext.loaded is true
once settings were fetched, and `failed` when a fetch returned
nothing before that; a failed refresh after a load keeps the last
known values and `loaded`.
* The Settings page shows its loader until loaded and its error only
when failed. The upgrade panel shows a loader until status loaded.
The Downloads toggle is disabled with a neutral label until then.
* A subsystem toggle is disabled without claiming "unsupported" while
status is unknown (undefined); "unsupported" (null) is reserved for
a loaded status that lacks the field. A call in flight is unknown,
not unsupported.
The context fixtures declare loaded so existing specs render as
before.
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
306386e801
commit
eead78007d
8 changed files with 235 additions and 25 deletions
|
|
@ -1154,7 +1154,8 @@ export const useDownloads = () => {
|
|||
|
||||
// Every hardware toggle works the same way: one field of the shared status object
|
||||
// says whether the subsystem is on, and starting/stopping it is a Controller call.
|
||||
// `on` is null when the status is unknown, which the UI renders as unsupported.
|
||||
// `on` is undefined until the status has loaded (unknown, the toggle is disabled), null once it
|
||||
// has loaded without the field (unsupported on this server), and otherwise a boolean.
|
||||
const useSubsystemToggle = ({
|
||||
statusField,
|
||||
onValue = 'on',
|
||||
|
|
@ -1165,23 +1166,26 @@ const useSubsystemToggle = ({
|
|||
errorDescription,
|
||||
refetchStatus = false,
|
||||
}) => {
|
||||
const [on, setOn] = useState(null);
|
||||
const {status, fetchStatus} = useContext(StatusContext);
|
||||
const [on, setOn] = useState(undefined);
|
||||
const {status, fetchStatus, loaded} = useContext(StatusContext);
|
||||
const value = status?.[statusField];
|
||||
|
||||
useEffect(() => {
|
||||
if (value === onValue) {
|
||||
if (!loaded) {
|
||||
setOn(undefined);
|
||||
} else if (value === onValue) {
|
||||
setOn(true);
|
||||
} else if (offValues.includes(value)) {
|
||||
setOn(false);
|
||||
} else {
|
||||
setOn(null);
|
||||
}
|
||||
}, [value]);
|
||||
}, [value, loaded]);
|
||||
|
||||
const setSubsystem = async (enable) => {
|
||||
const previous = on;
|
||||
setOn(null);
|
||||
// Unknown while the call is in flight, not unsupported.
|
||||
setOn(undefined);
|
||||
try {
|
||||
if (enable) {
|
||||
await start();
|
||||
|
|
@ -1331,8 +1335,16 @@ export const useSearchDirectories = (value) => {
|
|||
}
|
||||
|
||||
export const useSettings = () => {
|
||||
// `settings` is {} until the first fetch lands, and a consumer cannot tell that from real
|
||||
// settings by looking at it. `loaded` is true once a fetch returned settings; `failed` is
|
||||
// true when a fetch returned nothing before anything loaded. A failed refresh after a load
|
||||
// keeps the last known good values and `loaded`.
|
||||
const [settings, setSettings] = useState({});
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
const [failed, setFailed] = useState(false);
|
||||
const [pending, setPending] = useState(false);
|
||||
// Read by fetchSettings after an await, when the closed-over `loaded` may be stale.
|
||||
const loadedRef = useRef(false);
|
||||
|
||||
const fetchSettings = async () => {
|
||||
if (window.apiDown) {
|
||||
|
|
@ -1343,9 +1355,17 @@ export const useSettings = () => {
|
|||
const newSettings = await getSettings();
|
||||
if (newSettings) {
|
||||
setSettings(newSettings);
|
||||
loadedRef.current = true;
|
||||
setLoaded(true);
|
||||
setFailed(false);
|
||||
} else if (!loadedRef.current) {
|
||||
setFailed(true);
|
||||
}
|
||||
} catch (e) {
|
||||
// Don't clear settings on error — keep the last known good values.
|
||||
if (!loadedRef.current) {
|
||||
setFailed(true);
|
||||
}
|
||||
} finally {
|
||||
setPending(false);
|
||||
}
|
||||
|
|
@ -1355,15 +1375,15 @@ export const useSettings = () => {
|
|||
fetchSettings();
|
||||
}, []);
|
||||
|
||||
return {settings, pending, fetchSettings, saveSettings};
|
||||
return {settings, loaded, failed, pending, fetchSettings, saveSettings};
|
||||
}
|
||||
|
||||
export const useSettingsInterval = () => {
|
||||
const {settings, pending, fetchSettings, saveSettings} = useSettings();
|
||||
const {settings, loaded, failed, pending, fetchSettings, saveSettings} = useSettings();
|
||||
|
||||
useRecurringTimeout(fetchSettings, 1000 * 10);
|
||||
|
||||
return {settings, pending, fetchSettings, saveSettings};
|
||||
return {settings, loaded, failed, pending, fetchSettings, saveSettings};
|
||||
}
|
||||
|
||||
export const useMediaDirectory = () => {
|
||||
|
|
@ -1373,7 +1393,11 @@ export const useMediaDirectory = () => {
|
|||
}
|
||||
|
||||
export const useStatus = () => {
|
||||
// `status` is {} until the first poll lands, and a consumer cannot tell that from a server
|
||||
// that reported nothing. `loaded` is true once a poll returned a real status; the API being
|
||||
// down does not count.
|
||||
const [status, setStatus] = useState({});
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
|
||||
const fetchStatus = async () => {
|
||||
try {
|
||||
|
|
@ -1398,6 +1422,9 @@ export const useStatus = () => {
|
|||
|
||||
// Merge both responses - controller stats override app status for system info
|
||||
setStatus({...appStatus, ...controllerStats});
|
||||
if (!window.apiDown) {
|
||||
setLoaded(true);
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof ApiDownError) {
|
||||
// API is down, do not log this error.
|
||||
|
|
@ -1417,15 +1444,15 @@ export const useStatus = () => {
|
|||
window.apiDown = false;
|
||||
}, []);
|
||||
|
||||
return {status, fetchStatus}
|
||||
return {status, loaded, fetchStatus}
|
||||
}
|
||||
|
||||
export const useStatusInterval = () => {
|
||||
const {status, fetchStatus} = useStatus();
|
||||
const {status, loaded, fetchStatus} = useStatus();
|
||||
|
||||
useRecurringTimeout(fetchStatus, 1000 * 3);
|
||||
|
||||
return {status, fetchStatus};
|
||||
return {status, loaded, fetchStatus};
|
||||
}
|
||||
|
||||
export const StatusProvider = (props) => {
|
||||
|
|
|
|||
|
|
@ -2,9 +2,13 @@ import React from 'react';
|
|||
import {act, renderHook} from '@testing-library/react';
|
||||
import {render, screen} from '../test-utils';
|
||||
import {
|
||||
usePages, useDriveTemperature, useDriveHealth, useSearchChannels, useStatistics, useVideoExtras,
|
||||
usePages, useDriveTemperature, useDriveHealth, useSearchChannels, useSettings, useStatistics, useStatus,
|
||||
useVideoExtras,
|
||||
} from './customHooks';
|
||||
import {getStatistics, getVideoComments, getVideoDescription, searchChannels} from '../api';
|
||||
import {
|
||||
ApiDownError, getSettings, getStatistics, getStatus, getVideoComments, getVideoDescription, searchChannels,
|
||||
} from '../api';
|
||||
import {getControllerStats} from '../api/controller';
|
||||
import {QueryContext, StatusContext} from '../contexts/contexts';
|
||||
import {Paginator} from '../components/Common';
|
||||
|
||||
|
|
@ -42,6 +46,13 @@ jest.mock('../api', () => ({
|
|||
getVideoDescription: jest.fn(),
|
||||
searchChannels: jest.fn(),
|
||||
getStatistics: jest.fn(),
|
||||
getStatus: jest.fn(),
|
||||
getSettings: jest.fn(),
|
||||
}));
|
||||
|
||||
jest.mock('../api/controller', () => ({
|
||||
...jest.requireActual('../api/controller'),
|
||||
getControllerStats: jest.fn(),
|
||||
}));
|
||||
|
||||
// Mock Media so Paginator renders both mobile + tablet variants synchronously.
|
||||
|
|
@ -580,3 +591,81 @@ describe('useStatistics', () => {
|
|||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('useStatus loaded', () => {
|
||||
// `loaded` is false until the first poll returns a real status, so consumers can tell "not
|
||||
// polled yet" from "the server reported nothing".
|
||||
beforeEach(() => {
|
||||
getStatus.mockReset();
|
||||
getControllerStats.mockReset();
|
||||
getControllerStats.mockResolvedValue({});
|
||||
});
|
||||
|
||||
test('is false before any poll', () => {
|
||||
const {result} = renderHook(() => useStatus());
|
||||
expect(result.current.loaded).toBe(false);
|
||||
expect(result.current.status).toEqual({});
|
||||
});
|
||||
|
||||
test('is true once a poll returned a status', async () => {
|
||||
getStatus.mockResolvedValue({version: '1.0'});
|
||||
const {result} = renderHook(() => useStatus());
|
||||
await act(async () => {
|
||||
await result.current.fetchStatus();
|
||||
});
|
||||
expect(result.current.loaded).toBe(true);
|
||||
expect(result.current.status.version).toBe('1.0');
|
||||
});
|
||||
|
||||
test('stays false while the API is down', async () => {
|
||||
getStatus.mockRejectedValue(new ApiDownError('down'));
|
||||
const {result} = renderHook(() => useStatus());
|
||||
await act(async () => {
|
||||
await result.current.fetchStatus();
|
||||
});
|
||||
expect(result.current.loaded).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('useSettings loaded', () => {
|
||||
beforeEach(() => {
|
||||
getSettings.mockReset();
|
||||
window.apiDown = false;
|
||||
});
|
||||
|
||||
test('is false before the fetch returns, then true', async () => {
|
||||
let resolve;
|
||||
getSettings.mockReturnValue(new Promise(res => resolve = res));
|
||||
const {result} = renderHook(() => useSettings());
|
||||
expect(result.current.loaded).toBe(false);
|
||||
expect(result.current.failed).toBe(false);
|
||||
await act(async () => {
|
||||
resolve({media_directory: '/media/wrolpi'});
|
||||
});
|
||||
expect(result.current.loaded).toBe(true);
|
||||
expect(result.current.settings.media_directory).toBe('/media/wrolpi');
|
||||
});
|
||||
|
||||
test('a fetch that returns nothing before anything loaded marks failed', async () => {
|
||||
getSettings.mockResolvedValue(undefined);
|
||||
const {result} = renderHook(() => useSettings());
|
||||
await act(async () => {
|
||||
});
|
||||
expect(result.current.loaded).toBe(false);
|
||||
expect(result.current.failed).toBe(true);
|
||||
});
|
||||
|
||||
test('a failed refresh after a successful load keeps loaded and the last settings', async () => {
|
||||
getSettings.mockResolvedValueOnce({media_directory: '/media/wrolpi'});
|
||||
const {result} = renderHook(() => useSettings());
|
||||
await act(async () => {
|
||||
});
|
||||
getSettings.mockRejectedValue(new Error('boom'));
|
||||
await act(async () => {
|
||||
await result.current.fetchSettings();
|
||||
});
|
||||
expect(result.current.loaded).toBe(true);
|
||||
expect(result.current.failed).toBe(false);
|
||||
expect(result.current.settings.media_directory).toBe('/media/wrolpi');
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ const controllerApi = require('../api/controller');
|
|||
const fetchStatus = jest.fn();
|
||||
|
||||
const makeWrapper = (status) => ({children}) => (
|
||||
<StatusContext.Provider value={{status, fetchStatus}}>{children}</StatusContext.Provider>
|
||||
<StatusContext.Provider value={{status, fetchStatus, loaded: true}}>{children}</StatusContext.Provider>
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue