Add mocks for ConfigContext in tests to use default configuration values

This commit is contained in:
2025-11-15 00:45:02 +01:00
parent 2ceeddbce5
commit b594ade8f9
2 changed files with 69 additions and 17 deletions

View File

@@ -49,6 +49,21 @@ vi.mock('@/lib/healthService', () => ({
})), })),
})) }))
// Mock the ConfigContext hooks
vi.mock('@/contexts/ConfigContext', () => ({
useInstanceDefaults: () => ({
autoRestart: true,
maxRestarts: 3,
restartDelay: 5,
onDemandStart: false,
}),
useBackendSettings: () => ({
command: '/usr/bin/llama-server',
dockerEnabled: false,
dockerImage: '',
}),
}))
function renderApp() { function renderApp() {
return render( return render(
<AuthProvider> <AuthProvider>
@@ -119,8 +134,12 @@ describe('App Component - Critical Business Logic Only', () => {
// Verify correct API call // Verify correct API call
await waitFor(() => { await waitFor(() => {
expect(instancesApi.create).toHaveBeenCalledWith('new-test-instance', { expect(instancesApi.create).toHaveBeenCalledWith('new-test-instance', {
auto_restart: true, // Default value auto_restart: true, // Default value from config
backend_type: BackendType.LLAMA_CPP backend_type: BackendType.LLAMA_CPP,
docker_enabled: false,
max_restarts: 3,
on_demand_start: false,
restart_delay: 5
}) })
}) })

View File

@@ -5,6 +5,21 @@ import InstanceDialog from '@/components/InstanceDialog'
import type { Instance } from '@/types/instance' import type { Instance } from '@/types/instance'
import { BackendType } from '@/types/instance' import { BackendType } from '@/types/instance'
// Mock the ConfigContext hooks
vi.mock('@/contexts/ConfigContext', () => ({
useInstanceDefaults: () => ({
autoRestart: true,
maxRestarts: 3,
restartDelay: 5,
onDemandStart: false,
}),
useBackendSettings: () => ({
command: '/usr/bin/llama-server',
dockerEnabled: false,
dockerImage: '',
}),
}))
describe('InstanceModal - Form Logic and Validation', () => { describe('InstanceModal - Form Logic and Validation', () => {
const mockOnSave = vi.fn() const mockOnSave = vi.fn()
const mockOnOpenChange = vi.fn() const mockOnOpenChange = vi.fn()
@@ -91,8 +106,12 @@ afterEach(() => {
await user.click(screen.getByTestId('dialog-save-button')) await user.click(screen.getByTestId('dialog-save-button'))
expect(mockOnSave).toHaveBeenCalledWith('my-instance', { expect(mockOnSave).toHaveBeenCalledWith('my-instance', {
auto_restart: true, // Default value auto_restart: true, // Default value from config
backend_type: BackendType.LLAMA_CPP backend_type: BackendType.LLAMA_CPP,
docker_enabled: false,
max_restarts: 3,
on_demand_start: false,
restart_delay: 5
}) })
}) })
@@ -265,16 +284,22 @@ afterEach(() => {
// Fill form // Fill form
await user.type(screen.getByLabelText(/Instance Name/), 'test-instance') await user.type(screen.getByLabelText(/Instance Name/), 'test-instance')
// Set restart options // Clear default values and set new restart options
await user.type(screen.getByLabelText(/Max Restarts/), '5') const maxRestartsInput = screen.getByLabelText(/Max Restarts/)
await user.type(screen.getByLabelText(/Restart Delay/), '10') const restartDelayInput = screen.getByLabelText(/Restart Delay/)
await user.clear(maxRestartsInput)
await user.type(maxRestartsInput, '5')
await user.clear(restartDelayInput)
await user.type(restartDelayInput, '10')
await user.click(screen.getByTestId('dialog-save-button')) await user.click(screen.getByTestId('dialog-save-button'))
expect(mockOnSave).toHaveBeenCalledWith('test-instance', { expect(mockOnSave).toHaveBeenCalledWith('test-instance', {
auto_restart: true, auto_restart: true,
backend_type: BackendType.LLAMA_CPP, backend_type: BackendType.LLAMA_CPP,
docker_enabled: false,
max_restarts: 5, max_restarts: 5,
on_demand_start: false,
restart_delay: 10 restart_delay: 10
}) })
}) })
@@ -298,10 +323,14 @@ afterEach(() => {
await user.click(screen.getByTestId('dialog-save-button')) await user.click(screen.getByTestId('dialog-save-button'))
// Should only include non-empty values // Should include default values from config
expect(mockOnSave).toHaveBeenCalledWith('clean-instance', { expect(mockOnSave).toHaveBeenCalledWith('clean-instance', {
auto_restart: true, // Only this default value should be included auto_restart: true,
backend_type: BackendType.LLAMA_CPP backend_type: BackendType.LLAMA_CPP,
docker_enabled: false,
max_restarts: 3,
on_demand_start: false,
restart_delay: 5
}) })
}) })
@@ -328,6 +357,10 @@ afterEach(() => {
auto_restart: true, auto_restart: true,
backend_type: BackendType.LLAMA_CPP, backend_type: BackendType.LLAMA_CPP,
backend_options: { gpu_layers: 15 }, // Should be number, not string backend_options: { gpu_layers: 15 }, // Should be number, not string
docker_enabled: false,
max_restarts: 3,
on_demand_start: false,
restart_delay: 5
}) })
}) })
}) })