Implement basic tests for webui

This commit is contained in:
2025-07-26 19:20:09 +02:00
parent e7d95e934c
commit 4334b40fa9
15 changed files with 3188 additions and 158 deletions

View 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)
)
})
})