From e0a8d59fef67f82259f85c8a95c8a0efa557b1e3 Mon Sep 17 00:00:00 2001 From: Karl Power Date: Fri, 20 Feb 2026 15:57:15 +0100 Subject: [PATCH] tests for buildJSONExtractQuery --- .../src/components/DBRowJsonViewer.test.tsx | 55 ++++++++++++++++++- .../app/src/components/DBRowJsonViewer.tsx | 2 +- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/packages/app/src/components/DBRowJsonViewer.test.tsx b/packages/app/src/components/DBRowJsonViewer.test.tsx index 22fe2af8..3598e3ae 100644 --- a/packages/app/src/components/DBRowJsonViewer.test.tsx +++ b/packages/app/src/components/DBRowJsonViewer.test.tsx @@ -1,7 +1,7 @@ import React from 'react'; import { fireEvent, screen, within } from '@testing-library/react'; -import { DBRowJsonViewer } from './DBRowJsonViewer'; +import { buildJSONExtractQuery, DBRowJsonViewer } from './DBRowJsonViewer'; import { RowSidePanelContext } from './DBRowSidePanel'; // Mock Next.js router @@ -195,4 +195,57 @@ describe('DBRowJsonViewer', () => { ); }); }); + + describe('buildJSONExtractQuery', () => { + it('returns null when keyPath equals parsedJsonRootPath (no nested path)', () => { + expect( + buildJSONExtractQuery(['LogAttributes'], ['LogAttributes']), + ).toBeNull(); + }); + + it('returns null when keyPath is shorter than parsedJsonRootPath', () => { + expect(buildJSONExtractQuery([], ['LogAttributes'])).toBeNull(); + }); + + it('builds query for single-level path with default JSONExtractString', () => { + expect( + buildJSONExtractQuery(['LogAttributes', 'field1'], ['LogAttributes']), + ).toBe("JSONExtractString(LogAttributes, 'field1')"); + }); + + it('builds query for nested path', () => { + expect( + buildJSONExtractQuery( + ['LogAttributes', 'nested', 'field3'], + ['LogAttributes'], + ), + ).toBe("JSONExtractString(LogAttributes, 'nested', 'field3')"); + }); + + it('uses JSONExtractFloat when specified', () => { + expect( + buildJSONExtractQuery( + ['SpanAttributes', 'count'], + ['SpanAttributes'], + 'JSONExtractFloat', + ), + ).toBe("JSONExtractFloat(SpanAttributes, 'count')"); + }); + + it('uses JSONExtractBool when specified', () => { + expect( + buildJSONExtractQuery( + ['LogAttributes', 'enabled'], + ['LogAttributes'], + 'JSONExtractBool', + ), + ).toBe("JSONExtractBool(LogAttributes, 'enabled')"); + }); + + it('handles path segments that look like array indices', () => { + expect( + buildJSONExtractQuery(['LogAttributes', '0', 'id'], ['LogAttributes']), + ).toBe("JSONExtractString(LogAttributes, '0', 'id')"); + }); + }); }); diff --git a/packages/app/src/components/DBRowJsonViewer.tsx b/packages/app/src/components/DBRowJsonViewer.tsx index 62a950fb..3bdf0d96 100644 --- a/packages/app/src/components/DBRowJsonViewer.tsx +++ b/packages/app/src/components/DBRowJsonViewer.tsx @@ -35,7 +35,7 @@ type JSONExtractFn = | 'JSONExtractFloat' | 'JSONExtractBool'; -function buildJSONExtractQuery( +export function buildJSONExtractQuery( keyPath: string[], parsedJsonRootPath: string[], jsonExtractFn: JSONExtractFn = 'JSONExtractString',