Skip to content

Commit

Permalink
chore: Sync console-preview with next/main (#12198)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimblanc authored Oct 5, 2023
2 parents b5a42af + 4b9e817 commit 4ae78ed
Show file tree
Hide file tree
Showing 207 changed files with 6,936 additions and 2,512 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import {
getEventBuffer,
resolveConfig,
} from '../../../../src/providers/kinesis-firehose/utils';
import { resolveCredentials } from '../../../../src/utils';
import {
mockKinesisConfig,
mockCredentialConfig,
} from '../../../testUtils/mockConstants.test';
import { flushEvents } from '../../../../src/providers/kinesis-firehose/apis';
import { ConsoleLogger } from '@aws-amplify/core/internals/utils';

jest.mock('../../../../src/utils');
jest.mock('../../../../src/providers/kinesis-firehose/utils');

describe('Analytics Kinesis Firehose API: flushEvents', () => {
const mockResolveConfig = resolveConfig as jest.Mock;
const mockResolveCredentials = resolveCredentials as jest.Mock;
const mockGetEventBuffer = getEventBuffer as jest.Mock;
const mockFlushAll = jest.fn();
const loggerWarnSpy = jest.spyOn(ConsoleLogger.prototype, 'warn');

beforeEach(() => {
mockResolveConfig.mockReturnValue(mockKinesisConfig);
mockResolveCredentials.mockReturnValue(
Promise.resolve(mockCredentialConfig)
);
mockGetEventBuffer.mockImplementation(() => ({
flushAll: mockFlushAll,
}));
});

afterEach(() => {
mockResolveConfig.mockReset();
mockResolveCredentials.mockReset();
mockFlushAll.mockReset();
mockGetEventBuffer.mockReset();
});

it('trigger flushAll on event buffer', async () => {
flushEvents();
await new Promise(process.nextTick);
expect(mockResolveConfig).toHaveBeenCalledTimes(1);
expect(mockResolveCredentials).toHaveBeenCalledTimes(1);
expect(mockGetEventBuffer).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
...mockKinesisConfig,
...mockCredentialConfig,
})
);
expect(mockFlushAll).toHaveBeenCalledTimes(1);
});

