import assert from 'assert'; import { ensureScriptsBeforeBodyClose } from '../src/service/transformPage'; // --------------------------------------------------------------------------- // ensureScriptsBeforeBodyClose // --------------------------------------------------------------------------- describe('ensureScriptsBeforeBodyClose', () => { it('moves page-helpers and page-script to end of body in correct order', () => { const html = '' + '' + '
content
' + '' + ''; const result = ensureScriptsBeforeBodyClose(html); // Both scripts should be at the end of body const bodyContent = result.match(/([\s\S]*)<\/body>/)?.[1] ?? ''; const helpersIdx = bodyContent.lastIndexOf('id="page-helpers"'); const scriptIdx = bodyContent.lastIndexOf('id="page-script"'); const contentIdx = bodyContent.indexOf('
content
'); // Content should come before both scripts assert.ok(contentIdx < helpersIdx, 'content should be before page-helpers'); // Helpers should come before page-script assert.ok(helpersIdx < scriptIdx, 'page-helpers should be before page-script'); }); it('handles missing page-helpers gracefully', () => { const html = '' + '
content
' + '' + ''; const result = ensureScriptsBeforeBodyClose(html); assert.ok(result.includes('id="page-script"')); assert.ok(result.includes('
content
')); }); it('handles missing page-script gracefully', () => { const html = '' + '
content
' + '' + ''; const result = ensureScriptsBeforeBodyClose(html); assert.ok(result.includes('id="page-helpers"')); assert.ok(result.includes('
content
')); }); it('handles both scripts missing gracefully', () => { const html = '
content
'; const result = ensureScriptsBeforeBodyClose(html); assert.ok(result.includes('
content
')); }); it('returns HTML unchanged when no body tag', () => { const html = '
no body tag
'; const result = ensureScriptsBeforeBodyClose(html); // cheerio wraps in html/body, so just check the content is preserved assert.ok(result.includes('no body tag')); }); it('does nothing when scripts are already at end of body', () => { const html = '' + '
content
' + '' + '' + ''; const result = ensureScriptsBeforeBodyClose(html); const bodyContent = result.match(/([\s\S]*)<\/body>/)?.[1] ?? ''; const helpersIdx = bodyContent.lastIndexOf('id="page-helpers"'); const scriptIdx = bodyContent.lastIndexOf('id="page-script"'); assert.ok(helpersIdx < scriptIdx); }); });