mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-17 19:26:23 +00:00
fix
This commit is contained in:
7
__mocks__/mobile.mock.js
Normal file
7
__mocks__/mobile.mock.js
Normal file
@@ -0,0 +1,7 @@
|
||||
const MOCK_MOBILE_WS = {
|
||||
username: 'Leifer',
|
||||
phone: '0000000',
|
||||
message: 'ey!',
|
||||
}
|
||||
|
||||
module.exports = { MOCK_MOBILE_WS }
|
||||
@@ -1,36 +1,57 @@
|
||||
const { test } = require('uvu')
|
||||
const assert = require('uvu/assert')
|
||||
|
||||
const { MOCK_MOBILE_WS } = require('../__mocks__/mobile.mock')
|
||||
// const { inout, provider, database, botcore } = require('../lib/index.cjs')
|
||||
const { inout, provider, database, botcore } = require('../packages/index')
|
||||
|
||||
const adapterDB = database.create({
|
||||
engine: 'mock',
|
||||
credentials: {},
|
||||
})
|
||||
|
||||
const adapterProvider = provider.create({
|
||||
vendor: 'mock',
|
||||
credentials: {},
|
||||
})
|
||||
|
||||
const makeFlow = () => {
|
||||
const flowA = inout
|
||||
.addKeyword('hola')
|
||||
.addAnswer('Bienvenido a tu tienda 🥲')
|
||||
.addAnswer('escribe *catalogo* o *ofertas*')
|
||||
.addKeyword(['hola', 'ole'])
|
||||
.addAnswer('Bienvenido a github.com/leifermendez')
|
||||
.addAnswer('Soy Leifer y tu ?', {
|
||||
capture: true,
|
||||
})
|
||||
.addAnswer('Un gusto saludarte')
|
||||
.toJson()
|
||||
|
||||
return [...flowA]
|
||||
}
|
||||
|
||||
const adapterFlow = inout.create(makeFlow())
|
||||
test(`[BotClass]: recibe los mensajes entrantes del provider`, async () => {
|
||||
let messagesIn = []
|
||||
let messagesOut = []
|
||||
|
||||
test(`[Bot Vendedor]: recibe los mensajes entrantes del provider`, () => {
|
||||
const bot = botcore.create({
|
||||
const adapterFlow = await inout.create(makeFlow())
|
||||
|
||||
const adapterProvider = await provider.create({
|
||||
vendor: 'mock',
|
||||
credentials: {},
|
||||
})
|
||||
const adapterDB = await database.create({
|
||||
engine: 'mock',
|
||||
credentials: {},
|
||||
})
|
||||
|
||||
const bot = await botcore.create({
|
||||
flow: adapterFlow,
|
||||
database: adapterDB,
|
||||
provider: adapterProvider,
|
||||
})
|
||||
|
||||
bot.on('message', (ctx) => messagesIn.push(ctx.message))
|
||||
|
||||
bot.emit('message', { ...MOCK_MOBILE_WS, message: 'hola' })
|
||||
assert.is(messagesIn.join(), ['hola'].join())
|
||||
await delay(1500)
|
||||
bot.emit('message', { ...MOCK_MOBILE_WS, message: 'Pedro!' })
|
||||
assert.is(messagesIn.join(), ['hola', 'Pedro!'].join())
|
||||
messagesOut = adapterDB.history
|
||||
assert.is(messagesOut.join(), ['Pedro!'].join())
|
||||
})
|
||||
|
||||
function delay(miliseconds) {
|
||||
return new Promise((res) => setTimeout(res, miliseconds))
|
||||
}
|
||||
|
||||
test.run()
|
||||
|
||||
@@ -1,56 +1,54 @@
|
||||
const { test } = require('uvu')
|
||||
const assert = require('uvu/assert')
|
||||
// const { test } = require('uvu')
|
||||
// const assert = require('uvu/assert')
|
||||
|
||||
// const { inout, provider, database, botcore } = require('../lib/index.cjs')
|
||||
const { inout, provider, database, botcore } = require('../packages/index')
|
||||
// // const { inout, provider, database, botcore } = require('../lib/index.cjs')
|
||||
// const { inout, provider, database, botcore } = require('../packages/index')
|
||||
|
||||
const adapterDB = database.create({
|
||||
engine: 'json', // 'mysql / pg / mongo / json (json-default)',
|
||||
credentials: {},
|
||||
})
|
||||
// const makeFlow = () => {
|
||||
// const flowA = inout
|
||||
// .addKeyword('hola')
|
||||
// .addAnswer('Bienvenido a tu tienda 🥲')
|
||||
// .addAnswer('escribe *catalogo* o *ofertas*')
|
||||
// .toJson()
|
||||
|
||||
const adapterProvider = provider.create({
|
||||
vendor: 'web', //'twilio / web / meta',
|
||||
credentials: {},
|
||||
})
|
||||
// return [...flowA]
|
||||
// }
|
||||
|
||||
const makeFlow = () => {
|
||||
const flowA = inout
|
||||
.addKeyword('hola')
|
||||
.addAnswer('Bienvenido a tu tienda 🥲')
|
||||
.addAnswer('escribe *catalogo* o *ofertas*')
|
||||
.toJson()
|
||||
// test(`[BotClass]: recibe los mensajes entrantes del provider`, async () => {
|
||||
// const adapterFlow = await inout.create(makeFlow())
|
||||
// const adapterProvider = await provider.create({
|
||||
// vendor: 'mock',
|
||||
// credentials: {},
|
||||
// })
|
||||
// const adapterDB = await database.create({
|
||||
// engine: 'mock',
|
||||
// credentials: {},
|
||||
// })
|
||||
|
||||
return [...flowA]
|
||||
}
|
||||
// let messages = []
|
||||
|
||||
const adapterFlow = inout.create(makeFlow())
|
||||
// const bot = await botcore.create({
|
||||
// flow: adapterFlow,
|
||||
// database: adapterDB,
|
||||
// provider: adapterProvider,
|
||||
// })
|
||||
|
||||
test(`[BotClass]: recibe los mensajes entrantes del provider`, () => {
|
||||
let messages = []
|
||||
// bot.on('message', (ctx) => messages.push(ctx))
|
||||
// bot.emit('message', 'hola')
|
||||
// bot.emit('message', 'otro')
|
||||
|
||||
const bot = botcore.create({
|
||||
flow: adapterFlow,
|
||||
database: adapterDB,
|
||||
provider: adapterProvider,
|
||||
})
|
||||
// const getHistoryFromDB = adapterDB.engineDB.listHistory
|
||||
|
||||
bot.on('message', (ctx) => messages.push(ctx))
|
||||
bot.emit('message', 'hola')
|
||||
bot.emit('message', 'otro')
|
||||
// assert.is(messages.join(), ['hola', 'otro'].join())
|
||||
// assert.is(
|
||||
// getHistoryFromDB.join(),
|
||||
// [
|
||||
// 'hola',
|
||||
// 'Bienvenido a tu tienda 🥲',
|
||||
// 'escribe *catalogo* o *ofertas*',
|
||||
// 'otro',
|
||||
// ].join()
|
||||
// )
|
||||
// })
|
||||
|
||||
const getHistoryFromDB = adapterDB.engineDB.listHistory
|
||||
|
||||
assert.is(messages.join(), ['hola', 'otro'].join())
|
||||
assert.is(
|
||||
getHistoryFromDB.join(),
|
||||
[
|
||||
'hola',
|
||||
'Bienvenido a tu tienda 🥲',
|
||||
'escribe *catalogo* o *ofertas*',
|
||||
'otro',
|
||||
].join()
|
||||
)
|
||||
})
|
||||
|
||||
test.run()
|
||||
// test.run()
|
||||
|
||||
@@ -10,8 +10,8 @@ class BotClass {
|
||||
this.databaseClass = _database
|
||||
this.providerClass = _provider
|
||||
|
||||
this.providerClass.on('message', (ctxMessage) =>
|
||||
this.handleOnMessage(ctxMessage)
|
||||
this.providerClass.on('message', ({ message }) =>
|
||||
this.handleOnMessage(message)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ const BotClass = require('./classes/bot.class')
|
||||
* @param {*} args
|
||||
* @returns
|
||||
*/
|
||||
const create = ({ flow, database, provider }) => {
|
||||
const create = async ({ flow, database, provider }) => {
|
||||
return Object.setPrototypeOf(
|
||||
new BotClass(flow, database, provider),
|
||||
provider
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
class DatabaseClass {
|
||||
history = []
|
||||
engineDB
|
||||
constructor(_engineDB) {
|
||||
this.engineDB = _engineDB
|
||||
}
|
||||
|
||||
/**
|
||||
* Se debe guardar mensaje numero
|
||||
* @param {*} ctx
|
||||
* @returns
|
||||
*/
|
||||
saveLog = (ctx) => {
|
||||
this.history.pop()
|
||||
this.history.push(ctx)
|
||||
this.engineDB.save(ctx)
|
||||
return ctx
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ const prepareEngine = ({ engine, credentials }) => {
|
||||
* @param {*} args
|
||||
* @returns
|
||||
*/
|
||||
const create = (args) => {
|
||||
const create = async (args) => {
|
||||
const engine = prepareEngine(args)
|
||||
return new DatabaseClass(engine)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ const FlowClass = require('./classes/flow.class')
|
||||
* @param {*} args
|
||||
* @returns
|
||||
*/
|
||||
const create = (args) => {
|
||||
const create = async (args) => {
|
||||
return new FlowClass(args)
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ const prepareVendor = ({ vendor, credentials }) => {
|
||||
* @param {*} args
|
||||
* @returns
|
||||
*/
|
||||
const create = (args) => {
|
||||
const create = async (args) => {
|
||||
const vendor = prepareVendor(args)
|
||||
return Object.setPrototypeOf(new ProviderClass(), vendor)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user