#!/usr/bin/env node
// Task 626 — standing rename-completeness gate. A missed `/spawn` caller 404s
// silently in production (a no-event failure no action log can show), so this
// fails the build instead. It matches the route definition and the
// template-literal callers only; prose comments write the route as a markdown
// span (backtick before the slash) and never match the `}/spawn` + backtick
// caller shape.
import { readFileSync } from 'node:fs'
import { execFileSync } from 'node:child_process'

const ROOTS = [
  'maxy-code/platform/services/claude-session-manager/src',
  'maxy-code/platform/ui/app',
  'maxy-code/platform/ui/server',
]
const repoRoot = execFileSync('git', ['rev-parse', '--show-toplevel']).toString().trim()
const files = execFileSync('git', ['-C', repoRoot, 'ls-files', ...ROOTS])
  .toString()
  .trim()
  .split('\n')
  .filter((f) => f.endsWith('.ts') && !f.includes('__tests__'))

const BACKTICK = String.fromCharCode(96)
const BAD = [/app\.post\('\/spawn'/, new RegExp('\\}\\/spawn' + BACKTICK)]
const hits = []
for (const rel of files) {
  const text = readFileSync(`${repoRoot}/${rel}`, 'utf8')
  text.split('\n').forEach((line, i) => {
    if (BAD.some((re) => re.test(line))) hits.push(`${rel}:${i + 1}: ${line.trim()}`)
  })
}
if (hits.length) {
  console.error('[check-no-legacy-spawn-route] residual /spawn route reference(s):')
  for (const h of hits) console.error('  ' + h)
  process.exit(1)
}
console.log('[check-no-legacy-spawn-route] ok — no /spawn route definition or caller')
