All files / msw / handlers repositories.ts

47.61% Statements 20/42
40.9% Branches 9/22
50% Functions 5/10
52.94% Lines 18/34

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    37x 37x   14x 14x 14x 14x 14x 14x                   14x 7x 7x                                                           1x 1x 1x       2x 1x 1x 1x                                    
import { http, HttpResponse } from 'msw'
import { mockRepositories } from '@/msw/mock-data'
 
let nextId = 100
 
export const repositoryHandlers = [
  http.get('/api/v1/repositories', ({ 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 = mockRepositories.slice(offset, offset + limit)
    const hasMore = offset + limit < mockRepositories.length
    return HttpResponse.json({
      data: paginated,
      meta: { limit, returned: paginated.length, has_more: hasMore },
    })
  }),
 
  http.get('/api/v1/repositories/:id', ({ params }) => {
    const repo = mockRepositories.find((r) => r.id === params.id)
    if (!repo) return new HttpResponse(null, { status: 404 })
  I  return HttpResponse.json(repo)
  }),
 
  http.post('/api/v1/repositories', async ({ request }) => {
    const body = await request.json() as Record<string, unknown>
    const now = new Date().toISOString()
    const repo = {
      id: `repo-new-${nextId++}`,
      name: body.name as string,
      url: body.url as string,
      description: (body.description as string) ?? null,
      auth_token_encrypted: (body.auth_token_encrypted as string) ?? null,
      status: 'unknown' as const,
      last_checked_at: null,
      created_at: now,
      created_by: 'system',
      updated_at: now,
      updated_by: 'system',
    }
    mockRepositories.push(repo)
    return HttpResponse.json(repo, { status: 201 })
  }),

  http.put('/api/v1/repositories/:id', async ({ params, request }) => {
    const body = await request.json() as Record<string, unknown>
    const repo = mockRepositories.find((r) => r.id === params.id)
    if (!repo) return new HttpResponse(null, { status: 404 })
    const now = new Date().toISOString()
    Object.assign(repo, body, { updated_at: now })
    return HttpResponse.json(repo)
  }),
 
  http.delete('/api/v1/repositories/:id', ({ params }) => {
   I try {
      const blockedIds = JSON.parse(localStorage.getItem('__blocked_repo_ids__') || '[]')
      if (blockedIds.includes(params.id as string)) {
        return HttpResponse.json(
          { error: 'Cannot delete: repository is in use' },
  I        { status: 409 },
        )
      }
    } catch { /* localStorage may be unavailable */ }
    const idx = mockRepositories.findIndex((r) => r.id === params.id)
    if (idx === -1) return new HttpResponse(null, { status: 404 })
    mockRepositories.splice(idx, 1)
    return new HttpResponse(null, { status: 204 })
  }),
 
  http.get('/api/v1/repositories/:id/health', ({ params }) => {
    const repo = mockRepositories.find((r) => r.id === params.id)
    if (!repo) return new HttpResponse(null, { status: 404 })
    const status = repo.status
    return HttpResponse.json({
      status,
      latency_ms: status === 'connected' ? 150 + Math.floor(Math.random() * 500) : 5000 + Math.floor(Math.random() * 10000),
    })
  }),
]