All files validate.ts

57.89% Statements 77/133
82.35% Branches 28/34
50% Functions 1/2
57.89% Lines 77/133

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      1x   1x                         1x 8x     8x 15x 15x 24x 9x 1x 1x 1x 1x 9x 24x 15x 1x 1x 15x     8x 15x 15x 24x 5x 1x 1x   1x 1x 5x 24x 15x 1x 1x 15x     8x 8x 8x 15x 15x 24x 5x 24x     24x     5x 5x 15x     15x 8x         8x 8x 6x 6x 6x 1x 1x 1x 6x     8x   8x 15x   15x     15x   15x 15x 24x     24x 24x   15x 15x 1x 1x   15x 1x 1x 15x   8x 8x                                                                                                                        
// SPDX-License-Identifier: MIT
// Copyright (c) 2024–2026 Flavio De Musso
 
import pc from 'picocolors'
import type { Seed } from '@beechcms/core'
import { SEED_REGISTRY, sortSeedsByDependencies } from '@beechcms/core'
 
export interface ValidateOptions {
  registry?: Record<string, Seed> | null
}
 
export interface SeedValidationError {
  slug: string
  messages: string[]
  /** true = abort seed:load; false = warning only */
  fatal: boolean
}
 
export function validateSeeds(registry: Record<string, Seed>): SeedValidationError[] {
  const result: SeedValidationError[] = []
 
  // ── Fatal check 1: unknown relation targets ──────────────────────────────
  for (const seed of Object.values(registry)) {
    const messages: string[] = []
    for (const branch of seed.branches) {
      if (branch.type === 'relation' && branch.targetSeed) {
        if (!registry[branch.targetSeed]) {
          messages.push(
            `branch '${branch.alias}' targets unknown seed '${branch.targetSeed}'`,
          )
        }
      }
    }
    if (messages.length > 0) {
      result.push({ slug: seed.slug, messages, fatal: true })
    }
  }
 
  // ── Fatal check 2: multi-relation with SET NULL ──────────────────────────
  for (const seed of Object.values(registry)) {
    const messages: string[] = []
    for (const branch of seed.branches) {
      if (branch.type === 'relation' && branch.multiple === true) {
        if (branch.onDelete === 'SET NULL') {
          messages.push(
            `Branch '${branch.alias}': multi-relations cannot use ON DELETE SET NULL. ` +
            `Use 'CASCADE' or 'RESTRICT'.`,
          )
        }
      }
    }
    if (messages.length > 0) {
      result.push({ slug: seed.slug, messages, fatal: true })
    }
  }
 
  // ── Fatal check 3: junction table name collisions ────────────────────────
  {
    const junctionNames = new Set<string>()
    for (const seed of Object.values(registry)) {
      const messages: string[] = []
      for (const branch of seed.branches) {
        if (branch.type !== 'relation' || branch.multiple !== true) continue
        const name = `rel_${seed.slug}_${branch.alias}`
        if (name.length > 256) {
          messages.push(`Junction table name '${name}' exceeds 256 characters (${name.length})`)
        }
        if (junctionNames.has(name)) {
          messages.push(`Junction table name collision: '${name}' already used by another seed/branch`)
        }
        junctionNames.add(name)
      }
      if (messages.length > 0) {
        result.push({ slug: seed.slug, messages, fatal: true })
      }
    }
  }
 
  // ── Fatal check 4: dependency cycles ────────────────────────────────────
  // Skip if unknown targets were found — sortSeedsByDependencies would throw
  // on the same unknown targets, producing duplicate fatal messages.
  const hasUnknownTargets = result.some(e => e.fatal)
  if (!hasUnknownTargets) {
    try {
      sortSeedsByDependencies(Object.values(registry))
    } catch (err) {
      const msg = err instanceof Error ? err.message : String(err)
      result.push({ slug: '<graph>', messages: [msg], fatal: true })
    }
  }
 
  // ── Warning checks (existing) ────────────────────────────────────────────
  const slugsSeen = new Set<string>()
 
  for (const seed of Object.values(registry)) {
    const messages: string[] = []
 
    if (slugsSeen.has(seed.slug)) {
      messages.push(`duplicate slug "${seed.slug}" — each seed must have a unique slug`)
    }
    slugsSeen.add(seed.slug)
 
    const aliasesSeen = new Set<string>()
    for (const branch of seed.branches) {
      if (aliasesSeen.has(branch.alias)) {
        messages.push(`duplicate branch alias "${branch.alias}"`)
      }
      aliasesSeen.add(branch.alias)
    }
 
    const allAliases = new Set(seed.branches.map(b => b.alias))
    if (!allAliases.has(seed.displayNameAlias)) {
      messages.push(`displayNameAlias "${seed.displayNameAlias}" not found in branches`)
    }
 
    if (messages.length > 0) {
      result.push({ slug: seed.slug, messages, fatal: false })
    }
  }
 
  return result
}
 
export async function validate(args: ValidateOptions): Promise<void> {
  const registry = args.registry ?? SEED_REGISTRY
 
  if (Object.keys(registry).length === 0) {
    console.warn(pc.yellow('\n  Warning: SEED_REGISTRY is empty. Create a seeds.ts in your project root.\n'))
    return
  }
 
  console.log(pc.cyan('\n  beech validate — checking seeds\n'))
 
  const errors = validateSeeds(registry)
  const fatalErrors = errors.filter(e => e.fatal)
  const warnings = errors.filter(e => !e.fatal)
 
  // Print fatal errors first
  for (const e of fatalErrors) {
    console.log(pc.red(`  ✗ ${e.slug} (fatal)`))
    for (const msg of e.messages) {
      console.log(pc.red(`      → ${msg}`))
    }
  }
 
  // Print per-seed warnings
  const warningMap = new Map(warnings.map(e => [e.slug, e.messages]))
  const allWarningSlugsSeen = new Set(warnings.map(e => e.slug))
 
  for (const seed of Object.values(registry)) {
    const msgs = warningMap.get(seed.slug)
    if (!msgs) {
      if (!allWarningSlugsSeen.has(seed.slug)) {
        // only print ✓ if no fatal error for this slug either
        const hasFatal = fatalErrors.some(e => e.slug === seed.slug)
        if (!hasFatal) console.log(pc.green(`  ✓ ${seed.slug}`))
      }
    } else {
      console.log(pc.yellow(`  ⚠ ${seed.slug}`))
      for (const msg of msgs) {
        console.log(pc.yellow(`      → ${msg}`))
      }
    }
  }
 
  console.log('')
 
  const totalFatal = fatalErrors.reduce((n, e) => n + e.messages.length, 0)
  const totalWarnings = warnings.reduce((n, e) => n + e.messages.length, 0)
 
  if (totalFatal > 0) {
    const s = totalFatal !== 1 ? 's' : ''
    console.log(pc.red(`  Found ${totalFatal} fatal error${s}. Fix before loading.\n`))
    process.exit(1)
  } else if (totalWarnings > 0) {
    const s = totalWarnings !== 1 ? 's' : ''
    console.log(pc.yellow(`  Found ${totalWarnings} warning${s}. Review seeds above.\n`))
  } else {
    console.log(pc.green('  All seeds valid.\n'))
  }
}