|
| 1 | +// Copyright(C) 2025-2026 Advanced Micro Devices, Inc. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 5 | +import userEvent from '@testing-library/user-event'; |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 7 | +import { SettingsPage } from '../SettingsPage'; |
| 8 | +import { useChatStore } from '../../stores/chatStore'; |
| 9 | +import type { Settings } from '../../types'; |
| 10 | +import * as api from '../../services/api'; |
| 11 | + |
| 12 | +vi.mock('../../services/api'); |
| 13 | + |
| 14 | +// Heavy child sections make their own API calls; stub them so this test |
| 15 | +// stays focused on the Dynamic Tools toggle. |
| 16 | +vi.mock('../CustomAgentsSection', () => ({ CustomAgentsSection: () => null })); |
| 17 | +vi.mock('../ConnectorsSection', () => ({ ConnectorsSection: () => null })); |
| 18 | + |
| 19 | +const mockedApi = vi.mocked(api); |
| 20 | + |
| 21 | +function makeSettings(overrides: Partial<Settings> = {}): Settings { |
| 22 | + return { |
| 23 | + custom_model: null, |
| 24 | + model_status: null, |
| 25 | + context_size: null, |
| 26 | + dynamic_tools: false, |
| 27 | + dynamic_tools_locked: false, |
| 28 | + ...overrides, |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +beforeEach(() => { |
| 33 | + vi.clearAllMocks(); |
| 34 | + mockedApi.getSystemStatus.mockResolvedValue(null as never); |
| 35 | + mockedApi.getMCPRuntimeStatus.mockResolvedValue({ servers: [] } as never); |
| 36 | + mockedApi.getSettings.mockResolvedValue(makeSettings()); |
| 37 | + mockedApi.updateSettings.mockImplementation(async (patch) => |
| 38 | + makeSettings(patch as Partial<Settings>), |
| 39 | + ); |
| 40 | + useChatStore.setState({ sessions: [], agents: [] }); |
| 41 | +}); |
| 42 | + |
| 43 | +function getDynamicToolsToggle(): HTMLInputElement { |
| 44 | + // Both enabled/disabled labels resolve to the same control. |
| 45 | + return (screen.queryByLabelText('Enable dynamic tools') |
| 46 | + ?? screen.getByLabelText('Disable dynamic tools')) as HTMLInputElement; |
| 47 | +} |
| 48 | + |
| 49 | +describe('SettingsPage — Dynamic Tools toggle (#1798)', () => { |
| 50 | + it('renders the loaded value (off by default)', async () => { |
| 51 | + render(<SettingsPage />); |
| 52 | + const toggle = await waitFor(getDynamicToolsToggle); |
| 53 | + expect(toggle).not.toBeChecked(); |
| 54 | + expect(toggle).not.toBeDisabled(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('reflects a persisted-on value from the server', async () => { |
| 58 | + mockedApi.getSettings.mockResolvedValue(makeSettings({ dynamic_tools: true })); |
| 59 | + render(<SettingsPage />); |
| 60 | + await waitFor(() => expect(getDynamicToolsToggle()).toBeChecked()); |
| 61 | + }); |
| 62 | + |
| 63 | + it('persists dynamic_tools: true when toggled on', async () => { |
| 64 | + render(<SettingsPage />); |
| 65 | + const toggle = await waitFor(getDynamicToolsToggle); |
| 66 | + |
| 67 | + await userEvent.click(toggle); |
| 68 | + |
| 69 | + await waitFor(() => |
| 70 | + expect(mockedApi.updateSettings).toHaveBeenCalledWith({ dynamic_tools: true }), |
| 71 | + ); |
| 72 | + await waitFor(() => expect(getDynamicToolsToggle()).toBeChecked()); |
| 73 | + }); |
| 74 | + |
| 75 | + it('toggles when the visible row/label is clicked, not just the hidden input', async () => { |
| 76 | + // Regression: the checkbox is visually hidden (width:0/height:0) and the |
| 77 | + // track is a sibling <span>. Clicking the visible control only works if a |
| 78 | + // <label> forwards the click to the input. Clicking the input element |
| 79 | + // directly (as the other tests do) would pass even if that wiring is |
| 80 | + // missing — so click the visible label text here, the way a user does. |
| 81 | + render(<SettingsPage />); |
| 82 | + await waitFor(getDynamicToolsToggle); |
| 83 | + |
| 84 | + await userEvent.click(screen.getByText('Enable dynamic tool loading')); |
| 85 | + |
| 86 | + await waitFor(() => |
| 87 | + expect(mockedApi.updateSettings).toHaveBeenCalledWith({ dynamic_tools: true }), |
| 88 | + ); |
| 89 | + await waitFor(() => expect(getDynamicToolsToggle()).toBeChecked()); |
| 90 | + }); |
| 91 | + |
| 92 | + it('is disabled and reflects the env value when locked', async () => { |
| 93 | + mockedApi.getSettings.mockResolvedValue( |
| 94 | + makeSettings({ dynamic_tools: true, dynamic_tools_locked: true }), |
| 95 | + ); |
| 96 | + render(<SettingsPage />); |
| 97 | + const toggle = await waitFor(getDynamicToolsToggle); |
| 98 | + |
| 99 | + expect(toggle).toBeChecked(); |
| 100 | + expect(toggle).toBeDisabled(); |
| 101 | + expect(screen.getByText(/GAIA_DYNAMIC_TOOLS/)).toBeInTheDocument(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('guards against a double-click while a save is in flight (single PUT)', async () => { |
| 105 | + // Hold the first save open so the toggle stays mid-save between clicks. |
| 106 | + let resolveSave!: (value: Settings) => void; |
| 107 | + mockedApi.updateSettings.mockImplementationOnce( |
| 108 | + () => new Promise<Settings>((resolve) => { resolveSave = resolve; }), |
| 109 | + ); |
| 110 | + |
| 111 | + render(<SettingsPage />); |
| 112 | + const toggle = await waitFor(getDynamicToolsToggle); |
| 113 | + |
| 114 | + // First click starts the save; the toggle disables (savingDynamicTools). |
| 115 | + await userEvent.click(toggle); |
| 116 | + await waitFor(() => expect(getDynamicToolsToggle()).toBeDisabled()); |
| 117 | + |
| 118 | + // Second click while the save is pending must be a no-op, not a second PUT. |
| 119 | + await userEvent.click(getDynamicToolsToggle()); |
| 120 | + |
| 121 | + // Let the in-flight save settle. |
| 122 | + resolveSave(makeSettings({ dynamic_tools: true })); |
| 123 | + |
| 124 | + await waitFor(() => expect(getDynamicToolsToggle()).toBeChecked()); |
| 125 | + expect(mockedApi.updateSettings).toHaveBeenCalledTimes(1); |
| 126 | + }); |
| 127 | + |
| 128 | + it('reverts and surfaces an error when the save fails (no silent fallback)', async () => { |
| 129 | + mockedApi.updateSettings.mockRejectedValue(new Error('network down')); |
| 130 | + render(<SettingsPage />); |
| 131 | + const toggle = await waitFor(getDynamicToolsToggle); |
| 132 | + |
| 133 | + await userEvent.click(toggle); |
| 134 | + |
| 135 | + await waitFor(() => expect(screen.getByRole('alert')).toBeInTheDocument()); |
| 136 | + expect(screen.getByText('network down')).toBeInTheDocument(); |
| 137 | + // Optimistic flip reverted back to off. |
| 138 | + await waitFor(() => expect(getDynamicToolsToggle()).not.toBeChecked()); |
| 139 | + }); |
| 140 | +}); |
0 commit comments