From bdd8bed55ad122fa833747c99506b7068b98b139 Mon Sep 17 00:00:00 2001 From: Xinyi Ye Date: Wed, 11 Oct 2023 00:54:24 -0700 Subject: [PATCH] test: client level tests --- .../test/browser-client.test.ts | 62 +++++++++++++++- packages/analytics-core/src/index.ts | 9 +++ .../test/plugins/destination.test.ts | 73 ------------------- 3 files changed, 70 insertions(+), 74 deletions(-) diff --git a/packages/analytics-browser/test/browser-client.test.ts b/packages/analytics-browser/test/browser-client.test.ts index 72c858bd3..12f9e3711 100644 --- a/packages/analytics-browser/test/browser-client.test.ts +++ b/packages/analytics-browser/test/browser-client.test.ts @@ -2,7 +2,7 @@ import { AmplitudeBrowser } from '../src/browser-client'; import * as core from '@amplitude/analytics-core'; import * as Config from '../src/config'; import * as CookieMigration from '../src/cookie-migration'; -import { UserSession } from '@amplitude/analytics-types'; +import { Diagnostic, Status, UserSession } from '@amplitude/analytics-types'; import { CookieStorage, FetchTransport, @@ -261,6 +261,66 @@ describe('browser-client', () => { }).promise; expect(webAttributionPluginPlugin).toHaveBeenCalledTimes(1); }); + + test.each([ + ['api_key', undefined, core.INVALID_OR_MISSING_FIELDS_DIAGNOSTIC_MESSAGE], + [undefined, { time: [0] }, core.EVENT_ERROR_DIAGNOSTIC_MESSAGE], + ])('should diagnostic track when 400 invalid response', async (missingField, eventsWithInvalidFields, message) => { + const transportProvider = { + send: jest.fn().mockImplementationOnce(() => { + return Promise.resolve({ + status: Status.Invalid, + statusCode: 400, + body: { + error: 'error', + missingField: missingField, + eventsWithInvalidFields: eventsWithInvalidFields, + eventsWithMissingFields: {}, + eventsWithInvalidIdLengths: {}, + silencedEvents: [], + }, + }); + }), + }; + + await client.init(apiKey, { + defaultTracking: false, + }).promise; + const diagnosticTrack = jest.spyOn(client.config.diagnosticProvider as Diagnostic, 'track'); + client.config.transportProvider = transportProvider; + await client.track('event_type', { userId: 'user_0' }).promise; + + expect(diagnosticTrack).toHaveBeenCalledTimes(1); + expect(diagnosticTrack).toHaveBeenCalledWith(1, 400, message); + }); + + test('should diagnostic track when 429 rate limit when flush', async () => { + const transportProvider = { + send: jest.fn().mockImplementationOnce(() => { + return Promise.resolve({ + status: Status.RateLimit, + statusCode: 429, + body: { + exceededDailyQuotaUsers: { user_0: 1 }, + exceededDailyQuotaDevices: {}, + throttledEvents: [], + }, + }); + }), + }; + + await client.init(apiKey, { + defaultTracking: false, + }).promise; + const diagnosticTrack = jest.spyOn(client.config.diagnosticProvider as Diagnostic, 'track'); + client.config.transportProvider = transportProvider; + client.track('event_type', { userId: 'user_0' }); + // flush() calls destination.flush(useRetry: false) + await client.flush().promise; + + expect(diagnosticTrack).toHaveBeenCalledTimes(1); + expect(diagnosticTrack).toHaveBeenCalledWith(1, 429, core.EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE); + }); }); describe('getUserId', () => { diff --git a/packages/analytics-core/src/index.ts b/packages/analytics-core/src/index.ts index c16abd055..052283a19 100644 --- a/packages/analytics-core/src/index.ts +++ b/packages/analytics-core/src/index.ts @@ -3,6 +3,15 @@ export { Identify } from './identify'; export { Revenue } from './revenue'; export { Destination } from './plugins/destination'; export { Diagnostic } from './diagnostics/diagnostic'; +export { + EXCEEDED_MAX_RETRY_DIAGNOSTIC_MESSAGE, + MISSING_API_KEY_DIAGNOSTIC_MESSAGE, + UNEXPECTED_DIAGNOSTIC_MESSAGE, + INVALID_OR_MISSING_FIELDS_DIAGNOSTIC_MESSAGE, + EVENT_ERROR_DIAGNOSTIC_MESSAGE, + PAYLOAD_TOO_LARGE_DIAGNOSTIC_MESSAGE, + EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE, +} from './diagnostics/constants'; export { Config } from './config'; export { Logger } from './logger'; export { AMPLITUDE_PREFIX, STORAGE_PREFIX } from './constants'; diff --git a/packages/analytics-core/test/plugins/destination.test.ts b/packages/analytics-core/test/plugins/destination.test.ts index 846661723..3494542a1 100644 --- a/packages/analytics-core/test/plugins/destination.test.ts +++ b/packages/analytics-core/test/plugins/destination.test.ts @@ -621,79 +621,6 @@ describe('destination', () => { expect(diagnosticProvider.track).toHaveBeenCalledWith(dropCount, 429, EXCEEDED_DAILY_QUOTA_DIAGNOSTIC_MESSAGE); expect(destination.queue.length).toBe(queueCount); }); - // const destination = new Destination(); - // const callback = jest.fn(); - // const event = { - // event_type: 'event_type', - // }; - // const context = { - // attempts: 0, - // callback, - // event, - // timeout: 0, - // }; - // const transportProvider = { - // send: jest.fn().mockImplementationOnce(() => { - // return Promise.resolve({ - // status: Status.RateLimit, - // statusCode: 429, - // }); - // }), - // }; - // await destination.setup({ - // ...useDefaultConfig(), - // transportProvider, - // apiKey: API_KEY, - // diagnosticProvider, - // }); - // await destination.send([context], false); - // expect(callback).toHaveBeenCalledTimes(1); - // expect(callback).toHaveBeenCalledWith({ - // event, - // code: 429, - // message: Status.RateLimit, - // }); - // expect(diagnosticProvider.track).toHaveBeenCalledTimes(1); - // expect(diagnosticProvider.track).toHaveBeenCalledWith(1, 429, 'exceeded daily quota users or devices'); - // expect(destination.queue.length).toBe(0); - // }); - - // test('should track diagnostic when 429 and retry', async () => { - // const destination = new Destination(); - // const callback = jest.fn(); - // const event = { - // event_type: 'event_type', - // user_id: 'user_0', - // }; - // const context = { - // attempts: 0, - // callback, - // event, - // timeout: 0, - // }; - // const transportProvider = { - // send: jest.fn().mockImplementationOnce(() => { - // return Promise.resolve({ - // status: Status.RateLimit, - // statusCode: 429, - // body: { - // exceededDailyQuotaUsers: { user_0: 1 }, - // exceededDailyQuotaDevices: {}, - // throttledEvents: [], - // }, - // }); - // }), - // }; - // await destination.setup({ - // ...useDefaultConfig(), - // transportProvider, - // apiKey: API_KEY, - // diagnosticProvider, - // }); - // await destination.send([context]); - // expect(diagnosticProvider.track).toHaveBeenCalledTimes(1); - // expect(diagnosticProvider.track).toHaveBeenCalledWith(1, 429, 'exceeded daily quota users or devices'); - // }); }); describe('saveEvents', () => {