feat: implement launch at startup functionality in zn-ai
- Added a new setting for "launch at startup" in the GeneralSettingsPanel. - Integrated the setting with the existing settings store and IPC mechanisms. - Implemented platform-specific logic for enabling/disabling startup behavior in the main process. - Created a new service for managing launch at startup settings, including Linux desktop entry creation. - Added unit tests for the new functionality and ensured existing tests are updated accordingly. - Updated i18n messages for the new setting in English, Chinese, and Japanese.
This commit is contained in:
108
tests/general-settings-panel.test.tsx
Normal file
108
tests/general-settings-panel.test.tsx
Normal file
@@ -0,0 +1,108 @@
|
||||
import React from 'react';
|
||||
import { fireEvent, render, screen, within } from '@testing-library/react';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { setLocale } from '../src/i18n';
|
||||
import GeneralSettingsPanel from '../src/pages/Setting/components/GeneralSettingsPanel';
|
||||
import type { GatewaySettingState } from '../src/pages/Setting/useGatewaySettingState';
|
||||
import type { SettingUpdateState } from '../src/pages/Setting/useSettingUpdateState';
|
||||
|
||||
function createGatewayState(): GatewaySettingState {
|
||||
return {
|
||||
status: 'connected',
|
||||
loading: false,
|
||||
showLogs: false,
|
||||
gatewayAutoStart: true,
|
||||
gateway: {
|
||||
ok: true,
|
||||
status: 'connected',
|
||||
initialized: true,
|
||||
mode: null,
|
||||
port: 18789,
|
||||
pid: null,
|
||||
},
|
||||
logContent: '',
|
||||
statusLoading: false,
|
||||
logLoading: false,
|
||||
restarting: false,
|
||||
autoStartUpdating: false,
|
||||
error: null,
|
||||
lastFetchedAt: null,
|
||||
refreshStatus: async () => {},
|
||||
restartGateway: async () => {},
|
||||
toggleLogs: async () => {},
|
||||
loadLogs: async () => {},
|
||||
viewLogs: async () => {},
|
||||
setGatewayAutoStart: async () => {},
|
||||
};
|
||||
}
|
||||
|
||||
function createUpdateState(): SettingUpdateState {
|
||||
return {
|
||||
status: 'idle',
|
||||
currentVersion: '1.0.0',
|
||||
updateInfo: null,
|
||||
progress: null,
|
||||
error: null,
|
||||
autoCheckUpdate: true,
|
||||
autoDownloadUpdate: false,
|
||||
checkUpdate: async () => {},
|
||||
downloadUpdate: async () => {},
|
||||
installUpdate: async () => {},
|
||||
setAutoCheckUpdate: () => {},
|
||||
setAutoDownloadUpdate: () => {},
|
||||
};
|
||||
}
|
||||
|
||||
describe('GeneralSettingsPanel', () => {
|
||||
beforeEach(() => {
|
||||
setLocale('zh');
|
||||
});
|
||||
|
||||
it('renders the launch-at-startup setting after language and before gateway', () => {
|
||||
render(
|
||||
<GeneralSettingsPanel
|
||||
themeMode="system"
|
||||
language="zh"
|
||||
launchAtStartup={false}
|
||||
onThemeChange={async () => {}}
|
||||
onLanguageChange={async () => {}}
|
||||
onLaunchAtStartupChange={async () => {}}
|
||||
gatewayState={createGatewayState()}
|
||||
updateState={createUpdateState()}
|
||||
/>,
|
||||
);
|
||||
|
||||
const languageLabel = screen.getByText('语言');
|
||||
const launchLabel = screen.getByText('开机自动启动');
|
||||
const gatewayLabel = screen.getByText('网关');
|
||||
|
||||
expect(languageLabel.compareDocumentPosition(launchLabel) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
|
||||
expect(launchLabel.compareDocumentPosition(gatewayLabel) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy();
|
||||
});
|
||||
|
||||
it('calls the launch-at-startup handler when the switch is toggled', () => {
|
||||
const handleLaunchAtStartupChange = vi.fn();
|
||||
|
||||
render(
|
||||
<GeneralSettingsPanel
|
||||
themeMode="system"
|
||||
language="zh"
|
||||
launchAtStartup={false}
|
||||
onThemeChange={async () => {}}
|
||||
onLanguageChange={async () => {}}
|
||||
onLaunchAtStartupChange={handleLaunchAtStartupChange}
|
||||
gatewayState={createGatewayState()}
|
||||
updateState={createUpdateState()}
|
||||
/>,
|
||||
);
|
||||
|
||||
const launchLabel = screen.getByText('开机自动启动');
|
||||
const launchRow = launchLabel.parentElement?.parentElement;
|
||||
expect(launchRow).toBeTruthy();
|
||||
|
||||
const launchSwitch = within(launchRow as HTMLElement).getByRole('switch');
|
||||
fireEvent.click(launchSwitch);
|
||||
|
||||
expect(handleLaunchAtStartupChange).toHaveBeenCalledWith(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user