Skip to content

Commit

Permalink
refactor: improve the sample tests (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored and NimJay committed Nov 18, 2022
1 parent 0717fae commit 6c1fcd8
Show file tree
Hide file tree
Showing 4 changed files with 317 additions and 306 deletions.
7 changes: 3 additions & 4 deletions dialogflow/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"name": "nodejs-docs-samples-conversation",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google LLC",
Expand All @@ -13,18 +12,18 @@
"node": ">=8.0.0"
},
"scripts": {
"test": "mocha system-test/*.test.js --timeout=600000"
"test": "mocha system-test --timeout=600000"
},
"dependencies": {
"dialogflow": "^0.7.0",
"pump": "^3.0.0",
"through2": "^3.0.0",
"prompt": "^1.0.0",
"yargs": "^12.0.1",
"uuid": "^3.3.2"
},
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"chai": "^4.2.0",
"execa": "^1.0.0",
"mocha": "^5.2.0"
}
}
49 changes: 26 additions & 23 deletions dialogflow/system-test/detect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,40 @@
'use strict';

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

const cmd = 'node detect.js';
const cwd = path.join(__dirname, '..');

const audioFilepathBookARoom = path
.join(__dirname, '../resources/book_a_room.wav')
.replace(/(\s+)/g, '\\$1');

it('Should detect text queries', async () => {
const output = await runAsync(`${cmd} text -q "hello"`, cwd);
assert.strictEqual(output.includes('Detected intent'), true);
});
describe('basic detection', () => {
it('should detect text queries', async () => {
const {stdout} = await execa.shell(`${cmd} text -q "hello"`, {cwd});
assert.include(stdout, 'Detected intent');
});

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

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

it('Should detect audio query in streaming fashion', async () => {
const output = await runAsync(
`${cmd} stream ${audioFilepathBookARoom} -r 16000`,
cwd
);
assert.strictEqual(output.includes('Detected intent'), true);
it('should detect audio query in streaming fashion', async () => {
const {stdout} = await execa.shell(
`${cmd} stream ${audioFilepathBookARoom} -r 16000`,
{cwd}
);
assert.include(stdout, 'Detected intent');
});
});
217 changes: 111 additions & 106 deletions dialogflow/system-test/detect.v2beta1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,119 +16,124 @@
'use strict';

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

const cmd = 'node detect.v2beta1.js';
const cwd = path.join(__dirname, '..');
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';

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

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

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

// Get the knowledge base
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}"`,
cwd
);
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];
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}"`,
cwd
);
assert.strictEqual(output.includes('Detected Intent:'), true);

// Delete the Document
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}"`, cwd);
assert.strictEqual(output.includes(documentFullPath), false);

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

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

it('It should detect Intent with Model Selection', async () => {
const output = await runAsync(`${cmd} detectIntentwithModelSelection`, cwd);
assert.strictEqual(
output.includes(
const exec = async cmd => {
const res = await execa.shell(cmd, {cwd});
if (res.stderr) {
throw new Error(res.stderr);
}
return res.stdout;
};

describe('v2beta1 detection', () => {
let knowbaseFullName;
let knowbaseId;
let documentFullPath;

it('should create a knowledge base', async () => {
// Check that the knowledge base does not yet exist
let output = await exec(`${cmd} listKnowledgeBases`);
assert.notInclude(output, testKnowledgeBaseName);

// Creates a knowledge base
output = await exec(
`${cmd} createKnowledgeBase -k ${testKnowledgeBaseName}`
);
assert.include(output, `displayName: ${testKnowledgeBaseName}`);

knowbaseFullName = output
.split('\n')[0]
.split(':')[1]
.trim();
knowbaseId = output
.split('\n')[0]
.split('knowledgeBases/')[1]
.trim();
});

it('should list the knowledge bases', async () => {
const output = await exec(`${cmd} listKnowledgeBases`);
assert.include(output, testKnowledgeBaseName);
});

it('should get a knowledge base', async () => {
const output = await exec(`${cmd} getKnowledgeBase -b "${knowbaseId}"`);
assert.include(output, `displayName: ${testKnowledgeBaseName}`);
assert.include(output, `name: ${knowbaseFullName}`);
});

it('should create a document', async () => {
const output = await exec(
`${cmd} createDocument -n "${knowbaseFullName}" -z "${testDocumentPath}" -m "${testDocName}"`
);
assert.include(output, 'Document created');
});

it('should list documents', async () => {
const output = await exec(`${cmd} listDocuments -n "${knowbaseFullName}"`);
const parsedOut = output.split('\n');
documentFullPath = parsedOut[parsedOut.length - 1].split(':')[1];
assert.include(output, `There are 1 documents in ${knowbaseFullName}`);
});

it('should detect intent with a knowledge base', async () => {
const output = await exec(
`${cmd} detectIntentKnowledge -q "${testQuery}" -n "${knowbaseId}"`
);
assert.include(output, 'Detected Intent:');
});

it('should delete a document', async () => {
const output = await exec(`${cmd} deleteDocument -d ${documentFullPath}`);
assert.include(output, 'document deleted');
});

it('should list the document', async () => {
const output = await exec(`${cmd} listDocuments -n "${knowbaseFullName}"`);
assert.notInclude(output, documentFullPath);
});

it('should delete the Knowledge Base', async () => {
await exec(`${cmd} deleteKnowledgeBase -n "${knowbaseFullName}"`);
});

it('should list the Knowledge Base', async () => {
const output = await exec(`${cmd} listKnowledgeBases`);
assert.notInclude(output, testKnowledgeBaseName);
});

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

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

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

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

it('should detect sentiment with intent', async () => {
const output = await exec(
`${cmd} detectIntentandSentiment -q "${testQuery}"`
);
assert.include(output, 'Detected sentiment');
});
});
Loading

0 comments on commit 6c1fcd8

Please sign in to comment.