import * as _ from 'lodash'; import * as Promise from 'bluebird'; import * as util from 'util'; import * as uuid from 'uuid'; import * as Responses from './responses'; import { PlatformMiddleware } from '../types/platform'; import { IncomingMessage, Message, TextMessage, GreetingMessage, PostbackMessage } from '../types/message'; import { Button } from '../types/messages/button'; import { User } from '../types/user'; import TestPlatform from './platform'; export default class Tester { public readonly userId: string; private testPlatfom: TestPlatform; private promiseChain: Promise; private currentResolve: () => void; private currentReject: (err?: Error) => void; private currentExpect: Responses.Response = null; private startTest: () => void; private timeoutReject: (err?: Error) => void; private timeout: number = 20; private checkforExtraDialogs: boolean = false; constructor(platform: TestPlatform, userId: string = `test-${_.random(999999)}`) { this.testPlatfom = platform; this.userId = userId; this.promiseChain = new Promise((resolve, reject) => { this.startTest = resolve; }); const greeting: GreetingMessage = { type: 'greeting', id: uuid.v4(), conversation_id: userId, }; this.addSend(greeting); } /** * Add a promise to the chain to expect a response from the bot */ private addRespone(expectChecker: Responses.Response) { const savedThis = this; this.promiseChain = this.promiseChain.then(() => { savedThis.currentExpect = expectChecker; const aPromise = new Promise((resolve, reject) => { savedThis.currentResolve = resolve; savedThis.currentReject = reject; }); return aPromise; }); return this; } /** * Add a promise to chain to send amessage to the bot */ private addSend(message: IncomingMessage) { const savedThis = this; this.promiseChain = this.promiseChain.then(() => { savedThis.testPlatfom.receive(this.userId, message); return null; }); return this; } /** * Wait to recieve a text message from bot */ public expectText(allowedPhrases: Array | string): this { this.addRespone(new Responses.TextResponse(allowedPhrases)); return this; } /** * Wait to recieve a text message from bot */ public expectImage(url: string): this { this.addRespone(new Responses.ImageResponse(url)); return this; } /** * Wait to recieve a set of buttons from bot * @todo create a better inirializer to create button object * @param text Text for the button message to have * @param button Array of raw button strctures */ public expectButtons(text: string, button: Array