|
| 1 | +import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'; |
| 2 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 3 | + |
| 4 | +import { type OrthrusAgent } from '../../../api/orthrus'; |
| 5 | +import { AgentExternalProxyDialog } from '../AgentExternalProxyDialog'; |
| 6 | + |
| 7 | +const mockPatch = vi.fn(); |
| 8 | +const mockRefetchStatus = vi.fn(); |
| 9 | +let mockProxyStatus: Record<string, unknown> | undefined = undefined; |
| 10 | + |
| 11 | +vi.mock('../../../hooks/useOrthrus', () => ({ |
| 12 | + usePatchAgent: () => ({ mutate: mockPatch, isPending: false }), |
| 13 | + useAgentProxyStatus: () => ({ |
| 14 | + data: mockProxyStatus, |
| 15 | + refetch: mockRefetchStatus, |
| 16 | + }), |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock('react-i18next', () => ({ |
| 20 | + useTranslation: () => ({ |
| 21 | + t: (key: string, opts?: Record<string, string>) => |
| 22 | + opts?.name ? `${key}:${opts.name}` : key, |
| 23 | + }), |
| 24 | +})); |
| 25 | + |
| 26 | +const baseAgent: OrthrusAgent = { |
| 27 | + uuid: 'agent-1', |
| 28 | + name: 'Test Agent', |
| 29 | + status: 'online', |
| 30 | + capabilities: '["proxy"]', |
| 31 | + hecate_tunnel_uuid: undefined, |
| 32 | + resolved_address: undefined, |
| 33 | + device_id: undefined, |
| 34 | + created_at: '2025-01-01T00:00:00Z', |
| 35 | + updated_at: '2025-01-01T00:00:00Z', |
| 36 | + external_proxy_port: 2375, |
| 37 | +}; |
| 38 | + |
| 39 | +const renderDialog = (agent: OrthrusAgent = baseAgent, open = true, onClose = vi.fn()) => |
| 40 | + render(<AgentExternalProxyDialog agent={agent} open={open} onClose={onClose} />); |
| 41 | + |
| 42 | +beforeEach(() => { |
| 43 | + mockPatch.mockReset(); |
| 44 | + mockRefetchStatus.mockReset(); |
| 45 | + mockProxyStatus = undefined; |
| 46 | +}); |
| 47 | + |
| 48 | +describe('AgentExternalProxyDialog', () => { |
| 49 | + describe('port validation', () => { |
| 50 | + it('shows no error for port 0', () => { |
| 51 | + renderDialog({ ...baseAgent, external_proxy_port: 0 }); |
| 52 | + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('shows no error for a valid port in 1024–65535', () => { |
| 56 | + renderDialog({ ...baseAgent, external_proxy_port: 2375 }); |
| 57 | + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('shows an error when port is changed to a privileged value (1–1023)', () => { |
| 61 | + renderDialog({ ...baseAgent, external_proxy_port: 0 }); |
| 62 | + const input = screen.getByRole('spinbutton'); |
| 63 | + fireEvent.change(input, { target: { value: '80' } }); |
| 64 | + expect(screen.getByRole('alert')).toBeInTheDocument(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('shows an error when port exceeds 65535', () => { |
| 68 | + renderDialog({ ...baseAgent, external_proxy_port: 0 }); |
| 69 | + const input = screen.getByRole('spinbutton'); |
| 70 | + fireEvent.change(input, { target: { value: '99999' } }); |
| 71 | + expect(screen.getByRole('alert')).toBeInTheDocument(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('clears error when port is changed to 0', () => { |
| 75 | + renderDialog({ ...baseAgent, external_proxy_port: 0 }); |
| 76 | + const input = screen.getByRole('spinbutton'); |
| 77 | + fireEvent.change(input, { target: { value: '80' } }); |
| 78 | + expect(screen.getByRole('alert')).toBeInTheDocument(); |
| 79 | + fireEvent.change(input, { target: { value: '0' } }); |
| 80 | + expect(screen.queryByRole('alert')).not.toBeInTheDocument(); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('handleSave', () => { |
| 85 | + it('calls patch with the current port when valid', () => { |
| 86 | + renderDialog(); |
| 87 | + fireEvent.click(screen.getByText('common.save')); |
| 88 | + expect(mockPatch).toHaveBeenCalledWith( |
| 89 | + { uuid: 'agent-1', req: { external_proxy_port: 2375 } }, |
| 90 | + expect.any(Object), |
| 91 | + ); |
| 92 | + }); |
| 93 | + |
| 94 | + it('does not call patch when port is invalid', () => { |
| 95 | + renderDialog({ ...baseAgent, external_proxy_port: 0 }); |
| 96 | + const input = screen.getByRole('spinbutton'); |
| 97 | + fireEvent.change(input, { target: { value: '80' } }); |
| 98 | + fireEvent.click(screen.getByText('common.save')); |
| 99 | + expect(mockPatch).not.toHaveBeenCalled(); |
| 100 | + expect(screen.getByRole('alert')).toBeInTheDocument(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('calls refetchStatus and onClose on successful save', async () => { |
| 104 | + const onClose = vi.fn(); |
| 105 | + mockPatch.mockImplementation((_args: unknown, { onSuccess }: { onSuccess: () => void }) => { |
| 106 | + onSuccess(); |
| 107 | + }); |
| 108 | + renderDialog(baseAgent, true, onClose); |
| 109 | + fireEvent.click(screen.getByText('common.save')); |
| 110 | + expect(mockRefetchStatus).toHaveBeenCalled(); |
| 111 | + expect(onClose).toHaveBeenCalled(); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('offline agent', () => { |
| 116 | + it('shows offline status when agent is not online', () => { |
| 117 | + renderDialog({ ...baseAgent, status: 'offline' }); |
| 118 | + const statusNodes = screen.getAllByText('hecate.externalProxy.agentOffline'); |
| 119 | + expect(statusNodes.length).toBeGreaterThan(0); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('live proxy status', () => { |
| 124 | + it('shows error state when proxyStatus.error is set', () => { |
| 125 | + mockProxyStatus = { |
| 126 | + agent_uuid: 'agent-1', |
| 127 | + agent_online: true, |
| 128 | + configured_port: 2375, |
| 129 | + active: false, |
| 130 | + active_port: 0, |
| 131 | + bind_address: '', |
| 132 | + connection_string: '', |
| 133 | + error: 'bind failed: address already in use', |
| 134 | + }; |
| 135 | + renderDialog(); |
| 136 | + expect(screen.getByRole('alert')).toBeInTheDocument(); |
| 137 | + expect(screen.getByText('bind failed: address already in use')).toBeInTheDocument(); |
| 138 | + }); |
| 139 | + |
| 140 | + it('shows offline message when proxyStatus.agent_online is false', () => { |
| 141 | + mockProxyStatus = { |
| 142 | + agent_uuid: 'agent-1', |
| 143 | + agent_online: false, |
| 144 | + configured_port: 2375, |
| 145 | + active: false, |
| 146 | + active_port: 0, |
| 147 | + bind_address: '', |
| 148 | + connection_string: '', |
| 149 | + error: '', |
| 150 | + }; |
| 151 | + renderDialog(); |
| 152 | + const offlineMessages = screen.getAllByText('hecate.externalProxy.agentOffline'); |
| 153 | + expect(offlineMessages.length).toBeGreaterThan(0); |
| 154 | + }); |
| 155 | + |
| 156 | + it('shows active status with connection string when proxy is active', () => { |
| 157 | + mockProxyStatus = { |
| 158 | + agent_uuid: 'agent-1', |
| 159 | + agent_online: true, |
| 160 | + configured_port: 2375, |
| 161 | + active: true, |
| 162 | + active_port: 2375, |
| 163 | + bind_address: '0.0.0.0:2375', |
| 164 | + connection_string: 'tcp://charon:2375', |
| 165 | + error: '', |
| 166 | + }; |
| 167 | + renderDialog(); |
| 168 | + expect(screen.getByText('hecate.externalProxy.statusActive')).toBeInTheDocument(); |
| 169 | + expect(screen.getByText('tcp://charon:2375')).toBeInTheDocument(); |
| 170 | + }); |
| 171 | + |
| 172 | + it('shows inactive status when proxy is not active', () => { |
| 173 | + mockProxyStatus = { |
| 174 | + agent_uuid: 'agent-1', |
| 175 | + agent_online: true, |
| 176 | + configured_port: 2375, |
| 177 | + active: false, |
| 178 | + active_port: 0, |
| 179 | + bind_address: '', |
| 180 | + connection_string: '', |
| 181 | + error: '', |
| 182 | + }; |
| 183 | + renderDialog(); |
| 184 | + expect(screen.getByText('hecate.externalProxy.statusInactive')).toBeInTheDocument(); |
| 185 | + }); |
| 186 | + |
| 187 | + it('shows reconnect notice when configured port differs from active port', () => { |
| 188 | + mockProxyStatus = { |
| 189 | + agent_uuid: 'agent-1', |
| 190 | + agent_online: true, |
| 191 | + configured_port: 2375, |
| 192 | + active: true, |
| 193 | + active_port: 9999, |
| 194 | + bind_address: '0.0.0.0:9999', |
| 195 | + connection_string: 'tcp://charon:9999', |
| 196 | + error: '', |
| 197 | + }; |
| 198 | + renderDialog({ ...baseAgent, external_proxy_port: 2375 }); |
| 199 | + expect(screen.getByText('hecate.externalProxy.reconnectNotice')).toBeInTheDocument(); |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + describe('handleCopy', () => { |
| 204 | + it('copies connection string to clipboard and shows copied state', async () => { |
| 205 | + const writeText = vi.fn().mockResolvedValue(undefined); |
| 206 | + Object.assign(navigator, { clipboard: { writeText } }); |
| 207 | + |
| 208 | + mockProxyStatus = { |
| 209 | + agent_uuid: 'agent-1', |
| 210 | + agent_online: true, |
| 211 | + configured_port: 2375, |
| 212 | + active: true, |
| 213 | + active_port: 2375, |
| 214 | + bind_address: '0.0.0.0:2375', |
| 215 | + connection_string: 'tcp://charon:2375', |
| 216 | + error: '', |
| 217 | + }; |
| 218 | + renderDialog(); |
| 219 | + |
| 220 | + const copyButton = screen.getByRole('button', { |
| 221 | + name: 'hecate.externalProxy.copyConnectionString', |
| 222 | + }); |
| 223 | + await act(async () => { |
| 224 | + fireEvent.click(copyButton); |
| 225 | + }); |
| 226 | + |
| 227 | + await waitFor(() => |
| 228 | + expect(writeText).toHaveBeenCalledWith('tcp://charon:2375'), |
| 229 | + ); |
| 230 | + }); |
| 231 | + }); |
| 232 | + |
| 233 | + describe('cancel button', () => { |
| 234 | + it('calls onClose when cancel is clicked', () => { |
| 235 | + const onClose = vi.fn(); |
| 236 | + renderDialog(baseAgent, true, onClose); |
| 237 | + fireEvent.click(screen.getByText('common.cancel')); |
| 238 | + expect(onClose).toHaveBeenCalled(); |
| 239 | + }); |
| 240 | + }); |
| 241 | +}); |
0 commit comments