import { injectTrackerHead } from './inject-tracker-head.util';
// Pins the public-share trackerHead injection invariant (ShareSeoController).
// The admin snippet is trusted content and MUST land byte-for-byte before the
// first . The critical regression these tests guard is the function vs
// string replacer: a string replacement interprets `$&`/`$$`/`` $` ``/`$'`
// inside the snippet as substitution patterns and mangles the tracker. The
// byte-for-byte test below FAILS on the old string-replacer implementation and
// passes only with the function replacer.
const HTML = '
tb';
describe('injectTrackerHead', () => {
it('inserts the snippet immediately before the first ', () => {
const out = injectTrackerHead(HTML, '');
expect(out).toBe(
't\nb',
);
});
it('inserts a snippet containing $& byte-for-byte (function replacer)', () => {
const snippet = '';
const out = injectTrackerHead(HTML, snippet);
expect(out).toContain(`${snippet}\n`);
// The literal "$&" survives; a string replacer would have spliced in the
// matched "" here.
expect(out).toContain('$&');
expect(out).not.toContain('"');
});
it('inserts a snippet containing $$, $` and $\' byte-for-byte', () => {
// All four special replacement patterns in one snippet.
const snippet = "";
const out = injectTrackerHead(HTML, snippet);
expect(out).toContain(`${snippet}\n`);
});
it('returns html unchanged for an empty trackerHead', () => {
expect(injectTrackerHead(HTML, '')).toBe(HTML);
});
it('returns html unchanged for a whitespace-only trackerHead', () => {
expect(injectTrackerHead(HTML, ' \n\t ')).toBe(HTML);
});
it('returns html unchanged for an undefined trackerHead', () => {
expect(injectTrackerHead(HTML, undefined)).toBe(HTML);
});
it('returns html unchanged when there is no marker', () => {
const noHead = 'no head here';
expect(injectTrackerHead(noHead, '')).toBe(noHead);
});
it('injects before only the FIRST when several exist', () => {
const twoHeads = '';
const out = injectTrackerHead(twoHeads, 'X');
expect(out).toBe('X\n');
});
});