'use strict';
const { test } = require('node:test');
const assert = require('node:assert');
const { analyzeSkillConcat } = require('../../commands/overlay');

// analyzeSkillConcat powers the skill-overlay dry-run in `overlay validate` and
// the orphaned-marker warning in `overlay drift`. Skills use runtime concat, so
// the markers are NOT parsed — this helper only DESCRIBES what the agent will
// see and flags markers whose heading no longer anchors to a base section.

const BASE = `---
name: bug
---

## Phase 1
reproduce

## Phase 2
fix
`;

test('marker matching a base heading is not an orphan', () => {
  const overlay = `---
base_skill: bug
---

## [APPEND] Phase 1
project entry points
`;
  const a = analyzeSkillConcat(BASE, overlay);
  assert.strictEqual(a.markerSections.length, 1);
  assert.strictEqual(a.markerSections[0].orphan, false);
  assert.strictEqual(a.orphans.length, 0);
});

test('marker with no matching base heading is flagged orphan', () => {
  const overlay = `---
base_skill: bug
---

## [OVERRIDE] Phase 9
heading absent from base
`;
  const a = analyzeSkillConcat(BASE, overlay);
  assert.strictEqual(a.orphans.length, 1);
  assert.strictEqual(a.orphans[0].heading, 'Phase 9');
  assert.strictEqual(a.orphans[0].marker, 'OVERRIDE');
});

test('unmarked overlay sections count as new (plain), not markers', () => {
  const overlay = `---
base_skill: bug
---

## Brand vocabulary
project terms

## [APPEND] Phase 2
extra
`;
  const a = analyzeSkillConcat(BASE, overlay);
  assert.strictEqual(a.plainSections, 1);
  assert.strictEqual(a.markerSections.length, 1);
  assert.strictEqual(a.baseSectionCount, 2);
});

test('reports line counts for both base and overlay', () => {
  const overlay = `---
base_skill: bug
---

## [APPEND] Phase 1
x
`;
  const a = analyzeSkillConcat(BASE, overlay);
  assert.ok(a.baseLines > 0);
  assert.ok(a.overlayLines > 0);
});

test('empty overlay body yields no sections and no orphans', () => {
  const overlay = `---
base_skill: bug
---
`;
  const a = analyzeSkillConcat(BASE, overlay);
  assert.strictEqual(a.markerSections.length, 0);
  assert.strictEqual(a.plainSections, 0);
  assert.strictEqual(a.orphans.length, 0);
});
