mirror of
https://github.com/lordmathis/llamactl.git
synced 2025-12-24 18:14:24 +00:00
Implement basic tests for webui
This commit is contained in:
60
webui/src/lib/__tests__/api.test.ts
Normal file
60
webui/src/lib/__tests__/api.test.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { instancesApi } from '@/lib/api'
|
||||
|
||||
// Mock fetch globally
|
||||
const mockFetch = vi.fn()
|
||||
global.fetch = mockFetch
|
||||
|
||||
describe('API Error Handling', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('converts HTTP errors to meaningful messages', async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 409,
|
||||
text: () => Promise.resolve('Instance already exists')
|
||||
})
|
||||
|
||||
await expect(instancesApi.create('existing', {}))
|
||||
.rejects
|
||||
.toThrow('HTTP 409: Instance already exists')
|
||||
})
|
||||
|
||||
it('handles empty error responses gracefully', async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: false,
|
||||
status: 500,
|
||||
text: () => Promise.resolve('')
|
||||
})
|
||||
|
||||
await expect(instancesApi.list())
|
||||
.rejects
|
||||
.toThrow('HTTP 500')
|
||||
})
|
||||
|
||||
it('handles 204 No Content responses', async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
status: 204
|
||||
})
|
||||
|
||||
const result = await instancesApi.delete('test-instance')
|
||||
expect(result).toBeUndefined()
|
||||
})
|
||||
|
||||
it('builds query parameters correctly for logs', async () => {
|
||||
mockFetch.mockResolvedValue({
|
||||
ok: true,
|
||||
text: () => Promise.resolve('logs')
|
||||
})
|
||||
|
||||
await instancesApi.getLogs('test-instance', 100)
|
||||
|
||||
expect(mockFetch).toHaveBeenCalledWith(
|
||||
'/api/v1/instances/test-instance/logs?lines=100',
|
||||
expect.any(Object)
|
||||
)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user