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()
@@ -75,7 +90,7 @@ afterEach(() => {
it('submits form with correct data structure', async () => { it('submits form with correct data structure', async () => {
const user = userEvent.setup() const user = userEvent.setup()
render( render(
<InstanceDialog <InstanceDialog
open={true} open={true}
@@ -86,13 +101,17 @@ afterEach(() => {
// Fill required name // Fill required name
await user.type(screen.getByLabelText(/Instance Name/), 'my-instance') await user.type(screen.getByLabelText(/Instance Name/), 'my-instance')
// Submit form // Submit form
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
}) })
}) })
@@ -253,7 +272,7 @@ afterEach(() => {
it('includes restart options in form submission when enabled', async () => { it('includes restart options in form submission when enabled', async () => {
const user = userEvent.setup() const user = userEvent.setup()
render( render(
<InstanceDialog <InstanceDialog
open={true} open={true}
@@ -264,17 +283,23 @@ 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
}) })
}) })
@@ -284,7 +309,7 @@ afterEach(() => {
describe('Form Data Handling', () => { describe('Form Data Handling', () => {
it('cleans up undefined values before submission', async () => { it('cleans up undefined values before submission', async () => {
const user = userEvent.setup() const user = userEvent.setup()
render( render(
<InstanceDialog <InstanceDialog
open={true} open={true}
@@ -298,16 +323,20 @@ 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
}) })
}) })
it('handles numeric fields correctly', async () => { it('handles numeric fields correctly', async () => {
const user = userEvent.setup() const user = userEvent.setup()
render( render(
<InstanceDialog <InstanceDialog
open={true} open={true}
@@ -317,7 +346,7 @@ afterEach(() => {
) )
await user.type(screen.getByLabelText(/Instance Name/), 'numeric-test') await user.type(screen.getByLabelText(/Instance Name/), 'numeric-test')
// Test GPU layers field (numeric) // Test GPU layers field (numeric)
const gpuLayersInput = screen.getByLabelText(/GPU Layers/) const gpuLayersInput = screen.getByLabelText(/GPU Layers/)
await user.type(gpuLayersInput, '15') await user.type(gpuLayersInput, '15')
@@ -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
}) })
}) })
}) })