Skip to content

Commit

Permalink
fix(detectDialogflowIntent): update unit test
Browse files Browse the repository at this point in the history
Replace env with credential detection mock
  • Loading branch information
MrOrz committed Apr 27, 2023
1 parent 9c3804e commit c52c8de
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
33 changes: 18 additions & 15 deletions src/lib/__tests__/detectDialogflowIntent.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
const mockSessionPath = jest.fn();
const mockDetectIntent = jest.fn();
const mockGetProjectId = jest.fn();

let detectDialogflowIntent;
beforeEach(() => {
// following variables are just for `detectDialogflowIntent` env varialbe check
process.env.DAILOGFLOW_PROJECT_ID = 'projectId';
process.env.DAILOGFLOW_CLIENT_EMAIL = 'client_email';
process.env.DAILOGFLOW_PRIVATE_KEY = 'private_key';
const sleep = async (ms) => new Promise((resolve) => setTimeout(resolve, ms));

beforeEach(() => {
mockSessionPath.mockClear();
mockDetectIntent.mockClear();
mockGetProjectId.mockClear();

// To test different case of process.env.* in `detectDialogflowIntent` we should resetModules
// To test different case in `detectDialogflowIntent` we should resetModules
//
jest.resetModules();
jest.mock('@google-cloud/dialogflow', () => ({
SessionsClient: jest.fn().mockImplementation(() => ({
projectAgentEnvironmentUserSessionPath: mockSessionPath,
detectIntent: mockDetectIntent,
getProjectId: mockGetProjectId,
})),
}));
});

afterEach(() => {
delete process.env.DAILOGFLOW_PROJECT_ID;
delete process.env.DAILOGFLOW_CLIENT_EMAIL;
delete process.env.DAILOGFLOW_PRIVATE_KEY;
});

const intentResponse = [
{
responseId: 'response_Id',
Expand Down Expand Up @@ -80,18 +74,24 @@ const intentResponse = [
];

it('skip detecting intent', async () => {
delete process.env.DAILOGFLOW_CLIENT_EMAIL;
mockGetProjectId.mockImplementation(() =>
Promise.reject('Test the case when Google service account is not provided')
);
detectDialogflowIntent = require('../detectDialogflowIntent').default;
await sleep(1); // Wait for module initialization (project ID detection)

mockDetectIntent.mockImplementation(() => intentResponse);
expect(await detectDialogflowIntent('Hi')).toMatchInlineSnapshot(`undefined`);
expect(mockSessionPath).not.toHaveBeenCalled();
expect(mockDetectIntent).not.toHaveBeenCalled();
});

it('detects intent', async () => {
mockGetProjectId.mockImplementation(() => Promise.resolve('test-gcp-id'));
detectDialogflowIntent = require('../detectDialogflowIntent').default;
mockDetectIntent.mockImplementation(() => intentResponse);
expect(await detectDialogflowIntent('Hi')).toMatchInlineSnapshot(`
await sleep(1); // Wait for module initialization (project ID detection)

mockDetectIntent.mockImplementation(() => intentResponse); expect(await detectDialogflowIntent('Hi')).toMatchInlineSnapshot(`

Check failure on line 94 in src/lib/__tests__/detectDialogflowIntent.js

View workflow job for this annotation

GitHub Actions / install-and-test

Insert `⏎`

Check failure on line 94 in src/lib/__tests__/detectDialogflowIntent.js

View workflow job for this annotation

GitHub Actions / install-and-test

Insert `⏎`
Object {
"outputAudio": Object {
"data": Array [],
Expand Down Expand Up @@ -155,7 +155,10 @@ it('detects intent', async () => {
});

it('handles error', async () => {
mockGetProjectId.mockImplementation(() => Promise.resolve('test-gcp-id'));
detectDialogflowIntent = require('../detectDialogflowIntent').default;
await sleep(1); // Wait for module initialization (project ID detection)

mockDetectIntent.mockImplementation(() => {
const error = new Error(
`3 INVALID_ARGUMENT: Resource name 'projects/undefined_project_id/agent/sessions/sessionId/' does not match 'projects/*/locations/*/agent/environments/*/users/*/sessions/*'.`
Expand Down
9 changes: 1 addition & 8 deletions src/lib/detectDialogflowIntent.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@
import dialogflow from '@google-cloud/dialogflow';
import crypto from 'crypto';

// const projectId = process.env.DAILOGFLOW_PROJECT_ID;
// const credentials = {
// client_email: process.env.DAILOGFLOW_CLIENT_EMAIL,
// // https://stackoverflow.com/questions/39492587/escaping-issue-with-firebase-privatekey-as-a-heroku-config-variable/41044630#41044630
// private_key: (process.env.DAILOGFLOW_PRIVATE_KEY || '').replace(/\\n/g, '\n'),
// };

// https://googleapis.dev/nodejs/dialogflow/latest/v2beta1.SessionsClient.html
const sessionClient = new dialogflow.SessionsClient(/* { credentials } */);
let projectId = null;
Expand All @@ -20,7 +13,7 @@ sessionClient
projectId = id;
console.log(`[Dialogflow] Connected to project ID = ${id}`);
})
.catch((e) => ('[Dialogflow]', e));
.catch((e) => console.error('[Dialogflow]', e));

export default async function (input) {
if (!projectId) {
Expand Down

0 comments on commit c52c8de

Please sign in to comment.