All files / msw / handlers container-registries.ts

48.64% Statements 36/74
27.77% Branches 20/72
72.72% Functions 8/11
50% Lines 33/66

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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153    67x   1x 1x 1x 1x           1x   67x   14x 14x 14x 14x 14x 14x                     19x 8x     8x     1x 1x     1x 1x     1x 1x     4x 1x     1x 1x 1x                   1x 1x                                                                           2x 1x     1x 1x                                                                                
import { http, HttpResponse } from 'msw'
import { mockContainerRegistries } from '@/msw/mock-data'
 
let nextId = 100
I
function validateUrl(raw: string): string | null {
  if (raw === '') return 'url is required'
  Itry {
    const u = new URL(raw)
    if (u.protocol === '' || u.hostname === '') {
      return 'url must be a valid absolute URL with a scheme and host'
    }
  } catch {
    return 'url must be a valid absolute URL with a scheme and host'
  }
  return null
}
 
export const containerRegistryHandlers = [
  http.get('/api/v1/container-registries', ({ 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 = mockContainerRegistries.slice(offset, offset + limit)
    const hasMore = offset + limit < mockContainerRegistries.length
    return HttpResponse.json({
      data: paginated,
      meta: { limit, offset, returned: paginated.length, has_more: hasMore },
    })
  }),
 
  http.get('/api/v1/container-registries/:id', ({ params }) => {
    const registry = mockContainerRegistries.find((r) => r.id === params.id)
    if (!registry) {
  I    return HttpResponse.json(
        { error: 'container registry not found' },
        { status: 404 },
      )
    }
    return HttpResponse.json(registry)
  }),
I
  http.post('/api/v1/container-registries', async ({ request }) => {
    const body = await request.json() as Record<string, unknown>
    if (body.name === undefined || body.name === '') {
  I    return HttpResponse.json(
        { error: 'name is required' },
        { status: 422 },
      )
  I  }
    const name = String(body.name)
    if (name.length < 3 || name.length > 100) {
      return HttpResponse.json(
  I      { error: 'name must be between 3 and 100 characters' },
        { status: 422 },
      )
    }
    const urlError = validateUrl(body.url === undefined ? '' : String(body.url))
    if (urlError) {
      return HttpResponse.json({ error: urlError }, { status: 422 })
    }
    const duplicate = mockContainerRegistries.some((r) => r.name === name)
    if (duplicate) {
      return HttpResponse.json(
        { error: 'container registry with this name already exists' },
        { status: 409 },
      )
    }
    const now = new Date().toISOString()
    const username = body.username === null || body.username === undefined
      ? null
      : String(body.username)
    const registry = {
      id: `registry-new-${nextId++}`,
      name,
      url: String(body.url),
      username,
      created_by: 'user-1',
      updated_by: 'user-1',
      created_at: now,
      updated_at: now,
    }
    mockContainerRegistries.push(registry)
    return HttpResponse.json(registry, { status: 201 })
  }),
 
  http.put('/api/v1/container-registries/:id', async ({ params, request }) => {
    const body = await request.json() as Record<string, unknown>
    const registry = mockContainerRegistries.find((r) => r.id === params.id)
    if (!registry) {
      return HttpResponse.json(
        { error: 'container registry not found' },
        { status: 404 },
      )
    }
    if (body.name !== undefined && body.name !== null) {
      const name = String(body.name)
      if (name.length < 3 || name.length > 100) {
        return HttpResponse.json(
          { error: 'name must be between 3 and 100 characters' },
          { status: 422 },
        )
      }
      const duplicate = mockContainerRegistries.some(
        (r) => r.name === name && r.id !== params.id,
      )
      if (duplicate) {
        return HttpResponse.json(
  I        { error: 'container registry with this name already exists' },
          { status: 409 },
        )
      }
    }
    if (body.url !== undefined && body.url !== null) {
      const urlError = validateUrl(String(body.url))
      if (urlError) {
        return HttpResponse.json({ error: urlError }, { status: 422 })
      }
    }
    if (
      (body.name === undefined || body.name === null) &&
      (body.url === undefined || body.url === null) &&
      (body.username === undefined || body.username === null)
    ) {
      return HttpResponse.json(
        { error: 'no fields to update' },
        { status: 422 },
      )
    }
    const now = new Date().toISOString()
    if (body.name !== undefined && body.name !== null) registry.name = String(body.name)
    if (body.url !== undefined && body.url !== null) registry.url = String(body.url)
    if (body.username !== undefined && body.username !== null) {
      registry.username = String(body.username)
    }
    registry.updated_at = now
    registry.updated_by = 'user-1'
    return HttpResponse.json(registry)
  }),
 
  http.delete('/api/v1/container-registries/:id', ({ params }) => {
    const idx = mockContainerRegistries.findIndex((r) => r.id === params.id)
    if (idx === -1) {
      return HttpResponse.json(
        { error: 'container registry not found' },
        { status: 404 },
      )
    }
    mockContainerRegistries.splice(idx, 1)
    return new HttpResponse(null, { status: 204 })
  }),
]