Skip to content

Commit

Permalink
refactor(samples): convert sample tests from ava to mocha (#222)
Browse files Browse the repository at this point in the history
  • Loading branch information
nareshqlogic authored and NimJay committed Nov 18, 2022
1 parent b371d37 commit c812d09
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 246 deletions.
9 changes: 6 additions & 3 deletions dialogflow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
"license": "Apache-2.0",
"author": "Google LLC",
"repository": "googleapis/nodejs-dialogflow",
"files": [ "*.js", "resources" ],
"files": [
"*.js",
"resources"
],
"engines": {
"node": ">=8.0.0"
},
"scripts": {
"test": "ava system-test"
"test": "mocha system-test/*.test.js --timeout=600000"
},
"dependencies": {
"dialogflow": "^0.7.0",
Expand All @@ -22,6 +25,6 @@
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"ava": "^0.25.0"
"mocha": "^5.2.0"
}
}
3 changes: 3 additions & 0 deletions dialogflow/system-test/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
env:
mocha: true
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,37 @@
'use strict';

const path = require('path');
const test = require('ava');
const assert = require('assert');
const {runAsync} = require('@google-cloud/nodejs-repo-tools');

const cmd = 'node detect.js';
const cwd = path.join(__dirname, '..');
const audioFilepathBookARoom = path
.join(__dirname, `../resources/book_a_room.wav`)
.join(__dirname, '../resources/book_a_room.wav')
.replace(/(\s+)/g, '\\$1');