it('logs an error when credentials can not be fetched', async () => {
mockResolveCredentials.mockRejectedValue(new Error('Mock Error'));

flushEvents();
await new Promise(process.nextTick);
expect(loggerWarnSpy).toBeCalledWith(expect.any(String), expect.any(Error));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import {
getEventBuffer,
resolveConfig,
} from '../../../../src/providers/kinesis-firehose/utils';
import { isAnalyticsEnabled, resolveCredentials } from '../../../../src/utils';
import {
mockKinesisConfig,
mockCredentialConfig,
} from '../../../testUtils/mockConstants.test';
import { record } from '../../../../src/providers/kinesis-firehose';
import { ConsoleLogger as Logger } from '@aws-amplify/core/internals/utils';
import { RecordInput as KinesisFirehoseRecordInput } from '../../../../src/providers/kinesis-firehose/types';

jest.mock('../../../../src/utils');
jest.mock('../../../../src/providers/kinesis-firehose/utils');

describe('Analytics KinesisFirehose API: record', () => {
const mockRecordInput: KinesisFirehoseRecordInput = {
streamName: 'stream0',
data: new Uint8Array([0x01, 0x02, 0xff]),
};

const mockResolveConfig = resolveConfig as jest.Mock;
const mockResolveCredentials = resolveCredentials as jest.Mock;
const mockIsAnalyticsEnabled = isAnalyticsEnabled as jest.Mock;
const mockGetEventBuffer = getEventBuffer as jest.Mock;
const mockAppend = jest.fn();
const loggerWarnSpy = jest.spyOn(Logger.prototype, 'warn');
const loggerDebugSpy = jest.spyOn(Logger.prototype, 'debug');

beforeEach(() => {
mockIsAnalyticsEnabled.mockReturnValue(true);
mockResolveConfig.mockReturnValue(mockKinesisConfig);
mockResolveCredentials.mockReturnValue(
Promise.resolve(mockCredentialConfig)
);
mockGetEventBuffer.mockImplementation(() => ({
append: mockAppend,
}));
});

afterEach(() => {
mockResolveConfig.mockReset();
mockResolveCredentials.mockReset();
mockAppend.mockReset();
mockGetEventBuffer.mockReset();
mockIsAnalyticsEnabled.mockReset();
});

it('append to event buffer if record provided', async () => {
record(mockRecordInput);
await new Promise(process.nextTick);
expect(mockGetEventBuffer).toHaveBeenCalledTimes(1);
expect(mockAppend).toBeCalledWith(
expect.objectContaining({
region: mockKinesisConfig.region,
streamName: mockRecordInput.streamName,
event: mockRecordInput.data,
retryCount: 0,
})
);
});

it('logs an error when credentials can not be fetched', async () => {
mockResolveCredentials.mockRejectedValue(new Error('Mock Error'));

record(mockRecordInput);

await new Promise(process.nextTick);
expect(loggerWarnSpy).toBeCalledWith(expect.any(String), expect.any(Error));
});

it('logs and skip the event recoding if Analytics plugin is not enabled', async () => {
mockIsAnalyticsEnabled.mockReturnValue(false);
record(mockRecordInput);
await new Promise(process.nextTick);
expect(loggerDebugSpy).toBeCalledWith(expect.any(String));
expect(mockGetEventBuffer).not.toBeCalled();
expect(mockAppend).not.toBeCalled();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { getEventBuffer } from '../../../../src/providers/kinesis-firehose/utils';
import { EventBuffer } from '../../../../src/utils';
import {
mockBufferConfig,
mockCredentialConfig, mockKinesisConfig,
} from '../../../testUtils/mockConstants.test';

jest.mock('../../../../src/utils');

describe('KinesisFirehose Provider Util: getEventBuffer', () => {
const mockEventBuffer = EventBuffer as jest.Mock;

afterEach(() => {
mockEventBuffer.mockReset();
});

it("create a buffer if one doesn't exist", () => {
const testBuffer = getEventBuffer({
...mockKinesisConfig,
...mockCredentialConfig,
});

expect(mockEventBuffer).toBeCalledWith(
mockBufferConfig,
expect.any(Function)
);
expect(testBuffer).toBeInstanceOf(EventBuffer);
});

it('returns an existing buffer instance', () => {
const testBuffer1 = getEventBuffer({
...mockKinesisConfig,
...mockCredentialConfig,
});
const testBuffer2 = getEventBuffer({
...mockKinesisConfig,
...mockCredentialConfig,
});
expect(testBuffer1).toBe(testBuffer2);
});

it('release other buffers & creates a new one if credential has changed', () => {
const testBuffer1 = getEventBuffer({
...mockKinesisConfig,
...mockCredentialConfig,
});
const testBuffer2 = getEventBuffer({
...mockKinesisConfig,
...mockCredentialConfig,
identityId: 'identityId2',
});

expect(testBuffer1.release).toHaveBeenCalledTimes(1);
expect(testBuffer1).not.toBe(testBuffer2);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { Amplify } from '@aws-amplify/core';
import { resolveConfig } from '../../../../src/providers/kinesis-firehose/utils';
import { DEFAULT_KINESIS_FIREHOSE_CONFIG } from '../../../../src/providers/kinesis-firehose/utils/constants';

describe('Analytics KinesisFirehose Provider Util: resolveConfig', () => {
const providedConfig = {
region: 'us-east-1',
bufferSize: 100,
flushSize: 10,
flushInterval: 1000,
resendLimit: 3,
};

const getConfigSpy = jest.spyOn(Amplify, 'getConfig');

beforeEach(() => {
getConfigSpy.mockReset();
});

it('returns required config', () => {
getConfigSpy.mockReturnValue({
Analytics: { KinesisFirehose: providedConfig },
});

expect(resolveConfig()).toStrictEqual(providedConfig);
});

it('use default config for optional fields', () => {
const requiredFields = {
region: 'us-east-1',
bufferSize: undefined,
resendLimit: undefined,
};
getConfigSpy.mockReturnValue({
Analytics: { KinesisFirehose: requiredFields },
});

expect(resolveConfig()).toStrictEqual({
...DEFAULT_KINESIS_FIREHOSE_CONFIG,
region: requiredFields.region,
resendLimit: requiredFields.resendLimit,
});
});

it('throws if region is missing', () => {
getConfigSpy.mockReturnValue({
Analytics: { KinesisFirehose: { ...providedConfig, region: undefined } },
});

expect(resolveConfig).toThrow();
});

it('throws if flushSize is larger than bufferSize', () => {
getConfigSpy.mockReturnValue({
Analytics: {
KinesisFirehose: {
...providedConfig,
flushSize: providedConfig.bufferSize + 1,
},
},
});

expect(resolveConfig).toThrow();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { resolveConfig } from '../../../../src/providers/kinesis/utils/resolveConfig';
import { resolveCredentials } from '../../../../src/utils';
import {
mockKinesisConfig,
mockCredentialConfig,
} from '../../../testUtils/mockConstants.test';
import { getEventBuffer } from '../../../../src/providers/kinesis/utils/getEventBuffer';
import { flushEvents } from '../../../../src/providers/kinesis/apis';
import { ConsoleLogger } from '@aws-amplify/core/internals/utils';

jest.mock('../../../../src/utils');
jest.mock('../../../../src/providers/kinesis/utils/getEventBuffer');
jest.mock('../../../../src/providers/kinesis/utils/resolveConfig');

describe('Analytics Kinesis API: flushEvents', () => {
const mockResolveConfig = resolveConfig as jest.Mock;
const mockResolveCredentials = resolveCredentials as jest.Mock;
const mockGetEventBuffer = getEventBuffer as jest.Mock;
const mockFlushAll = jest.fn();
const loggerWarnSpy = jest.spyOn(ConsoleLogger.prototype, 'warn');

beforeEach(() => {
mockResolveConfig.mockReturnValue(mockKinesisConfig);
mockResolveCredentials.mockReturnValue(
Promise.resolve(mockCredentialConfig)
);
mockGetEventBuffer.mockImplementation(() => ({
flushAll: mockFlushAll,
}));
});

afterEach(() => {
mockResolveConfig.mockReset();
mockResolveCredentials.mockReset();
mockFlushAll.mockReset();
mockGetEventBuffer.mockReset();
});

it('trigger flushAll on event buffer', async () => {
flushEvents();
await new Promise(process.nextTick);
expect(mockResolveConfig).toHaveBeenCalledTimes(1);
expect(mockResolveCredentials).toHaveBeenCalledTimes(1);
expect(mockGetEventBuffer).toHaveBeenNthCalledWith(
1,
expect.objectContaining({
...mockKinesisConfig,
...mockCredentialConfig,
})
);
expect(mockFlushAll).toHaveBeenCalledTimes(1);
});

it('logs an error when credentials can not be fetched', async () => {
mockResolveCredentials.mockRejectedValue(new Error('Mock Error'));

flushEvents();
await new Promise(process.nextTick);
expect(loggerWarnSpy).toBeCalledWith(expect.any(String), expect.any(Error));
});
});
Loading

0 comments on commit 4ae78ed

Please sign in to comment.