Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* "Update Settings": which fields each choice shows, and that the form hands
* the settings to its submit for UPDATE only.
*/

import { fireEvent, render, screen } from '@testing-library/react';
import { useState } from 'react';
import { describe, expect, it, vi } from 'vitest';
import {
SoftwareActionForm,
type SoftwareActionFormProps,
type SoftwareActionFormValues,
} from '../software-action-form';
import {
DEFAULT_SOFTWARE_UPDATE_SETTINGS,
SoftwareUpdateSettingsFields,
type SoftwareUpdateSettingsValues,
} from '../update-settings-fields';

function Harness({ initial = DEFAULT_SOFTWARE_UPDATE_SETTINGS }: { initial?: SoftwareUpdateSettingsValues }) {
const [value, setValue] = useState(initial);
return <SoftwareUpdateSettingsFields value={value} onChange={setValue} />;
}

/** The header's primary button; the phone's bottom bar draws a second copy of it. */
const submitButton = (name: string) => screen.getAllByRole('button', { name })[0];

const base: SoftwareActionFormProps = {
action: 'UPDATE',
onSubmit: () => {},
timing: {
timeReferenceOptions: [{ value: 'SERVER', label: 'Your account timezone' }],
getTimeOptions: () => [],
getEarliestDay: () => undefined,
getStartError: () => undefined,
},
initialValues: { timeReference: 'SERVER' },
renderPackageSearch: () => <span>package field</span>,
renderDevicePicker: () => <span>device picker</span>,
showUpdateSettings: true,
};

describe('SoftwareUpdateSettingsFields', () => {
it('opens on the defaults: wait & notify, 7 days, 5 minutes, auto-updated with the same settings', () => {
render(<Harness />);
expect(screen.getByRole('radio', { name: /Wait & notify/ }).getAttribute('aria-checked')).toBe('true');
expect(screen.getByLabelText('Deadline').textContent).toContain('7 days after the first try');
expect(screen.getByLabelText('Countdown').textContent).toContain('5 minutes');
expect(screen.getByRole('checkbox', { name: /Keep it auto-updated/ }).getAttribute('aria-checked')).toBe('true');
expect(screen.getByRole('checkbox', { name: /Same settings for auto-update/ })).toBeTruthy();
expect(screen.queryByText('For auto-updates')).toBeNull();
});

it('Force Update keeps only the countdown, and says later releases are forced too', () => {
render(<Harness />);
fireEvent.click(screen.getByRole('radio', { name: /Force Update/ }));
expect(screen.queryByRole('checkbox', { name: /Countdown after the deadline/ })).toBeNull();
expect(screen.queryByLabelText('Deadline')).toBeNull();
expect(screen.getByLabelText('Countdown')).toBeTruthy();
expect(screen.getByText('Later releases will also be force-updated.')).toBeTruthy();
});

it('without a deadline there is nothing to pick', () => {
render(<Harness />);
fireEvent.click(screen.getByRole('checkbox', { name: /Countdown after the deadline/ }));
expect(screen.queryByLabelText('Deadline')).toBeNull();
expect(screen.queryByLabelText('Countdown')).toBeNull();
});

it('auto-update off hides its settings; separate settings get their own block', () => {
render(<Harness />);
fireEvent.click(screen.getByRole('checkbox', { name: /Same settings for auto-update/ }));
expect(screen.getByText('For auto-updates')).toBeTruthy();
expect(screen.getAllByRole('radiogroup')).toHaveLength(2);

fireEvent.click(screen.getByRole('checkbox', { name: /Keep it auto-updated/ }));
expect(screen.queryByRole('checkbox', { name: /Same settings for auto-update/ })).toBeNull();
expect(screen.queryByText('For auto-updates')).toBeNull();
});
});

