|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 10 | +import { ObjectStackAdapter } from './index'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests for ObjectStackAdapter.aggregate() — verifies that the analytics |
| 14 | + * query payload uses the correct string-based measure/dimension format |
| 15 | + * expected by the backend analytics service (MemoryAnalyticsService). |
| 16 | + * |
| 17 | + * See: https://github.com/objectstack-ai/objectui/issues (measures format bug) |
| 18 | + */ |
| 19 | +describe('ObjectStackAdapter aggregate()', () => { |
| 20 | + let adapter: ObjectStackAdapter; |
| 21 | + let mockAnalyticsQuery: ReturnType<typeof vi.fn>; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + mockAnalyticsQuery = vi.fn().mockResolvedValue({ data: [] }); |
| 25 | + |
| 26 | + adapter = new ObjectStackAdapter({ |
| 27 | + baseUrl: 'http://localhost:3000', |
| 28 | + autoReconnect: false, |
| 29 | + }); |
| 30 | + |
| 31 | + // Inject mock client and mark as connected to bypass connect() |
| 32 | + (adapter as any).client = { |
| 33 | + data: { |
| 34 | + find: vi.fn().mockResolvedValue({ records: [], total: 0 }), |
| 35 | + }, |
| 36 | + analytics: { |
| 37 | + query: mockAnalyticsQuery, |
| 38 | + }, |
| 39 | + connect: vi.fn().mockResolvedValue(undefined), |
| 40 | + discover: vi.fn().mockResolvedValue({ status: 'ok' }), |
| 41 | + }; |
| 42 | + (adapter as any).connected = true; |
| 43 | + }); |
| 44 | + |
| 45 | + it('should send measures as string array with field_function format for sum', async () => { |
| 46 | + mockAnalyticsQuery.mockResolvedValue({ data: [] }); |
| 47 | + |
| 48 | + await adapter.aggregate('opportunity', { |
| 49 | + field: 'amount', |
| 50 | + function: 'sum', |
| 51 | + groupBy: 'stage', |
| 52 | + }); |
| 53 | + |
| 54 | + expect(mockAnalyticsQuery).toHaveBeenCalledWith({ |
| 55 | + cube: 'opportunity', |
| 56 | + measures: ['amount_sum'], |
| 57 | + dimensions: ['stage'], |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should send measures as ["count"] for count aggregation', async () => { |
| 62 | + mockAnalyticsQuery.mockResolvedValue({ data: [] }); |
| 63 | + |
| 64 | + await adapter.aggregate('opportunity', { |
| 65 | + field: 'amount', |
| 66 | + function: 'count', |
| 67 | + groupBy: 'stage', |
| 68 | + }); |
| 69 | + |
| 70 | + expect(mockAnalyticsQuery).toHaveBeenCalledWith({ |
| 71 | + cube: 'opportunity', |
| 72 | + measures: ['count'], |
| 73 | + dimensions: ['stage'], |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should send measures as string for avg aggregation', async () => { |
| 78 | + mockAnalyticsQuery.mockResolvedValue({ data: [] }); |
| 79 | + |
| 80 | + await adapter.aggregate('opportunity', { |
| 81 | + field: 'amount', |
| 82 | + function: 'avg', |
| 83 | + groupBy: 'stage', |
| 84 | + }); |
| 85 | + |
| 86 | + expect(mockAnalyticsQuery).toHaveBeenCalledWith({ |
| 87 | + cube: 'opportunity', |
| 88 | + measures: ['amount_avg'], |
| 89 | + dimensions: ['stage'], |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should send empty dimensions when groupBy is _all', async () => { |
| 94 | + mockAnalyticsQuery.mockResolvedValue({ data: [] }); |
| 95 | + |
| 96 | + await adapter.aggregate('opportunity', { |
| 97 | + field: 'amount', |
| 98 | + function: 'sum', |
| 99 | + groupBy: '_all', |
| 100 | + }); |
| 101 | + |
| 102 | + expect(mockAnalyticsQuery).toHaveBeenCalledWith({ |
| 103 | + cube: 'opportunity', |
| 104 | + measures: ['amount_sum'], |
| 105 | + dimensions: [], |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should include filters in payload when provided', async () => { |
| 110 | + const filter = [{ member: 'stage', operator: 'equals', values: ['Closed Won'] }]; |
| 111 | + mockAnalyticsQuery.mockResolvedValue({ data: [] }); |
| 112 | + |
| 113 | + await adapter.aggregate('opportunity', { |
| 114 | + field: 'amount', |
| 115 | + function: 'sum', |
| 116 | + groupBy: 'stage', |
| 117 | + filter, |
| 118 | + }); |
| 119 | + |
| 120 | + expect(mockAnalyticsQuery).toHaveBeenCalledWith({ |
| 121 | + cube: 'opportunity', |
| 122 | + measures: ['amount_sum'], |
| 123 | + dimensions: ['stage'], |
| 124 | + filters: filter, |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should map measure key back to field name in response', async () => { |
| 129 | + mockAnalyticsQuery.mockResolvedValue({ |
| 130 | + data: [ |
| 131 | + { stage: 'Prospect', amount_sum: 300 }, |
| 132 | + { stage: 'Closed Won', amount_sum: 500 }, |
| 133 | + ], |
| 134 | + }); |
| 135 | + |
| 136 | + const result = await adapter.aggregate('opportunity', { |
| 137 | + field: 'amount', |
| 138 | + function: 'sum', |
| 139 | + groupBy: 'stage', |
| 140 | + }); |
| 141 | + |
| 142 | + expect(result).toEqual([ |
| 143 | + { stage: 'Prospect', amount: 300 }, |
| 144 | + { stage: 'Closed Won', amount: 500 }, |
| 145 | + ]); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should map count measure back to field name in response', async () => { |
| 149 | + mockAnalyticsQuery.mockResolvedValue({ |
| 150 | + data: [ |
| 151 | + { stage: 'Prospect', count: 5 }, |
| 152 | + { stage: 'Closed Won', count: 3 }, |
| 153 | + ], |
| 154 | + }); |
| 155 | + |
| 156 | + const result = await adapter.aggregate('opportunity', { |
| 157 | + field: 'amount', |
| 158 | + function: 'count', |
| 159 | + groupBy: 'stage', |
| 160 | + }); |
| 161 | + |
| 162 | + expect(result).toEqual([ |
| 163 | + { stage: 'Prospect', amount: 5 }, |
| 164 | + { stage: 'Closed Won', amount: 3 }, |
| 165 | + ]); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should handle direct array response from analytics', async () => { |
| 169 | + mockAnalyticsQuery.mockResolvedValue([ |
| 170 | + { stage: 'Prospect', amount_sum: 300 }, |
| 171 | + ]); |
| 172 | + |
| 173 | + const result = await adapter.aggregate('opportunity', { |
| 174 | + field: 'amount', |
| 175 | + function: 'sum', |
| 176 | + groupBy: 'stage', |
| 177 | + }); |
| 178 | + |
| 179 | + expect(result).toEqual([ |
| 180 | + { stage: 'Prospect', amount: 300 }, |
| 181 | + ]); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should handle results wrapper in response', async () => { |
| 185 | + mockAnalyticsQuery.mockResolvedValue({ |
| 186 | + results: [ |
| 187 | + { stage: 'Prospect', amount_avg: 150 }, |
| 188 | + ], |
| 189 | + }); |
| 190 | + |
| 191 | + const result = await adapter.aggregate('opportunity', { |
| 192 | + field: 'amount', |
| 193 | + function: 'avg', |
| 194 | + groupBy: 'stage', |
| 195 | + }); |
| 196 | + |
| 197 | + expect(result).toEqual([ |
| 198 | + { stage: 'Prospect', amount: 150 }, |
| 199 | + ]); |
| 200 | + }); |
| 201 | + |
| 202 | + it('should fall back to client-side aggregation when analytics endpoint fails', async () => { |
| 203 | + mockAnalyticsQuery.mockRejectedValue(new Error('Analytics not available')); |
| 204 | + |
| 205 | + // Mock find() to return records for client-side aggregation |
| 206 | + (adapter as any).client.data.find = vi.fn().mockResolvedValue({ |
| 207 | + records: [ |
| 208 | + { stage: 'Prospect', amount: 100 }, |
| 209 | + { stage: 'Prospect', amount: 200 }, |
| 210 | + { stage: 'Closed Won', amount: 500 }, |
| 211 | + ], |
| 212 | + total: 3, |
| 213 | + }); |
| 214 | + |
| 215 | + const result = await adapter.aggregate('opportunity', { |
| 216 | + field: 'amount', |
| 217 | + function: 'sum', |
| 218 | + groupBy: 'stage', |
| 219 | + }); |
| 220 | + |
| 221 | + expect(result).toHaveLength(2); |
| 222 | + expect(result.find((r: any) => r.stage === 'Prospect')?.amount).toBe(300); |
| 223 | + expect(result.find((r: any) => r.stage === 'Closed Won')?.amount).toBe(500); |
| 224 | + }); |
| 225 | +}); |
0 commit comments