|
| 1 | +import { |
| 2 | + BuilderChartConfigWithDateRange, |
| 3 | + DisplayType, |
| 4 | + SourceKind, |
| 5 | + TMetricSource, |
| 6 | +} from '@hyperdx/common-utils/dist/types'; |
| 7 | +import { notifications } from '@mantine/notifications'; |
| 8 | +import { act, screen, within } from '@testing-library/react'; |
| 9 | +import userEvent from '@testing-library/user-event'; |
| 10 | + |
| 11 | +import { buildEventsSearchUrl } from '@/ChartUtils'; |
| 12 | + |
| 13 | +const DISMISSED_KEY = 'drilldown-metric-correlated-log-warning'; |
| 14 | +const TOAST_ID = 'no-log-source-associated'; |
| 15 | + |
| 16 | +const dateRange: [Date, Date] = [ |
| 17 | + new Date('2026-01-01T00:00:00.000Z'), |
| 18 | + new Date('2026-01-01T01:00:00.000Z'), |
| 19 | +]; |
| 20 | + |
| 21 | +const metricSource = { |
| 22 | + id: 'metrics', |
| 23 | + name: 'Metrics', |
| 24 | + kind: SourceKind.Metric, |
| 25 | + connection: 'clickhouse', |
| 26 | + from: { databaseName: 'default', tableName: '' }, |
| 27 | + timestampValueExpression: 'TimeUnix', |
| 28 | + metricTables: { |
| 29 | + gauge: 'otel_metrics_gauge', |
| 30 | + histogram: '', |
| 31 | + sum: '', |
| 32 | + summary: '', |
| 33 | + 'exponential histogram': '', |
| 34 | + }, |
| 35 | + resourceAttributesExpression: 'ResourceAttributes', |
| 36 | +} satisfies TMetricSource; |
| 37 | + |
| 38 | +const config = { |
| 39 | + displayType: DisplayType.Line, |
| 40 | + connection: 'clickhouse', |
| 41 | + from: { databaseName: 'default', tableName: '' }, |
| 42 | + select: [ |
| 43 | + { |
| 44 | + aggFn: 'avg', |
| 45 | + aggCondition: '', |
| 46 | + aggConditionLanguage: 'lucene', |
| 47 | + valueExpression: 'Value', |
| 48 | + }, |
| 49 | + ], |
| 50 | + where: '', |
| 51 | + whereLanguage: 'lucene', |
| 52 | + filters: [], |
| 53 | + timestampValueExpression: 'TimeUnix', |
| 54 | + metricTables: metricSource.metricTables, |
| 55 | + dateRange, |
| 56 | +} satisfies BuilderChartConfigWithDateRange; |
| 57 | + |
| 58 | +const drillDown = () => |
| 59 | + act(async () => |
| 60 | + buildEventsSearchUrl({ source: metricSource, config, dateRange }), |
| 61 | + ); |
| 62 | + |
| 63 | +describe('metric drill-down warning without a correlated log source', () => { |
| 64 | + beforeEach(() => { |
| 65 | + localStorage.clear(); |
| 66 | + notifications.clean(); |
| 67 | + }); |
| 68 | + |
| 69 | + afterEach(() => { |
| 70 | + jest.restoreAllMocks(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('warns and returns null', async () => { |
| 74 | + renderWithMantine(<div />); |
| 75 | + |
| 76 | + expect(await drillDown()).toBeNull(); |
| 77 | + |
| 78 | + expect( |
| 79 | + await screen.findByText(/drill-down is unavailable/i), |
| 80 | + ).toBeInTheDocument(); |
| 81 | + }); |
| 82 | + |
| 83 | + it('persists the dismissal when the close button is clicked', async () => { |
| 84 | + const user = userEvent.setup(); |
| 85 | + renderWithMantine(<div />); |
| 86 | + await drillDown(); |
| 87 | + const toast = await screen.findByRole('alert'); |
| 88 | + expect(localStorage.getItem(DISMISSED_KEY)).toBeNull(); |
| 89 | + |
| 90 | + await user.click(within(toast).getByRole('button')); |
| 91 | + |
| 92 | + expect(localStorage.getItem(DISMISSED_KEY)).toBe('true'); |
| 93 | + const show = jest.spyOn(notifications, 'show'); |
| 94 | + expect(await drillDown()).toBeNull(); |
| 95 | + expect(show).not.toHaveBeenCalled(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('warns instead of throwing when localStorage is unavailable', async () => { |
| 99 | + renderWithMantine(<div />); |
| 100 | + jest.spyOn(Storage.prototype, 'getItem').mockImplementation(() => { |
| 101 | + throw new DOMException('Access denied', 'SecurityError'); |
| 102 | + }); |
| 103 | + |
| 104 | + expect(await drillDown()).toBeNull(); |
| 105 | + |
| 106 | + expect( |
| 107 | + await screen.findByText(/drill-down is unavailable/i), |
| 108 | + ).toBeInTheDocument(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('does not persist when the toast closes without the close button', async () => { |
| 112 | + renderWithMantine(<div />); |
| 113 | + await drillDown(); |
| 114 | + await screen.findByRole('alert'); |
| 115 | + |
| 116 | + // Auto-close goes through the same hide path as notifications.hide. |
| 117 | + act(() => { |
| 118 | + notifications.hide(TOAST_ID); |
| 119 | + }); |
| 120 | + |
| 121 | + expect(localStorage.getItem(DISMISSED_KEY)).toBeNull(); |
| 122 | + const show = jest.spyOn(notifications, 'show'); |
| 123 | + await drillDown(); |
| 124 | + expect(show).toHaveBeenCalledTimes(1); |
| 125 | + }); |
| 126 | +}); |
0 commit comments