-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreports.test.ts
More file actions
338 lines (308 loc) · 10.8 KB
/
reports.test.ts
File metadata and controls
338 lines (308 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import { describe, it, expect, beforeAll, afterAll, afterEach, vi } from 'vitest';
import nock from 'nock';
const DATA_API_BASE = 'https://analyticsdata.googleapis.com';
describe('reports commands', () => {
beforeAll(() => {
nock.disableNetConnect();
vi.stubEnv('GA4_ACCESS_TOKEN', 'test-token-123');
});
afterAll(() => {
nock.enableNetConnect();
vi.unstubAllEnvs();
});
afterEach(() => {
nock.cleanAll();
});
describe('reports run', () => {
it('runs a report with dimensions and metrics', async () => {
const mockResponse = {
dimensionHeaders: [{ name: 'city' }, { name: 'country' }],
metricHeaders: [
{ name: 'activeUsers', type: 'TYPE_INTEGER' },
{ name: 'sessions', type: 'TYPE_INTEGER' },
],
rows: [
{
dimensionValues: [{ value: 'New York' }, { value: 'United States' }],
metricValues: [{ value: '1234' }, { value: '5678' }],
},
{
dimensionValues: [{ value: 'London' }, { value: 'United Kingdom' }],
metricValues: [{ value: '987' }, { value: '2345' }],
},
],
rowCount: 2,
metadata: {
currencyCode: 'USD',
timeZone: 'America/New_York',
},
};
const scope = nock(DATA_API_BASE)
.post('/v1beta/properties/123456:runReport', (body) => {
return (
body.metrics?.length === 2 &&
body.metrics[0].name === 'activeUsers' &&
body.dimensions?.length === 2 &&
body.dateRanges?.[0]?.startDate === '2024-01-01'
);
})
.matchHeader('authorization', 'Bearer test-token-123')
.reply(200, mockResponse);
const { request } = await import('../lib/http.js');
const data = await request<typeof mockResponse>(
`${DATA_API_BASE}/v1beta/properties/123456:runReport`,
{
method: 'POST',
headers: { Authorization: 'Bearer test-token-123' },
body: {
metrics: [{ name: 'activeUsers' }, { name: 'sessions' }],
dimensions: [{ name: 'city' }, { name: 'country' }],
dateRanges: [{ startDate: '2024-01-01', endDate: '2024-01-31' }],
limit: 100,
offset: 0,
},
},
);
expect(data.dimensionHeaders).toHaveLength(2);
expect(data.metricHeaders).toHaveLength(2);
expect(data.rows).toHaveLength(2);
expect(data.rows[0].dimensionValues[0].value).toBe('New York');
expect(data.rows[0].metricValues[0].value).toBe('1234');
expect(scope.isDone()).toBe(true);
});
it('runs a report with metrics only (no dimensions)', async () => {
const mockResponse = {
dimensionHeaders: [],
metricHeaders: [{ name: 'activeUsers', type: 'TYPE_INTEGER' }],
rows: [
{
dimensionValues: [],
metricValues: [{ value: '42000' }],
},
],
rowCount: 1,
metadata: {},
};
const scope = nock(DATA_API_BASE)
.post('/v1beta/properties/123456:runReport', (body) => {
return body.metrics?.length === 1 && !body.dimensions;
})
.reply(200, mockResponse);
const { request } = await import('../lib/http.js');
const data = await request<typeof mockResponse>(
`${DATA_API_BASE}/v1beta/properties/123456:runReport`,
{
method: 'POST',
headers: { Authorization: 'Bearer test-token-123' },
body: {
metrics: [{ name: 'activeUsers' }],
dateRanges: [{ startDate: '30daysAgo', endDate: 'today' }],
limit: 100,
offset: 0,
},
},
);
expect(data.rows).toHaveLength(1);
expect(data.rows[0].metricValues[0].value).toBe('42000');
expect(scope.isDone()).toBe(true);
});
it('handles API error response', async () => {
nock(DATA_API_BASE)
.post('/v1beta/properties/123456:runReport')
.reply(400, {
error: {
code: 400,
message: 'Invalid dimension name: "invalid_dim".',
status: 'INVALID_ARGUMENT',
},
});
const { request, HttpError } = await import('../lib/http.js');
await expect(
request(`${DATA_API_BASE}/v1beta/properties/123456:runReport`, {
method: 'POST',
headers: { Authorization: 'Bearer test-token-123' },
body: {
metrics: [{ name: 'activeUsers' }],
dimensions: [{ name: 'invalid_dim' }],
dateRanges: [{ startDate: '7daysAgo', endDate: 'today' }],
},
}),
).rejects.toThrow(HttpError);
});
it('passes dimension filter through to API request body', async () => {
const dimensionFilter = {
filter: {
fieldName: 'pagePath',
stringFilter: { matchType: 'CONTAINS', value: '/pricing', caseSensitive: false },
},
};
const mockResponse = {
dimensionHeaders: [{ name: 'pagePath' }],
metricHeaders: [{ name: 'totalUsers', type: 'TYPE_INTEGER' }],
rows: [
{
dimensionValues: [{ value: '/www.example.com/pricing' }],
metricValues: [{ value: '500' }],
},
],
rowCount: 1,
metadata: {},
};
const scope = nock(DATA_API_BASE)
.post('/v1beta/properties/123456:runReport', (body) => {
return (
body.dimensionFilter?.filter?.fieldName === 'pagePath' &&
body.dimensionFilter?.filter?.stringFilter?.matchType === 'CONTAINS'
);
})
.reply(200, mockResponse);
const { request } = await import('../lib/http.js');
const data = await request<typeof mockResponse>(
`${DATA_API_BASE}/v1beta/properties/123456:runReport`,
{
method: 'POST',
headers: { Authorization: 'Bearer test-token-123' },
body: {
metrics: [{ name: 'totalUsers' }],
dimensions: [{ name: 'pagePath' }],
dateRanges: [{ startDate: '30daysAgo', endDate: 'today' }],
dimensionFilter,
limit: 100,
offset: 0,
},
},
);
expect(data.rows).toHaveLength(1);
expect(data.rows[0].dimensionValues[0].value).toBe('/www.example.com/pricing');
expect(scope.isDone()).toBe(true);
});
it('passes and_group dimension filter through to API', async () => {
const dimensionFilter = {
andGroup: {
expressions: [
{
filter: {
fieldName: 'pagePath',
stringFilter: { matchType: 'CONTAINS', value: '/pricing' },
},
},
{
filter: {
fieldName: 'eventName',
stringFilter: { matchType: 'EXACT', value: 'form_submit' },
},
},
],
},
};
const mockResponse = {
dimensionHeaders: [{ name: 'date' }],
metricHeaders: [{ name: 'eventCount', type: 'TYPE_INTEGER' }],
rows: [
{
dimensionValues: [{ value: '20240115' }],
metricValues: [{ value: '12' }],
},
],
rowCount: 1,
metadata: {},
};
const scope = nock(DATA_API_BASE)
.post('/v1beta/properties/123456:runReport', (body) => {
return (
body.dimensionFilter?.andGroup?.expressions?.length === 2 &&
body.dimensionFilter.andGroup.expressions[0].filter.fieldName === 'pagePath' &&
body.dimensionFilter.andGroup.expressions[1].filter.fieldName === 'eventName'
);
})
.reply(200, mockResponse);
const { request } = await import('../lib/http.js');
const data = await request<typeof mockResponse>(
`${DATA_API_BASE}/v1beta/properties/123456:runReport`,
{
method: 'POST',
headers: { Authorization: 'Bearer test-token-123' },
body: {
metrics: [{ name: 'eventCount' }],
dimensions: [{ name: 'date' }],
dateRanges: [{ startDate: '30daysAgo', endDate: 'today' }],
dimensionFilter,
limit: 100,
offset: 0,
},
},
);
expect(data.rows).toHaveLength(1);
expect(scope.isDone()).toBe(true);
});
it('returns metadata envelope when include-metadata is used', async () => {
const mockResponse = {
dimensionHeaders: [{ name: 'date' }],
metricHeaders: [{ name: 'totalUsers', type: 'TYPE_INTEGER' }],
rows: [
{
dimensionValues: [{ value: '20240115' }],
metricValues: [{ value: '250' }],
},
],
rowCount: 1,
metadata: {
currencyCode: 'USD',
timeZone: 'America/New_York',
samplingMetadatas: [
{ samplesReadCount: '500000', samplingSpaceSize: '1000000' },
],
},
};
const scope = nock(DATA_API_BASE)
.post('/v1beta/properties/123456:runReport')
.reply(200, mockResponse);
const { request } = await import('../lib/http.js');
const data = await request<typeof mockResponse>(
`${DATA_API_BASE}/v1beta/properties/123456:runReport`,
{
method: 'POST',
headers: { Authorization: 'Bearer test-token-123' },
body: {
metrics: [{ name: 'totalUsers' }],
dimensions: [{ name: 'date' }],
dateRanges: [{ startDate: '30daysAgo', endDate: 'today' }],
limit: 100,
offset: 0,
},
},
);
// Verify sampling metadata is present in response
expect(data.metadata.samplingMetadatas).toHaveLength(1);
expect(data.metadata.samplingMetadatas![0].samplesReadCount).toBe('500000');
expect(data.metadata.samplingMetadatas![0].samplingSpaceSize).toBe('1000000');
expect(scope.isDone()).toBe(true);
});
it('handles auth error', async () => {
nock(DATA_API_BASE)
.post('/v1beta/properties/123456:runReport')
.reply(401, {
error: {
code: 401,
message: 'Request had invalid authentication credentials.',
status: 'UNAUTHENTICATED',
},
});
const { request, HttpError } = await import('../lib/http.js');
try {
await request(`${DATA_API_BASE}/v1beta/properties/123456:runReport`, {
method: 'POST',
headers: { Authorization: 'Bearer bad-token' },
body: {
metrics: [{ name: 'activeUsers' }],
dateRanges: [{ startDate: '7daysAgo', endDate: 'today' }],
},
});
expect.unreachable('Should have thrown');
} catch (error) {
expect(error).toBeInstanceOf(HttpError);
expect((error as InstanceType<typeof HttpError>).code).toBe('AUTH_FAILED');
}
});
});
});