All files / web/src/components/toys/euclid/engine validatePropositionDef.ts

91.3% Statements 168/184
93.47% Branches 43/46
100% Functions 4/4
91.3% Lines 168/184

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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 1851x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 10x 10x 4x 4x 10x 3x     1x 1x 1x 1x 39x 39x 39x 39x 39x 13x 13x 13x 39x 39x 8x 8x 8x 39x 39x 11x 2x 2x 11x 5x 7x 7x 5x 11x 5x 7x 7x 5x 11x 39x 39x 5x 11x 11x 5x 39x 39x 2x 2x 2x 39x 39x 39x 39x 1x 1x 1x 1x 39x 39x 6x 6x 39x 3x 3x 39x 2x 2x 28x 28x 1x 1x 1x 1x 1x 1x 1x 1x 25x 25x 25x 25x 25x 52x 47x 47x 52x 25x 25x 25x 52x 5x               5x 1x 1x 1x 1x 1x 1x 1x 5x 52x 25x 25x 25x 39x 39x 39x 39x 73x 19x 19x 19x 19x 19x 19x 19x 73x 39x 39x 39x 28x 2x 2x 2x 2x 2x 2x 2x 28x 39x 39x 39x 11x 11x 39x 25x 25x 25x 4x 6x               6x 1x 1x 1x 1x 1x 1x 1x 6x 4x 25x 25x 25x  
import type { PropositionDef, ExpectedAction, ElementSelector } from '../types'
 
export interface ValidationError {
  stepIndex: number
  field: string
  pointId: string
  message: string
}
 
/**
 * Extract all point IDs referenced by an ElementSelector.
 * String selectors that look like point IDs ('pt-X') are included.
 * Structured selectors yield their defining point IDs.
 */
function pointIdsFromSelector(sel: ElementSelector): string[] {
  if (typeof sel === 'string') {
    return sel.startsWith('pt-') ? [sel] : []
  }
  if (sel.kind === 'circle') return [sel.centerId, sel.radiusPointId]
  if (sel.kind === 'segment') return [sel.fromId, sel.toId]
  return []
}
 
/**
 * Extract all point IDs referenced by an ExpectedAction.
 */
function referencedPointIds(expected: ExpectedAction): { field: string; pointId: string }[] {
  const refs: { field: string; pointId: string }[] = []
 
  switch (expected.type) {
    case 'compass':
      refs.push({ field: 'centerId', pointId: expected.centerId })
      refs.push({ field: 'radiusPointId', pointId: expected.radiusPointId })
      break
 
    case 'straightedge':
      refs.push({ field: 'fromId', pointId: expected.fromId })
      refs.push({ field: 'toId', pointId: expected.toId })
      break
 
    case 'intersection':
      if (expected.beyondId) {
        refs.push({ field: 'beyondId', pointId: expected.beyondId })
      }
      if (expected.ofA != null) {
        for (const id of pointIdsFromSelector(expected.ofA)) {
          refs.push({ field: 'ofA', pointId: id })
        }
      }
      if (expected.ofB != null) {
        for (const id of pointIdsFromSelector(expected.ofB)) {
          refs.push({ field: 'ofB', pointId: id })
        }
      }
      break
 
    case 'macro':
      for (const id of expected.inputPointIds) {
        refs.push({ field: 'inputPointIds', pointId: id })
      }
      break
 
    case 'extend':
      refs.push({ field: 'baseId', pointId: expected.baseId })
      refs.push({ field: 'throughId', pointId: expected.throughId })
      break
  }
 
  return refs
}
 
/**
 * Determine what new point IDs a step introduces after it completes.
 */
function introducedPointIds(expected: ExpectedAction): string[] {
  if (expected.type === 'intersection' && expected.label) {
    return [`pt-${expected.label}`]
  }
  if (expected.type === 'macro' && expected.outputLabels) {
    return Object.values(expected.outputLabels).map((label) => `pt-${label}`)
  }
  if (expected.type === 'extend') {
    return [`pt-${expected.label}`]
  }
  return []
}
 
/**
 * Validate a PropositionDef by walking its steps in order and checking that
 * every referenced point ID is either given or introduced by a prior step.
 *
 * Returns an empty array if the definition is valid.
 */
export function validatePropositionDef(prop: PropositionDef): ValidationError[] {
  const errors: ValidationError[] = []
 
  // Collect initial point IDs from given elements
  const knownPoints = new Set<string>()
  for (const el of prop.givenElements) {
    if (el.kind === 'point') {
      knownPoints.add(el.id)
    }
  }
 
  // Also validate givenElements internal references (segment fromId/toId)
  for (const el of prop.givenElements) {
    if (el.kind === 'segment') {
      if (!knownPoints.has(el.fromId)) {
        errors.push({
          stepIndex: -1,
          field: 'givenElements.segment.fromId',
          pointId: el.fromId,
          message: `Given segment ${el.id} references unknown point '${el.fromId}'`,
        })
      }
      if (!knownPoints.has(el.toId)) {
        errors.push({
          stepIndex: -1,
          field: 'givenElements.segment.toId',
          pointId: el.toId,
          message: `Given segment ${el.id} references unknown point '${el.toId}'`,
        })
      }
    }
  }
 
  // Walk steps in order
  for (let i = 0; i < prop.steps.length; i++) {
    const step = prop.steps[i]
    const refs = referencedPointIds(step.expected)
 
    for (const ref of refs) {
      if (!knownPoints.has(ref.pointId)) {
        errors.push({
          stepIndex: i,
          field: ref.field,
          pointId: ref.pointId,
          message: `Step ${i} (${step.expected.type}) references unknown point '${ref.pointId}' in ${ref.field}`,
        })
      }
    }
 
    // Check highlightIds
    for (const id of step.highlightIds) {
      if (id.startsWith('pt-') && !knownPoints.has(id)) {
        errors.push({
          stepIndex: i,
          field: 'highlightIds',
          pointId: id,
          message: `Step ${i} highlightIds references unknown point '${id}'`,
        })
      }
    }
 
    // Add points introduced by this step
    for (const id of introducedPointIds(step.expected)) {
      knownPoints.add(id)
    }
  }
 
  // Validate resultSegments
  if (prop.resultSegments) {
    for (const seg of prop.resultSegments) {
      if (!knownPoints.has(seg.fromId)) {
        errors.push({
          stepIndex: -1,
          field: 'resultSegments.fromId',
          pointId: seg.fromId,
          message: `resultSegments references unknown point '${seg.fromId}'`,
        })
      }
      if (!knownPoints.has(seg.toId)) {
        errors.push({
          stepIndex: -1,
          field: 'resultSegments.toId',
          pointId: seg.toId,
          message: `resultSegments references unknown point '${seg.toId}'`,
        })
      }
    }
  }
 
  return errors
}