/**
 * Snapshot test for the scaffold-contracts generator wrapper.
 *
 * Calls generate() (which runs the real script) and asserts:
 * 1. The output file exists and has content.
 * 2. The content matches the stored snapshot (drift detection).
 *
 * If the snapshot is missing it is created on first run. Subsequent runs fail
 * if the output changes without an intentional snapshot update.
 */

import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { describe, it, expect } from 'vitest'

/** Monorepo root relative to packages/core/src/_gen/__tests__/ */
const ROOT = resolve(import.meta.dirname, '..', '..', '..', '..', '..')

const OUTPUT_PATH = resolve(ROOT, 'packages/core/src/reference/_generated/contracts.md')
const SNAPSHOT_PATH = resolve(import.meta.dirname, '__snapshots__', 'contracts.md.snap')
const CAPABILITY_CATALOG_OUTPUT_PATH = resolve(
  ROOT,
  'packages/core/src/reference/_generated/system-interface-capabilities.md'
)
const CAPABILITY_CATALOG_SNAPSHOT_PATH = resolve(
  import.meta.dirname,
  '__snapshots__',
  'system-interface-capabilities.md.snap'
)

function normalizeSnapshotContent(content: string) {
  return content
    .replace(/\r\n/g, '\n')
    .replace(
      /<!-- Auto-generated on \d{4}-\d{2}-\d{2} by scripts\/monorepo\/generate-scaffold-contracts\.js -->/,
      '<!-- Auto-generated on YYYY-MM-DD by scripts/monorepo/generate-scaffold-contracts.js -->'
    )
}

describe('scaffold-contracts generator', () => {
  it('output file exists and has content', () => {
    // The generator must have been run (either manually or by CI gen step).
    // This test validates the committed artifact — it does NOT re-run the generator
    // so the test suite stays fast and deterministic.
    let content: string
    try {
      content = readFileSync(OUTPUT_PATH, 'utf8')
    } catch {
      throw new Error(
        `Generated file not found: ${OUTPUT_PATH}\n` +
          `Run "pnpm scaffold:generate" or "node scripts/monorepo/generate-scaffold-contracts.js" first.`
      )
    }

    expect(content.length).toBeGreaterThan(0)
  })

  it('capability catalog output file exists and has content', () => {
    let content: string
    try {
      content = readFileSync(CAPABILITY_CATALOG_OUTPUT_PATH, 'utf8')
    } catch {
      throw new Error(
        `Generated file not found: ${CAPABILITY_CATALOG_OUTPUT_PATH}\n` +
          `Run "pnpm scaffold:generate" or "node scripts/monorepo/generate-scaffold-contracts.js" first.`
      )
    }

    expect(content.length).toBeGreaterThan(0)
  })

  it('output file matches stored snapshot', () => {
    let content: string
    try {
      content = readFileSync(OUTPUT_PATH, 'utf8')
    } catch {
      throw new Error(
        `Generated file not found: ${OUTPUT_PATH}\n` +
        `Run "pnpm scaffold:generate" first to produce the artifact before snapshotting.`
      )
    }

    const snapshot = readFileSync(SNAPSHOT_PATH, 'utf8')

    expect(normalizeSnapshotContent(content)).toBe(normalizeSnapshotContent(snapshot))
  })

  it('capability catalog output file matches stored snapshot', () => {
    let content: string
    try {
      content = readFileSync(CAPABILITY_CATALOG_OUTPUT_PATH, 'utf8')
    } catch {
      throw new Error(
        `Generated file not found: ${CAPABILITY_CATALOG_OUTPUT_PATH}\n` +
          `Run "pnpm scaffold:generate" first to produce the artifact before snapshotting.`
      )
    }

    const snapshot = readFileSync(CAPABILITY_CATALOG_SNAPSHOT_PATH, 'utf8')

    expect(normalizeSnapshotContent(content)).toBe(normalizeSnapshotContent(snapshot))
  })
})