describe('SoftwareActionForm update settings', () => {
it('UPDATE shows them and submits them', () => {
const onSubmit = vi.fn<(values: SoftwareActionFormValues) => void>();
render(<SoftwareActionForm {...base} onSubmit={onSubmit} />);
expect(screen.getByRole('heading', { name: 'Update Settings' })).toBeTruthy();
fireEvent.click(submitButton('Run Update'));
expect(onSubmit.mock.calls[0][0].updateSettings).toEqual(DEFAULT_SOFTWARE_UPDATE_SETTINGS);
});

it('INSTALL has none', () => {
const onSubmit = vi.fn<(values: SoftwareActionFormValues) => void>();
render(<SoftwareActionForm {...base} action="INSTALL" onSubmit={onSubmit} />);
expect(screen.queryByRole('heading', { name: 'Update Settings' })).toBeNull();
fireEvent.click(submitButton('Run Installation'));
expect(onSubmit.mock.calls[0][0].updateSettings).toBeUndefined();
});

it('a host that does not ask for them gets none and submits none', () => {
const onSubmit = vi.fn<(values: SoftwareActionFormValues) => void>();
const { showUpdateSettings: _, ...withoutSettings } = base;
render(<SoftwareActionForm {...withoutSettings} onSubmit={onSubmit} />);
expect(screen.queryByRole('heading', { name: 'Update Settings' })).toBeNull();
fireEvent.click(submitButton('Run Update'));
expect(onSubmit.mock.calls[0][0].updateSettings).toBeUndefined();
});
});
11 changes: 11 additions & 0 deletions openframe-frontend-core/src/components/features/software/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,14 @@ export {
} from './software-action-form';
export { newSoftwareRow, type SelectedPackage, type SoftwareRow } from './software-row';
export { type PackageSearchSlotProps, SoftwareRowFields, type SoftwareRowFieldsProps } from './software-row-fields';
export {
DEFAULT_SOFTWARE_UPDATE_POLICY,
DEFAULT_SOFTWARE_UPDATE_SETTINGS,
SOFTWARE_UPDATE_COUNTDOWN_OPTIONS,
SOFTWARE_UPDATE_DEADLINE_OPTIONS,
type SoftwareUpdateMode,
type SoftwareUpdatePolicy,
SoftwareUpdateSettingsFields,
type SoftwareUpdateSettingsFieldsProps,
type SoftwareUpdateSettingsValues,
} from './update-settings-fields';
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { SoftwareScheduleFields, type SoftwareScheduleTiming } from './schedule-
import { SOFTWARE_ACTION_COPY, type SoftwareActionKind } from './software-action-copy';
import { newSoftwareRow, type SoftwareRow } from './software-row';
import { type PackageSearchSlotProps, SoftwareRowFields } from './software-row-fields';
import {
DEFAULT_SOFTWARE_UPDATE_SETTINGS,
SoftwareUpdateSettingsFields,
type SoftwareUpdateSettingsValues,
} from './update-settings-fields';

/** Run the packages now, or at a scheduled start. */
export type SoftwareRunMode = 'now' | 'schedule';
Expand All @@ -20,6 +25,8 @@ export interface SoftwareActionFormValues<TRef extends string = string> {
date: Date | null;
time: string;
timeReference: TRef;
/** UPDATE only: "Update Settings". Undefined for INSTALL, and when the form hides them (`showUpdateSettings`). */
updateSettings?: SoftwareUpdateSettingsValues;
}

/** What the device picker slot is told: which devices the chosen packages can run on. */
Expand Down Expand Up @@ -62,6 +69,11 @@ export interface SoftwareActionFormProps<TRef extends string = string> {
* schedule's fields on the same row, so the devices start higher.
*/
runModeLayout?: 'stacked' | 'inline';
/**
* UPDATE only: the "Update Settings" block under the timing. Default false, so a host that does not send the
* settings yet never shows choices that go nowhere; the host that submits `updateSettings` passes true.
*/
showUpdateSettings?: boolean;
/** The "Device Selection" heading over the picker. Default true; a host that titles the picker itself passes false. */
showDeviceHeading?: boolean;
className?: string;
Expand Down Expand Up @@ -91,6 +103,7 @@ export function SoftwareActionForm<TRef extends string = string>({
renderDevicePicker,
addRows = true,
runModeLayout = 'stacked',
showUpdateSettings = false,
showDeviceHeading = true,
className = 'px-[var(--spacing-system-l)] pb-[var(--spacing-system-l)]',
}: SoftwareActionFormProps<TRef>) {
Expand All @@ -101,6 +114,10 @@ export function SoftwareActionForm<TRef extends string = string>({
const [date, setDate] = useState<Date | null>(initialValues.date ?? null);
const [time, setTime] = useState(initialValues.time ?? '');
const [timeReference, setTimeReference] = useState<TRef>(initialValues.timeReference);
const [updateSettings, setUpdateSettings] = useState<SoftwareUpdateSettingsValues>(
() => initialValues.updateSettings ?? DEFAULT_SOFTWARE_UPDATE_SETTINGS,
);
const withUpdateSettings = action === 'UPDATE' && showUpdateSettings;

// The picker's frame: devices on the OS the chosen packages install on.
const osTypesKey = [...new Set(rows.map(row => PACKAGE_MANAGER_OS[row.packageManager]))].sort().join(',');
Expand All @@ -110,7 +127,15 @@ export function SoftwareActionForm<TRef extends string = string>({
{
label: mode === 'now' ? copy.runLabel : copy.scheduleLabel,
variant: 'accent',
onClick: () => onSubmit({ rows, mode, date, time, timeReference }),
onClick: () =>
onSubmit({
rows,
mode,
date,
time,
timeReference,
updateSettings: withUpdateSettings ? updateSettings : undefined,
}),
disabled: submitDisabled,
loading: submitting,
},
Expand Down Expand Up @@ -190,6 +215,8 @@ export function SoftwareActionForm<TRef extends string = string>({
</>
)}

{withUpdateSettings && <SoftwareUpdateSettingsFields value={updateSettings} onChange={setUpdateSettings} />}

{showDeviceHeading && (
<h2 className="pt-[var(--spacing-system-l)] text-ods-text-primary text-h2">Device Selection</h2>
)}
Expand Down
Loading
Loading