#!/usr/bin/env node
/**
 * Fixture-driven test for the git-hooks active-vs-expected decision.
 *
 * Run: node src/utils/__tests__/githooks-active.test.js
 * Exits 0 on success, 1 on any mismatch (CI-friendly).
 *
 * `isActiveServing` is the one decision that, if wrong, silently invalidates
 * the entire git-hooks health check (src/utils/githooks.js). It is pure (path
 * logic only) so it is fully unit-testable without a live repo — covering the
 * husky-version branches that fs/git probing can't easily exercise.
 */
const path = require('path');
const { isActiveServing } = require('../githooks');

// Absolute roots so the fixtures don't depend on cwd. Normalize through
// path.resolve the same way the implementation does.
const ROOT = path.resolve('/repo');
const j = (...p) => path.join(ROOT, ...p);

const FIXTURES = [
  // ─── the mayo bug: core.hooksPath=.git/hooks, hooks in scripts/git-hooks ──
  { active: j('.git', 'hooks'), expected: j('scripts', 'git-hooks'), want: false, note: 'mayo: inactive' },

  // ─── husky v9: active=.husky/_ wrappers, user hooks in .husky ─────────────
  { active: j('.husky', '_'),   expected: j('.husky'),               want: true,  note: 'husky v9 nested' },
  // ─── husky v5-v8: core.hooksPath=.husky, hooks in .husky ──────────────────
  { active: j('.husky'),        expected: j('.husky'),               want: true,  note: 'husky v8 identical' },

  // ─── scripts convention, correctly wired ─────────────────────────────────
  { active: j('scripts', 'git-hooks'), expected: j('scripts', 'git-hooks'), want: true, note: 'scripts identical' },

  // ─── .githooks convention, wired to default .git/hooks (inactive) ─────────
  { active: j('.git', 'hooks'), expected: j('.githooks'),            want: false, note: '.githooks inactive' },

  // ─── reverse nesting (expected within active) also counts as serving ──────
  { active: j('.husky'),        expected: j('.husky', '_'),          want: true,  note: 'reverse nesting' },

  // ─── sibling dirs sharing a prefix must NOT match (prefix-not-boundary) ───
  { active: j('hooks'),         expected: j('hooks-extra'),          want: false, note: 'prefix not a path boundary' },

  // ─── trailing-slash / non-normalized inputs normalize equal ──────────────
  { active: j('.husky') + path.sep, expected: j('.husky'),           want: true,  note: 'trailing sep normalized' },
];

let failures = 0;
for (const f of FIXTURES) {
  const got = isActiveServing(f.active, f.expected);
  const ok = got === f.want;
  if (!ok) {
    failures++;
    console.error(`✗ ${f.note}: isActiveServing(${f.active}, ${f.expected}) = ${got}, expected ${f.want}`);
  } else {
    console.log(`✓ ${f.note}`);
  }
}

if (failures) {
  console.error(`\n${failures} failure(s).`);
  process.exit(1);
}
console.log(`\nAll ${FIXTURES.length} fixtures passed.`);
process.exit(0);
