All files / msw / handlers repositories.ts

21.05% Statements 8/38
22.22% Branches 4/18
10% Functions 1/10
26.66% Lines 8/30

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    40x 40x   1x 1x 1x 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 })
    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,
      updated_at: now,
    }
    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 }) => {
    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),
    })
  }),
]