Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: enable lint on tests for api, interactions, predictions, pubsub packages #13547

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
4 changes: 0 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,9 @@ module.exports = {
'setupTests.ts',
'jest.setup.*',
'jest.config.*',
'packages/api/__tests__',
'packages/api-graphql/__tests__',
'packages/datastore/__tests__',
'packages/datastore-storage-adapter/__tests__',
'packages/interactions/__tests__',
'packages/predictions/__tests__',
'packages/pubsub/__tests__',
],
rules: {
camelcase: [
Expand Down
11 changes: 2 additions & 9 deletions packages/api/__tests__/API.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { ResourcesConfig } from 'aws-amplify';
import { InternalGraphQLAPIClass } from '@aws-amplify/api-graphql/internals';
import { generateClient, CONNECTION_STATE_CHANGE } from '@aws-amplify/api';
import { AmplifyClassV6 } from '@aws-amplify/core';
// import { runWithAmplifyServerContext } from 'aws-amplify/internals/adapter-core';

const serverManagedFields = {
id: 'some-id',
owner: 'wirejobviously',
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
import { CONNECTION_STATE_CHANGE, generateClient } from '@aws-amplify/api';
// import { runWithAmplifyServerContext } from 'aws-amplify/internals/adapter-core';

describe('API generateClient', () => {
afterEach(() => {
Expand Down
59 changes: 36 additions & 23 deletions packages/interactions/__tests__/lex-v1/AWSLexProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ import {
PostTextCommand,
PostTextCommandOutput,
} from '@aws-sdk/client-lex-runtime-service';
import { lexProvider } from '../../src/lex-v1/AWSLexProvider';
import { fetchAuthSession } from '@aws-amplify/core';

import { lexProvider } from '../../src/lex-v1/AWSLexProvider';
import { InteractionsOnCompleteCallback } from '../../src/types/Interactions';

jest.mock('@aws-amplify/core');

(global as any).Response = class Response {
arrayBuffer(blob: Blob) {
arrayBuffer() {
return Promise.resolve(new ArrayBuffer(0));
}
};

type AWSLexProvider = typeof lexProvider;

// mock stream response
const createBlob = () => {
return new Blob();
Expand Down Expand Up @@ -47,7 +51,7 @@ const credentials = {

const mockFetchAuthSession = fetchAuthSession as jest.Mock;

LexRuntimeServiceClient.prototype.send = jest.fn((command, callback) => {
LexRuntimeServiceClient.prototype.send = jest.fn(command => {
if (command instanceof PostTextCommand) {
if (command.input.inputText === 'done') {
const result = {
Expand All @@ -58,18 +62,21 @@ LexRuntimeServiceClient.prototype.send = jest.fn((command, callback) => {
m2: 'done',
},
};

return Promise.resolve(result);
} else if (command.input.inputText === 'error') {
const result = {
message: 'echo:' + command.input.inputText,
dialogState: 'Failed',
};

return Promise.resolve(result);
} else {
const result = {
message: 'echo:' + command.input.inputText,
dialogState: 'ElicitSlot',
};

return Promise.resolve(result);
}
} else if (command instanceof PostContentCommand) {
Expand All @@ -78,7 +85,7 @@ LexRuntimeServiceClient.prototype.send = jest.fn((command, callback) => {
'audio/x-l16; sample-rate=16000; channel-count=1'
) {
const bot = command.input.botName as string;
const [botName, status] = bot.split(':');
const [_botName, status] = bot.split(':');

if (status === 'done') {
// we add the status to the botName
Expand All @@ -92,20 +99,23 @@ LexRuntimeServiceClient.prototype.send = jest.fn((command, callback) => {
},
audioStream: createBlob(),
};

return Promise.resolve(result);
} else if (status === 'error') {
const result = {
message: 'voice:echo:' + command.input.botName,
dialogState: 'Failed',
audioStream: createBlob(),
};

return Promise.resolve(result);
} else {
const result = {
message: 'voice:echo:' + command.input.botName,
dialogState: 'ElicitSlot',
audioStream: createBlob(),
};

return Promise.resolve(result);
}
} else {
Expand All @@ -119,20 +129,23 @@ LexRuntimeServiceClient.prototype.send = jest.fn((command, callback) => {
},
audioStream: createBlob(),
};

return Promise.resolve(result);
} else if (command.input.inputStream === 'error') {
const result = {
message: 'echo:' + command.input.inputStream,
dialogState: 'Failed',
audioStream: createBlob(),
};

return Promise.resolve(result);
} else {
const result = {
message: 'echo:' + command.input.inputStream,
dialogState: 'ElicitSlot',
audioStream: createBlob(),
};

return Promise.resolve(result);
}
}
Expand All @@ -146,7 +159,7 @@ afterEach(() => {
describe('Interactions', () => {
// send text and audio message to bot
describe('send API', () => {
let provider;
let provider: AWSLexProvider;

beforeEach(() => {
mockFetchAuthSession.mockReturnValue(credentials);
Expand Down Expand Up @@ -266,6 +279,7 @@ describe('Interactions', () => {
provider.sendMessage(botConfig.BookTrip, {
content: createBlob(),
options: {
// @ts-expect-error for testing messageType mismatches content type
messageType: 'text',
},
}),
Expand All @@ -274,6 +288,7 @@ describe('Interactions', () => {
// obj voice in wrong format
await expect(
provider.sendMessage(botConfig.BookTrip, {
// @ts-expect-error for testing messageType mismatches content type
content: 'Hi',
options: {
messageType: 'voice',
Expand All @@ -285,8 +300,8 @@ describe('Interactions', () => {

// attach 'onComplete' callback to bot
describe('onComplete API', () => {
const callback = (err, confirmation) => {};
let provider;
const callback = jest.fn();
let provider: AWSLexProvider;

beforeEach(() => {
mockFetchAuthSession.mockReturnValue(credentials);
Expand All @@ -298,34 +313,32 @@ describe('Interactions', () => {
});

test('Configure onComplete callback for a configured bot successfully', () => {
expect(() =>
provider.onComplete(botConfig.BookTrip, callback),
).not.toThrow();
expect(() => {
provider.onComplete(botConfig.BookTrip, callback);
}).not.toThrow();
expect.assertions(1);
});
});

// test if 'onComplete' callback is fired for different actions
describe('reportBotStatus API', () => {
jest.useFakeTimers();
let provider;
let provider: AWSLexProvider;

let inProgressResp;
let completeSuccessResp;
let completeFailResp;
let inProgressResp: PostTextCommandOutput;
let completeSuccessResp: PostTextCommandOutput;
let completeFailResp: PostTextCommandOutput;

let inProgressCallback;
let completeSuccessCallback;
let completeFailCallback;
let inProgressCallback: InteractionsOnCompleteCallback;
let completeSuccessCallback: InteractionsOnCompleteCallback;
let completeFailCallback: InteractionsOnCompleteCallback;

beforeEach(async () => {
mockFetchAuthSession.mockReturnValue(credentials);
provider = lexProvider;

// mock callbacks
inProgressCallback = jest.fn((err, confirmation) =>
fail(`callback shouldn't be called`),
);
inProgressCallback = jest.fn(() => fail(`callback shouldn't be called`));

completeSuccessCallback = jest.fn((err, confirmation) => {
expect(err).toEqual(undefined);
Expand All @@ -339,9 +352,9 @@ describe('Interactions', () => {
});
});

completeFailCallback = jest.fn((err, confirmation) =>
expect(err).toEqual(new Error('Bot conversation failed')),
);
completeFailCallback = jest.fn(err => {
expect(err).toEqual(new Error('Bot conversation failed'));
});

// mock responses
inProgressResp = (await provider.sendMessage(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { v4 as uuid } from 'uuid';
import { lexProvider } from '../../../src/lex-v1/AWSLexProvider';
import { onComplete } from '../../../src/lex-v1/apis';
import { generateRandomLexV1Config } from '../../testUtils/randomConfigGeneration';
Expand All @@ -27,7 +26,6 @@ describe('Interactions LexV1 API: onComplete', () => {
});

it('invokes provider onComplete API', () => {
const message = uuid();
const mockCallback = jest.fn();
onComplete({ botName: v1BotConfig.name, callback: mockCallback });
expect(mockLexProvider).toHaveBeenCalledTimes(1);
Expand All @@ -36,8 +34,8 @@ describe('Interactions LexV1 API: onComplete', () => {

it('rejects when bot config does not exist', async () => {
mockResolveBotConfig.mockReturnValue(undefined);
expect(() =>
onComplete({ botName: v1BotConfig.name, callback: jest.fn }),
).toThrow(InteractionsError);
expect(() => {
onComplete({ botName: v1BotConfig.name, callback: jest.fn });
}).toThrow(InteractionsError);
});
});
1 change: 1 addition & 0 deletions packages/interactions/__tests__/lex-v1/apis/send.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { v4 as uuid } from 'uuid';

import { lexProvider } from '../../../src/lex-v1/AWSLexProvider';
import { send } from '../../../src/lex-v1/apis';
import { generateRandomLexV1Config } from '../../testUtils/randomConfigGeneration';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { Amplify } from '@aws-amplify/core';

import {
generateRandomLexV1Config,
generateRandomLexV2Config,
Expand Down
Loading
Loading