mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-20 12:39:24 +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 { test } = require('uvu')
|
||||||
const assert = require('uvu/assert')
|
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 { inout, provider, database, botcore } = require('../packages/index')
|
||||||
|
|
||||||
const adapterDB = database.create({
|
|
||||||
engine: 'mock',
|
|
||||||
credentials: {},
|
|
||||||
})
|
|
||||||
|
|
||||||
const adapterProvider = provider.create({
|
|
||||||
vendor: 'mock',
|
|
||||||
credentials: {},
|
|
||||||
})
|
|
||||||
|
|
||||||
const makeFlow = () => {
|
const makeFlow = () => {
|
||||||
const flowA = inout
|
const flowA = inout
|
||||||
.addKeyword('hola')
|
.addKeyword(['hola', 'ole'])
|
||||||
.addAnswer('Bienvenido a tu tienda 🥲')
|
.addAnswer('Bienvenido a github.com/leifermendez')
|
||||||
.addAnswer('escribe *catalogo* o *ofertas*')
|
.addAnswer('Soy Leifer y tu ?', {
|
||||||
|
capture: true,
|
||||||
|
})
|
||||||
|
.addAnswer('Un gusto saludarte')
|
||||||
.toJson()
|
.toJson()
|
||||||
|
|
||||||
return [...flowA]
|
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 adapterFlow = await inout.create(makeFlow())
|
||||||
const bot = botcore.create({
|
|
||||||
|
const adapterProvider = await provider.create({
|
||||||
|
vendor: 'mock',
|
||||||
|
credentials: {},
|
||||||
|
})
|
||||||
|
const adapterDB = await database.create({
|
||||||
|
engine: 'mock',
|
||||||
|
credentials: {},
|
||||||
|
})
|
||||||
|
|
||||||
|
const bot = await botcore.create({
|
||||||
flow: adapterFlow,
|
flow: adapterFlow,
|
||||||
database: adapterDB,
|
database: adapterDB,
|
||||||
provider: adapterProvider,
|
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()
|
test.run()
|
||||||
|
|||||||
@@ -1,56 +1,54 @@
|
|||||||
const { test } = require('uvu')
|
// const { test } = require('uvu')
|
||||||
const assert = require('uvu/assert')
|
// const assert = require('uvu/assert')
|
||||||
|
|
||||||
// const { inout, provider, database, botcore } = require('../lib/index.cjs')
|
// // const { inout, provider, database, botcore } = require('../lib/index.cjs')
|
||||||
const { inout, provider, database, botcore } = require('../packages/index')
|
// const { inout, provider, database, botcore } = require('../packages/index')
|
||||||
|
|
||||||
const adapterDB = database.create({
|
// const makeFlow = () => {
|
||||||
engine: 'json', // 'mysql / pg / mongo / json (json-default)',
|
// const flowA = inout
|
||||||
credentials: {},
|
// .addKeyword('hola')
|
||||||
})
|
// .addAnswer('Bienvenido a tu tienda 🥲')
|
||||||
|
// .addAnswer('escribe *catalogo* o *ofertas*')
|
||||||
|
// .toJson()
|
||||||
|
|
||||||
const adapterProvider = provider.create({
|
// return [...flowA]
|
||||||
vendor: 'web', //'twilio / web / meta',
|
// }
|
||||||
credentials: {},
|
|
||||||
})
|
|
||||||
|
|
||||||
const makeFlow = () => {
|
// test(`[BotClass]: recibe los mensajes entrantes del provider`, async () => {
|
||||||
const flowA = inout
|
// const adapterFlow = await inout.create(makeFlow())
|
||||||
.addKeyword('hola')
|
// const adapterProvider = await provider.create({
|
||||||
.addAnswer('Bienvenido a tu tienda 🥲')
|
// vendor: 'mock',
|
||||||
.addAnswer('escribe *catalogo* o *ofertas*')
|
// credentials: {},
|
||||||
.toJson()
|
// })
|
||||||
|
// 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`, () => {
|
// bot.on('message', (ctx) => messages.push(ctx))
|
||||||
let messages = []
|
// bot.emit('message', 'hola')
|
||||||
|
// bot.emit('message', 'otro')
|
||||||
|
|
||||||
const bot = botcore.create({
|
// const getHistoryFromDB = adapterDB.engineDB.listHistory
|
||||||
flow: adapterFlow,
|
|
||||||
database: adapterDB,
|
|
||||||
provider: adapterProvider,
|
|
||||||
})
|
|
||||||
|
|
||||||
bot.on('message', (ctx) => messages.push(ctx))
|
// assert.is(messages.join(), ['hola', 'otro'].join())
|
||||||
bot.emit('message', 'hola')
|
// assert.is(
|
||||||
bot.emit('message', 'otro')
|
// getHistoryFromDB.join(),
|
||||||
|
// [
|
||||||
|
// 'hola',
|
||||||
|
// 'Bienvenido a tu tienda 🥲',
|
||||||
|
// 'escribe *catalogo* o *ofertas*',
|
||||||
|
// 'otro',
|
||||||
|
// ].join()
|
||||||
|
// )
|
||||||
|
// })
|
||||||
|
|
||||||
const getHistoryFromDB = adapterDB.engineDB.listHistory
|
// test.run()
|
||||||
|
|
||||||
assert.is(messages.join(), ['hola', 'otro'].join())
|
|
||||||
assert.is(
|
|
||||||
getHistoryFromDB.join(),
|
|
||||||
[
|
|
||||||
'hola',
|
|
||||||
'Bienvenido a tu tienda 🥲',
|
|
||||||
'escribe *catalogo* o *ofertas*',
|
|
||||||
'otro',
|
|
||||||
].join()
|
|
||||||
)
|
|
||||||
})
|
|
||||||
|
|
||||||
test.run()
|
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ class BotClass {
|
|||||||
this.databaseClass = _database
|
this.databaseClass = _database
|
||||||
this.providerClass = _provider
|
this.providerClass = _provider
|
||||||
|
|
||||||
this.providerClass.on('message', (ctxMessage) =>
|
this.providerClass.on('message', ({ message }) =>
|
||||||
this.handleOnMessage(ctxMessage)
|
this.handleOnMessage(message)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ const BotClass = require('./classes/bot.class')
|
|||||||
* @param {*} args
|
* @param {*} args
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
const create = ({ flow, database, provider }) => {
|
const create = async ({ flow, database, provider }) => {
|
||||||
return Object.setPrototypeOf(
|
return Object.setPrototypeOf(
|
||||||
new BotClass(flow, database, provider),
|
new BotClass(flow, database, provider),
|
||||||
provider
|
provider
|
||||||
|
|||||||
@@ -1,10 +1,18 @@
|
|||||||
class DatabaseClass {
|
class DatabaseClass {
|
||||||
|
history = []
|
||||||
engineDB
|
engineDB
|
||||||
constructor(_engineDB) {
|
constructor(_engineDB) {
|
||||||
this.engineDB = _engineDB
|
this.engineDB = _engineDB
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Se debe guardar mensaje numero
|
||||||
|
* @param {*} ctx
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
saveLog = (ctx) => {
|
saveLog = (ctx) => {
|
||||||
|
this.history.pop()
|
||||||
|
this.history.push(ctx)
|
||||||
this.engineDB.save(ctx)
|
this.engineDB.save(ctx)
|
||||||
return ctx
|
return ctx
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ const prepareEngine = ({ engine, credentials }) => {
|
|||||||
* @param {*} args
|
* @param {*} args
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
const create = (args) => {
|
const create = async (args) => {
|
||||||
const engine = prepareEngine(args)
|
const engine = prepareEngine(args)
|
||||||
return new DatabaseClass(engine)
|
return new DatabaseClass(engine)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ const FlowClass = require('./classes/flow.class')
|
|||||||
* @param {*} args
|
* @param {*} args
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
const create = (args) => {
|
const create = async (args) => {
|
||||||
return new FlowClass(args)
|
return new FlowClass(args)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ const prepareVendor = ({ vendor, credentials }) => {
|
|||||||
* @param {*} args
|
* @param {*} args
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
const create = (args) => {
|
const create = async (args) => {
|
||||||
const vendor = prepareVendor(args)
|
const vendor = prepareVendor(args)
|
||||||
return Object.setPrototypeOf(new ProviderClass(), vendor)
|
return Object.setPrototypeOf(new ProviderClass(), vendor)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user