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() {
return render(
<AuthProvider>
@@ -119,8 +134,12 @@ describe('App Component - Critical Business Logic Only', () => {
// Verify correct API call
await waitFor(() => {
expect(instancesApi.create).toHaveBeenCalledWith('new-test-instance', {
auto_restart: true, // Default value
backend_type: BackendType.LLAMA_CPP
auto_restart: true, // Default value from config
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 { 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', () => {
const mockOnSave = vi.fn()
const mockOnOpenChange = vi.fn()
@@ -75,7 +90,7 @@ afterEach(() => {
it('submits form with correct data structure', async () => {
const user = userEvent.setup()
render(
<InstanceDialog
open={true}
@@ -86,13 +101,17 @@ afterEach(() => {
// Fill required name
await user.type(screen.getByLabelText(/Instance Name/), 'my-instance')
// Submit form
await user.click(screen.getByTestId('dialog-save-button'))
expect(mockOnSave).toHaveBeenCalledWith('my-instance', {
auto_restart: true, // Default value
backend_type: BackendType.LLAMA_CPP
auto_restart: true, // Default value from config
backend_type: BackendType.LLAMA_CPP,
docker_enabled: false,
max_restarts: 3,
on_demand_start: false,
restart_delay: 5
})
})
@@ -253,7 +272,7 @@ afterEach(() => {
it('includes restart options in form submission when enabled', async () => {
const user = userEvent.setup()
render(
<InstanceDialog
open={true}
@@ -264,17 +283,23 @@ afterEach(() => {
// Fill form
await user.type(screen.getByLabelText(/Instance Name/), 'test-instance')
// Set restart options
await user.type(screen.getByLabelText(/Max Restarts/), '5')
await user.type(screen.getByLabelText(/Restart Delay/), '10')
// Clear default values and set new restart options
const maxRestartsInput = screen.getByLabelText(/Max Restarts/)
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'))
expect(mockOnSave).toHaveBeenCalledWith('test-instance', {
auto_restart: true,
backend_type: BackendType.LLAMA_CPP,
docker_enabled: false,
max_restarts: 5,
on_demand_start: false,
restart_delay: 10
})
})
@@ -284,7 +309,7 @@ afterEach(() => {
describe('Form Data Handling', () => {
it('cleans up undefined values before submission', async () => {
const user = userEvent.setup()
render(
<InstanceDialog
open={true}
@@ -298,16 +323,20 @@ afterEach(() => {
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', {
auto_restart: true, // Only this default value should be included
backend_type: BackendType.LLAMA_CPP
auto_restart: true,
backend_type: BackendType.LLAMA_CPP,
docker_enabled: false,
max_restarts: 3,
on_demand_start: false,
restart_delay: 5
})
})
it('handles numeric fields correctly', async () => {
const user = userEvent.setup()
render(
<InstanceDialog
open={true}
@@ -317,7 +346,7 @@ afterEach(() => {
)
await user.type(screen.getByLabelText(/Instance Name/), 'numeric-test')
// Test GPU layers field (numeric)
const gpuLayersInput = screen.getByLabelText(/GPU Layers/)
await user.type(gpuLayersInput, '15')
@@ -328,6 +357,10 @@ afterEach(() => {
auto_restart: true,
backend_type: BackendType.LLAMA_CPP,
backend_options: { gpu_layers: 15 }, // Should be number, not string
docker_enabled: false,
max_restarts: 3,
on_demand_start: false,
restart_delay: 5
})
})
})