/**
 * Regression test: Recovery implementation completeness
 *
 * Verifies that checkPidLiveness and scanOrphanReferences have real
 * implementations, not just stub return values.
 */

import { describe, test, expect } from 'bun:test'
import { readFileSync } from 'fs'
import { join } from 'path'

const SOURCE_PATH = join(
  import.meta.dir,
  '..',
  '..',
  'src',
  'storage',
  'Recovery.ts'
)

const source = readFileSync(SOURCE_PATH, 'utf-8')

describe('Recovery implementation', () => {
  test('checkPidLiveness is not a stub (has implementation code)', () => {
    // Should have actual implementation with loop logic
    expect(source).toContain('for (const agent of agents)')
    expect(source).toContain("action: alive ? 'keep' : 'mark_lost'")
    // Should have more than just a bare return []
    expect(source).toContain('const reports: PidLivenessReport[] = []')
  })

  test('checkPidLiveness uses process.kill for liveness check', () => {
    // Should use process.kill(pid, 0) for signal-0 liveness check
    expect(source).toContain('process.kill(agent.pid, 0)')
  })

  test('scanOrphanReferences returns OrphanReferenceReport structure', () => {
    // Should define fkChecks array with the 8 invariant checks
    expect(source).toContain('fkChecks')
    expect(source).toContain("table: 'tasks'")
    expect(source).toContain("table: 'messages'")
    expect(source).toContain("table: 'task_attempts'")
    expect(source).toContain("table: 'agents'")
    expect(source).toContain("table: 'tool_runs'")
    expect(source).toContain("table: 'command_runs'")
    expect(source).toContain("table: 'artifacts'")
    expect(source).toContain("table: 'evidence_refs'")

    // Should iterate over checks
    expect(source).toContain('for (const check of fkChecks)')

    // Should return a proper report
    expect(source).toContain('return report')
  })
})
