|
1 | 1 | import { Provider } from 'jotai'; |
2 | 2 | import type { ReactElement } from 'react'; |
3 | | -import { screen, waitFor, within } from '@testing-library/react'; |
| 3 | +import type { BuilderChartConfigWithDateRange } from '@hyperdx/common-utils/dist/types'; |
| 4 | +import { renderHook, screen, waitFor, within } from '@testing-library/react'; |
4 | 5 | import userEvent from '@testing-library/user-event'; |
5 | 6 |
|
6 | 7 | import { |
7 | 8 | appendSelectWithAdditionalKeys, |
| 9 | + getPersistedBlockColumns, |
8 | 10 | RawLogTable, |
| 11 | + useConfigWithAdditionalSelect, |
9 | 12 | } from '@/components/DBRowTable'; |
10 | 13 | import * as useChartConfigModule from '@/hooks/useChartConfig'; |
11 | 14 | import { RowWhereResult } from '@/hooks/useRowWhere'; |
12 | 15 | import { RowClickAction, UserPreferences } from '@/useUserPreferences'; |
13 | 16 |
|
14 | 17 | const mockSetQueryState = jest.fn(); |
15 | 18 |
|
| 19 | +// useConfigWithAdditionalSelect reads table metadata through react-query |
| 20 | +// hooks that return a large UseQueryResult; the hook only reads `data`. |
| 21 | +// Reading the handles back through `requireMock` keeps them typed as mocks |
| 22 | +// without asserting a complete UseQueryResult. |
| 23 | +jest.mock('@/hooks/useMetadata', () => ({ |
| 24 | + ...jest.requireActual('@/hooks/useMetadata'), |
| 25 | + useTableMetadata: jest.fn(), |
| 26 | + useColumns: jest.fn(), |
| 27 | +})); |
| 28 | + |
| 29 | +const { useTableMetadata: mockUseTableMetadata, useColumns: mockUseColumns } = |
| 30 | + jest.requireMock<{ |
| 31 | + useTableMetadata: jest.Mock; |
| 32 | + useColumns: jest.Mock; |
| 33 | + }>('@/hooks/useMetadata'); |
| 34 | + |
16 | 35 | // The inline expanded row reads the row query params through nuqs, which needs |
17 | 36 | // a mounted Next router. Routing every setter through one mock lets tests assert |
18 | 37 | // which param the expanded row's side panel button writes. |
@@ -774,3 +793,160 @@ describe('appendSelectWithAdditionalKeys', () => { |
774 | 793 | }); |
775 | 794 | }); |
776 | 795 | }); |
| 796 | + |
| 797 | +describe('getPersistedBlockColumns', () => { |
| 798 | + const engine = (settings: string) => |
| 799 | + `MergeTree PARTITION BY toDate(Timestamp) ORDER BY (ServiceName, Timestamp) SETTINGS index_granularity = 8192${settings}`; |
| 800 | + |
| 801 | + it('returns both block columns when both are persisted', () => { |
| 802 | + expect( |
| 803 | + getPersistedBlockColumns( |
| 804 | + engine( |
| 805 | + ', enable_block_number_column = 1, enable_block_offset_column = 1', |
| 806 | + ), |
| 807 | + ), |
| 808 | + ).toEqual(['_block_number', '_block_offset']); |
| 809 | + }); |
| 810 | + |
| 811 | + it('returns only _block_offset when only enable_block_offset_column is set', () => { |
| 812 | + expect( |
| 813 | + getPersistedBlockColumns(engine(', enable_block_offset_column = 1')), |
| 814 | + ).toEqual(['_block_offset']); |
| 815 | + }); |
| 816 | + |
| 817 | + it('returns only _block_number when only enable_block_number_column is set', () => { |
| 818 | + expect( |
| 819 | + getPersistedBlockColumns(engine(', enable_block_number_column = 1')), |
| 820 | + ).toEqual(['_block_number']); |
| 821 | + }); |
| 822 | + |
| 823 | + it('returns nothing when neither setting is present', () => { |
| 824 | + expect(getPersistedBlockColumns(engine(''))).toEqual([]); |
| 825 | + expect(getPersistedBlockColumns('')).toEqual([]); |
| 826 | + }); |
| 827 | +}); |
| 828 | + |
| 829 | +describe('useConfigWithAdditionalSelect', () => { |
| 830 | + const config: BuilderChartConfigWithDateRange = { |
| 831 | + connection: 'conn', |
| 832 | + from: { databaseName: 'default', tableName: 'otel_logs' }, |
| 833 | + select: 'Timestamp, ServiceName, Body', |
| 834 | + where: '', |
| 835 | + whereLanguage: 'sql', |
| 836 | + timestampValueExpression: 'Timestamp', |
| 837 | + dateRange: [new Date(0), new Date(1)], |
| 838 | + }; |
| 839 | + |
| 840 | + const mockMetadata = ({ |
| 841 | + engineFull, |
| 842 | + columns = [], |
| 843 | + }: { |
| 844 | + engineFull: string; |
| 845 | + columns?: { name: string; type: string }[]; |
| 846 | + }) => { |
| 847 | + mockUseTableMetadata.mockReturnValue({ |
| 848 | + data: { |
| 849 | + primary_key: 'toStartOfFiveMinutes(Timestamp), ServiceName, Timestamp', |
| 850 | + partition_key: 'toDate(Timestamp)', |
| 851 | + engine_full: engineFull, |
| 852 | + }, |
| 853 | + }); |
| 854 | + mockUseColumns.mockReturnValue({ data: columns }); |
| 855 | + }; |
| 856 | + |
| 857 | + afterEach(() => { |
| 858 | + mockUseTableMetadata.mockReset(); |
| 859 | + mockUseColumns.mockReset(); |
| 860 | + }); |
| 861 | + |
| 862 | + it('restricts the row lookup to PK + _block_offset when only the offset column is persisted', () => { |
| 863 | + mockMetadata({ |
| 864 | + engineFull: |
| 865 | + 'MergeTree PARTITION BY toDate(Timestamp) ORDER BY (toStartOfFiveMinutes(Timestamp), ServiceName, Timestamp) SETTINGS index_granularity = 8192, enable_block_offset_column = 1', |
| 866 | + }); |
| 867 | + |
| 868 | + const { result } = renderHook(() => |
| 869 | + useConfigWithAdditionalSelect(config, 'source-id'), |
| 870 | + ); |
| 871 | + |
| 872 | + expect(result.current?.select).toBe( |
| 873 | + 'Timestamp,ServiceName,Body,toDate(Timestamp),toStartOfFiveMinutes(Timestamp),_block_offset', |
| 874 | + ); |
| 875 | + expect(result.current?.additionalKeysLength).toBe(3); |
| 876 | + expect(result.current?.rowKeyColumns).toEqual( |
| 877 | + new Set([ |
| 878 | + 'toDate(Timestamp)', |
| 879 | + 'toStartOfFiveMinutes(Timestamp)', |
| 880 | + 'ServiceName', |
| 881 | + 'Timestamp', |
| 882 | + '_block_offset', |
| 883 | + ]), |
| 884 | + ); |
| 885 | + expect(result.current?.rowKeyColumns?.has('Body')).toBe(false); |
| 886 | + }); |
| 887 | + |
| 888 | + it('restricts the row lookup to PK + _block_number when only the number column is persisted', () => { |
| 889 | + mockMetadata({ |
| 890 | + engineFull: |
| 891 | + 'MergeTree ORDER BY Timestamp SETTINGS enable_block_number_column = 1', |
| 892 | + }); |
| 893 | + |
| 894 | + const { result } = renderHook(() => |
| 895 | + useConfigWithAdditionalSelect(config, 'source-id'), |
| 896 | + ); |
| 897 | + |
| 898 | + expect(result.current?.select).toBe( |
| 899 | + 'Timestamp,ServiceName,Body,toDate(Timestamp),toStartOfFiveMinutes(Timestamp),_block_number', |
| 900 | + ); |
| 901 | + expect(result.current?.rowKeyColumns?.has('_block_number')).toBe(true); |
| 902 | + expect(result.current?.rowKeyColumns?.has('Body')).toBe(false); |
| 903 | + }); |
| 904 | + |
| 905 | + it('uses both block columns when both are persisted', () => { |
| 906 | + mockMetadata({ |
| 907 | + engineFull: |
| 908 | + 'MergeTree ORDER BY Timestamp SETTINGS enable_block_number_column = 1, enable_block_offset_column = 1', |
| 909 | + }); |
| 910 | + |
| 911 | + const { result } = renderHook(() => |
| 912 | + useConfigWithAdditionalSelect(config, 'source-id'), |
| 913 | + ); |
| 914 | + |
| 915 | + expect(result.current?.select).toBe( |
| 916 | + 'Timestamp,ServiceName,Body,toDate(Timestamp),toStartOfFiveMinutes(Timestamp),_block_number,_block_offset', |
| 917 | + ); |
| 918 | + expect(result.current?.rowKeyColumns?.has('_block_number')).toBe(true); |
| 919 | + expect(result.current?.rowKeyColumns?.has('_block_offset')).toBe(true); |
| 920 | + }); |
| 921 | + |
| 922 | + it('falls back to __hdx_id and the full row when no block column is persisted', () => { |
| 923 | + mockMetadata({ |
| 924 | + engineFull: |
| 925 | + 'MergeTree ORDER BY Timestamp SETTINGS index_granularity = 8192', |
| 926 | + columns: [{ name: '__hdx_id', type: 'String' }], |
| 927 | + }); |
| 928 | + |
| 929 | + const { result } = renderHook(() => |
| 930 | + useConfigWithAdditionalSelect(config, 'source-id'), |
| 931 | + ); |
| 932 | + |
| 933 | + expect(result.current?.select).toBe( |
| 934 | + 'Timestamp,ServiceName,Body,toDate(Timestamp),toStartOfFiveMinutes(Timestamp),__hdx_id', |
| 935 | + ); |
| 936 | + expect(result.current?.rowKeyColumns).toBeUndefined(); |
| 937 | + }); |
| 938 | + |
| 939 | + it('ignores block columns for aggregate queries without a sourceId', () => { |
| 940 | + mockMetadata({ |
| 941 | + engineFull: |
| 942 | + 'MergeTree ORDER BY Timestamp SETTINGS enable_block_offset_column = 1', |
| 943 | + }); |
| 944 | + |
| 945 | + const { result } = renderHook(() => useConfigWithAdditionalSelect(config)); |
| 946 | + |
| 947 | + expect(result.current?.select).toBe( |
| 948 | + 'Timestamp,ServiceName,Body,toDate(Timestamp),toStartOfFiveMinutes(Timestamp)', |
| 949 | + ); |
| 950 | + expect(result.current?.rowKeyColumns).toBeUndefined(); |
| 951 | + }); |
| 952 | +}); |
0 commit comments