All files / msw / handlers projects.ts

64.86% Statements 24/37
63.63% Branches 28/44
66.66% Functions 8/12
65.62% Lines 21/32

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      141x                       141x 47x         47x 20x                 21x 6x     6x     2x 2x     2x 2x 2x                     2x 2x     1x 4x 1x     1x     1x             1x                                                                                      
import { http, HttpResponse } from 'msw'
import { mockProjects } from '@/msw/mock-data'
 
interface StoredProject {
  id: string
  name: string
  description: string | null
  config: Record<string, unknown> | null
  pipeline_count: number
  created_by: string
  updated_by: string
  created_at: string
  updated_at: string
}
 
function normalizeMock(p: Record<string, unknown>, idx: number): StoredProject {
  return {
    id: (p.id as string) ?? `proj-${idx}`,
    name: (p.name as string) ?? 'Unnamed',
    description: (p.description as string) ?? null,
    config: (p.config as Record<string, unknown>) ?? null,
    pipeline_count: (p.pipeline_count as number) ?? 0,
    created_by: (p.created_by as string) ?? 'user-1',
    updated_by: (p.updated_by as string) ?? 'user-1',
    created_at: (p.created_at as string) ?? new Date(Date.now() - 60 * 24 * 3600000).toISOString(),
    updated_at: (p.updated_at as string) ?? new Date().toISOString(),
  }
}
 
let projectStore: StoredProject[] = mockProjects.map((p, i) => normalizeMock(p as Record<string, unknown>, i + 1))
let idCounter = 10
 
exIport function resetProjectStore() {
  projectStore = mockProjects.map((p, i) => normalizeMock(p as Record<string, unknown>, i + 1))
  idCounter = 10
}
 
export const projectHandlers = [
  http.get('/api/v1/projects', () =>
  I  HttpResponse.json({
      data: projectStore,
      meta: { limit: 50, returned: projectStore.length, has_more: false },
    }),
  ),
 
  http.get('/api/v1/projects/:id', ({ params }) => {
    const project = projectStore.find((p) => p.id === params.id)
    if (!project) {
      return new HttpResponse(null, { status: 404 })
    }
    return HttpResponse.json(project)
  }),
 
  http.post('/api/v1/projects', async ({ request }) => {
    const body = (await request.json()) as { name: string; description?: string | null; config?: Record<string, unknown> | null; secrets?: Record<string, unknown> }
 
    if (!body.name || body.name.length < 3 || body.name.length > 100) {
      return HttpResponse.json(
        { error: 'Project name must be 3 to 100 characters' },
        { status: 400 },
      )
  I  }

    const now = new Date().toISOString()
  I  idCounter++
    const newProject: StoredProject = {
      id: `proj-${idCounter}`,
      name: body.name,
      description: body.description ?? null,
      config: body.config ?? null,
      pipeline_count: 0,
      created_by: 'user-1',
      updated_by: 'user-1',
      created_at: now,
      updated_at: now,
    }

    projectStore.push(newProject)

    return HttpResponse.json(newProject, { status: 201 })
  }),

  http.put('/api/v1/projects/:id', async ({ params, request }) => {
    const body = (await request.json()) as { name?: string; description?: string | null; config?: Record<string, unknown> | null; secrets?: Record<string, unknown> }
    const existing = projectStore.find((p) => p.id === params.id)
 
    if (!existing) {
      return new HttpResponse(null, { status: 404 })
    }
 
    if (body.name !== undefined && (body.name.length < 3 || body.name.length > 100)) {
      return HttpResponse.json(
        { error: 'Project name must be 3 to 100 characters' },
        { status: 400 },
      )
    }
 
    Object.assign(existing, {
      name: body.name ?? existing.name,
      description: body.description !== undefined ? body.description : existing.description,
      config: body.config !== undefined ? body.config : existing.config,
      updated_at: new Date().toISOString(),
      updated_by: 'user-1',
    })
 
    return HttpResponse.json(existing)
  }),
 
  http.delete('/api/v1/projects/:id', ({ params }) => {
    const idx = projectStore.findIndex((p) => p.id === params.id)
    if (idx === -1) {
      return new HttpResponse(null, { status: 404 })
    }
    projectStore.splice(idx, 1)
    return new HttpResponse(null, { status: 204 })
  }),
]