|
| 1 | +// ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. ========= |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ========= Copyright 2025-2026 @ Eigent.ai All Rights Reserved. ========= |
| 14 | + |
| 15 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 16 | +import userEvent from '@testing-library/user-event'; |
| 17 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 18 | + |
| 19 | +import SettingGeneral from '@/pages/Setting/General'; |
| 20 | + |
| 21 | +vi.mock('react-i18next', () => ({ |
| 22 | + Trans: ({ children }: { children: React.ReactNode }) => children, |
| 23 | + useTranslation: () => ({ |
| 24 | + t: (key: string) => key, |
| 25 | + }), |
| 26 | +})); |
| 27 | + |
| 28 | +const mockClearTasks = vi.fn(); |
| 29 | + |
| 30 | +vi.mock('@/hooks/useChatStoreAdapter', () => ({ |
| 31 | + default: () => ({ |
| 32 | + chatStore: { |
| 33 | + clearTasks: mockClearTasks, |
| 34 | + }, |
| 35 | + }), |
| 36 | +})); |
| 37 | + |
| 38 | +const mockResetInstallation = vi.fn(); |
| 39 | +const mockSetNeedsBackendRestart = vi.fn(); |
| 40 | + |
| 41 | +vi.mock('@/store/installationStore', async () => { |
| 42 | + const actual = |
| 43 | + await vi.importActual<typeof import('@/store/installationStore')>( |
| 44 | + '@/store/installationStore' |
| 45 | + ); |
| 46 | + return { |
| 47 | + ...actual, |
| 48 | + useInstallationStore: (selector: any) => |
| 49 | + selector({ |
| 50 | + reset: mockResetInstallation, |
| 51 | + setNeedsBackendRestart: mockSetNeedsBackendRestart, |
| 52 | + }), |
| 53 | + }; |
| 54 | +}); |
| 55 | + |
| 56 | +const mockLogout = vi.fn(); |
| 57 | + |
| 58 | +vi.mock('@/store/authStore', async () => { |
| 59 | + const actual = await vi.importActual<typeof import('@/store/authStore')>( |
| 60 | + '@/store/authStore' |
| 61 | + ); |
| 62 | + return { |
| 63 | + ...actual, |
| 64 | + useAuthStore: () => ({ |
| 65 | + email: 'test@example.com', |
| 66 | + appearance: 'dark', |
| 67 | + language: 'system', |
| 68 | + setAppearance: vi.fn(), |
| 69 | + setLanguage: vi.fn(), |
| 70 | + logout: mockLogout, |
| 71 | + }), |
| 72 | + getAuthStore: () => ({ |
| 73 | + token: 'token', |
| 74 | + }), |
| 75 | + }; |
| 76 | +}); |
| 77 | + |
| 78 | +const toastErrorMock = vi.fn(); |
| 79 | +const toastSuccessMock = vi.fn(); |
| 80 | + |
| 81 | +vi.mock('sonner', () => ({ |
| 82 | + toast: { |
| 83 | + error: (msg: string) => toastErrorMock(msg), |
| 84 | + success: (msg: string) => toastSuccessMock(msg), |
| 85 | + }, |
| 86 | +})); |
| 87 | + |
| 88 | +const navigateMock = vi.fn(); |
| 89 | + |
| 90 | +vi.mock('react-router-dom', async () => { |
| 91 | + const actual = await vi.importActual<typeof import('react-router-dom')>( |
| 92 | + 'react-router-dom' |
| 93 | + ); |
| 94 | + return { |
| 95 | + ...actual, |
| 96 | + useNavigate: () => navigateMock, |
| 97 | + }; |
| 98 | +}); |
| 99 | + |
| 100 | +describe('SettingGeneral Network Proxy', () => { |
| 101 | + beforeEach(() => { |
| 102 | + vi.clearAllMocks(); |
| 103 | + |
| 104 | + (window as any).electronAPI = { |
| 105 | + getPlatform: () => 'linux', |
| 106 | + readGlobalEnv: vi.fn().mockResolvedValue({ value: 'http://proxy:8080' }), |
| 107 | + envWrite: vi.fn().mockResolvedValue({ success: true }), |
| 108 | + envRemove: vi.fn().mockResolvedValue({ success: true }), |
| 109 | + restartApp: vi.fn(), |
| 110 | + }; |
| 111 | + }); |
| 112 | + |
| 113 | + it('loads existing proxy value and disables save button until changed', async () => { |
| 114 | + render(<SettingGeneral />); |
| 115 | + |
| 116 | + const input = await screen.findByPlaceholderText( |
| 117 | + 'setting.proxy-placeholder' |
| 118 | + ); |
| 119 | + expect((input as HTMLInputElement).value).toBe('http://proxy:8080'); |
| 120 | + |
| 121 | + const button = screen.getByRole('button', { name: 'setting.save' }); |
| 122 | + expect(button).toBeDisabled(); |
| 123 | + |
| 124 | + await userEvent.clear(input); |
| 125 | + await userEvent.type(input, 'http://proxy:9090'); |
| 126 | + |
| 127 | + expect(button).not.toBeDisabled(); |
| 128 | + }); |
| 129 | + |
| 130 | + it('validates proxy URL and shows error for invalid protocol', async () => { |
| 131 | + render(<SettingGeneral />); |
| 132 | + |
| 133 | + const input = await screen.findByPlaceholderText( |
| 134 | + 'setting.proxy-placeholder' |
| 135 | + ); |
| 136 | + |
| 137 | + await userEvent.clear(input); |
| 138 | + await userEvent.type(input, 'ftp://invalid-proxy'); |
| 139 | + |
| 140 | + const button = screen.getByRole('button', { name: 'setting.save' }); |
| 141 | + await userEvent.click(button); |
| 142 | + |
| 143 | + await waitFor(() => { |
| 144 | + expect(toastErrorMock).toHaveBeenCalledWith('setting.proxy-invalid-url'); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | + it('saves proxy URL and requires restart', async () => { |
| 149 | + render(<SettingGeneral />); |
| 150 | + |
| 151 | + const input = await screen.findByPlaceholderText( |
| 152 | + 'setting.proxy-placeholder' |
| 153 | + ); |
| 154 | + |
| 155 | + await userEvent.clear(input); |
| 156 | + await userEvent.type(input, 'http://proxy:9090'); |
| 157 | + |
| 158 | + const button = screen.getByRole('button', { name: 'setting.save' }); |
| 159 | + await userEvent.click(button); |
| 160 | + |
| 161 | + await waitFor(() => { |
| 162 | + expect((window as any).electronAPI.envWrite).toHaveBeenCalledWith( |
| 163 | + 'test@example.com', |
| 164 | + { |
| 165 | + key: 'HTTP_PROXY', |
| 166 | + value: 'http://proxy:9090', |
| 167 | + } |
| 168 | + ); |
| 169 | + expect(toastSuccessMock).toHaveBeenCalledWith( |
| 170 | + 'setting.proxy-saved-restart-required' |
| 171 | + ); |
| 172 | + }); |
| 173 | + |
| 174 | + // Button should now show restart label and trigger restart |
| 175 | + const restartButton = screen.getByRole('button', { |
| 176 | + name: 'setting.restart-to-apply', |
| 177 | + }); |
| 178 | + await userEvent.click(restartButton); |
| 179 | + |
| 180 | + expect((window as any).electronAPI.restartApp).toHaveBeenCalled(); |
| 181 | + }); |
| 182 | + it('removes proxy when input cleared', async () => { |
| 183 | + // Start with existing value |
| 184 | + render(<SettingGeneral />); |
| 185 | + |
| 186 | + const input = await screen.findByPlaceholderText( |
| 187 | + 'setting.proxy-placeholder' |
| 188 | + ); |
| 189 | + |
| 190 | + await userEvent.clear(input); |
| 191 | + |
| 192 | + const button = screen.getByRole('button', { name: 'setting.save' }); |
| 193 | + await userEvent.click(button); |
| 194 | + |
| 195 | + await waitFor(() => { |
| 196 | + expect((window as any).electronAPI.envRemove).toHaveBeenCalledWith( |
| 197 | + 'test@example.com', |
| 198 | + 'HTTP_PROXY' |
| 199 | + ); |
| 200 | + expect(toastSuccessMock).toHaveBeenCalledWith( |
| 201 | + 'setting.proxy-saved-restart-required' |
| 202 | + ); |
| 203 | + }); |
| 204 | + }); |
| 205 | + |
| 206 | + it('shows error when electron env APIs are missing', async () => { |
| 207 | + (window as any).electronAPI = { |
| 208 | + getPlatform: () => 'linux', |
| 209 | + readGlobalEnv: vi.fn().mockResolvedValue({ value: '' }), |
| 210 | + // envWrite/envRemove intentionally omitted |
| 211 | + }; |
| 212 | + |
| 213 | + render(<SettingGeneral />); |
| 214 | + |
| 215 | + const input = await screen.findByPlaceholderText( |
| 216 | + 'setting.proxy-placeholder' |
| 217 | + ); |
| 218 | + await userEvent.type(input, 'http://proxy:8080'); |
| 219 | + |
| 220 | + const button = screen.getByRole('button', { name: 'setting.save' }); |
| 221 | + await userEvent.click(button); |
| 222 | + |
| 223 | + await waitFor(() => { |
| 224 | + expect(toastErrorMock).toHaveBeenCalledWith('setting.proxy-save-failed'); |
| 225 | + }); |
| 226 | + }); |
| 227 | +}); |
| 228 | + |
0 commit comments