// tests/lib/doc-consistency.test.mjs
// Pin invariants between prose (CLAUDE.md, README.md) and source files
// (agents/*.md, commands/*.md, templates/, settings.json).
//
// When this test fails, fix the source-of-truth — do NOT rewrite the test to
// hide drift. Borrowed pattern from llm-security commit 97c5c9d.

import { test } from 'node:test';
import { strict as assert } from 'node:assert';
import { readFileSync, readdirSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { parseDocument } from '../../lib/util/frontmatter.mjs';

const HERE = dirname(fileURLToPath(import.meta.url));
const ROOT = join(HERE, '..', '..');

function read(rel) { return readFileSync(join(ROOT, rel), 'utf-8'); }
function listMd(rel) { return readdirSync(join(ROOT, rel)).filter(f => f.endsWith('.md')); }

test('CLAUDE.md agents table row count == agents/*.md file count', () => {
  const md = read('CLAUDE.md');
  const agentFiles = listMd('agents');
  const agentTable = md.split('## Agents')[1] || '';
  const tableSection = agentTable.split('\n## ')[0];
  const dataRows = tableSection
    .split('\n')
    .filter(l => l.startsWith('|') && !l.match(/^\|[\s-]+\|/) && !l.match(/^\|\s*Agent\s*\|/));
  assert.equal(
    dataRows.length,
    agentFiles.length,
    `Drift: ${agentFiles.length} agent files vs ${dataRows.length} CLAUDE.md table rows. ` +
    `Sync agents/ ↔ CLAUDE.md.`,
  );
});

test('CLAUDE.md commands table mentions every commands/*.md file', () => {
  const md = read('CLAUDE.md');
  const commandFiles = listMd('commands');
  for (const f of commandFiles) {
    const cmdName = `/${f.replace(/\.md$/, '')}`;
    assert.ok(
      md.includes(cmdName),
      `commands/${f} not mentioned in CLAUDE.md (looked for ${cmdName})`,
    );
  }
});

test('every command frontmatter name matches its filename', () => {
  for (const f of listMd('commands')) {
    const text = read(`commands/${f}`);
    const doc = parseDocument(text);
    if (!doc.valid) continue;
    const expected = f.replace(/\.md$/, '');
    if (doc.parsed.frontmatter && doc.parsed.frontmatter.name !== undefined) {
      assert.equal(
        doc.parsed.frontmatter.name,
        expected,
        `commands/${f} frontmatter.name="${doc.parsed.frontmatter.name}" should be "${expected}"`,
      );
    }
  }
});

test('templates/plan-template.md declares plan_version: 1.7', () => {
  const tpl = read('templates/plan-template.md');
  assert.match(tpl, /plan_version:\s*['"]?1\.7['"]?/);
});

test('commands/ultraexecute-local.md still parses v1.7 plan schema', () => {
  const cmd = read('commands/ultraexecute-local.md');
  const tpl = read('templates/plan-template.md');
  const tplVersion = (tpl.match(/plan_version:\s*['"]?([\d.]+)['"]?/) || [])[1];
  assert.ok(tplVersion, 'templates/plan-template.md missing plan_version');
  assert.ok(
    cmd.includes(`plan_version`) || cmd.includes(`Step N:`) || cmd.includes('### Step '),
    'commands/ultraexecute-local.md should reference v1.7 plan-schema parsing',
  );
});

test('settings.json has only known top-level scopes after Spor 0 cleanup', () => {
  const cfg = JSON.parse(read('settings.json'));
  const known = ['ultraplan', 'ultraresearch'];
  for (const k of Object.keys(cfg)) {
    assert.ok(known.includes(k), `Unknown top-level scope in settings.json: ${k}`);
  }
});

test('settings.json no longer carries vestigial exploration block', () => {
  const cfg = JSON.parse(read('settings.json'));
  assert.equal(cfg.ultraplan?.exploration, undefined,
    'exploration block was vestigial — should be deleted in v3.1.0 Spor 0');
  assert.equal(cfg.ultraplan?.agentTeam, undefined,
    'agentTeam block was vestigial — should be deleted in v3.1.0 Spor 0');
});

test('CLAUDE.md mentions all five pipeline commands', () => {
  const md = read('CLAUDE.md');
  for (const c of ['/ultrabrief-local', '/ultraresearch-local', '/ultraplan-local', '/ultraexecute-local', '/ultrareview-local']) {
    assert.ok(md.includes(c), `CLAUDE.md missing reference to ${c}`);
  }
});

test('HANDOVER-CONTRACTS.md contains Handover 6 section', () => {
  const text = read('docs/HANDOVER-CONTRACTS.md');
  assert.ok(
    text.includes('## Handover 6'),
    'docs/HANDOVER-CONTRACTS.md should document Handover 6 (review → plan)',
  );
});

test('HANDOVER-CONTRACTS.md contains Handover 7 section (session-state)', () => {
  const text = read('docs/HANDOVER-CONTRACTS.md');
  assert.ok(
    text.includes('## Handover 7'),
    'docs/HANDOVER-CONTRACTS.md should document Handover 7 (.session-state.local.json) ' +
    'consumed by /ultracontinue',
  );
  assert.ok(
    text.includes('.session-state.local.json'),
    'Handover 7 section should name the artifact path',
  );
});

test('review-validator has CLI shim', () => {
  const text = read('lib/validators/review-validator.mjs');
  assert.ok(
    text.includes('import.meta.url === '),
    'lib/validators/review-validator.mjs should expose the standard CLI shim ' +
    '(if (import.meta.url === `file://${process.argv[1]}`)) so commands can call it from Bash',
  );
});

test('session-state-validator has CLI shim', () => {
  const text = read('lib/validators/session-state-validator.mjs');
  assert.ok(
    text.includes('import.meta.url === '),
    'lib/validators/session-state-validator.mjs should expose the standard CLI shim ' +
    '(if (import.meta.url === `file://${process.argv[1]}`)) so /ultracontinue can call it from Bash',
  );
});

test('next-session-prompt-validator has CLI shim', () => {
  const text = read('lib/validators/next-session-prompt-validator.mjs');
  assert.ok(
    text.includes('import.meta.url === '),
    'lib/validators/next-session-prompt-validator.mjs should expose the standard CLI shim ' +
    '(if (import.meta.url === `file://${process.argv[1]}`)) so /ultracontinue Phase 1.5 can call it from Bash',
  );
});

test('HANDOVER-CONTRACTS.md Handover 7 documents § Lifecycle subsection', () => {
  const text = read('docs/HANDOVER-CONTRACTS.md');
  const h7Start = text.indexOf('## Handover 7');
  assert.ok(h7Start >= 0, 'Handover 7 heading missing');
  const h7End = text.indexOf('## Stability summary', h7Start);
  assert.ok(h7End > h7Start, 'Stability summary heading missing — could not bound Handover 7');
  const h7 = text.slice(h7Start, h7End);
  assert.ok(
    h7.includes('Lifecycle'),
    'Handover 7 section should include a § Lifecycle subsection (SC-5 stale-file principle)',
  );
});

test('HANDOVER-CONTRACTS.md Handover 7 § Lifecycle names --cleanup and produced_by contract', () => {
  const text = read('docs/HANDOVER-CONTRACTS.md');
  const h7Start = text.indexOf('## Handover 7');
  const h7End = text.indexOf('## Stability summary', h7Start);
  const h7 = text.slice(h7Start, h7End);
  assert.ok(
    h7.includes('--cleanup'),
    'Handover 7 § Lifecycle should mention --cleanup as the operator-invoked stale-file remover',
  );
  assert.ok(
    h7.includes('produced_by'),
    'Handover 7 § Lifecycle should document the produced_by frontmatter contract for NEXT-SESSION-PROMPT.local.md',
  );
});

test('CLAUDE.md mentions /ultracontinue-local command', () => {
  const md = read('CLAUDE.md');
  assert.ok(
    md.includes('/ultracontinue-local') || md.includes('ultracontinue-local'),
    'CLAUDE.md should document /ultracontinue-local in the Commands table ' +
    '(added in v3.3.0 alongside the new command file)',
  );
});

test('rule-catalogue has exactly 12 entries', async () => {
  const mod = await import('../../lib/review/rule-catalogue.mjs');
  assert.strictEqual(
    mod.RULE_CATALOGUE.length,
    12,
    'lib/review/rule-catalogue.mjs RULE_CATALOGUE size invariant: must be 12 (v1.0 baseline)',
  );
});

test('headless-launch-template.md mirrors Phase 2.6 hardenings', () => {
  const tpl = read('templates/headless-launch-template.md');
  for (const needle of [
    'GIT_OPTIONAL_LOCKS',
    '--max-turns',
    '--max-budget-usd',
    '--append-system-prompt-file',
    'SHARED_CONTEXT_FILE',
    'SAFETY_PREAMBLE',
    'git push origin',
    'GH #36071',
    'push-before-cleanup',
  ]) {
    assert.ok(
      tpl.includes(needle),
      `templates/headless-launch-template.md should include "${needle}" (Step 10 mirrors Phase 2.6)`,
    );
  }
});

test('Phase 9 prose mandates parallel single-message dispatch + inline dedup', () => {
  const cmd = read('commands/ultraplan-local.md');
  const orch = read('agents/planning-orchestrator.md');
  // Single-message reinforcement appears in both (command + orchestrator)
  assert.ok(
    cmd.includes('single assistant message turn'),
    'commands/ultraplan-local.md Phase 9 should reinforce single-message parallel dispatch',
  );
  assert.ok(
    orch.includes('single assistant message turn'),
    'agents/planning-orchestrator.md Phase 6 should mirror the single-message parallel-dispatch contract',
  );
  // Dedup CLI shim is wired in both
  assert.ok(
    cmd.includes('plan-review-dedup.mjs'),
    'commands/ultraplan-local.md Phase 9 should call lib/review/plan-review-dedup.mjs after both reviewers complete',
  );
  assert.ok(
    orch.includes('plan-review-dedup.mjs'),
    'agents/planning-orchestrator.md Phase 6 should reference the dedup helper',
  );
});

test('commands/ultraplan-local.md Phase 8 seals Opus-4.7 schema-drift defense', () => {
  const cmd = read('commands/ultraplan-local.md');
  // Locate Phase 8 section
  const phase8Start = cmd.indexOf('## Phase 8');
  assert.ok(phase8Start >= 0, 'Phase 8 heading missing');
  const phase8End = cmd.indexOf('## Phase 9', phase8Start);
  assert.ok(phase8End > phase8Start, 'Phase 9 heading missing — could not bound Phase 8');
  const phase8 = cmd.slice(phase8Start, phase8End);
  // Required regex source-of-truth references
  assert.ok(
    phase8.includes('STEP_HEADING_REGEX'),
    'Phase 8 should inline STEP_HEADING_REGEX so format contract survives without orchestrator-doc loading',
  );
  assert.ok(
    phase8.includes('FORBIDDEN_HEADING_REGEX'),
    'Phase 8 should inline FORBIDDEN_HEADING_REGEX (Step 7 — schema-drift seal)',
  );
  // Required validator self-check
  assert.ok(
    phase8.includes('plan-validator.mjs --strict'),
    'Phase 8 should mandate post-write `plan-validator.mjs --strict` self-check',
  );
  // Forbidden-headings list (literal "FORBIDDEN" appears more than once: in regex const + in human-readable list)
  assert.ok(
    /FORBIDDEN/.test(phase8),
    'Phase 8 should explicitly enumerate FORBIDDEN headings',
  );
});
