|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { getTasks } from '../events'; |
| 3 | + |
| 4 | +// Mock the analytics module |
| 5 | +vi.mock('@/lib/server', () => ({ |
| 6 | + analytics: { |
| 7 | + query: vi.fn(), |
| 8 | + }, |
| 9 | +})); |
| 10 | + |
| 11 | +// Mock the auth module |
| 12 | +vi.mock('@/actions/auth', () => ({ |
| 13 | + authorizeAnalytics: vi.fn().mockResolvedValue({ |
| 14 | + effectiveUserId: 'test-user-id', |
| 15 | + }), |
| 16 | +})); |
| 17 | + |
| 18 | +// Mock the db module |
| 19 | +vi.mock('@roo-code-cloud/db/server', () => ({ |
| 20 | + getUsersById: vi.fn().mockResolvedValue({ |
| 21 | + 'test-user-id': { |
| 22 | + id: 'test-user-id', |
| 23 | + name: 'Test User', |
| 24 | + |
| 25 | + }, |
| 26 | + }), |
| 27 | +})); |
| 28 | + |
| 29 | +describe('getTasks with filters', () => { |
| 30 | + let mockQuery: ReturnType<typeof vi.fn>; |
| 31 | + |
| 32 | + beforeEach(async () => { |
| 33 | + vi.clearAllMocks(); |
| 34 | + const { analytics } = await import('@/lib/server'); |
| 35 | + mockQuery = vi.mocked(analytics.query); |
| 36 | + mockQuery.mockResolvedValue({ |
| 37 | + json: () => |
| 38 | + Promise.resolve([ |
| 39 | + { |
| 40 | + taskId: 'task-1', |
| 41 | + userId: 'test-user-id', |
| 42 | + provider: 'openai', |
| 43 | + model: 'gpt-4', |
| 44 | + mode: 'code', |
| 45 | + completed: true, |
| 46 | + tokens: 1000, |
| 47 | + cost: 0.02, |
| 48 | + timestamp: 1640995200, |
| 49 | + title: 'Test Task', |
| 50 | + repositoryUrl: 'https://github.com/test/repo', |
| 51 | + repositoryName: 'test-repo', |
| 52 | + defaultBranch: 'main', |
| 53 | + }, |
| 54 | + ]), |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should include userId filter in query parameters', async () => { |
| 59 | + await getTasks({ |
| 60 | + orgId: 'test-org', |
| 61 | + filterType: 'userId', |
| 62 | + filterValue: 'filter-user-id', |
| 63 | + limit: 20, |
| 64 | + }); |
| 65 | + |
| 66 | + expect(mockQuery).toHaveBeenCalledWith( |
| 67 | + expect.objectContaining({ |
| 68 | + query_params: expect.objectContaining({ |
| 69 | + filterUserId: 'filter-user-id', |
| 70 | + }), |
| 71 | + }), |
| 72 | + ); |
| 73 | + |
| 74 | + const queryCall = mockQuery.mock.calls[0]?.[0]; |
| 75 | + expect(queryCall?.query).toContain('AND e.userId = {filterUserId: String}'); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should include model filter in query parameters', async () => { |
| 79 | + await getTasks({ |
| 80 | + orgId: 'test-org', |
| 81 | + filterType: 'model', |
| 82 | + filterValue: 'gpt-4', |
| 83 | + limit: 20, |
| 84 | + }); |
| 85 | + |
| 86 | + expect(mockQuery).toHaveBeenCalledWith( |
| 87 | + expect.objectContaining({ |
| 88 | + query_params: expect.objectContaining({ |
| 89 | + filterModel: 'gpt-4', |
| 90 | + }), |
| 91 | + }), |
| 92 | + ); |
| 93 | + |
| 94 | + const queryCall = mockQuery.mock.calls[0]?.[0]; |
| 95 | + expect(queryCall?.query).toContain('AND e.modelId = {filterModel: String}'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should include repository filter in query parameters', async () => { |
| 99 | + await getTasks({ |
| 100 | + orgId: 'test-org', |
| 101 | + filterType: 'repositoryName', |
| 102 | + filterValue: 'test-repo', |
| 103 | + limit: 20, |
| 104 | + }); |
| 105 | + |
| 106 | + expect(mockQuery).toHaveBeenCalledWith( |
| 107 | + expect.objectContaining({ |
| 108 | + query_params: expect.objectContaining({ |
| 109 | + filterRepository: 'test-repo', |
| 110 | + }), |
| 111 | + }), |
| 112 | + ); |
| 113 | + |
| 114 | + const queryCall = mockQuery.mock.calls[0]?.[0]; |
| 115 | + expect(queryCall?.query).toContain( |
| 116 | + 'AND e.repositoryName = {filterRepository: String}', |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + it('should not include filter clauses when no filter is provided', async () => { |
| 121 | + await getTasks({ |
| 122 | + orgId: 'test-org', |
| 123 | + limit: 20, |
| 124 | + }); |
| 125 | + |
| 126 | + const queryCall = mockQuery.mock.calls[0]?.[0]; |
| 127 | + expect(queryCall?.query).not.toContain('filterUserId'); |
| 128 | + expect(queryCall?.query).not.toContain('filterModel'); |
| 129 | + expect(queryCall?.query).not.toContain('filterRepository'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should return properly formatted tasks with user data', async () => { |
| 133 | + const result = await getTasks({ |
| 134 | + orgId: 'test-org', |
| 135 | + filterType: 'model', |
| 136 | + filterValue: 'gpt-4', |
| 137 | + limit: 20, |
| 138 | + }); |
| 139 | + |
| 140 | + expect(result).toEqual({ |
| 141 | + tasks: [ |
| 142 | + expect.objectContaining({ |
| 143 | + taskId: 'task-1', |
| 144 | + userId: 'test-user-id', |
| 145 | + model: 'gpt-4', |
| 146 | + user: expect.objectContaining({ |
| 147 | + id: 'test-user-id', |
| 148 | + name: 'Test User', |
| 149 | + |
| 150 | + }), |
| 151 | + }), |
| 152 | + ], |
| 153 | + hasMore: false, |
| 154 | + nextCursor: undefined, |
| 155 | + }); |
| 156 | + }); |
| 157 | +}); |
0 commit comments