Skip to content

Commit

Permalink
feat: implement scene test
Browse files Browse the repository at this point in the history
  • Loading branch information
fletcherist committed Jul 24, 2018
1 parent 6d14ccd commit 0615599
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
8 changes: 4 additions & 4 deletions src/alice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,16 @@ export default class Alice implements IAlice {
*/
if (matchedScene) {
if (await matchedScene.isLeaveCommand(ctxWithMiddlewares)) {
await matchedScene.handleSceneRequest(req, sendResponse, ctxWithMiddlewares, 'leave')
const sceneResponse = await matchedScene.handleSceneRequest(req, sendResponse, ctxWithMiddlewares, 'leave')
session.setData('currentScene', null)
this._handleLeaveScene()
return true
return sceneResponse
} else {
const sceneResponse = await matchedScene.handleSceneRequest(
req, sendResponse, ctxWithMiddlewares,
)
if (sceneResponse) {
return true
return sceneResponse
}
}
}
Expand All @@ -205,7 +205,7 @@ export default class Alice implements IAlice {
req, sendResponse, ctxWithMiddlewares, 'enter',
)
if (sceneResponse) {
return true
return sceneResponse
}
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export default class Context implements IContext {
public sendResponse: (response: WebhookResponse) => void
public enterScene: (sceneName: string) => void
public leaveScene: () => void

private _isReplied: boolean // forbids to send reply twice

constructor(params) {
const {
req,
Expand Down Expand Up @@ -63,6 +66,8 @@ export default class Context implements IContext {
this.replyBuilder = new ReplyBuilder(this.req)
this.buttonBuilder = new ButtonBuilder()

this._isReplied = false

if (enterScene && leaveScene) {
this.enterScene = enterScene
this.leaveScene = leaveScene
Expand All @@ -82,13 +87,13 @@ export default class Context implements IContext {
return null
}

public reply(replyMessage: string | IReply): void {
public reply(replyMessage: string | IReply): WebhookResponse {
if (typeof replyMessage === 'undefined') {
throw new Error('Reply message could not be empty!')
}

const message = this._createReply(replyMessage)
this._sendReply(message)
return this._sendReply(message)
}

public async replyWithImage(params: string | BigImageCard) {
Expand Down Expand Up @@ -143,7 +148,9 @@ export default class Context implements IContext {
return replyMessage
}

private _sendReply(replyMessage: WebhookResponse) {
private _sendReply(replyMessage: WebhookResponse): any {
if (this._isReplied) return
this._isReplied = true
/*
* That fires when listening on port.
*/
Expand Down
50 changes: 47 additions & 3 deletions src/tests/scene.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Alice from '../alice';

const Scene = require('../scene')
import Alice from '../alice'
import Scene from '../scene'
import { generateRequest } from './testUtils'

test('creating scene with name', () => {
const scene = new Scene('testName')
Expand All @@ -18,3 +18,47 @@ test('registering an array of scenes', () => {
expect(alice.scenes.length).toBe(2)
})

test('register scene and enter in', async () => {
const alice = new Alice()
const scene = new Scene('123')
scene.enter('1', ctx => ctx.reply('enter'))
scene.any(ctx => ctx.reply('scene-any'))
scene.command('3', ctx => ctx.reply('command'))
scene.leave('2', ctx => ctx.reply('leave'))

alice.registerScene(scene)
alice.any(ctx => ctx.reply('hi'))

let res
res = await alice.handleRequest(generateRequest('hello'))
expect(res.response.text).toBe('hi')

res = await alice.handleRequest(generateRequest('1'))
expect(res.response.text).toBe('enter')
res = await alice.handleRequest(generateRequest('blablabla'))
expect(res.response.text).toBe('scene-any')

res = await alice.handleRequest(generateRequest('2'))
expect(res.response.text).toBe('leave')
})

// test('changing scene', async () => {
// const alice = new Alice()
// const scene1 = new Scene('scene1')
// const scene2 = new Scene('scene2')
// scene1.enter('keyword', ctx => {
// ctx.reply('test')
// // ctx.leaveScene()
// // ctx.enterScene(scene2)
// })
// scene2.any(ctx => ctx.reply('wazzup'))

// alice.registerScene([scene1, scene2])
// alice.any(ctx => ctx.reply('1'))

// let data
// data = await alice.handleRequest(generateRequest('keyword'))
// expect(data.response.text).toBe('1')
// console.log(data)
// })

0 comments on commit 0615599

Please sign in to comment.