test('Should detect text queries', async t => {
const output = await runAsync(`${cmd} text -q "hello"`);
t.true(output.includes('Detected intent'));
it('Should detect text queries', async () => {
const output = await runAsync(`${cmd} text -q "hello"`, cwd);
assert.strictEqual(output.includes('Detected intent'), true);
});

test('Should detect event query', async t => {
const output = await runAsync(`${cmd} event WELCOME`);
t.true(output.includes('Query: WELCOME'));
it('Should detect event query', async () => {
const output = await runAsync(`${cmd} event WELCOME`, cwd);
assert.strictEqual(output.includes('Query: WELCOME'), true);
});

test('Should detect audio query', async t => {
it('Should detect audio query', async () => {
const output = await runAsync(
`${cmd} audio ${audioFilepathBookARoom} -r 16000`
`${cmd} audio ${audioFilepathBookARoom} -r 16000`,
cwd
);
t.true(output.includes('Detected intent'));
assert.strictEqual(output.includes('Detected intent'), true);
});

test('Should detect audio query in streaming fashion', async t => {
it('Should detect audio query in streaming fashion', async () => {
const output = await runAsync(
`${cmd} stream ${audioFilepathBookARoom} -r 16000`
`${cmd} stream ${audioFilepathBookARoom} -r 16000`,
cwd
);
t.true(output.includes('Detected intent'));
assert.strictEqual(output.includes('Detected intent'), true);
});
107 changes: 63 additions & 44 deletions dialogflow/system-test/detect.v2beta1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,101 +15,120 @@

'use strict';

const test = require(`ava`);
const path = require('path');
const assert = require('assert');
const {runAsync} = require('@google-cloud/nodejs-repo-tools');
const uuid = require('uuid/v4');

const cmd = 'node detect.v2beta1.js';
const testQuery = `Where is my data stored?`;
const cwd = path.join(__dirname, '..');
const testQuery = 'Where is my data stored?';
const testKnowledgeBaseName = `${uuid().split('-')[0]}-TestKnowledgeBase`;
const testDocName = `TestDoc`;
const testDocumentPath = `https://cloud.google.com/storage/docs/faq`;
const testDocName = 'TestDoc';
const testDocumentPath = 'https://cloud.google.com/storage/docs/faq';

test.serial(`It should create a knowledge base`, async t => {
it('It should create a knowledge base', async () => {
// Check that the knowledge base does not yet exist
let output = await runAsync(`${cmd} listKnowledgeBases`);
t.false(output.includes(testKnowledgeBaseName));
let output = await runAsync(`${cmd} listKnowledgeBases`, cwd);
assert.strictEqual(output.includes(testKnowledgeBaseName), false);

// Creates a knowledge base
output = await runAsync(
`${cmd} createKnowledgeBase -k ${testKnowledgeBaseName}`
`${cmd} createKnowledgeBase -k ${testKnowledgeBaseName}`,
cwd
);
assert.strictEqual(
output.includes(`displayName: ${testKnowledgeBaseName}`),
true
);
t.true(output.includes(`displayName: ${testKnowledgeBaseName}`));
const knowbaseFullName = output
.split(`\n`)[0]
.split(`:`)[1]
.split('\n')[0]
.split(':')[1]
.trim();
const knowbaseId = output
.split(`\n`)[0]
.split(`knowledgeBases/`)[1]
.split('\n')[0]
.split('knowledgeBases/')[1]
.trim();

// List the knowledge base
output = await runAsync(`${cmd} listKnowledgeBases`);
t.true(output.includes(testKnowledgeBaseName));
output = await runAsync(`${cmd} listKnowledgeBases`, cwd);
assert.strictEqual(output.includes(testKnowledgeBaseName), true);

// Get the knowledge base
output = await runAsync(`${cmd} getKnowledgeBase -b "${knowbaseId}"`);
t.true(output.includes(`displayName: ${testKnowledgeBaseName}`));
t.true(output.includes(`name: ${knowbaseFullName}`));
output = await runAsync(`${cmd} getKnowledgeBase -b "${knowbaseId}"`, cwd);
assert.strictEqual(
output.includes(`displayName: ${testKnowledgeBaseName}`),
true
);
assert.strictEqual(output.includes(`name: ${knowbaseFullName}`), true);

// Create a document
output = await runAsync(
`${cmd} createDocument -n "${knowbaseFullName}" -z "${testDocumentPath}" -m "${testDocName}"`
`${cmd} createDocument -n "${knowbaseFullName}" -z "${testDocumentPath}" -m "${testDocName}"`,
cwd
);
t.true(output.includes(`Document created`));
assert.strictEqual(output.includes('Document created'), true);

// List the Document
output = await runAsync(`${cmd} listDocuments -n "${knowbaseFullName}"`);
const parsedOut = output.split(`\n`);
const documentFullPath = parsedOut[parsedOut.length - 1].split(`:`)[1];
t.true(output.includes(`There are 1 documents in ${knowbaseFullName}`));
const parsedOut = output.split('\n');
const documentFullPath = parsedOut[parsedOut.length - 1].split(':')[1];
assert.strictEqual(
output.includes(`There are 1 documents in ${knowbaseFullName}`),
true
);

// Detect intent with Knowledge Base
output = await runAsync(
`${cmd} detectIntentKnowledge -q "${testQuery}" -n "${knowbaseId}"`
`${cmd} detectIntentKnowledge -q "${testQuery}" -n "${knowbaseId}"`,
cwd
);
t.true(output.includes(`Detected Intent:`));
assert.strictEqual(output.includes('Detected Intent:'), true);

// Delete the Document
output = await runAsync(`${cmd} deleteDocument -d ${documentFullPath}`);
t.true(output.includes(`document deleted`));
output = await runAsync(`${cmd} deleteDocument -d ${documentFullPath}`, cwd);
assert.strictEqual(output.includes('document deleted'), true);

// List the Document
output = await runAsync(`${cmd} listDocuments -n "${knowbaseFullName}"`);
t.false(output.includes(documentFullPath));
output = await runAsync(`${cmd} listDocuments -n "${knowbaseFullName}"`, cwd);
assert.strictEqual(output.includes(documentFullPath), false);

// Delete the Knowledge Base
output = await runAsync(
`${cmd} deleteKnowledgeBase -n "${knowbaseFullName}"`
`${cmd} deleteKnowledgeBase -n "${knowbaseFullName}"`,
cwd
);

// List the Knowledge Base
output = await runAsync(`${cmd} listKnowledgeBases`);
t.false(output.includes(testKnowledgeBaseName));
output = await runAsync(`${cmd} listKnowledgeBases`, cwd);
assert.strictEqual(output.includes(testKnowledgeBaseName), false);
});

test(`It should detect Intent with Model Selection`, async t => {
const output = await runAsync(`${cmd} detectIntentwithModelSelection`);
t.true(
it('It should detect Intent with Model Selection', async () => {
const output = await runAsync(`${cmd} detectIntentwithModelSelection`, cwd);
assert.strictEqual(
output.includes(
`Response: I can help with that. Where would you like to reserve a room?`
)
'Response: I can help with that. Where would you like to reserve a room?'
),
true
);
});

test(`It should detect Intent with Text to Speech Response`, async t => {
it('It should detect Intent with Text to Speech Response', async () => {
const output = await runAsync(
`${cmd} detectIntentwithTexttoSpeechResponse -q "${testQuery}"`
`${cmd} detectIntentwithTexttoSpeechResponse -q "${testQuery}"`,
cwd
);
t.true(
output.includes(`Audio content written to file: ./resources/output.wav`)
assert.strictEqual(
output.includes('Audio content written to file: ./resources/output.wav'),
true
);
});

test(`It should detect sentiment with intent`, async t => {
it('It should detect sentiment with intent', async () => {
const output = await runAsync(
`${cmd} detectIntentandSentiment -q "${testQuery}"`
`${cmd} detectIntentandSentiment -q "${testQuery}"`,
cwd
);
t.true(output.includes(`Detected sentiment`));
assert.strictEqual(output.includes('Detected sentiment'), true);
});
Loading

0 comments on commit c812d09

Please sign in to comment.