import assert from 'node:assert/strict'; import test from 'node:test'; import Slugger from 'github-slugger'; import { headingText, isSelfNumbered, rehypeHeadingMarks } from '../src/lib/rehype-heading-marks.mjs'; const SHEET = { history: ['/repo/src/content/sheets/active-directory/attack-3.md'] }; const MIRROR = { history: ['/repo/src/content/payloads/xss/README.md'] }; const h = (tagName, ...children) => ({ type: 'element', tagName, properties: {}, children: children.map((c) => (typeof c === 'string' ? { type: 'text', value: c } : c)), }); const doc = (...children) => ({ type: 'root', children }); const run = (tree, file = SHEET) => { rehypeHeadingMarks()(tree, file); return tree.children; }; test('the emoji leaves the heading but the anchor is the one Astro assigned', () => { const titles = ['📖 How It Works', '⚙️ Prerequisites', '🛠️ Tools']; // What Astro's own pass produces today, from the untouched text. const astro = new Slugger(); const expected = titles.map((t) => astro.slug(t)); assert.deepEqual(expected, ['-how-it-works', '️-prerequisites', '️-tools'], 'the variation selector is part of the id'); const out = run(doc(...titles.map((t) => h('h2', t)))); assert.deepEqual(out.map((n) => n.properties.id), expected); assert.deepEqual(out.map((n) => headingText(n)), ['How It Works', 'Prerequisites', 'Tools']); }); test('repeated headings keep their -1, -2 suffixes', () => { const out = run(doc(h('h3', '🔴 Rubeus'), h('h3', '🔴 Rubeus'), h('h3', 'Rubeus'))); assert.deepEqual(out.map((n) => n.properties.id), ['-rubeus', '-rubeus-1', 'rubeus']); }); test('a coloured dot becomes a mark in the matching hue; other emoji are just removed', () => { const [enumerate, exploit, plain, none] = run(doc( h('h3', '🔵 Step 0 — Enumerate'), h('h3', '🔴 Rubeus — Windows'), h('h3', '🧩 Troubleshooting'), h('h3', 'No emoji here'), )); assert.equal(enumerate.properties.dataMark, 'foam'); assert.equal(exploit.properties.dataMark, 'love'); assert.equal(plain.properties.dataMark, undefined); assert.equal(headingText(plain), 'Troubleshooting'); assert.equal(headingText(none), 'No emoji here'); }); test('sections are numbered in order, across whatever sits between them', () => { const out = run(doc(h('h2', '📖 How It Works'), h('h3', 'Detail'), h('h2', 'Tools'), h('h2', 'Detection'))); assert.deepEqual(out.map((n) => n.properties.dataN), ['01', undefined, '02', '03']); }); test('a sheet that numbers its own sections is not numbered again', () => { for (const titles of [['1. Command Anatomy', '2. Attack Modes'], ['Phase 1 — Flag Hunting', 'Notes'], ['2 + 3. Open and dump']]) { assert.equal(isSelfNumbered(titles), true, titles[0]); const out = run(doc(...titles.map((t) => h('h2', t)))); assert.deepEqual(out.map((n) => n.properties.dataN), titles.map(() => undefined)); } assert.equal(isSelfNumbered(['How It Works', 'Tools', 'Phased rollout', 'Step by step']), false); }); test('headings that do not open with text, or are nothing but emoji, are left whole', () => { const code = h('h3', { type: 'element', tagName: 'code', properties: {}, children: [{ type: 'text', value: '🔴 literal' }] }, ' flag'); const [first, second] = run(doc(code, h('h3', '🔴'))); assert.equal(headingText(first), '🔴 literal flag'); assert.equal(headingText(second), '🔴'); assert.equal(second.properties.dataMark, undefined); }); test('an id that is already set is kept, and mirrors are not touched at all', () => { const preset = h('h2', '📖 Overview'); preset.properties.id = 'custom'; assert.equal(run(doc(preset))[0].properties.id, 'custom'); const [mirror] = run(doc(h('h2', '📖 Summary')), MIRROR); assert.equal(headingText(mirror), '📖 Summary'); assert.deepEqual(mirror.properties, {}); });