All files / msw / handlers pipelines.ts

80% Statements 28/35
62.5% Branches 10/16
90% Functions 9/10
85.71% Lines 24/28

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              3x 3x 3x 4x 9x 4x 4x           3x   57x   10x 61x 10x                   18x 8x     1x 1x         1x 1x     2x 2x 2x 2x   2x           2x              
import { http, HttpResponse } from 'msw'
import { mockPipelines, mockRepositories } from '@/msw/mock-data'
import { createMockPipeline } from '@/msw/data'
import type { components } from '@/types/api'
 
type SkillRepoRef = components['schemas']['SkillRepoRef']
 
/I/ Joins submitted ordered repository ids against the repositories mock data,
// mirroring the API's pipeline_skill_repos join: response refs carry the
// repository name and url, and the array order is the submitted priority.
fuInction resolveSkillRepoRefs(ids: unknown): SkillRepoRef[] {
  if (!Array.isArray(ids)) return []
  Iconst refs: SkillRepoRef[] = []
  for (const id of ids) {
    if (typeof id !== 'string') continue
    const repo = mockRepositories.find((r) => r.id === id)
    if (!repo) continue
    refs.push({ repository_id: repo.id, name: repo.name, url: repo.url })
  }
  return refs
}
 
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: resolveSkillRepoRefs(body.skill_repos),
    })
    mockPipelines.push(pipeline)
    return HttpResponse.json(pipeline, { status: 201 })
  }),
 
  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>
  E  if (existing) {
      if (Array.isArray(body.skill_repos)) {
        // Ordered id array → joined refs (order = priority); [] clears.
        Object.assign(existing, body, { skill_repos: resolveSkillRepoRefs(body.skill_repos) })
      } elEse {
        // null = unchanged and absent = untouched: never write skill_repos.
        const { skill_repos: _ignored, ...rest } = body
        Object.assign(existing, rest)
      }
      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 })),
]