diff --git a/common/config/rush/pnpm-lock.yaml b/common/config/rush/pnpm-lock.yaml index d1b0cb31a..197cb851a 100644 --- a/common/config/rush/pnpm-lock.yaml +++ b/common/config/rush/pnpm-lock.yaml @@ -25,6 +25,7 @@ dependencies: applicationinsights: 1.6.0 await-delay: 1.0.0 azure-cognitiveservices-luis-authoring: 2.1.1 + azure-cognitiveservices-luis-runtime: 3.0.0 botframework-schema: 4.6.2 camelcase: 4.1.0 chai: 4.2.0 @@ -347,7 +348,7 @@ packages: /@oclif/plugin-help/2.2.1: dependencies: '@oclif/command': 1.5.19 - chalk: 2.4.2 + chalk: 2.4.1 indent-string: 3.2.0 lodash.template: 4.5.0 string-width: 3.1.0 @@ -452,13 +453,13 @@ packages: integrity: sha512-EaObqwIvayI5a8dCzhFrjKzVwKLxjoG9T6Ppd5CEo07LRKfQ8Yokw54r5+Wq7FaBQ+yXRvQAYPrHwya1/UFt9g== /@types/fs-extra/5.1.0: dependencies: - '@types/node': 11.15.2 + '@types/node': 11.15.3 dev: false resolution: integrity: sha512-AInn5+UBFIK9FK5xc9yP5e3TQSPNNgjHByqYcj9g5elVBnDQcQL7PlO1CIRy2gWlbwK7UPYqi7vRvFA44dCmYQ== /@types/fs-extra/8.0.1: dependencies: - '@types/node': 10.17.5 + '@types/node': 10.17.6 dev: false resolution: integrity: sha512-J00cVDALmi/hJOYsunyT52Hva5TnJeKP5yd1r+mH/ZU0mbYZflR0Z5kw5kITtKTRYMhm1JMClOFYdHnQszEvqw== @@ -466,7 +467,7 @@ packages: dependencies: '@types/events': 3.0.0 '@types/minimatch': 3.0.3 - '@types/node': 12.12.12 + '@types/node': 12.12.14 dev: false resolution: integrity: sha512-1Bh06cbWJUHMC97acuD6UMG29nMt0Aqz1vF3guLfG+kHHJhy3AyohZFFxYk2f7Q1SQIrNwvncxAE0N/9s70F2w== @@ -501,22 +502,22 @@ packages: integrity: sha512-jI/ewavBQ7X5178262JQR0ewicPAcJhXS/iFaNJl0VHLfyosZ/kwSrsa6VNQNSO8i9d8SqdRgOtZSOKJ/+iNMw== /@types/nock/9.3.1: dependencies: - '@types/node': 11.15.2 + '@types/node': 11.15.3 dev: false resolution: integrity: sha512-eOVHXS5RnWOjTVhu3deCM/ruy9E6JCgeix2g7wpFiekQh3AaEAK1cz43tZDukKmtSmQnwvSySq7ubijCA32I7Q== - /@types/node/10.17.5: + /@types/node/10.17.6: dev: false resolution: - integrity: sha512-RElZIr/7JreF1eY6oD5RF3kpmdcreuQPjg5ri4oQ5g9sq7YWU8HkfB3eH8GwAwxf5OaCh0VPi7r4N/yoTGelrA== - /@types/node/11.15.2: + integrity: sha512-0a2X6cgN3RdPBL2MIlR6Lt0KlM7fOFsutuXcdglcOq6WvLnYXgPQSh0Mx6tO1KCAE8MxbHSOSTWDoUxRq+l3DA== + /@types/node/11.15.3: dev: false resolution: - integrity: sha512-BqCU9uIFkUH9Sgo2uLYbmIiFB1T+VBiM8AI/El3LIAI5KzwtckeSG+3WOYZr9aMoX4UIvRFBWBeSaOu6hFue2Q== - /@types/node/12.12.12: + integrity: sha512-5RzvXVietaB8S4dwDjxjltAOHtTO87fiksjqjWGZih97j6KSrdCDaRfmYMNrgrLM87odGBrsTHAl6N3fLraQaw== + /@types/node/12.12.14: dev: false resolution: - integrity: sha512-MGuvYJrPU0HUwqF7LqvIj50RZUX23Z+m583KBygKYUZLlZ88n6w28XRNJRJgsHukLEnLz6w6SvxZoLgbr5wLqQ== + integrity: sha512-u/SJDyXwuihpwjXy7hOOghagLEV1KdAST6syfnOk6QZAMzZuWZqXy5aYYZbh8Jdpd4escVFP0MvftHNDb9pruA== /@types/proxyquire/1.3.28: dev: false resolution: @@ -524,7 +525,7 @@ packages: /@types/rimraf/2.0.3: dependencies: '@types/glob': 7.1.1 - '@types/node': 10.17.5 + '@types/node': 10.17.6 dev: false resolution: integrity: sha512-dZfyfL/u9l/oi984hEXdmAjX3JHry7TLWw43u1HQ8HhPv6KtfxnrZ3T/bleJ0GEvnk9t5sM7eePkgMqz3yBcGg== @@ -542,7 +543,7 @@ packages: integrity: sha512-WxwTXnHTIsk7srax1icjLgX+6w1MUAJbhyCpRP/45paEElsPDQUJZDgr1UpKuL2S3Tb+ZyX9MjWwmcSD4bUoOQ== /@types/xml2js/0.4.5: dependencies: - '@types/node': 12.12.12 + '@types/node': 12.12.14 dev: false resolution: integrity: sha512-yohU3zMn0fkhlape1nxXG2bLEGZRc1FeqF80RoHaYXJN7uibaauXfhzhOJr1Xh36sn+/tx21QAOf07b/xYVk1w== @@ -632,10 +633,10 @@ packages: dev: false resolution: integrity: sha1-+cjBN1fMHde8N5rHeyxipcKGjEA= - /arg/4.1.1: + /arg/4.1.2: dev: false resolution: - integrity: sha512-SlmP3fEA88MBv0PypnXZ8ZfJhwmDeIE3SP71j37AiXQBXYosPV0x6uISAaHYSlSVhmHOVkomen0tbGk6Anlebw== + integrity: sha512-+ytCkGcBtHZ3V2r2Z06AncYO8jz46UEamcspGoU8lHcEbpn6J77QK0vdWvChsclg/tM5XIJC5tnjmPp7Eq6Obg== /argparse/1.0.10: dependencies: sprintf-js: 1.0.3 @@ -772,6 +773,12 @@ packages: requiresBuild: true resolution: integrity: sha512-EL+efZmZIRAKDPkHMW3UzbGnBEyfQOCMBOos/N4+4iOGwh0XVyMbfShcVb9zmxp0kUVwh4MhSFdduLEvFkbNjw== + /azure-cognitiveservices-luis-runtime/3.0.0: + dependencies: + ms-rest: 2.5.3 + dev: false + resolution: + integrity: sha512-S50fyMEeKPjb/YfZDERlFJE8+E/MU6XAnlh33hlC4OivcRF2vHIZhenN6Apq1GWDF2fcn0TK4i7JtrrRHG72bg== /babel-polyfill/6.26.0: dependencies: babel-runtime: 6.26.0 @@ -1482,7 +1489,7 @@ packages: dev: false resolution: integrity: sha512-7dFHNmqeFSEt2ZBsCriorKnn3Z2pj+fd9kmI6QoWw4//DL+icEBfc0U7qJCisqrTsKTjw4fNFy2pW9OqStD84g== - /es-abstract/1.16.0: + /es-abstract/1.16.2: dependencies: es-to-primitive: 1.2.1 function-bind: 1.1.1 @@ -1498,7 +1505,7 @@ packages: engines: node: '>= 0.4' resolution: - integrity: sha512-xdQnfykZ9JMEiasTAJZJdMWCQ1Vm00NBw79/AWi7ELfZuuPCSOMDZbT9mkOfSctVtfhb+sAAzrm+j//GjjLHLg== + integrity: sha512-jYo/J8XU2emLXl3OLwfwtuFfuF2w6DYPs+xy9ZfVyPkDcrauu6LYrw/q2TyCtrbc/KUdCiC5e9UajRhgNkVopA== /es-to-primitive/1.2.1: dependencies: is-callable: 1.1.4 @@ -1630,7 +1637,7 @@ packages: '@types/lodash': 4.14.149 '@types/mocha': 5.2.7 '@types/nock': 11.1.0 - '@types/node': 12.12.12 + '@types/node': 12.12.14 '@types/sinon': 7.5.1 lodash: 4.17.15 mock-stdin: 0.3.1 @@ -2046,7 +2053,7 @@ packages: node: '>=0.4.7' hasBin: true optionalDependencies: - uglify-js: 3.6.9 + uglify-js: 3.7.0 resolution: integrity: sha512-3yPecJoJHK/4c6aZhSvxOyG4vJKDshV36VHp0iVCDVh7o9w2vwi3NSnL2MMPj3YdduqaBcu7cGbggJQM0br9xA== /har-schema/2.0.0: @@ -3186,7 +3193,7 @@ packages: /normalize-package-data/2.5.0: dependencies: hosted-git-info: 2.8.5 - resolve: 1.12.2 + resolve: 1.13.1 semver: 5.7.1 validate-npm-package-license: 3.0.4 dev: false @@ -3309,7 +3316,7 @@ packages: /object.getownpropertydescriptors/2.0.3: dependencies: define-properties: 1.1.3 - es-abstract: 1.16.0 + es-abstract: 1.16.2 dev: false engines: node: '>= 0.8' @@ -3617,7 +3624,7 @@ packages: dependencies: fill-keys: 1.0.2 module-not-found-error: 1.0.1 - resolve: 1.12.2 + resolve: 1.13.1 dev: false resolution: integrity: sha512-BQWfCqYM+QINd+yawJz23tbBM40VIGXOdDw3X344KcclI/gtBbdWF6SlQ4nK/bYhF9d27KYug9WzljHC6B9Ysg== @@ -3685,7 +3692,7 @@ packages: /quibble/0.5.7: dependencies: lodash: 4.17.15 - resolve: 1.12.2 + resolve: 1.13.1 dev: false engines: iojs: '>= 1.0.0' @@ -3894,12 +3901,12 @@ packages: dev: false resolution: integrity: sha1-LGN/53yJOv0qZj/iGqkIAGjiBSo= - /resolve/1.12.2: + /resolve/1.13.1: dependencies: path-parse: 1.0.6 dev: false resolution: - integrity: sha512-cAVTI2VLHWYsGOirfeYVVQ7ZDejtQ9fp4YhYckWDEkFfqbVjaT11iM8k6xSAfGFMM+gDpZjMnFssPu8we+mqFw== + integrity: sha512-CxqObCX8K8YtAhOBRg+lrcdn+LK+WYOS8tSjqSFbjtrI5PnS63QPhZl4+yKfrU9tdsbMu9Anr/amegT87M9Z6w== /responselike/1.0.2: dependencies: lowercase-keys: 1.0.0 @@ -4486,7 +4493,7 @@ packages: integrity: sha512-1m4RA7xVAJrSGrrXGs0L3YTwyvBs2S8PbRHaLZAkFw7JR8oIFwYtysxlBZhYIa7xSyiYJKZ3iGrrk55cGA3i9A== /ts-node/8.5.2_typescript@3.7.2: dependencies: - arg: 4.1.1 + arg: 4.1.2 diff: 4.0.1 make-error: 1.3.5 source-map-support: 0.5.16 @@ -4555,14 +4562,14 @@ packages: dependencies: '@babel/code-frame': 7.5.5 builtin-modules: 1.1.1 - chalk: 2.4.2 + chalk: 2.4.1 commander: 2.20.3 diff: 4.0.1 glob: 7.1.6 js-yaml: 3.13.1 minimatch: 3.0.4 mkdirp: 0.5.1 - resolve: 1.12.2 + resolve: 1.13.1 semver: 5.7.1 tslib: 1.10.0 tsutils: 2.29.0_typescript@3.7.2 @@ -4657,7 +4664,7 @@ packages: hasBin: true resolution: integrity: sha512-ml7V7JfiN2Xwvcer+XAf2csGO1bPBdRbFCkYBczNZggrBZ9c7G3riSUeJmqEU5uOtXNPMhE3n+R4FA/3YOAWOQ== - /uglify-js/3.6.9: + /uglify-js/3.7.0: dependencies: commander: 2.20.3 source-map: 0.6.1 @@ -4667,7 +4674,7 @@ packages: hasBin: true optional: true resolution: - integrity: sha512-pcnnhaoG6RtrvHJ1dFncAe8Od6Nuy30oaJ82ts6//sGSXOP5UjBMEthiProjXmMNHOfd93sqlkztifFMcb+4yw== + integrity: sha512-PC/ee458NEMITe1OufAjal65i6lB58R1HWMRcxwvdz1UopW0DYqlRL3xdu3IcTvTXsB02CRHykidkTRL+A3hQA== /union-value/1.0.1: dependencies: arr-union: 3.1.0 @@ -4994,7 +5001,7 @@ packages: '@oclif/tslint': 3.1.1 '@types/chai': 4.2.5 '@types/mocha': 5.2.7 - '@types/node': 10.17.5 + '@types/node': 10.17.6 '@types/rimraf': 2.0.3 botframework-schema: 4.6.2 chai: 4.2.0 @@ -5038,7 +5045,7 @@ packages: '@types/fs-extra': 5.1.0 '@types/mocha': 5.2.7 '@types/nock': 9.3.1 - '@types/node': 11.15.2 + '@types/node': 11.15.3 '@types/proxyquire': 1.3.28 '@types/sinon': 7.5.1 '@types/supports-color': 5.3.0 @@ -5076,7 +5083,7 @@ packages: '@oclif/tslint': 3.1.1 '@types/chai': 4.2.5 '@types/mocha': 5.2.7 - '@types/node': 10.17.5 + '@types/node': 10.17.6 chai: 4.2.0 cli-ux: 5.3.3 fs-extra: 7.0.1 @@ -5107,7 +5114,7 @@ packages: '@types/fs-extra': 8.0.1 '@types/lru-cache': 5.1.0 '@types/mocha': 5.2.7 - '@types/node': 10.17.5 + '@types/node': 10.17.6 '@types/semver': 6.2.0 '@types/xml2js': 0.4.5 ajv: 6.10.2 @@ -5147,7 +5154,7 @@ packages: '@types/chai': 4.2.5 '@types/lodash': 4.14.149 '@types/mocha': 5.2.7 - '@types/node': 10.17.5 + '@types/node': 10.17.6 antlr4: 4.7.2 chai: 4.2.0 chalk: 2.4.1 @@ -5183,10 +5190,11 @@ packages: '@oclif/tslint': 3.1.1 '@types/chai': 4.2.5 '@types/mocha': 5.2.7 - '@types/node': 10.17.5 + '@types/node': 10.17.6 '@types/rimraf': 2.0.3 '@types/sinon': 7.5.1 azure-cognitiveservices-luis-authoring: 2.1.1 + azure-cognitiveservices-luis-runtime: 3.0.0 chai: 4.2.0 fs-extra: 8.1.0 globby: 10.0.1 @@ -5203,7 +5211,7 @@ packages: dev: false name: '@rush-temp/bf-luis-cli' resolution: - integrity: sha512-sQN7UBvo8aDP1CdRrT2kaBF1idsW34oAEeK/oIddqtf00105sVtJZX9KUqWA3pu4doKSdkJubSqHRt80MEd2pg== + integrity: sha512-E6f+yAl/XwWjr5M5jOm73KV6T//8MkgBnWIqcYPd38zkMNqmrEpGAoCkF7n7VCf0RFAmEEdzR5yzuHEUYrhDJw== tarball: 'file:projects/bf-luis-cli.tgz' version: 0.0.0 'file:projects/bf-qnamaker.tgz': @@ -5218,7 +5226,7 @@ packages: '@types/chai': 4.2.5 '@types/mocha': 5.2.7 '@types/nock': 11.1.0 - '@types/node': 10.17.5 + '@types/node': 10.17.6 await-delay: 1.0.0 camelcase: 4.1.0 chai: 4.2.0 @@ -5264,7 +5272,7 @@ packages: '@types/chai': 4.2.5 '@types/fs-extra': 5.1.0 '@types/mocha': 5.2.7 - '@types/node': 10.17.5 + '@types/node': 10.17.6 chai: 4.2.0 chalk: 2.4.1 cli-ux: 5.3.3 @@ -5313,6 +5321,7 @@ specifiers: applicationinsights: ^1.0.8 await-delay: ^1.0.0 azure-cognitiveservices-luis-authoring: 2.1.1 + azure-cognitiveservices-luis-runtime: ~3.0.0 botframework-schema: ^4.5.1 camelcase: ^4.1.0 chai: ^4.2.0 diff --git a/packages/luis/package.json b/packages/luis/package.json index 2309007b2..f0d1151e5 100644 --- a/packages/luis/package.json +++ b/packages/luis/package.json @@ -2,36 +2,6 @@ "name": "@microsoft/bf-luis-cli", "version": "0.0.0", "bugs": "https://github.com/packages/bf-luis-cli/issues", - "dependencies": { - "@microsoft/bf-cli-command": "1.0.0", - "@oclif/command": "^1.5.19", - "@oclif/config": "^1.13.3", - "@types/sinon": "^7.5.0", - "azure-cognitiveservices-luis-authoring": "2.1.1", - "fs-extra": "^8.1.0", - "ms-rest": "2.5.3", - "tslib": "^1.10.0" - }, - "devDependencies": { - "@oclif/dev-cli": "^1.22.2", - "@oclif/plugin-help": "^2.2.1", - "@oclif/test": "^1.2.5", - "@oclif/tslint": "^3.1.1", - "@types/chai": "^4.2.4", - "@types/mocha": "^5.2.7", - "@types/node": "^10.17.4", - "@types/rimraf": "^2.0.3", - "chai": "^4.2.0", - "globby": "^10.0.1", - "mocha": "^6.2.2", - "nyc": "^14.1.1", - "rimraf": "^3.0.0", - "sinon": "^7.5.0", - "ts-node": "^8.4.1", - "tslint": "^5.20.1", - "typescript": "^3.7.2", - "uuid": "^3.3.3" - }, "engines": { "node": ">=8.0.0" }, @@ -57,13 +27,44 @@ "scripts": { "postpack": "rimraf oclif.manifest.json", "posttest": "tslint -p test -t stylish", - "build":"tsc -b", - "clean":"rimraf ./.nyc_output ./lib ./package-lock.json ./tsconfig.tsbuildinfo", + "build": "tsc -b", + "clean": "rimraf ./.nyc_output ./lib ./package-lock.json ./tsconfig.tsbuildinfo", "test": "mocha", "coverage": "nyc npm run test", - "report:coverage":"npm run coverage && nyc report --reporter=text-lcov | coveralls", + "report:coverage": "npm run coverage && nyc report --reporter=text-lcov | coveralls", "doc": "npm run build && npm run doc:readme && rimraf oclif.manifest.json", - "doc:readme":"oclif-dev manifest && oclif-dev readme", + "doc:readme": "oclif-dev manifest && oclif-dev readme", "version": "npm run doc:readme && git add README.md" + }, + "dependencies": { + "@microsoft/bf-cli-command": "1.0.0", + "@oclif/command": "^1.5.19", + "@oclif/config": "^1.13.3", + "@types/sinon": "^7.5.0", + "azure-cognitiveservices-luis-authoring": "2.1.1", + "azure-cognitiveservices-luis-runtime": "~3.0.0", + "fs-extra": "^8.1.0", + "ms-rest": "2.5.3", + "tslib": "^1.10.0" + }, + "devDependencies": { + "@oclif/dev-cli": "^1.22.2", + "@oclif/plugin-help": "^2.2.1", + "@oclif/test": "^1.2.5", + "@oclif/tslint": "^3.1.1", + "@types/chai": "^4.2.4", + "@types/mocha": "^5.2.7", + "@types/node": "^10.17.4", + "@types/rimraf": "^2.0.3", + "chai": "^4.2.0", + "globby": "^10.0.1", + "mocha": "^6.2.2", + "nyc": "^14.1.1", + "rimraf": "^3.0.0", + "sinon": "^7.5.0", + "ts-node": "^8.4.1", + "tslint": "^5.20.1", + "typescript": "^3.7.2", + "uuid": "^3.3.3" } } diff --git a/packages/luis/src/commands/luis/application/query.ts b/packages/luis/src/commands/luis/application/query.ts new file mode 100644 index 000000000..c3037e078 --- /dev/null +++ b/packages/luis/src/commands/luis/application/query.ts @@ -0,0 +1,76 @@ +/*! + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */ + +import {CLIError, Command, flags} from '@microsoft/bf-cli-command' + +const utils = require('../../../utils/index') + +export default class LuisApplicationQuery extends Command { + static description = 'Queries application for intent predictions' + + static examples = [` + $ bf luis:application:query --endpoint {ENDPOINT} --subscriptionKey {SUBSCRIPTION_KEY} --appId {APP_ID} --query {QUERY} --versionId {INITIAL_VERSION_ID} + `] + + static flags: any = { + help: flags.help({char: 'h'}), + endpoint: flags.string({description: 'LUIS endpoint hostname'}), + subscriptionKey: flags.string({description: 'LUIS cognitive services subscription key (mandatory, default: config:LUIS:subscriptionKey)'}), + appId: flags.string({description: 'LUIS application Id (mandatory, defaults to config:LUIS:appId)'}), + versionId: flags.string({description: 'LUIS application initial version Id'}), + query: flags.string({description: 'Query string to predict (mandatory)'}), + verbose: flags.string({description: 'Returns all intents, otherwise only top scoring intent. (default: false)'}), + timezoneOffset: flags.string({description: 'Timezone offset for the location of the request in minutes (optional)'}), + log: flags.string({description: 'Logs query operation on service (default: true)'}), + } + + async run() { + const {flags} = this.parse(LuisApplicationQuery) + const flagLabels = Object.keys(LuisApplicationQuery.flags) + const configDir = this.config.configDir + + const { + endpoint, + subscriptionKey, + appId, + versionId, + query, + verbose, + timezoneOffset, + log + } = await utils.processInputs(flags, flagLabels, configDir) + + const requiredProps = {endpoint, subscriptionKey, appId, versionId, query} + utils.validateRequiredProps(requiredProps) + + const client = utils.getLUISClient(subscriptionKey, endpoint, true) + const options: any = {} + + if (verbose) { + options.verbose = verbose + } + + if (log) { + options.log = log + } + + const predictionRequestOptions: any = {} + const predictionRequest = { + query, + options: predictionRequestOptions + } + + if (timezoneOffset) { + predictionRequest.options.datetimeReference = timezoneOffset + } + + try { + const predictionData = await client.predictionOperations.getVersionPrediction(appId, versionId, predictionRequest, options) + this.log(`Successfully fetched prediction data ${JSON.stringify(predictionData)}.`) + } catch (err) { + throw new CLIError(`Failed to fetch prediction data: ${err}`) + } + } +} diff --git a/packages/luis/src/utils/index.ts b/packages/luis/src/utils/index.ts index c1d87f6b5..b3e088e0d 100644 --- a/packages/luis/src/utils/index.ts +++ b/packages/luis/src/utils/index.ts @@ -8,6 +8,7 @@ const path = require('path') const fs = require('fs-extra') const msRest = require('ms-rest') const {LUISAuthoringClient} = require('azure-cognitiveservices-luis-authoring') +const {LUISRuntimeClient} = require('azure-cognitiveservices-luis-runtime') const filterConfig = (config: any, prefix: string) => { return Object.keys(config) @@ -35,15 +36,19 @@ const getUserConfig = async (configPath: string) => { } } -const getLUISClient = (subscriptionKey: string, endpoint: string) => { +const getLUISClient = (subscriptionKey: string, endpoint: string, runtime: boolean) => { const token = { inHeader: { 'Ocp-Apim-Subscription-Key': subscriptionKey } } const creds = new msRest.ApiKeyCredentials(token) - const luisClient = new LUISAuthoringClient(creds, endpoint) - luisClient.baseUri = 'https://westus.api.cognitive.microsoft.com/luis/authoring/v3.0-preview/' + const luisClient = runtime ? + new LUISRuntimeClient(creds, endpoint) : + new LUISAuthoringClient(creds, endpoint) + luisClient.baseUri = runtime ? + 'https://westus.api.cognitive.microsoft.com/luis/prediction/v3.0-preview/' : + 'https://westus.api.cognitive.microsoft.com/luis/authoring/v3.0-preview/' return luisClient } diff --git a/packages/luis/test/commands/luis/application/query.test.ts b/packages/luis/test/commands/luis/application/query.test.ts new file mode 100644 index 000000000..58d7d116f --- /dev/null +++ b/packages/luis/test/commands/luis/application/query.test.ts @@ -0,0 +1,70 @@ +import {expect, test} from '@oclif/test' +const sinon = require('sinon') +const uuidv1 = require('uuid/v1') +const utils = require('../../../../src/utils/index') + +describe('luis:application:query', () => { + + before(() => { + const newAppId = uuidv1() + }) + + beforeEach(() => { + sinon.stub(utils, 'processInputs').returnsArg(0) + }) + + afterEach(() => { + sinon.restore(); + }); + + test + .stdout() + .command(['luis:application:query', '--help']) + .it('should print the help contents when --help is passed as an argument', ctx => { + expect(ctx.stdout).to.contain('Queries application for intent predictions') + }) + + test + .stdout() + .stderr() + .command(['luis:application:query', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--appId', uuidv1(), '--subscriptionKey', uuidv1(), '--versionId', '0.1']) + .it('displays an error if any required input parameters are missing', ctx => { + expect(ctx.stderr).to.contain(`Required input property 'query' missing.`) + }) + + test + .stdout() + .stderr() + .command(['luis:application:query', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--subscriptionKey', uuidv1(), '--query', 'test query', '--versionId', '0.1']) + .it('displays an error if any required input parameters are missing', ctx => { + expect(ctx.stderr).to.contain(`Required input property 'appId' missing.`) + }) + + test + .nock('https://westus.api.cognitive.microsoft.com', api => api + .post(uri => uri.includes('apps')) + .reply(200, { + "query": "test intent", + "topScoringIntent": { + "intent": "testIntent", + "score": 0.9987 + } + }) + ) + .stdout() + .stderr() + .command(['luis:application:query', '--endpoint', 'https://westus.api.cognitive.microsoft.com', '--appId', uuidv1(), '--subscriptionKey', uuidv1(), '--query', 'test query', '--versionId', '0.1']) + .it('queries an application for intent predictions and displays the results', ctx => { + expect(ctx.stdout).to.contain('Successfully fetched prediction data') + expect(ctx.stdout).to.contain('test intent') + }) + + test + .stdout() + .stderr() + .command(['luis:application:query', '--endpoint', 'undefined', '--appId', uuidv1(), '--subscriptionKey', uuidv1(), '--query', 'test query', '--versionId', '0.1']) + .it('fails to query app and displays an error message if the endpoint is null', ctx => { + expect(ctx.stderr).to.contain('Failed to fetch prediction data') + }) + +}) \ No newline at end of file