From 06cb498a46764c492ae12b09f7e55f603deed170 Mon Sep 17 00:00:00 2001 From: Nate Kelley Date: Wed, 8 Oct 2025 17:20:39 -0600 Subject: [PATCH] update tests --- .../BusterAssets/useIsAssetFileChanged.ts | 1 - .../BusterAssets/useSelectedAssetId.test.ts | 134 ++++++++++++ .../BusterAssets/useSelectedAssetType.test.ts | 194 ++++++++++++++++++ .../BusterAssets/useSelectedAssetType.ts | 8 +- apps/web/src/mocks/MOCK_CHAT.ts | 1 + 5 files changed, 333 insertions(+), 5 deletions(-) create mode 100644 apps/web/src/context/BusterAssets/useSelectedAssetId.test.ts create mode 100644 apps/web/src/context/BusterAssets/useSelectedAssetType.test.ts diff --git a/apps/web/src/context/BusterAssets/useIsAssetFileChanged.ts b/apps/web/src/context/BusterAssets/useIsAssetFileChanged.ts index 069ebc8ad..a42a17306 100644 --- a/apps/web/src/context/BusterAssets/useIsAssetFileChanged.ts +++ b/apps/web/src/context/BusterAssets/useIsAssetFileChanged.ts @@ -16,7 +16,6 @@ export const useIsAssetFileChanged = () => { dashboardId: assetId, enabled: assetType === 'dashboard_file', }); - console.log('reportParams', assetId, assetType); const reportParams = useIsReportFileChanged({ reportId: assetId, enabled: assetType === 'report_file', diff --git a/apps/web/src/context/BusterAssets/useSelectedAssetId.test.ts b/apps/web/src/context/BusterAssets/useSelectedAssetId.test.ts new file mode 100644 index 000000000..c00080462 --- /dev/null +++ b/apps/web/src/context/BusterAssets/useSelectedAssetId.test.ts @@ -0,0 +1,134 @@ +import { renderHook } from '@testing-library/react'; +import { describe, expect, it, vi } from 'vitest'; +import { useSelectedAssetId } from './useSelectedAssetType'; + +// Mock @tanstack/react-router +vi.mock('@tanstack/react-router', () => ({ + useParams: vi.fn(), + useMatches: vi.fn(), + useSearch: vi.fn(), +})); + +import { useParams } from '@tanstack/react-router'; + +describe('useSelectedAssetId', () => { + it('should return metricId when present', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: 'metric-123', + dashboardId: undefined, + reportId: undefined, + chatId: undefined, + collectionId: undefined, + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetId()); + + expect(result.current).toBe('metric-123'); + }); + + it('should return dashboardId when present and metricId is not', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: 'dashboard-456', + reportId: undefined, + chatId: undefined, + collectionId: undefined, + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetId()); + + expect(result.current).toBe('dashboard-456'); + }); + + it('should return reportId when present and metricId and dashboardId are not', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: undefined, + reportId: 'report-789', + chatId: undefined, + collectionId: undefined, + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetId()); + + expect(result.current).toBe('report-789'); + }); + + it('should return chatId when present and other IDs (except messageId and collectionId) are not', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: undefined, + reportId: undefined, + chatId: 'chat-abc', + collectionId: undefined, + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetId()); + + expect(result.current).toBe('chat-abc'); + }); + + it('should return collectionId when present and other IDs (except messageId) are not', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: undefined, + reportId: undefined, + chatId: undefined, + collectionId: 'collection-def', + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetId()); + + expect(result.current).toBe('collection-def'); + }); + + it('should return messageId when present and all other IDs are not', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: undefined, + reportId: undefined, + chatId: undefined, + collectionId: undefined, + messageId: 'message-ghi', + }); + + const { result } = renderHook(() => useSelectedAssetId()); + + expect(result.current).toBe('message-ghi'); + }); + + it('should return null when no params are present', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: undefined, + reportId: undefined, + chatId: undefined, + collectionId: undefined, + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetId()); + + expect(result.current).toBeNull(); + }); + + it('should prioritize metricId over all other params', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: 'metric-123', + dashboardId: 'dashboard-456', + reportId: 'report-789', + chatId: 'chat-abc', + collectionId: 'collection-def', + messageId: 'message-ghi', + }); + + const { result } = renderHook(() => useSelectedAssetId()); + + expect(result.current).toBe('metric-123'); + }); +}); diff --git a/apps/web/src/context/BusterAssets/useSelectedAssetType.test.ts b/apps/web/src/context/BusterAssets/useSelectedAssetType.test.ts new file mode 100644 index 000000000..5d0322df3 --- /dev/null +++ b/apps/web/src/context/BusterAssets/useSelectedAssetType.test.ts @@ -0,0 +1,194 @@ +import { renderHook } from '@testing-library/react'; +import { describe, expect, it, vi } from 'vitest'; +import { useSelectedAssetType } from './useSelectedAssetType'; + +// Mock @tanstack/react-router +vi.mock('@tanstack/react-router', () => ({ + useParams: vi.fn(), + useMatches: vi.fn(), + useSearch: vi.fn(), +})); + +import { useParams } from '@tanstack/react-router'; + +describe('useSelectedAssetType', () => { + it('should return "metric_file" when metricId is present', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: 'metric-123', + dashboardId: undefined, + reportId: undefined, + chatId: undefined, + collectionId: undefined, + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetType()); + + expect(result.current).toBe('metric_file'); + }); + + it('should return "reasoning" when messageId is present', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: undefined, + reportId: undefined, + chatId: undefined, + collectionId: undefined, + messageId: 'message-123', + }); + + const { result } = renderHook(() => useSelectedAssetType()); + + expect(result.current).toBe('reasoning'); + }); + + it('should return "dashboard_file" when dashboardId is present', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: 'dashboard-123', + reportId: undefined, + chatId: undefined, + collectionId: undefined, + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetType()); + + expect(result.current).toBe('dashboard_file'); + }); + + it('should return "report_file" when reportId is present', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: undefined, + reportId: 'report-123', + chatId: undefined, + collectionId: undefined, + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetType()); + + expect(result.current).toBe('report_file'); + }); + + it('should return "chat" when chatId is present', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: undefined, + reportId: undefined, + chatId: 'chat-123', + collectionId: undefined, + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetType()); + + expect(result.current).toBe('chat'); + }); + + it('should return "collection" when collectionId is present', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: undefined, + reportId: undefined, + chatId: undefined, + collectionId: 'collection-123', + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetType()); + + expect(result.current).toBe('collection'); + }); + + it('should return "metric_file" as default when no params are present', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: undefined, + reportId: undefined, + chatId: undefined, + collectionId: undefined, + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetType()); + + expect(result.current).toBe('metric_file'); + }); + + it('should prioritize metricId over all other params', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: 'metric-123', + dashboardId: 'dashboard-123', + reportId: 'report-123', + chatId: 'chat-123', + collectionId: 'collection-123', + messageId: 'message-123', + }); + + const { result } = renderHook(() => useSelectedAssetType()); + + expect(result.current).toBe('metric_file'); + }); + + it('should prioritize messageId over dashboardId, reportId, chatId, and collectionId', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: 'dashboard-123', + reportId: 'report-123', + chatId: 'chat-123', + collectionId: 'collection-123', + messageId: 'message-123', + }); + + const { result } = renderHook(() => useSelectedAssetType()); + + expect(result.current).toBe('reasoning'); + }); + + it('should prioritize dashboardId over reportId, chatId, and collectionId', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: 'dashboard-123', + reportId: 'report-123', + chatId: 'chat-123', + collectionId: 'collection-123', + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetType()); + + expect(result.current).toBe('dashboard_file'); + }); + + it('should prioritize reportId over chatId and collectionId', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: undefined, + reportId: 'report-123', + chatId: 'chat-123', + collectionId: 'collection-123', + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetType()); + + expect(result.current).toBe('report_file'); + }); + + it('should prioritize chatId over collectionId', () => { + vi.mocked(useParams).mockReturnValue({ + metricId: undefined, + dashboardId: undefined, + reportId: undefined, + chatId: 'chat-123', + collectionId: 'collection-123', + messageId: undefined, + }); + + const { result } = renderHook(() => useSelectedAssetType()); + + expect(result.current).toBe('chat'); + }); +}); diff --git a/apps/web/src/context/BusterAssets/useSelectedAssetType.ts b/apps/web/src/context/BusterAssets/useSelectedAssetType.ts index 4ce04856c..9e0d016db 100644 --- a/apps/web/src/context/BusterAssets/useSelectedAssetType.ts +++ b/apps/web/src/context/BusterAssets/useSelectedAssetType.ts @@ -46,6 +46,10 @@ export const useSelectedAssetId = () => { return metricId; } + if (messageId) { + return messageId; + } + if (dashboardId) { return dashboardId; } @@ -62,10 +66,6 @@ export const useSelectedAssetId = () => { return collectionId; } - if (messageId) { - return messageId; - } - return null; }; diff --git a/apps/web/src/mocks/MOCK_CHAT.ts b/apps/web/src/mocks/MOCK_CHAT.ts index b1e9ac954..c3c4fd8b3 100644 --- a/apps/web/src/mocks/MOCK_CHAT.ts +++ b/apps/web/src/mocks/MOCK_CHAT.ts @@ -181,5 +181,6 @@ export const MOCK_CHAT = (chatIndex = 0): BusterChat => { workspace_sharing: 'full_access', workspace_member_count: 0, individual_permissions: [], + screenshot_taken_at: null, }; };