All files / msw / handlers pipelines.ts

90.47% Statements 19/21
66.66% Branches 8/12
88.88% Functions 8/9
88.88% Lines 16/18

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        1x   56x   10x 61x 10x                   15x 5x     1x 1x         1x 1x     1x 1x 1x 1x 1x                      
import { http, HttpResponse } from 'msw'
import { mockPipelines } from '@/msw/mock-data'
import { createMockPipeline } from '@/msw/data'
import type { components } from '@/types/api'

type SkillRepo = components['schemas']['SkillRepo']
 
function parseSkillRepos(body: Record<string, unknown>): SkillRepo[] | undefined {
  return Array.isArray(body.skill_repos) ? (body.skill_repos as SkillRepo[]) : undefined
}
 
export const pipelineHandlers = [
  http.get('/api/v1/projects/:projectId/pipelines', ({ params }) => {
    const projectId = params.projectId as string
    const pipelines = mockPipelines.filter((p) => p.project_id === projectId)
    return HttpResponse.json({
      data: pipelines.length > 0 ? pipelines : [createMockPipeline({ project_id: projectId })],
      meta: { limit: 50, returned: Math.max(pipelines.length, 1), has_more: false },
    })
  }),
 
  http.get('/api/v1/pipelines/:id', ({ params }) => {
    const pipeline = mockPipelines.find((p) => p.id === params.id)
    return HttpResponse.json(pipeline ?? createMockPipeline({ id: params.id as string }))
  }),
 
  http.post('/api/v1/projects/:projectId/pipelines', async ({ params, request }) => {
    const body = (await request.json()) as Record<string, unknown>
    const pipeline = createMockPipeline({
      project_id: params.projectId as string,
      config: (body.config as Record<string, unknown> | null | undefined) ?? null,
      skill_repos: parseSkillRepos(body) ?? [],
    })
    mockPipelines.push(pipeline)
    return HttpResponse.json(pipeline, { status: 201 })
  }),
E
  http.put('/api/v1/pipelines/:id', async ({ params, request }) => {
    const existing = mockPipelines.find((p) => p.id === params.id)
    const body = (await request.json()) as Record<string, unknown>
    if (existing) {
      Object.assign(existing, body)
      return HttpResponse.json(existing)
    }
    return HttpResponse.json(createMockPipeline({ id: params.id as string, ...body }))
  }),
 
  http.delete('/api/v1/pipelines/:id', () => new HttpResponse(null, { status: 204 })),
]