mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-29 19:36:20 -05:00
controller: de-duplicate the hardware toggles
The Hotspot, Bluetooth, Desktop, VNC and Throttle toggles had each been written out longhand at four layers. Collapse the shared parts: - useSubsystemToggle() replaces five near-identical hooks. This also fixes a latent bug: only useDesktop/useVnc restored state when a request failed, so a failed Bluetooth/Throttle/Hotspot toggle left at null and — since the status poll sees no change — the toggle stayed disabled and read as unsupported until it remounted. - SubsystemToggle renders all five, with the Desktop's stop confirmation and VNC's desktop gating as declared options rather than bespoke copies. - The fallback UI's four update*/toggle* pairs become one config-driven implementation (-100 lines). - _raise_for_action() centralises the 13 endpoint failure checks, including VNC's 409-vs-500 split. Deliberately not shared: the six get_*_status_dict() builders (payloads genuinely differ) and the per-subsystem mechanisms (nmcli/rfkill/sysfs/systemctl). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
parent
5313ab57ef
commit
07af0c9f28
5 changed files with 410 additions and 515 deletions
129
app/src/hooks/useSubsystemToggle.test.js
Normal file
129
app/src/hooks/useSubsystemToggle.test.js
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
import React from 'react';
|
||||
import {act, renderHook, waitFor} from '@testing-library/react';
|
||||
import {useBluetooth, useDesktop, useHotspot, useThrottle, useVnc} from './customHooks';
|
||||
import {StatusContext} from '../contexts/contexts';
|
||||
|
||||
jest.mock('../api/controller', () => ({
|
||||
unblockBluetooth: jest.fn().mockResolvedValue({}),
|
||||
blockBluetooth: jest.fn().mockResolvedValue({}),
|
||||
startHotspot: jest.fn().mockResolvedValue({}),
|
||||
stopHotspot: jest.fn().mockResolvedValue({}),
|
||||
startDesktop: jest.fn().mockResolvedValue({}),
|
||||
stopDesktop: jest.fn().mockResolvedValue({}),
|
||||
startVnc: jest.fn().mockResolvedValue({}),
|
||||
stopVnc: jest.fn().mockResolvedValue({}),
|
||||
enableThrottle: jest.fn().mockResolvedValue({}),
|
||||
disableThrottle: jest.fn().mockResolvedValue({}),
|
||||
getControllerStats: jest.fn().mockResolvedValue({}),
|
||||
}));
|
||||
|
||||
jest.mock('react-semantic-toasts-2', () => ({toast: jest.fn()}));
|
||||
|
||||
const controllerApi = require('../api/controller');
|
||||
|
||||
const fetchStatus = jest.fn();
|
||||
|
||||
const makeWrapper = (status) => ({children}) => (
|
||||
<StatusContext.Provider value={{status, fetchStatus}}>{children}</StatusContext.Provider>
|
||||
);
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
Object.values(controllerApi).forEach(fn => fn.mockResolvedValue && fn.mockResolvedValue({}));
|
||||
});
|
||||
|
||||
describe('subsystem toggle hooks', () => {
|
||||
// Each hook reads a different status field with different values for on/off.
|
||||
const cases = [
|
||||
{
|
||||
name: 'bluetooth', hook: useBluetooth, setter: 'setBluetooth',
|
||||
status: {bluetooth_status: 'on'}, offStatus: {bluetooth_status: 'off'},
|
||||
start: 'unblockBluetooth', stop: 'blockBluetooth',
|
||||
},
|
||||
{
|
||||
name: 'desktop', hook: useDesktop, setter: 'setDesktop',
|
||||
status: {desktop_status: 'on'}, offStatus: {desktop_status: 'off'},
|
||||
start: 'startDesktop', stop: 'stopDesktop',
|
||||
},
|
||||
{
|
||||
name: 'vnc', hook: useVnc, setter: 'setVnc',
|
||||
status: {vnc_status: 'on'}, offStatus: {vnc_status: 'off'},
|
||||
start: 'startVnc', stop: 'stopVnc',
|
||||
},
|
||||
{
|
||||
name: 'throttle', hook: useThrottle, setter: 'setThrottle',
|
||||
status: {throttle_status: 'powersave'}, offStatus: {throttle_status: 'ondemand'},
|
||||
start: 'enableThrottle', stop: 'disableThrottle',
|
||||
},
|
||||
{
|
||||
name: 'hotspot', hook: useHotspot, setter: 'setHotspot',
|
||||
status: {hotspot_status: 'connected'}, offStatus: {hotspot_status: 'disconnected'},
|
||||
start: 'startHotspot', stop: 'stopHotspot',
|
||||
},
|
||||
];
|
||||
|
||||
cases.forEach(({name, hook, setter, status, offStatus, start, stop}) => {
|
||||
describe(name, () => {
|
||||
it('reports on and off from the status field', async () => {
|
||||
const {result} = renderHook(hook, {wrapper: makeWrapper(status)});
|
||||
await waitFor(() => expect(result.current.on).toBe(true));
|
||||
|
||||
const {result: offResult} = renderHook(hook, {wrapper: makeWrapper(offStatus)});
|
||||
await waitFor(() => expect(offResult.current.on).toBe(false));
|
||||
});
|
||||
|
||||
it('reports unknown status as null so the UI can disable the toggle', async () => {
|
||||
const {result} = renderHook(hook, {wrapper: makeWrapper({})});
|
||||
await waitFor(() => expect(result.current.on).toBe(null));
|
||||
});
|
||||
|
||||
it('starts and stops through the Controller', async () => {
|
||||
const {result} = renderHook(hook, {wrapper: makeWrapper(offStatus)});
|
||||
await waitFor(() => expect(result.current.on).toBe(false));
|
||||
|
||||
await act(async () => await result.current[setter](true));
|
||||
expect(controllerApi[start]).toHaveBeenCalled();
|
||||
|
||||
await act(async () => await result.current[setter](false));
|
||||
expect(controllerApi[stop]).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('restores the previous state when the request fails', async () => {
|
||||
// Otherwise `on` stays null, and because the status poll sees no change
|
||||
// the toggle is left disabled and reading as unsupported until remount.
|
||||
controllerApi[stop].mockRejectedValue(new Error('boom'));
|
||||
const {result} = renderHook(hook, {wrapper: makeWrapper(status)});
|
||||
await waitFor(() => expect(result.current.on).toBe(true));
|
||||
|
||||
await act(async () => await result.current[setter](false));
|
||||
|
||||
expect(result.current.on).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('throttle refetches status because the governor is read from sysfs', async () => {
|
||||
const {result} = renderHook(useThrottle, {wrapper: makeWrapper({throttle_status: 'ondemand'})});
|
||||
await waitFor(() => expect(result.current.on).toBe(false));
|
||||
|
||||
await act(async () => await result.current.setThrottle(true));
|
||||
|
||||
expect(fetchStatus).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('hotspot reports a WiFi device that is in use for a network', async () => {
|
||||
const {result} = renderHook(useHotspot, {wrapper: makeWrapper({hotspot_status: 'in_use'})});
|
||||
|
||||
await waitFor(() => expect(result.current.inUse).toBe(true));
|
||||
// In use means the hotspot is not running, but starting it leaves that network.
|
||||
expect(result.current.on).toBe(false);
|
||||
});
|
||||
|
||||
it('vnc exposes whether the desktop it serves is running', async () => {
|
||||
const {result} = renderHook(useVnc, {wrapper: makeWrapper({vnc_status: 'off', desktop_status: 'on'})});
|
||||
await waitFor(() => expect(result.current.desktopRunning).toBe(true));
|
||||
|
||||
const {result: stopped} = renderHook(useVnc, {wrapper: makeWrapper({vnc_status: 'off', desktop_status: 'off'})});
|
||||
await waitFor(() => expect(stopped.current.desktopRunning).toBe(false));
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue