import { describe, it, expect } from 'vitest' import { parseJsonPath, pathToJsonPath, pathMatchesJsonPath, getDataByPath, } from '../../src/utils/jsonFormatter/jsonPath.js' describe('jsonPath', () => { describe('parseJsonPath', () => { it('returns null for empty path', () => { expect(parseJsonPath('')).toBeNull() expect(parseJsonPath(' ')).toBeNull() }) it('returns empty array for root $', () => { expect(parseJsonPath('$')).toEqual([]) expect(parseJsonPath('$.')).toEqual([]) }) it('parses object keys', () => { expect(parseJsonPath('$.name')).toEqual([{ type: 'key', key: 'name' }]) expect(parseJsonPath('$.user.name')).toEqual([ { type: 'key', key: 'user' }, { type: 'key', key: 'name' }, ]) }) it('parses array index', () => { expect(parseJsonPath('$.items[0]')).toEqual([ { type: 'key', key: 'items' }, { type: 'index', index: 0 }, ]) }) it('parses wildcard index', () => { expect(parseJsonPath('$.items[*]')).toEqual([ { type: 'key', key: 'items' }, { type: 'wildcard', index: '*' }, ]) }) }) describe('pathToJsonPath', () => { it('converts internal path to JSONPath', () => { expect(pathToJsonPath('root')).toBe('$') expect(pathToJsonPath('root.user.name')).toBe('$.user.name') }) }) describe('pathMatchesJsonPath', () => { it('matches exact key path', () => { const segments = parseJsonPath('$.user.name') expect(pathMatchesJsonPath('root.user.name', segments)).toBe(true) expect(pathMatchesJsonPath('root.user.age', segments)).toBe(false) }) it('requires exact segment count', () => { const segments = parseJsonPath('$.user') expect(pathMatchesJsonPath('root.user.name', segments)).toBe(false) }) it('matches wildcard index', () => { const segments = parseJsonPath('$.items[*]') expect(pathMatchesJsonPath('root.items[2]', segments)).toBe(true) expect(pathMatchesJsonPath('root.items.name', segments)).toBe(false) }) it('returns true for empty segments (match all)', () => { expect(pathMatchesJsonPath('root.any.path', [])).toBe(true) }) }) describe('getDataByPath', () => { const data = { user: { name: 'Alice', tags: ['a', 'b'] }, count: 3 } it('gets nested object value', () => { expect(getDataByPath(data, 'root.user.name')).toBe('Alice') }) it('gets array element', () => { expect(getDataByPath(data, 'root.user.tags[1]')).toBe('b') }) it('returns undefined for missing path', () => { expect(getDataByPath(data, 'root.missing')).toBeUndefined() }) }) })