All files / msw / handlers llm-configs.ts

21.05% Statements 12/57
7.5% Branches 3/40
30% Functions 3/10
24.44% Lines 11/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101    125x 125x   14x 14x 14x 14x 14x 14x                     20x 10x 10x                                                                                                                                                          
import { http, HttpResponse } from 'msw'
import { mockLlmConfigs } from '@/msw/mock-data'
 
let nextId = 100
 
export const llmConfigHandlers = [
  http.get('/api/v1/llm-configs', ({ request }) => {
    const url = new URL(request.url)
    const limit = parseInt(url.searchParams.get('limit') ?? '50')
    const offset = parseInt(url.searchParams.get('offset') ?? '0')
    const paginated = mockLlmConfigs.slice(offset, offset + limit)
    const hasMore = offset + limit < mockLlmConfigs.length
    return HttpResponse.json({
      data: paginated,
      meta: { limit, offset, returned: paginated.length, has_more: hasMore },
    })
  }),
 
  http.get('/api/v1/llm-configs/:id', ({ params }) => {
    const config = mockLlmConfigs.find((c) => c.id === params.id)
    if (!config) return new HttpResponse(null, { status: 404 })
    return HttpResponse.json(config)
  I}),
 
  http.post('/api/v1/llm-configs', async ({ request }) => {
    const body = await request.json() as Record<string, unknown>
    if (!body.name || !body.base_url || !body.model || !body.api_key) {
      return HttpResponse.json(
        { error: 'Name, Base URL, Model, and API Key are required' },
        { status: 422 },
      )
    }
    if (String(body.name).length < 3 || String(body.name).length > 100) {
      return HttpResponse.json(
        { error: 'Name must be 3-100 characters' },
        { status: 400 },
      )
    }
    const duplicate = mockLlmConfigs.some((c) => c.name === body.name)
    if (duplicate) {
      return HttpResponse.json(
        { error: 'An LLM config with this name already exists' },
        { status: 409 },
      )
    }
    const now = new Date().toISOString()
    const config = {
      id: `llmcfg-new-${nextId++}`,
      name: body.name as string,
      base_url: body.base_url as string,
      model: body.model as string,
      default_temperature: body.default_temperature as number | undefined,
      created_by: 'system',
      updated_by: 'system',
      created_at: now,
      updated_at: now,
    }
    mockLlmConfigs.push(config)
    return HttpResponse.json(config, { status: 201 })
  }),
 
  http.put('/api/v1/llm-configs/:id', async ({ params, request }) => {
    const body = await request.json() as Record<string, unknown>
    const config = mockLlmConfigs.find((c) => c.id === params.id)
    if (!config) return new HttpResponse(null, { status: 404 })
    if (body.name !== undefined) {
      const name = String(body.name)
      if (name.length < 3 || name.length > 100) {
        return HttpResponse.json(
          { error: 'Name must be 3-100 characters' },
          { status: 400 },
        )
      }
      const duplicate = mockLlmConfigs.some(
        (c) => c.name === name && c.id !== params.id,
      )
      if (duplicate) {
        return HttpResponse.json(
          { error: 'An LLM config with this name already exists' },
          { status: 409 },
        )
      }
    }
    const now = new Date().toISOString()
    if (body.name !== undefined) config.name = body.name as string
    if (body.base_url !== undefined) config.base_url = body.base_url as string
    if (body.model !== undefined) config.model = body.model as string
    if (body.default_temperature !== undefined) config.default_temperature = body.default_temperature as number
    config.updated_at = now
    config.updated_by = 'system'
    return HttpResponse.json(config)
  }),
 
  http.delete('/api/v1/llm-configs/:id', ({ params }) => {
    const idx = mockLlmConfigs.findIndex((c) => c.id === params.id)
    if (idx === -1) return new HttpResponse(null, { status: 404 })
    mockLlmConfigs.splice(idx, 1)
    return new HttpResponse(null, { status: 204 })
  }),
]