extends conditional class

This commit is contained in:
Leifer Mendez
2022-11-09 12:28:20 +01:00
parent 24484015b3
commit 39e2356feb
15 changed files with 137 additions and 75 deletions

View File

@@ -1,77 +1,43 @@
const { test } = require('uvu')
const assert = require('uvu/assert')
/**
* require('@bot-whatsapp')
*/
const { inout, provider, bot } = require('../lib/index.cjs')
/**
* MockDB
*/
class DatabaseClass {
constructor() {}
// const { inout, provider, database, botcore } = require('../lib/index.cjs')
const { inout, provider, database, botcore } = require('../packages/index')
saveLog = (ctx) => {
return ctx
}
const adapterDB = database.create({
engine: 'json', // 'mysql / pg / mongo / json (json-default)',
credentials: {},
})
const adapterProvider = provider.create({
vendor: 'web', //'twilio / web / meta',
credentials: {},
})
const makeFlow = () => {
const flowA = inout
.addKeyword('hola')
.addAnswer('Bienvenido a tu tienda 🥲')
.addAnswer('escribe *catalogo* o *ofertas*')
.toJson()
return [...flowA]
}
const adapterDB = new DatabaseClass()
const adapterFlow = inout.create(makeFlow())
const adapterFlow = new inout.instance(
(() => {
const flowA = inout
.addKeyword('hola')
.addAnswer('Bienvenido a tu tienda 🥲')
.addAnswer('escribe *catalogo* o *ofertas*')
.toJson()
const flowB = inout
.addKeyword(['catalogo', 'ofertas'])
.addAnswer('Este es nuestro CATALOGO mas reciente!', {
buttons: [{ body: 'Xiaomi' }, { body: 'Samsung' }],
})
.toJson()
const flowC = inout
.addKeyword('Xiaomi')
.addAnswer('Estos son nuestro productos XIAOMI ....', {
media: 'https://....',
})
.addAnswer('Si quieres mas info escrbie *info*')
.toJson()
const flowD = inout
.addKeyword('chao!')
.addAnswer('bye!')
.addAnswer('Recuerda que tengo esta promo', {
media: 'https://media2.giphy.com/media/VQJu0IeULuAmCwf5SL/giphy.gif',
})
.toJson()
const flowE = inout
.addKeyword('Modelo C', { sensitive: false })
.addAnswer('100USD', { media: 'http//:...' })
.toJson()
return [...flowA, ...flowB, ...flowC, ...flowC, ...flowD, ...flowE]
})()
)
const adapterProvider = new provider.instance()
test(`[Flow Basico]: BotClass`, () => {
test(`[BotClass]: recibe los mensajes entrantes del provider`, () => {
let messages = []
const flows = adapterFlow
const databases = adapterDB
const providers = adapterProvider
const bot = botcore.create({
flow: adapterFlow,
database: adapterDB,
provider: adapterProvider,
})
const botBasic = new bot.instance(flows, databases, providers)
botBasic.on('message', (ctx) => messages.push(ctx.body))
botBasic.emit('message', { body: 'hola' })
botBasic.emit('message', { body: 'otro' })
bot.on('message', (ctx) => messages.push(ctx.body))
bot.emit('message', { body: 'hola' })
bot.emit('message', { body: 'otro' })
assert.is(messages.join(','), ['hola', 'otro'].join(','))
})