Files
agent/ui/src/App.test.jsx
T
Cédric VerstraetenandClaude Opus 5.5 7e979023ae ui: migrate from Create React App to Vite (#355)
* ui: migrate from Create React App to Vite

react-scripts 5 is unmaintained and pins vulnerable versions of
webpack-dev-server, webpack-dev-middleware, postcss 7 and svgo.

- build and dev server: Vite 8 with @vitejs/plugin-react
- tests: Vitest 4 with jsdom (jest.* -> vi.*; mocked default exports
  now return { default })
- index.html moves to ui/ and loads src/index.jsx as a module
- REACT_APP_ENVIRONMENT is replaced by import.meta.env.PROD/MODE, and
  REACT_APP_AGENT_API_PORT is renamed to VITE_AGENT_API_PORT
- bundled output goes to static/ so it does not mix with public/assets
- package.json is not "type": "module": that would switch CommonJS
  interop to Node semantics and break default imports such as
  @giantmachines/redux-websocket
- sass 1.54 -> 1.108 (Vite needs the modern compiler API); sass-loader
  removed
- requires Node >= 20.19

`yarn build` still writes to machinery/www, so the Dockerfile and CI
workflow are unchanged.

Clears the remaining 9 react-scripts Dependabot alerts. Five alerts for
svgo 1 and decode-uri-component 0.2 remain; they come from build-tool
dependencies of @kerberos-io/ui and are not part of the bundle.

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

* ui: vendor @kerberos-io/ui 1.77.1 to clear the last Dependabot alerts

@kerberos-io/ui 1.77.1 moves its build tools (@svgr/webpack,
svg-react-loader, sass-loader, Storybook, Rollup plugins) out of its
runtime dependencies. Those pulled svgo 1 and decode-uri-component 0.2
into the agent's lockfile.

1.77.1 is tagged on GitHub but was never published to npm, so install
it from a tarball built from kerberos-io/ui@ae1faa7 (tag v1.77.1) and
committed under ui/vendor. Compared with the published 1.77.0 the
exports are identical; the only code change is the bundled uuid (8 -> 11).

The Dockerfile copies ui/vendor before `yarn --frozen-lockfile`.
ui/vendor/README.md records the checksum and how to switch back to the
registry once a fixed version is published.

Clears the remaining 5 Dependabot alerts (svgo, decode-uri-component).

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

* ui: install @kerberos-io/ui 1.77.1 from its GitHub release

Replace the vendored tarball with the one attached to the kerberos-io/ui
v1.77.1 GitHub release, as uug-ai/factory already does. That tarball is
built by the ui repo's release workflow and ships with a .sha256 file
(e9f94f44...); yarn.lock pins it by hash.

Its JS is identical to the locally built tarball, and its CSS differs
only in selector order within @extend-generated rules.

This removes ui/vendor and the extra COPY line in the Dockerfile.

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

* ui: address lint findings in the Vite migration

- vite.config.mjs: disable import/no-unresolved; eslint-plugin-import 2.x
  cannot resolve packages that only define "exports" (vite, plugin-react)
- ui/README.md: turn the scripts table into a list to keep lines short
- README.md: wrap the edited UI paragraph

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

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 22:52:44 +01:00

164 lines
4.0 KiB
React

/* eslint-env jest */
import { vi } from 'vitest';
/* eslint-disable react/prop-types */
import React from 'react';
import { interval } from 'rxjs';
import { App } from './App';
const subscriptions = [];
vi.mock('rxjs', () => ({
interval: vi.fn(),
}));
vi.mock('@kerberos-io/ui', () => {
const Mock = ({ children }) => <div>{children}</div>;
return {
Badge: Mock,
Main: Mock,
MainBody: Mock,
Gradient: Mock,
Sidebar: Mock,
Navigation: Mock,
NavigationSection: Mock,
NavigationItem: Mock,
NavigationGroup: Mock,
Profilebar: Mock,
Icon: Mock,
};
});
vi.mock('./components/LanguageSelect/LanguageSelect', () => ({
default: () => null,
}));
vi.mock('./actions', () => ({ logout: vi.fn() }));
vi.mock('./actions/agent', () => ({
getDashboardInformation: vi.fn(),
}));
vi.mock('./config', () => ({
default: {
MODE: 'release',
URL: 'http://localhost',
WS_URL: 'ws://localhost/ws',
},
}));
vi.mock('react-redux', () => ({
connect: () => (Component) => Component,
}));
vi.mock('react-i18next', () => ({
withTranslation: () => (Component) => Component,
}));
vi.mock('react-router-dom', () => ({
Link: ({ children }) => <>{children}</>,
}));
vi.mock('@giantmachines/redux-websocket', () => ({
connect: vi.fn(),
disconnect: vi.fn(),
send: vi.fn(),
}));
vi.mock('uuid', () => ({
v4: vi.fn(() => 'uuid-1'),
}));
describe('App lifecycle refresh behaviour', () => {
const baseProps = () => ({
t: (value) => value,
dispatchLogout: vi.fn(),
dispatchConnect: vi.fn(),
dispatchDisconnect: vi.fn(),
dispatchSend: vi.fn(),
dispatchGetDashboardInformation: vi.fn((onSuccess) => {
if (onSuccess) {
onSuccess();
}
}),
children: [],
username: 'admin',
connected: false,
dashboard: {
cloudOnline: 0,
offlineMode: 'false',
},
});
beforeEach(() => {
subscriptions.length = 0;
vi.clearAllMocks();
interval.mockImplementation(() => ({
subscribe: vi.fn((callback) => {
const subscription = {
callback,
unsubscribe: vi.fn(),
};
subscriptions.push(subscription);
return subscription;
}),
}));
Object.defineProperty(document, 'hidden', {
configurable: true,
writable: true,
value: false,
});
});
it('uses a single dashboard interval and skips hidden or in-flight refreshes', () => {
const props = baseProps();
const app = new App(props);
app.props = props;
app.componentDidMount();
expect(props.dispatchConnect).toHaveBeenCalledTimes(1);
expect(props.dispatchGetDashboardInformation).toHaveBeenCalledTimes(1);
expect(interval).toHaveBeenCalledWith(5000);
expect(subscriptions).toHaveLength(1);
document.hidden = true;
subscriptions[0].callback();
expect(props.dispatchGetDashboardInformation).toHaveBeenCalledTimes(1);
document.hidden = false;
app.dashboardRequestInFlight = true;
subscriptions[0].callback();
expect(props.dispatchGetDashboardInformation).toHaveBeenCalledTimes(1);
app.dashboardRequestInFlight = false;
subscriptions[0].callback();
expect(props.dispatchGetDashboardInformation).toHaveBeenCalledTimes(2);
});
it('sends hello after reconnect without issuing a second manual reconnect', () => {
const props = {
...baseProps(),
connected: true,
};
const app = new App(props);
app.props = props;
app.componentDidUpdate({
...props,
connected: false,
});
expect(props.dispatchSend).toHaveBeenCalledWith(
expect.objectContaining({
client_id: 'uuid-1',
message_type: 'hello',
})
);
props.dispatchSend.mockClear();
props.dispatchConnect.mockClear();
app.props = {
...props,
connected: false,
};
app.componentDidUpdate({
...props,
connected: true,
});
expect(props.dispatchSend).not.toHaveBeenCalled();
expect(props.dispatchConnect).not.toHaveBeenCalled();
});
});