import assert from 'assert';
import * as fs from 'fs/promises';
import * as os from 'os';
import path from 'path';
import {
assignNodeIds,
stripNodeIds,
applyChangeList,
parseChangeList,
deduplicateInlineScripts,
normalizedIndexOf,
transformPage,
ChangeList,
TransformPageArgs,
} from '../src/service/transformPage';
import { Builder, BuilderResult, ContextSection } from '../src/builders/types';
// ---------------------------------------------------------------------------
// assignNodeIds
// ---------------------------------------------------------------------------
describe('assignNodeIds', () => {
it('assigns sequential data-nid to every element', () => {
const html = '
';
const { html: result, nodeCount } = assignNodeIds(html);
assert.ok(result.includes('data-nid="0"'));
assert.ok(nodeCount > 0);
// Every tag should have an id — count occurrences
const ids = result.match(/data-nid="/g);
assert.strictEqual(ids?.length, nodeCount);
});
it('returns nodeCount matching the number of tags', () => {
const html = 'AB
';
const { nodeCount } = assignNodeIds(html);
// cheerio wraps in ... so count includes those
assert.ok(nodeCount >= 4); // html, head, body, div, span, span = 6
});
it('assigns data-nid to script and style elements', () => {
const html = '';
const { html: result, nodeCount } = assignNodeIds(html);
// All 6 elements should have ids: html, head, style, script(inline), body, script(src)
const ids = result.match(/data-nid="/g);
assert.strictEqual(ids?.length, nodeCount);
assert.strictEqual(nodeCount, 6);
// Verify the style and script tags specifically got ids
assert.ok(result.match(/