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:
lrnselfreliance 2026-09-26 17:25:46 -06:00 • committed by GitHub
parent 306386e801
commit eead78007d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 235 additions and 25 deletions

View file

@ -928,7 +928,10 @@ export function HelpHeader({
// why it cannot be used. `confirmStop` asks before switching off.
function SubsystemToggle({label, on, onChange, unsupportedMessage, info = null, disabled = null, confirmStop = null}) {
const [confirmOpen, setConfirmOpen] = React.useState(false);
// null = the status has loaded without this subsystem; undefined = the status has not loaded
// yet (or a call is in flight). Both disable the toggle; only the first claims unsupported.
const unsupported = on === null;
const unknown = on === undefined;
const handleChange = (checked) => {
if (!checked && confirmStop) {
@ -961,11 +964,11 @@ function SubsystemToggle({label, on, onChange, unsupportedMessage, info = null,
</Confirm>}
<Toggle
label={label}
disabled={disabled === null ? unsupported : disabled}
disabled={disabled === null ? (unsupported || unknown) : disabled}
checked={on === true}
onChange={handleChange}
/>
{popup && <InfoPopup content={popup}/>}
{popup && <span data-testid='subsystem-popup'><InfoPopup content={popup}/></span>}
</div>;
}
@ -1065,9 +1068,10 @@ export function Toggle({label, checked, disabled, onChange, icon, popupContent =
export function DisableDownloadsToggle() {
const [pending, setPending] = React.useState(false);
const {status, fetchStatus} = React.useContext(StatusContext);
const {status, fetchStatus, loaded} = React.useContext(StatusContext);
const {downloads} = status ? status : {downloads: null};
// Until the first status poll lands there is no downloads state to show, only a disabled toggle.
const downloads = loaded ? status?.downloads : null;
const wrolModeEnabled = useWROLMode();
const setDownloads = async (enable) => {
@ -1082,8 +1086,9 @@ export function DisableDownloadsToggle() {
}
const on = downloads && downloads['disabled'] === false && downloads['stopped'] === false;
const label = downloads === null ? 'Downloading' : on === true ? 'Downloading Enabled' : 'Downloading Disabled';
return <Toggle
label={on === true ? 'Downloading Enabled' : 'Downloading Disabled'}
label={label}
disabled={wrolModeEnabled || pending || downloads === null}
checked={on === true}
onChange={setDownloads}

View file

@ -0,0 +1,69 @@
import React from 'react';
import {render, screen} from '../test-utils';
import {DisableDownloadsToggle, HotspotToggle} from './Common';
import {SettingsPage, UpgradeSegment} from './admin/Settings';
// The status and settings contexts start empty and are filled by the first poll. Until then a
// consumer must not claim a negative ("Downloading Disabled", "not supported", "up to date") or
// render a form over blank values; `loaded` on each context says whether the first poll landed.
jest.mock('../api', () => ({
...jest.requireActual('../api'),
getConfigs: jest.fn(() => new Promise(() => {
})),
}));
const unloadedStatus = {status: {}, loaded: false};
describe('DisableDownloadsToggle before status has loaded', () => {
test('is disabled and does not claim downloading is disabled', () => {
render(<DisableDownloadsToggle/>, {status: unloadedStatus});
expect(screen.getByTestId('toggle')).toBeDisabled();
expect(screen.queryByText('Downloading Disabled')).not.toBeInTheDocument();
});
test('once loaded, reads the downloads state', () => {
render(<DisableDownloadsToggle/>, {status: {status: {downloads: {disabled: true, stopped: false}}, loaded: true}});
expect(screen.getByText('Downloading Disabled')).toBeInTheDocument();
expect(screen.getByTestId('toggle')).toBeEnabled();
});
});
describe('HotspotToggle before status has loaded', () => {
test('is disabled without claiming the hotspot is unsupported', () => {
render(<HotspotToggle/>, {status: unloadedStatus});
expect(screen.getByTestId('toggle')).toBeDisabled();
expect(screen.queryByTestId('subsystem-popup')).not.toBeInTheDocument();
});
test('once loaded without a hotspot field, is unsupported', () => {
render(<HotspotToggle/>, {status: {status: {}, loaded: true}});
expect(screen.getByTestId('toggle')).toBeDisabled();
expect(screen.getByTestId('subsystem-popup')).toBeInTheDocument();
});
});
describe('UpgradeSegment before status has loaded', () => {
test('does not claim the WROLPi is up to date', () => {
render(<UpgradeSegment/>, {status: unloadedStatus});
expect(screen.queryByText(/up to date/)).not.toBeInTheDocument();
});
test('once loaded with no update, says so', () => {
render(<UpgradeSegment/>, {status: {status: {update_available: false, version: '1.0'}, loaded: true}});
expect(screen.getByText(/up to date/)).toBeInTheDocument();
});
});
describe('SettingsPage before settings have loaded', () => {
test('shows a loader, not a form over blank values', () => {
const {container} = render(<SettingsPage/>, {settings: {settings: {}, loaded: false, failed: false}});
expect(container.querySelector('.wrolpi-loading')).toBeInTheDocument();
expect(screen.queryByText(/Any changes will be written/)).not.toBeInTheDocument();
});
test('shows an error when the settings could not be fetched', () => {
render(<SettingsPage/>, {settings: {settings: {}, loaded: false, failed: true}});
expect(screen.getByText('Unable to fetch settings')).toBeInTheDocument();
});
});

View file

@ -101,8 +101,8 @@ export function ShutdownButton() {
</APIButton>
}
function UpgradeSegment() {
const {status, fetchStatus} = React.useContext(StatusContext);
export function UpgradeSegment() {
const {status, fetchStatus, loaded} = React.useContext(StatusContext);
const dockerized = useDockerized();
const [upgrading, setUpgrading] = React.useState(false);
const [checking, setChecking] = React.useState(false);
@ -144,6 +144,14 @@ function UpgradeSegment() {
</Panel>;
}
// Nothing is known until the first status poll lands; "up to date" is a claim about the server.
if (!loaded) {
return <Panel id='upgrade'>
<Header as='h3'>System Upgrade</Header>
<Loading/>
</Panel>;
}
// No update available
if (!status?.update_available) {
return <Panel id='upgrade'>
@ -325,7 +333,9 @@ export function SettingsPage() {
const [isDevMode, setIsDevMode] = React.useState(false);
// Get current settings from the API.
const {settings, saveSettings, fetchSettings} = React.useContext(SettingsContext);
const {
settings, saveSettings, fetchSettings, loaded: settingsLoaded, failed: settingsFailed,
} = React.useContext(SettingsContext);
const {status} = React.useContext(StatusContext);
// Used to track changes to the settings form between saves/loads.
const [state, setState] = React.useState({});
@ -463,7 +473,8 @@ export function SettingsPage() {
React.useEffect(() => {
console.debug('settings changed, replacing state...');
setReady(settings ? true : undefined);
// true = show the form, false = still loading, undefined = could not fetch.
setReady(settingsLoaded ? true : settingsFailed ? undefined : false);
setState({
// Only these settings can be changed on the SettingsPage.
archive_destination: settings.archive_destination,
@ -490,7 +501,7 @@ export function SettingsPage() {
playlists_destination: settings.playlists_destination,
save_ffprobe_json: settings.save_ffprobe_json,
});
}, [JSON.stringify(settings)]);
}, [JSON.stringify(settings), settingsLoaded, settingsFailed]);
const handleInputChange = async (name, value) => {
setState({...state, [name]: value});