All files / features / pipelines laneLint.ts

85.33% Statements 128/150
84.78% Branches 78/92
100% Functions 10/10
92.59% Lines 100/108

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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159            152x 152x 152x 75x 75x   75x             152x         75x           144x           242x 242x     8x     45x 38x   64x   29x       160x 38x 38x   36x 36x   36x 38x 38x 4x 33x 33x 38x 38x 34x   33x 34x 34x 5x   26x 25x 1x 38x 38x       34x 36x 24x 1x     34x 34x 34x 34x 34x 246x 23x 4x 23x 242x 242x 34x 34x 46x   46x 34x 22x 1x 1x 34x 34x 34x 1x   20x 34x 21x 4x 4x 34x 34x 34x 1x   16x 34x 16x   208x 208x 16x     208x 208x 3x 3x   208x 21x 2x 34x   19x 34x 272x 64x 23x 1x 34x   23x 184x 160x       23x    
import type { LintIssue, LintResult } from '@/layers/interfaces'
 
// Mirrors api/internal/handlers/lanes.go LintLaneConfig exactly (ADR-0012):
// same rule semantics, same message strings, same warning emission order.
// The lint is wired to the pipeline `config` field, so `parsed` is the full
// pipeline config object ({ vikunja?: { lanes?: ... } }). Root and vikunja
// strictness mirrors the API (Phase 4b/4b2): undeclared root keys, a
// non-object vikunja value, and undeclared vikunja keys are errors.
 
const CONFIG_ROOT_KEYS = new Set(['vikunja'])
const VIKUNJA_CONFIG_KEYS = new Set(['lanes'])
 
const LANE_KEYS = new Set([
  'start',
  'open_questions',
  'blocked',
  'in_progress',
  'pr_open',
  'needs_rework',
  'done',
  'pr_rejected',
  'failed',
])
 
const OPTIONAL_LANE_KEYS = [
  'open_questions',
  'blocked',
  'in_progress',
  'pr_open',
  'needs_rework',
  'done',
  'pr_rejected',
  'failed',
] as const
 
function isPlainObject(value: unknown): value is Record<string, unknown> {
 I return typeof value === 'object' && value !== null && !Array.isArray(value)
}
 
// Mirrors parseLaneID: the value must be a positive integer. YAML numbers
// arrive as JS numbers; string literals like "1" are rejected, as are
// fractions and non-positive values.
function positiveInteger(raw: unknown): number | null {
  if (typeof raw !== 'number' || !Number.isInteger(raw) || raw <= 0) return null
  return raw
}
 
function error(field: string, message: string): LintIssue {
  return { field, message }
}
 
function warning(field: string, message: string): LintIssue {
  return { field, message }
}
I
export function laneLint(parsed: unknown, _raw: string): LintResult {
  const errors: LintIssue[] = []
  const warnings: LintIssue[] = []
 
  if (!isPlainObject(parsed)) {
    return { errors: [error('config', 'config must be a JSON object')], warnings }
  }
 
  for (const key of Object.keys(parsed)) {
    if (!CONFIG_ROOT_KEYS.has(key)) {
      errors.push(error('config', `config: unknown key "${key}"`))
    }
  I}

  const vikunja = parsed['vikunja']
  if (vikunja !== undefined) {
    Iif (!isPlainObject(vikunja)) {
      errors.push(error('vikunja', 'vikunja: must be an object'))
    } else {
      for (const key of Object.keys(vikunja)) {
        if (!VIKUNJA_CONFIG_KEYS.has(key)) {
          errors.push(error('vikunja', `vikunja: unknown key "${key}"`))
        }
      }
    }
  }
 
 I const lanes = isPlainObject(vikunja) ? vikunja['lanes'] : undefined
  if (lanes === undefined) return { errors, warnings }
  if (!isPlainObject(lanes)) {
    return {
      errors: [error('vikunja.lanes', 'vikunja.lanes: must be a JSON object')],
      warnings,
    }
  }
 
  const configured = new Set<string>()
  let startPresent = false
  const seenStart = new Set<number>()
  const owner = new Map<number, string>()
 
  for (const [key, raw] of Object.entries(lanes)) {
    if (!LANE_KEYS.has(key)) {
      errors.push(error('vikunja.lanes', `vikunja.lanes: unknown lane key "${key}"`))
      continue
   I }
    configured.add(key)

    if (key === 'start') {
   I   startPresent = true
      if (!Array.isArray(raw)) {
        errors.push(error('vikunja.lanes.start', 'vikunja.lanes.start: must be an array of positive integers'))
        continue
      }
      if (raw.length === 0) {
    I    errors.push(error('vikunja.lanes.start', 'vikunja.lanes.start: must contain at least one bucket id'))
        continue
      }
      for (const el of raw) {
    I    const id = positiveInteger(el)
        if (id === null) {
          errors.push(error('vikunja.lanes.start', 'vikunja.lanes.start: must be a positive integer'))
          continue
        }
        if (seenStart.has(id)) {
    I      errors.push(error('vikunja.lanes.start', `vikunja.lanes.start: duplicate bucket id ${id}`))
          continue
        }
        seenStart.add(id)
        const prev = owner.get(id)
        Iif (prev !== undefined) {
          errors.push(error('vikunja.lanes', `vikunja.lanes: duplicate bucket id ${id} across lanes (${prev}, start)`))
          continue
   I     }
        owner.set(id, 'start')
      }
    } else {
      const id = positiveInteger(raw)
   I   if (id === null) {
        errors.push(error(`vikunja.lanes.${key}`, `vikunja.lanes.${key}: must be a positive integer`))
        continue
      }
      const prev = owner.get(id)
      if (prev !== undefined) {
        errors.push(error('vikunja.lanes', `vikunja.lanes: duplicate bucket id ${id} across lanes (${prev}, ${key})`))
 I       continue
      }
      owner.set(id, key)
    }
  }
 
  if (!startPresent) {
    errors.push(error('vikunja.lanes.start', 'vikunja.lanes.start: is required'))
  }
 
  for (const key of OPTIONAL_LANE_KEYS) {
    if (!configured.has(key)) {
      warnings.push(warning(`vikunja.lanes.${key}`, `vikunja.lanes.${key}: not configured`))
    }
  }
 
  return { errors, warnings }
}