import {extractPathAtPosition} from './outputExtractor';
function posOf(content: string, needle: string) {
const idx = content.indexOf(needle);
if (idx < 0) throw new Error(`needle not found: ${needle}`);
const pre = content.slice(0, idx);
const lines = pre.split(/\n/);
const line = lines.length; // 1-based
const lastNl = pre.lastIndexOf('\n');
const col = idx - (lastNl >= 0 ? lastNl + 1 : 0) + 1; // 1-based
return {line, col};
}
describe('extractPathAtPosition - JSON', () => {
it('returns path for nested object value', () => {
const json = `{
"user": {
"profile": {
"name": "mehrdad"
}
}
}`;
const path = extractPathAtPosition(json, 'json', 4, 16);
expect(path).toEqual(['user', 'profile', 'name']);
});
it('returns path including array index', () => {
const json = `{
"items": [
{ "name": "a" },
{ "name": "b" }
]
}`;
const pos = posOf(json, '"b"');
const path = extractPathAtPosition(json, 'json', pos.line, pos.col);
expect(path).toEqual(['items', 1, 'name']);
});
it('returns path when clicking on a key name', () => {
const json = `{ "user": { "name": "mehrdad" } }`;
const pos = posOf(json, '"name"');
const path = extractPathAtPosition(json, 'json', pos.line, pos.col);
expect(path).toEqual(['user', 'name']);
});
it('returns path when clicking on a key with empty string value', () => {
const json = `{ "data": { "value": "" } }`;
const pos = posOf(json, '"value"');
const path = extractPathAtPosition(json, 'json', pos.line, pos.col);
expect(path).toEqual(['data', 'value']);
});
it('returns path when clicking on a key with null value', () => {
const json = `{ "item": null }`;
const pos = posOf(json, '"item"');
const path = extractPathAtPosition(json, 'json', pos.line, pos.col);
expect(path).toEqual(['item']);
});
});
describe('extractPathAtPosition - JSON with \n', () => {
it('returns path for nested object value', () => {
const json = `{\n"user": {\n"profile": {\n"name": "mehrdad"\n}\n}\n}`;
const path = extractPathAtPosition(json, 'json', 4, 16);
expect(path).toEqual(['user', 'profile', 'name']);
});
});
describe('extractPathAtPosition - XML', () => {
it('returns path for nested element text', () => {
const xml =
`\n \n \n mehrdad\n \n \n`;
const pos = posOf(xml, 'mehrdad');
const path = extractPathAtPosition(xml, 'xml', pos.line, pos.col);
expect(path).toEqual(['root', 'user', 'profile', 'name']);
});
it('returns path including index for repeated elements', () => {
const xml =
`\n \n - a
\n - b
\n \n`;
const pos = posOf(xml, 'b');
// Adjust to inside 'b'
const adjusted = {line: pos.line, col: pos.col + ''.length};
const path = extractPathAtPosition(xml, 'xml', adjusted.line, adjusted.col);
expect(path).toEqual(['root', 'items', 'item', 1, 'name']);
});
it('returns path when clicking on opening tag name', () => {
const xml = `\n \n mehrdad\n \n`;
// Click on the 'n' in ''
const pos = posOf(xml, 'name>mehrdad');
const path = extractPathAtPosition(xml, 'xml', pos.line, pos.col);
expect(path).toEqual(['root', 'user', 'name']);
});
it('returns path when clicking on closing tag name', () => {
const xml = `\n \n mehrdad\n \n`;
// Click on the 'n' in ''
const pos = posOf(xml, 'name>\n ');
const path = extractPathAtPosition(xml, 'xml', pos.line, pos.col);
expect(path).toEqual(['root', 'user', 'name']);
});
it('returns path when clicking on tag name of empty element', () => {
const xml = `\n \n`;
// Click on 'd' in ''
const pos = posOf(xml, 'data>');
const path = extractPathAtPosition(xml, 'xml', pos.line, pos.col);
expect(path).toEqual(['root', 'data']);
});
});
describe('extractPathAtPosition - XML prolog, indices and attributes', () => {
const xml = [
'',
'',
'',
' 1',
' ',
' api',
' testing',
' automation',
' ',
' ',
' ',
' ',
' ',
'',
].join('\n');
it('ignores the XML declaration and comments (no empty leading segment)', () => {
const pos = posOf(xml, '1');
const path = extractPathAtPosition(
xml, 'xml', pos.line, pos.col + ''.length);
expect(path).toEqual(['root', 'id']);
});
it('indexes every repeated element, including the first', () => {
const first = posOf(xml, 'api<');
expect(extractPathAtPosition(xml, 'xml', first.line, first.col))
.toEqual(['root', 'tags', 'tag', 0]);
const last = posOf(xml, 'automation');
expect(extractPathAtPosition(xml, 'xml', last.line, last.col))
.toEqual(['root', 'tags', 'tag', 2]);
});
it('returns attribute paths for attribute names and values', () => {
const attrName = posOf(xml, 'count="42"');
expect(extractPathAtPosition(xml, 'xml', attrName.line, attrName.col))
.toEqual(['root', 'nested', 'count']);
const attrValue = posOf(xml, 'beta"');
expect(extractPathAtPosition(xml, 'xml', attrValue.line, attrValue.col))
.toEqual(['root', 'nested', 'item', 1, 'key']);
});
it('keeps self-closing elements out of the parent path', () => {
const pos = posOf(xml, 'value="2"');
expect(extractPathAtPosition(xml, 'xml', pos.line, pos.col))
.toEqual(['root', 'nested', 'item', 1, 'value']);
});
});
describe('extractPathAtPosition - JSON literals and bounds', () => {
it('tracks numbers booleans escaped strings and whitespace', () => {
const json = `{ "n": 12.5e1, "ok": true, "no": false, "s": "a\\"b", "arr": [null] }`;
expect(extractPathAtPosition(json, 'json', 1, json.indexOf('12') + 1)).toEqual(['n']);
expect(extractPathAtPosition(json, 'json', 1, json.indexOf('true') + 1)).toEqual(['ok']);
expect(extractPathAtPosition(json, 'json', 1, json.indexOf('false') + 1)).toEqual(['no']);
expect(extractPathAtPosition(json, 'json', 1, json.indexOf('null') + 1)).toEqual(['arr', 0]);
});
it('clamps out-of-range positions and rejects unknown types', () => {
const json = `{ "a": 1 }`;
expect(extractPathAtPosition(json, 'json', 99, 99)).toBeNull();
expect(extractPathAtPosition(json, 'text' as any, 1, 1)).toBeNull();
});
});