From d9492eeee6eae94dd1ff5fc5b52a14bb216f523c Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Tue, 8 Nov 2022 15:46:38 +0100 Subject: [PATCH] add class --- __tests__/basic.test.js | 143 +++++++++++++++++++++++++++++++ lib/io/bundle.cjs | 83 +++++++++++++++--- package.json | 1 + packages/io/methods/addAnswer.js | 4 +- 4 files changed, 218 insertions(+), 13 deletions(-) create mode 100644 __tests__/basic.test.js diff --git a/__tests__/basic.test.js b/__tests__/basic.test.js new file mode 100644 index 0000000..650fe3f --- /dev/null +++ b/__tests__/basic.test.js @@ -0,0 +1,143 @@ +const { test } = require('uvu') +const assert = require('uvu/assert') +const { EventEmitter } = require('node:events') +const { addKeyword } = require('../packages/io') +const database = require('mime-db') + +const flow = addKeyword('hola') + .addAnswer('bienvenido') + .addAnswer('chao') + .toJson() + +const provider = { + sendMessage: (ctx) => { + console.log('Enviando...', ctx) + }, +} +//// DataBaseMock --------------------------------------------- +class DataBaseMock { + flow + provider + constructor(_flow, _provider) { + this.flow = _flow + this.provider = _provider + } + + continue = (message, ref = false) => { + let keyRef = ref + let ansRef = null + if (!keyRef) { + keyRef = this.flow.find((n) => n.keyword.includes(message)).ref + } + ansRef = this.flow.find((n) => n.keyword === keyRef) + + if (ansRef) { + this.provider.sendMessage(ansRef.answer) + this.continue(null, ansRef.ref) + } + } +} +//// ProviderMock --------------------------------------------- +class ProviderMock { + constructor() { + //twilio ... + } + + sendMessage = (ctx) => { + console.log('Enviando...', ctx) + } +} + +// const bot = { +// start: ({ flow, database, provider }) => { +// // console.log(database instanceof DataBaseMock) +// const flowCtx = database +// const botEmitter = new MyEmitter() + +// botEmitter.on('message', (ctx) => flowCtx.continue(ctx.body)) +// return botEmitter +// }, +// } + +//// BotMock --------------------------------------------- + +// test(`[Flow Basico]: Saludar y Responder`, () => { +// let messages = [] + +// const botBasic = new BotMock( +// flow, +// new DataBaseMock(flow, provider), +// provider +// ) + +// botBasic.on('message', (ctx) => messages.push(ctx.body)) + +// // Esta linea emula el llegar un mensaje! +// botBasic.emit('message', { body: 'hola' }) + +// assert.is(messages.join(','), 'hola') +// }) + +// test.run() + +/*** + * NEW + */ + +class BotClass extends EventEmitter { + /** + * Emitter para tener on and emit + */ + + flowClass + databaseClass + providerClass + constructor(_flow, _database, _provider) { + super() + this.flowClass = _flow + this.databaseClass = _database + this.providerClass = _provider + } + + continue = () => { + const r = this.flowClass.find() + if (r) { + this.provider.sendMessage(r.answer) + this.continue(null, r.ref) + console.log(r) + } + } +} + +class FlowClass { + flow + constructor(_flow) { + this.flow = _flow + } + + find = (message, ref = false) => { + let keyRef = ref + let ansRef = null + if (!keyRef) { + keyRef = this.flow.find((n) => n.keyword.includes(message)).ref + } + ansRef = this.flow.find((n) => n.keyword === keyRef) + if (ansRef) return ansRef + return false + } +} + +test(`[Flow Basico]: Saludar y Responder`, () => { + let messages = [] + + const botBasic = new BotClass(new FlowClass(flow), null, null) + + botBasic.on('message', (ctx) => messages.push(ctx.body)) + + // Esta linea emula el llegar un mensaje! + botBasic.emit('message', { body: 'hola' }) + + assert.is(messages.join(','), 'hola') +}) + +test.run() diff --git a/lib/io/bundle.cjs b/lib/io/bundle.cjs index 9685dec..3e97d32 100644 --- a/lib/io/bundle.cjs +++ b/lib/io/bundle.cjs @@ -14,17 +14,48 @@ const { generateRef: generateRef$2 } = hash; var utils = { generateRef: generateRef$2 }; -const { generateRef: generateRef$1 } = utils; +const toJson$3 = (inCtx) => () => { + const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx; + return lastCtx.json +}; + +var toJson_1 = { toJson: toJson$3 }; + +const { generateRef: generateRef$1 } = utils; +const { toJson: toJson$2 } = toJson_1; +/** + * + * @param answer string + * @param options {media:string, buttons:[], capture:true default false} + * @returns + */ +const addAnswer$3 = (inCtx) => (answer, options) => { + const getAnswerOptions = () => ({ + media: typeof options?.media === 'string' ? `${options?.media}` : null, + buttons: Array.isArray(options?.buttons) ? options.buttons : [], + capture: + typeof options?.capture === 'boolean' ? options?.capture : false, + }); -const addAnswer$3 = (inCtx) => (message, options) => { const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx; const ctxAnswer = () => { - const ref = generateRef$1(); - /** - * Se guarda en db - */ + const ref = `ans_${generateRef$1()}`; - return { ...lastCtx, ref, message } + const options = { + ...getAnswerOptions(), + keyword: {}, + }; + + const json = [].concat(inCtx.json).concat([ + { + ref, + keyword: lastCtx.ref, + answer, + options, + }, + ]); + + return { ...lastCtx, ref, answer, json, options } }; const ctx = ctxAnswer(); @@ -33,6 +64,7 @@ const addAnswer$3 = (inCtx) => (message, options) => { ctx, ref: ctx.ref, addAnswer: addAnswer$3(ctx), + toJson: toJson$2(ctx), } }; @@ -40,6 +72,7 @@ var addAnswer_1 = { addAnswer: addAnswer$3 }; const { generateRef } = utils; const { addAnswer: addAnswer$2 } = addAnswer_1; +const { toJson: toJson$1 } = toJson_1; /** * addKeyword: * Es necesario que genere id|hash @@ -48,16 +81,40 @@ const { addAnswer: addAnswer$2 } = addAnswer_1; /** * * @param {*} message `string | string[]` - * @param {*} options {sensitivy:boolean} defaulta false + * @param {*} options {sensitive:boolean} default false */ -const addKeyword$2 = (message, options) => { +const addKeyword$2 = (keyword, options) => { + /** + * Esta funcion deberia parsear y validar las opciones + * del keyword + * @returns + */ + const parseOptions = () => { + const defaultProperties = { + sensitive: + typeof options?.sensitive === 'boolean' + ? options?.sensitive + : false, + }; + + return defaultProperties + }; + const ctxAddKeyword = () => { - const ref = generateRef(); + const ref = `key_${generateRef()}`; + const options = parseOptions(); + const json = [ + { + ref, + keyword, + options, + }, + ]; /** * Se guarda en db */ - return { ref, keyword: message } + return { ref, keyword, options, json } }; const ctx = ctxAddKeyword(); @@ -66,6 +123,7 @@ const addKeyword$2 = (message, options) => { ctx, ref: ctx.ref, addAnswer: addAnswer$2(ctx), + toJson: toJson$1(ctx), } }; @@ -73,8 +131,9 @@ var addKeyword_1 = { addKeyword: addKeyword$2 }; const { addAnswer: addAnswer$1 } = addAnswer_1; const { addKeyword: addKeyword$1 } = addKeyword_1; +const { toJson } = toJson_1; -var methods = { addAnswer: addAnswer$1, addKeyword: addKeyword$1 }; +var methods = { addAnswer: addAnswer$1, addKeyword: addKeyword$1, toJson }; const { addKeyword, addAnswer } = methods; var io = { addKeyword, addAnswer }; diff --git a/package.json b/package.json index a083be7..2d2ad2c 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "cli:rollup": "rollup ./packages/cli/index.js --config ./packages/cli/rollup-cli.config.js", "io:rollup": "rollup ./packages/io/index.js --config ./packages/io/rollup-cli.config.js", "test.unit": "node ./node_modules/uvu/bin.js packages test", + "test.e2e": "node ./node_modules/uvu/bin.js __tests__ test", "test.coverage": "node ./node_modules/c8/bin/c8.js --check-coverage --lines=90 npm run test.unit" }, "workspaces": [ diff --git a/packages/io/methods/addAnswer.js b/packages/io/methods/addAnswer.js index 353a094..d383c3c 100644 --- a/packages/io/methods/addAnswer.js +++ b/packages/io/methods/addAnswer.js @@ -3,13 +3,15 @@ const { toJson } = require('./toJson') /** * * @param answer string - * @param options {media:string, buttons:[]} + * @param options {media:string, buttons:[], capture:true default false} * @returns */ const addAnswer = (inCtx) => (answer, options) => { const getAnswerOptions = () => ({ media: typeof options?.media === 'string' ? `${options?.media}` : null, buttons: Array.isArray(options?.buttons) ? options.buttons : [], + capture: + typeof options?.capture === 'boolean' ? options?.capture : false, }) const lastCtx = inCtx.hasOwnProperty('ctx') ? inCtx.ctx : inCtx