wrolpi/app/src/components/admin/UpgradeSegment.test.js
lrnselfreliance d88db90ce9
The Settings upgrade can switch to a chosen branch (#954)
* The Settings upgrade can switch to a chosen branch

The Upgrade panel gains a Branch input: a dropdown listing the current
branch, master, and release that also takes a typed branch name.
/api/upgrade/start accepts an optional {"branch": ...}; without it the
current branch is upgraded as before.  A branch is written into the systemd
env file and passed to `upgrade.sh -b`, so only plain git branch names are
accepted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* The Controller upgrade refuses a branch that is not a plain branch name

The Controller's upgrade script wrote its branch straight into the systemd
env file, so a newline in it added a variable, and a leading dash became an
upgrade.sh option.  It now accepts the same branch names as the API, and no
script parameter may span lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 08:49:26 -07:00

63 lines
2.6 KiB
JavaScript

import React from 'react';
import {fireEvent, screen, waitFor} from '@testing-library/react';
import {render} from '../../test-utils';
import {UpgradeSegment} from './Settings';
import {triggerUpgrade} from '../../api';
jest.mock('../../api', () => ({
...jest.requireActual('../../api'),
// Not ok, so the page does not navigate to the Controller.
triggerUpgrade: jest.fn(() => Promise.resolve({ok: false})),
}));
const upToDate = {status: {update_available: false, version: '1.0', git_branch: 'feature'}, loaded: true};
const updateAvailable = {
status: {update_available: true, version: '1.0', git_branch: 'release', commits_behind: 2},
loaded: true,
};
const branchInput = () => screen.getByLabelText('Branch', {selector: 'input'});
const confirmUpgrade = async (buttonName) => {
fireEvent.click(screen.getByRole('button', {name: buttonName}));
fireEvent.click(await screen.findByRole('button', {name: 'Start Upgrade'}));
};
beforeEach(() => {
triggerUpgrade.mockClear();
});
describe('UpgradeSegment branch picker', () => {
test('starts on the current branch and lists it with master and release', async () => {
render(<UpgradeSegment/>, {status: upToDate});
expect(branchInput()).toHaveValue('feature');
fireEvent.focus(branchInput());
fireEvent.click(branchInput());
const options = (await screen.findAllByRole('option')).map(i => i.textContent);
expect(options).toEqual(['feature', 'master', 'release']);
});
test('an up to date branch offers no upgrade until another branch is chosen', async () => {
render(<UpgradeSegment/>, {status: upToDate});
expect(screen.queryByRole('button', {name: /^Upgrade/})).not.toBeInTheDocument();
fireEvent.change(branchInput(), {target: {value: 'master'}});
await confirmUpgrade('Upgrade to master');
await waitFor(() => expect(triggerUpgrade).toHaveBeenCalledWith('master'));
});
test('a typed branch name is upgraded to', async () => {
render(<UpgradeSegment/>, {status: updateAvailable});
fireEvent.change(branchInput(), {target: {value: ' my-branch '}});
await confirmUpgrade('Upgrade to my-branch');
await waitFor(() => expect(triggerUpgrade).toHaveBeenCalledWith('my-branch'));
});
test('an available update upgrades the current branch by default', async () => {
render(<UpgradeSegment/>, {status: updateAvailable});
expect(branchInput()).toHaveValue('release');
await confirmUpgrade('Upgrade Now');
await waitFor(() => expect(triggerUpgrade).toHaveBeenCalledWith('release'));
});
});