From 9c36c48e5328a35d533bb01216cbe1aa5bb53b84 Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Thu, 13 Dec 2018 13:58:13 -0800 Subject: [PATCH] refactor: improve the sample tests (#251) --- dialogflow/package.json | 7 +- dialogflow/system-test/detect.test.js | 49 +-- dialogflow/system-test/detect.v2beta1.test.js | 217 +++++------ dialogflow/system-test/resource.test.js | 350 +++++++++--------- 4 files changed, 317 insertions(+), 306 deletions(-) diff --git a/dialogflow/package.json b/dialogflow/package.json index 7e337db2628..9f015d3ed9d 100644 --- a/dialogflow/package.json +++ b/dialogflow/package.json @@ -1,6 +1,5 @@ { "name": "nodejs-docs-samples-conversation", - "version": "0.0.1", "private": true, "license": "Apache-2.0", "author": "Google LLC", @@ -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" } } diff --git a/dialogflow/system-test/detect.test.js b/dialogflow/system-test/detect.test.js index e3806d5e317..817dc28a577 100644 --- a/dialogflow/system-test/detect.test.js +++ b/dialogflow/system-test/detect.test.js @@ -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'); + }); }); diff --git a/dialogflow/system-test/detect.v2beta1.test.js b/dialogflow/system-test/detect.v2beta1.test.js index fec725c2e2e..ef6b1692402 100644 --- a/dialogflow/system-test/detect.v2beta1.test.js +++ b/dialogflow/system-test/detect.v2beta1.test.js @@ -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'); + }); }); diff --git a/dialogflow/system-test/resource.test.js b/dialogflow/system-test/resource.test.js index 5102639a7aa..c8b6531c6b4 100644 --- a/dialogflow/system-test/resource.test.js +++ b/dialogflow/system-test/resource.test.js @@ -16,179 +16,183 @@ 'use strict'; const path = require('path'); -const assert = require('assert'); -const tools = require('@google-cloud/nodejs-repo-tools'); +const {assert} = require('chai'); +const execa = require('execa'); +const uuid = require('uuid'); -const cmd = 'node resource.js'; const cwd = path.join(__dirname, '..'); -const sessionId = require('uuid/v1')(); -const contextName = 'fake_context_name'; -const displayName = 'fake_display_name'; -const entityName = 'fake_entity'; -const synonym1 = 'synonym_1'; -const synonym2 = 'synonym_2'; -const phrase1 = 'training_phrase_1'; -const phrase2 = 'training_phrase_2'; -const message1 = 'message_1'; -const message2 = 'message_2'; - -it('Test creating / listing / deleting a context.', async () => { - let output = await tools.runAsync( - `${cmd} create-context -s ${sessionId} -c ${contextName} -l 3`, - cwd - ); - assert.strictEqual(output.includes(sessionId), true); - assert.strictEqual(output.includes(contextName), true); - - output = await tools.runAsync(`${cmd} list-contexts -s ${sessionId}`, cwd); - assert.strictEqual(output.includes(sessionId), true); - assert.strictEqual(output.includes(contextName), true); - assert.strictEqual(output.includes('3'), true); - - output = await tools.runAsync( - `${cmd} delete-context -s ${sessionId} -c ${contextName}`, - cwd - ); - assert.strictEqual(output.includes(sessionId), true); - assert.strictEqual(output.includes(contextName), true); - - output = await tools.runAsync(`${cmd} list-contexts -s ${sessionId}`, cwd); - assert.strictEqual(output.includes(sessionId), false); - assert.strictEqual(output.includes(contextName), false); -}); - -it('Test creating / listing / deleting a entity type and entity.', async () => { - // Create the Entity Type - let output = await tools.runAsync( - `${cmd} create-entity-type -d ${displayName} -k KIND_MAP`, - cwd - ); - assert.strictEqual(output.includes('entityTypes'), true); - - const entityTypeId = output.split(' ')[1].split('/')[4]; - - // List the Entity Type - output = await tools.runAsync(`${cmd} list-entity-types`, cwd); - assert.strictEqual(output.includes(displayName), true); - assert.strictEqual(output.includes(entityTypeId), true); - - // Create an Entity for the Entity Type - output = await tools.runAsync( - `${cmd} create-entity -e ${entityTypeId} -v ${entityName} -s ${synonym1} -s ${synonym2}`, - cwd - ); - - // List the Entity - output = await tools.runAsync(`${cmd} list-entities -e ${entityTypeId}`, cwd); - assert.strictEqual(output.includes(entityName), true); - assert.strictEqual(output.includes(synonym1), true); - assert.strictEqual(output.includes(synonym2), true); - - // Delete the Entity - output = await tools.runAsync( - `${cmd} delete-entity -e ${entityTypeId} -v ${entityName}`, - cwd - ); - assert.strictEqual(output.includes(entityName), true); - - // Verify the Entity is Deleted - output = await tools.runAsync(`${cmd} list-entities -e ${entityTypeId}`, cwd); - assert.strictEqual(output.includes(entityName), false); - assert.strictEqual(output.includes(synonym1), false); - assert.strictEqual(output.includes(synonym2), false); - - // Delete the Entity Type - output = await tools.runAsync( - `${cmd} delete-entity-type -e ${entityTypeId}`, - cwd - ); - assert.strictEqual(output.includes(entityTypeId), true); - - // Verify the Entity Type is Deleted - output = await tools.runAsync(`${cmd} list-entity-types`, cwd); - assert.strictEqual(output.includes(displayName), false); - assert.strictEqual(output.includes(entityTypeId), false); -}); - -it('Test creating / listing / deleting a intent.', async () => { - let output = await tools.runAsync( - `${cmd} create-intent -d ${displayName} -t ${phrase1} -t ${phrase2} -m ${message1} -m ${message2}`, - cwd - ); - assert.strictEqual(output.includes('intents'), true); - const intentId = output.split(' ')[1].split('/')[4]; - - output = await tools.runAsync(`${cmd} list-intents`, cwd); - assert.strictEqual(output.includes(intentId), true); - assert.strictEqual(output.includes(displayName), true); - - output = await tools.runAsync(`${cmd} delete-intent -i ${intentId}`, cwd); - assert.strictEqual(output.includes(intentId), true); - - output = await tools.runAsync(`${cmd} list-intents`, cwd); - assert.strictEqual(output.includes(intentId), false); - assert.strictEqual(output.includes(displayName), false); -}); - -it('Test creating / listing / deleting a session entity type', async () => { - // Create the Entity Type - let output = await tools.runAsync( - `${cmd} create-entity-type -d ${displayName} -k KIND_MAP`, - cwd - ); - assert.strictEqual(output.includes('entityTypes'), true); - - const entityTypeId = output.split(' ')[1].split('/')[4]; - - // List the Entity Type - output = await tools.runAsync(`${cmd} list-entity-types`, cwd); - assert.strictEqual(output.includes(displayName), true); - assert.strictEqual(output.includes(entityTypeId), true); - - // Create a Session Entity Type - output = await tools.runAsync( - `${cmd} create-session-entity-type -s ${sessionId} -e ${synonym1} -e ${synonym2} -d ${displayName} -o ENTITY_OVERRIDE_MODE_OVERRIDE`, - cwd - ); - assert.strictEqual(output.includes(sessionId), true); - assert.strictEqual(output.includes(displayName), true); - assert.strictEqual(output.includes(synonym1), true); - assert.strictEqual(output.includes(synonym2), true); - - // List the Session Entity Type - output = await tools.runAsync( - `${cmd} list-session-entity-types -s ${sessionId}`, - cwd - ); - assert.strictEqual(output.includes(sessionId), true); - assert.strictEqual(output.includes(displayName), true); - assert.strictEqual(output.includes('2'), true); - - // Delete the Session Entity Type - output = await tools.runAsync( - `${cmd} delete-session-entity-type -s ${sessionId} -d ${displayName}`, - cwd - ); - assert.strictEqual(output.includes(displayName), true); - - // Verify the Session Entity Type is Deleted - output = await tools.runAsync( - `${cmd} list-session-entity-types -s ${sessionId}`, - cwd - ); - assert.strictEqual(output.includes(sessionId), false); - assert.strictEqual(output.includes(displayName), false); - assert.strictEqual(output.includes('2'), false); - - // Delete the Entity Type - output = await tools.runAsync( - `${cmd} delete-entity-type -e ${entityTypeId}`, - cwd - ); - assert.strictEqual(output.includes(entityTypeId), true); - - // Verify the Entity Type is Deleted - output = await tools.runAsync(`${cmd} list-entity-types`, cwd); - assert.strictEqual(output.includes(displayName), false); - assert.strictEqual(output.includes(entityTypeId), false); +const exec = async cmd => { + const {stdout} = await execa.shell(cmd, {cwd}); + return stdout; +}; + +describe('resources', () => { + const cmd = 'node resource.js'; + const sessionId = uuid.v1(); + const contextName = 'fake_context_name'; + const displayName = `fake_display_name_${uuid.v4().split('-')[0]}`; + const entityName = 'fake_entity'; + const synonym1 = 'synonym_1'; + const synonym2 = 'synonym_2'; + const phrase1 = 'training_phrase_1'; + const phrase2 = 'training_phrase_2'; + const message1 = 'message_1'; + const message2 = 'message_2'; + let entityTypeId; + let intentId; + + it('should create a context', async () => { + const output = await exec( + `${cmd} create-context -s ${sessionId} -c ${contextName} -l 3` + ); + assert.include(output, sessionId); + assert.include(output, contextName); + }); + + it('should list contexts', async () => { + const output = await exec(`${cmd} list-contexts -s ${sessionId}`); + assert.include(output, sessionId); + assert.include(output, contextName); + assert.include(output, '3'); + }); + + it('should delete a context', async () => { + let output = await exec( + `${cmd} delete-context -s ${sessionId} -c ${contextName}` + ); + assert.include(output, sessionId); + assert.include(output, contextName); + + output = await exec(`${cmd} list-contexts -s ${sessionId}`); + assert.notInclude(output, sessionId); + assert.notInclude(output, contextName); + }); + + it('should create an entity type and entity', async () => { + const output = await exec( + `${cmd} create-entity-type -d ${displayName} -k KIND_MAP` + ); + assert.include(output, 'entityTypes'); + entityTypeId = output.split(' ')[1].split('/')[4]; + }); + + it('should List the Entity Type', async () => { + const output = await exec(`${cmd} list-entity-types`); + assert.include(output, displayName); + assert.include(output, entityTypeId); + }); + + it('should Create an Entity for the Entity Type', async () => { + await exec( + `${cmd} create-entity -e ${entityTypeId} -v ${entityName} -s ${synonym1} -s ${synonym2}` + ); + }); + + it('should List the Entity', async () => { + const output = await exec(`${cmd} list-entities -e ${entityTypeId}`); + assert.include(output, entityName); + assert.include(output, synonym1); + assert.include(output, synonym2); + }); + + it('should Delete the Entity', async () => { + let output = await exec( + `${cmd} delete-entity -e ${entityTypeId} -v ${entityName}` + ); + assert.include(output, entityName); + + // Verify the Entity is Deleted + output = await exec(`${cmd} list-entities -e ${entityTypeId}`); + assert.notInclude(output, entityName); + assert.notInclude(output, synonym1); + assert.notInclude(output, synonym2); + }); + + it('should Delete the Entity Type', async () => { + let output = await exec(`${cmd} delete-entity-type -e ${entityTypeId}`); + assert.include(output, entityTypeId); + + // Verify the Entity Type is Deleted + output = await exec(`${cmd} list-entity-types`); + assert.notInclude(output, displayName); + assert.notInclude(output, entityTypeId); + }); + + it('should create an intent', async () => { + const output = await exec( + `${cmd} create-intent -d ${displayName} -t ${phrase1} -t ${phrase2} -m ${message1} -m ${message2}` + ); + assert.include(output, 'intents'); + intentId = output.split(' ')[1].split('/')[4]; + }); + + it('should list the intents', async () => { + const output = await exec(`${cmd} list-intents`); + assert.include(output, intentId); + assert.include(output, displayName); + }); + + it('should delete the intent', async () => { + let output = await exec(`${cmd} delete-intent -i ${intentId}`); + assert.include(output, intentId); + output = await exec(`${cmd} list-intents`); + assert.notInclude(output, intentId); + assert.notInclude(output, displayName); + }); + + it('should create a session entity type', async () => { + const output = await exec( + `${cmd} create-entity-type -d ${displayName} -k KIND_MAP` + ); + assert.include(output, 'entityTypes'); + entityTypeId = output.split(' ')[1].split('/')[4]; + }); + + it('should List the Entity Type', async () => { + const output = await exec(`${cmd} list-entity-types`); + assert.include(output, displayName); + assert.include(output, entityTypeId); + }); + + it('should Create a Session Entity Type', async () => { + const output = await exec( + `${cmd} create-session-entity-type -s ${sessionId} -e ${synonym1} -e ${synonym2} -d ${displayName} -o ENTITY_OVERRIDE_MODE_OVERRIDE` + ); + assert.include(output, sessionId); + assert.include(output, displayName); + assert.include(output, synonym1); + assert.include(output, synonym2); + }); + + it('should List the Session Entity Type', async () => { + const output = await exec( + `${cmd} list-session-entity-types -s ${sessionId}` + ); + assert.include(output, sessionId); + assert.include(output, displayName); + assert.include(output, '2'); + }); + + it('should Delete the Session Entity Type', async () => { + let output = await exec( + `${cmd} delete-session-entity-type -s ${sessionId} -d ${displayName}` + ); + assert.include(output, displayName); + + // Verify the Session Entity Type is Deleted + output = await exec(`${cmd} list-session-entity-types -s ${sessionId}`); + assert.notInclude(output, sessionId); + assert.notInclude(output, displayName); + assert.notInclude(output, '2'); + }); + + it('should Delete the Entity Type', async () => { + let output = await exec(`${cmd} delete-entity-type -e ${entityTypeId}`); + assert.include(output, entityTypeId); + + // Verify the Entity Type is Deleted + output = await exec(`${cmd} list-entity-types`); + assert.notInclude(output, displayName); + assert.notInclude(output, entityTypeId); + }); });