Skip to content

Commit

Permalink
refactor: replace execa with execSync in sample tests (#372)
Browse files Browse the repository at this point in the history
* fix #354

* matching execsync sample shared in issue

* dropped pipe option for execSync in examples, unified exec calls, removed unused path requires

* removed unnecessary awaits for exec calls
  • Loading branch information
jbdoster authored and NimJay committed Nov 18, 2022
1 parent 0efd267 commit 1fc42ce
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 78 deletions.
1 change: 0 additions & 1 deletion dialogflow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
},
"devDependencies": {
"chai": "^4.2.0",
"execa": "^1.0.0",
"mocha": "^6.0.0"
}
}
33 changes: 13 additions & 20 deletions dialogflow/system-test/detect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@

const path = require('path');
const {assert} = require('chai');
const execa = require('execa');

const execSync = require('child_process').execSync;
const cmd = 'node detect.js';
const cmd_tts = 'node detect-intent-TTS-response.v2.js';
const cmd_sentiment = 'node detect-intent-sentiment.v2.js';
const cwd = path.join(__dirname, '..');
const projectId =
process.env.GCLOUD_PROJECT || process.env.GOOGLE_CLOUD_PROJECT;
const testQuery = 'Where is my data stored?';
Expand All @@ -31,48 +29,43 @@ const audioFilepathBookARoom = path
.join(__dirname, '../resources/book_a_room.wav')
.replace(/(\s+)/g, '\\$1');

const exec = cmd => execSync(cmd, {encoding: 'utf8'});

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

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

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

it('should detect audio query in streaming fashion', async () => {
const {stdout} = await execa.shell(
`${cmd} stream ${audioFilepathBookARoom} -r 16000`,
{cwd}
);
const stdout = exec(
`${cmd} stream ${audioFilepathBookARoom} -r 16000`);
assert.include(stdout, 'Detected intent');
});

it('should detect Intent with Text to Speech Response', async () => {
const {stdout} = await execa.shell(
`${cmd_tts} ${projectId} 'SESSION_ID' '${testQuery}' 'en-US' './resources/output.wav'`,
{cwd}
);
const stdout = exec(
`${cmd_tts} ${projectId} 'SESSION_ID' '${testQuery}' 'en-US' './resources/output.wav'`);
assert.include(
stdout,
'Audio content written to file: ./resources/output.wav'
);
});

it('should detect sentiment with intent', async () => {
const {stdout} = await execa.shell(
`${cmd_sentiment} ${projectId} 'SESSION_ID' '${testQuery}' 'en-US'`,
{cwd}
const stdout = exec(
`${cmd_sentiment} ${projectId} 'SESSION_ID' '${testQuery}' 'en-US'`
);
assert.include(stdout, 'Detected sentiment');
});
Expand Down
41 changes: 16 additions & 25 deletions dialogflow/system-test/detect.v2beta1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,16 @@

'use strict';

const path = require('path');
const {assert} = require('chai');
const execa = require('execa');
const execSync = require('child_process').execSync;
const uuid = require('uuid/v4');

const cmd = 'node detect.v2beta1.js';
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 exec = async cmd => {
const res = await execa.shell(cmd, {cwd});
if (res.stderr) {
throw new Error(res.stderr);
}
return res.stdout;
};
const exec = cmd => execSync(cmd, {encoding: 'utf8'});

describe('v2beta1 detection', () => {
let knowbaseFullName;
Expand All @@ -42,11 +33,11 @@ describe('v2beta1 detection', () => {

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

// Creates a knowledge base
output = await exec(
output = exec(
`${cmd} createKnowledgeBase -k ${testKnowledgeBaseName}`
);
assert.include(output, `displayName: ${testKnowledgeBaseName}`);
Expand All @@ -62,66 +53,66 @@ describe('v2beta1 detection', () => {
});

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

it('should get a knowledge base', async () => {
const output = await exec(`${cmd} getKnowledgeBase -b "${knowbaseId}"`);
const output = 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(
const output = 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 output = 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(
const output = 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}`);
const output = exec(`${cmd} deleteDocument -d ${documentFullPath}`);
assert.include(output, 'document deleted');
});

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

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

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

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

it('should detect Intent with Text to Speech Response', async () => {
const output = await exec(
const output = exec(
`${cmd} detectIntentwithTexttoSpeechResponse -q "${testQuery}"`
);
assert.include(
Expand All @@ -131,7 +122,7 @@ describe('v2beta1 detection', () => {
});

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

0 comments on commit 1fc42ce

Please sign in to comment.