-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add get and search task smapi commands
- Loading branch information
Showing
7 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const { AbstractCommand } = require('@src/commands/abstract-command'); | ||
const jsonView = require('@src/view/json-view'); | ||
const Messenger = require('@src/view/messenger'); | ||
const optionModel = require('@src/commands/option-model'); | ||
const profileHelper = require('@src/utils/profile-helper'); | ||
const SmapiClient = require('@src/clients/smapi-client'); | ||
|
||
|
||
class GetTaskCommand extends AbstractCommand { | ||
name() { | ||
return 'get-task'; | ||
} | ||
|
||
description() { | ||
return 'Get the task definition details specified by the taskName and version.'; | ||
} | ||
|
||
requiredOptions() { | ||
return ['skill-id', 'task-name', 'task-version']; | ||
} | ||
|
||
optionalOptions() { | ||
return ['profile', 'debug']; | ||
} | ||
|
||
handle(cmd, cb) { | ||
const { skillId, taskName, taskVersion, profile, debug } = cmd; | ||
const smapiClient = new SmapiClient({ | ||
profile: profileHelper.runtimeProfile(profile), | ||
doDebug: debug | ||
}); | ||
smapiClient.task.getTask(skillId, taskName, taskVersion, (err, result) => { | ||
if (err || result.statusCode >= 400) { | ||
const error = err || jsonView.toString(result.body); | ||
Messenger.getInstance().error(error); | ||
cb(error); | ||
} else { | ||
const res = jsonView.toString(JSON.parse(result.body.definition)); | ||
Messenger.getInstance().info(res); | ||
cb(null, res); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports = GetTaskCommand; | ||
module.exports.createCommand = new GetTaskCommand(optionModel).createCommand(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const R = require('ramda'); | ||
|
||
const { AbstractCommand } = require('@src/commands/abstract-command'); | ||
const jsonView = require('@src/view/json-view'); | ||
const Messenger = require('@src/view/messenger'); | ||
const optionModel = require('@src/commands/option-model'); | ||
const profileHelper = require('@src/utils/profile-helper'); | ||
const SmapiClient = require('@src/clients/smapi-client'); | ||
|
||
|
||
class SearchTaskCommand extends AbstractCommand { | ||
name() { | ||
return 'search-task'; | ||
} | ||
|
||
description() { | ||
return 'List the tasks summary information based on keywords or provider skillId. ' | ||
+ 'If both keywords and provider skillId are not specified, will list all the tasks ' | ||
+ 'summary information accessible by the skillId.'; | ||
} | ||
|
||
requiredOptions() { | ||
return ['skill-id']; | ||
} | ||
|
||
optionalOptions() { | ||
return ['next-token', 'max-results', 'provider-skill-id', 'keywords', 'profile', 'debug']; | ||
} | ||
|
||
static encodeSpaces(keywords) { | ||
return keywords ? keywords.replace(/\s/g, '%20') : keywords; | ||
} | ||
|
||
handle(cmd, cb) { | ||
const { skillId, providerSkillId, maxResults, nextToken, profile, debug } = cmd; | ||
const keywords = SearchTaskCommand.encodeSpaces(cmd.keywords); | ||
const queryParams = R.reject(R.isNil, { maxResults, nextToken }); | ||
|
||
const smapiClient = new SmapiClient({ | ||
profile: profileHelper.runtimeProfile(profile), | ||
doDebug: debug | ||
}); | ||
|
||
smapiClient.task.searchTask(skillId, keywords, providerSkillId, queryParams, (err, result) => { | ||
if (err || result.statusCode >= 400) { | ||
const error = err || jsonView.toString(result.body); | ||
Messenger.getInstance().error(error); | ||
cb(error); | ||
} else { | ||
const res = jsonView.toString(result.body); | ||
Messenger.getInstance().info(res); | ||
cb(null, res); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
|
||
module.exports = SearchTaskCommand; | ||
module.exports.createCommand = new SearchTaskCommand(optionModel).createCommand(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
test/unit/commands/smapi/appended-commands/get-task/index-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const { expect } = require('chai'); | ||
const sinon = require('sinon'); | ||
|
||
const AuthorizationController = require('@src/controllers/authorization-controller'); | ||
const GetTaskCommand = require('@src/commands/smapi/appended-commands/get-task'); | ||
const httpClient = require('@src/clients/http-client'); | ||
const jsonView = require('@src/view/json-view'); | ||
const Messenger = require('@src/view/messenger'); | ||
const optionModel = require('@src/commands/option-model'); | ||
const profileHelper = require('@src/utils/profile-helper'); | ||
|
||
|
||
describe('Command get-task test ', () => { | ||
let infoStub; | ||
let errorStub; | ||
let instance; | ||
const cmdOptions = { | ||
skillId: 'test', | ||
taskName: 'test', | ||
taskVersion: 'test' | ||
}; | ||
|
||
beforeEach(() => { | ||
infoStub = sinon.stub(); | ||
errorStub = sinon.stub(); | ||
sinon.stub(Messenger, 'getInstance').returns({ | ||
info: infoStub, | ||
error: errorStub, | ||
}); | ||
sinon.stub(AuthorizationController.prototype, 'tokenRefreshAndRead').callsArgWith(1); | ||
sinon.stub(profileHelper, 'runtimeProfile').returns('test'); | ||
instance = new GetTaskCommand(optionModel); | ||
}); | ||
|
||
it('| should have options configured', () => { | ||
expect(instance.name()).be.a('string'); | ||
expect(instance.description()).be.a('string'); | ||
expect(instance.requiredOptions()).be.a('array'); | ||
expect(instance.optionalOptions()).be.a('array'); | ||
}); | ||
|
||
it('| should display task definition', (done) => { | ||
const definition = '{ "x":"y", "b": "z"}'; | ||
const expectedOutput = jsonView.toString(JSON.parse(definition)); | ||
sinon.stub(httpClient, 'request').yields(null, { body: { definition }, statusCode: 200 }); | ||
|
||
instance.handle(cmdOptions, () => { | ||
expect(infoStub.calledOnceWith(expectedOutput)).eql(true); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('| should display error thrown by smapi client', (done) => { | ||
const testError = 'testError'; | ||
sinon.stub(httpClient, 'request').yields(new Error(testError)); | ||
|
||
instance.handle(cmdOptions, (err) => { | ||
expect(err.message).eql(testError); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('| should display error thrown by smapi server', (done) => { | ||
const body = { message: 'Bad request.' }; | ||
sinon.stub(httpClient, 'request').yields(null, { body, statusCode: 400 }); | ||
|
||
instance.handle(cmdOptions, (err) => { | ||
expect(err).includes('"message": "Bad request."'); | ||
done(); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
}); |
93 changes: 93 additions & 0 deletions
93
test/unit/commands/smapi/appended-commands/search-task/index-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
const { expect } = require('chai'); | ||
const sinon = require('sinon'); | ||
|
||
const AuthorizationController = require('@src/controllers/authorization-controller'); | ||
const SearchTaskCommand = require('@src/commands/smapi/appended-commands/search-task'); | ||
const httpClient = require('@src/clients/http-client'); | ||
const jsonView = require('@src/view/json-view'); | ||
const Messenger = require('@src/view/messenger'); | ||
const optionModel = require('@src/commands/option-model'); | ||
const profileHelper = require('@src/utils/profile-helper'); | ||
|
||
|
||
describe('Command search-task test ', () => { | ||
let infoStub; | ||
let errorStub; | ||
let instance; | ||
const cmdOptions = { | ||
skillId: 'test', | ||
providerSkillId: 'test', | ||
maxResults: 'test', | ||
nextToken: 'test' | ||
}; | ||
|
||
beforeEach(() => { | ||
infoStub = sinon.stub(); | ||
errorStub = sinon.stub(); | ||
sinon.stub(Messenger, 'getInstance').returns({ | ||
info: infoStub, | ||
error: errorStub, | ||
}); | ||
sinon.stub(AuthorizationController.prototype, 'tokenRefreshAndRead').callsArgWith(1); | ||
sinon.stub(profileHelper, 'runtimeProfile').returns('test'); | ||
instance = new SearchTaskCommand(optionModel); | ||
}); | ||
|
||
it('| should have options configured', () => { | ||
expect(instance.name()).be.a('string'); | ||
expect(instance.description()).be.a('string'); | ||
expect(instance.requiredOptions()).be.a('array'); | ||
expect(instance.optionalOptions()).be.a('array'); | ||
}); | ||
|
||
it('| should display task list', (done) => { | ||
const body = { | ||
taskSummaryList: [ | ||
{ | ||
description: 'y', | ||
name: 'x', | ||
version: '1' | ||
} | ||
], | ||
totalCount: 1 | ||
}; | ||
const expectedOutput = jsonView.toString(body); | ||
sinon.stub(httpClient, 'request').yields(null, { body, statusCode: 200 }); | ||
|
||
instance.handle(cmdOptions, () => { | ||
expect(infoStub.calledOnceWith(expectedOutput)).eql(true); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('| should encode spaces', () => { | ||
const input = 'Test, TestTwo, Three,Four'; | ||
const expected = 'Test,%20TestTwo,%20Three,Four'; | ||
const result = SearchTaskCommand.encodeSpaces(input); | ||
expect(result).eql(expected); | ||
}); | ||
|
||
it('| should display error thrown by smapi client', (done) => { | ||
const testError = 'testError'; | ||
sinon.stub(httpClient, 'request').yields(new Error(testError)); | ||
|
||
instance.handle(cmdOptions, (err) => { | ||
expect(err.message).eql(testError); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('| should display error thrown by smapi server', (done) => { | ||
const body = { message: 'Bad request.' }; | ||
sinon.stub(httpClient, 'request').yields(null, { body, statusCode: 400 }); | ||
|
||
instance.handle(cmdOptions, (err) => { | ||
expect(err).includes('"message": "Bad request."'); | ||
done(); | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
sinon.restore(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters