mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-21 13:09:16 +00:00
111
__test__/06-case.test.js
Normal file
111
__test__/06-case.test.js
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
const { test } = require('uvu')
|
||||||
|
const assert = require('uvu/assert')
|
||||||
|
const MOCK_DB = require('../packages/database/src/mock')
|
||||||
|
const PROVIDER_DB = require('../packages/provider/src/mock')
|
||||||
|
const {
|
||||||
|
addKeyword,
|
||||||
|
createBot,
|
||||||
|
createFlow,
|
||||||
|
createProvider,
|
||||||
|
} = require('../packages/bot/index')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Falsear peticion async
|
||||||
|
* @param {*} fakeData
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
const fakeHTTP = async (fakeData = []) => {
|
||||||
|
await delay(5)
|
||||||
|
const data = fakeData.map((u, i) => ({ body: `${i + 1} ${u}` }))
|
||||||
|
return Promise.resolve(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
test(`[Caso - 06] Finalizar Flujo (endFlow)`, async () => {
|
||||||
|
const MOCK_VALUES = [
|
||||||
|
'¿CUal es tu email?',
|
||||||
|
'Continuamos....',
|
||||||
|
'¿Cual es tu edad?',
|
||||||
|
]
|
||||||
|
const provider = createProvider(PROVIDER_DB)
|
||||||
|
const database = new MOCK_DB()
|
||||||
|
|
||||||
|
const flujoPrincipal = addKeyword(['hola'])
|
||||||
|
.addAnswer(
|
||||||
|
MOCK_VALUES[0],
|
||||||
|
{
|
||||||
|
capture: true,
|
||||||
|
},
|
||||||
|
async (ctx, { flowDynamic, fallBack }) => {
|
||||||
|
const validation = ctx.body.includes('@')
|
||||||
|
|
||||||
|
if (validation) {
|
||||||
|
const getDataFromApi = await fakeHTTP([
|
||||||
|
'Gracias por tu email se ha validado de manera correcta',
|
||||||
|
])
|
||||||
|
return flowDynamic(getDataFromApi)
|
||||||
|
}
|
||||||
|
return fallBack(validation)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.addAnswer(MOCK_VALUES[1], null, async (_, { endFlow }) => {
|
||||||
|
return endFlow()
|
||||||
|
})
|
||||||
|
.addAnswer(
|
||||||
|
MOCK_VALUES[2],
|
||||||
|
{ capture: true },
|
||||||
|
async (ctx, { flowDynamic, fallBack }) => {
|
||||||
|
if (ctx.body !== '18') {
|
||||||
|
await delay(50)
|
||||||
|
return fallBack(false, 'Ups creo que no eres mayor de edad')
|
||||||
|
}
|
||||||
|
return flowDynamic('Bien tu edad es correcta!')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.addAnswer('Puedes pasar')
|
||||||
|
|
||||||
|
createBot({
|
||||||
|
database,
|
||||||
|
flow: createFlow([flujoPrincipal]),
|
||||||
|
provider,
|
||||||
|
})
|
||||||
|
|
||||||
|
provider.delaySendMessage(0, 'message', {
|
||||||
|
from: '000',
|
||||||
|
body: 'hola',
|
||||||
|
})
|
||||||
|
|
||||||
|
provider.delaySendMessage(10, 'message', {
|
||||||
|
from: '000',
|
||||||
|
body: 'this is not email value',
|
||||||
|
})
|
||||||
|
|
||||||
|
provider.delaySendMessage(20, 'message', {
|
||||||
|
from: '000',
|
||||||
|
body: 'test@test.com',
|
||||||
|
})
|
||||||
|
|
||||||
|
provider.delaySendMessage(90, 'message', {
|
||||||
|
from: '000',
|
||||||
|
body: '20',
|
||||||
|
})
|
||||||
|
|
||||||
|
await delay(1200)
|
||||||
|
const getHistory = database.listHistory.map((i) => i.answer)
|
||||||
|
assert.is(MOCK_VALUES[0], getHistory[0])
|
||||||
|
assert.is('this is not email value', getHistory[1])
|
||||||
|
assert.is(MOCK_VALUES[0], getHistory[2])
|
||||||
|
assert.is('test@test.com', getHistory[3])
|
||||||
|
assert.is(
|
||||||
|
'1 Gracias por tu email se ha validado de manera correcta',
|
||||||
|
getHistory[4]
|
||||||
|
)
|
||||||
|
assert.is(MOCK_VALUES[1], getHistory[5])
|
||||||
|
assert.is('20', getHistory[6])
|
||||||
|
assert.is(undefined, getHistory[7])
|
||||||
|
})
|
||||||
|
|
||||||
|
test.run()
|
||||||
|
|
||||||
|
function delay(ms) {
|
||||||
|
return new Promise((res) => setTimeout(res, ms))
|
||||||
|
}
|
||||||
@@ -91,6 +91,25 @@ class CoreClass {
|
|||||||
this.databaseClass.save(ctxByNumber)
|
this.databaseClass.save(ctxByNumber)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 📄 Crar CTX de mensaje (uso private)
|
||||||
|
const createCtxMessage = (payload = {}, index = 0) => {
|
||||||
|
const body =
|
||||||
|
typeof payload === 'string'
|
||||||
|
? payload
|
||||||
|
: payload?.body ?? payload?.answer
|
||||||
|
const media = payload?.media ?? null
|
||||||
|
const buttons = payload?.buttons ?? []
|
||||||
|
const capture = payload?.capture ?? false
|
||||||
|
|
||||||
|
return toCtx({
|
||||||
|
body,
|
||||||
|
from,
|
||||||
|
keyword: null,
|
||||||
|
index,
|
||||||
|
options: { media, buttons, capture },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// 📄 Limpiar cola de procesos
|
// 📄 Limpiar cola de procesos
|
||||||
const clearQueue = () => {
|
const clearQueue = () => {
|
||||||
QueuePrincipal.pendingPromise = false
|
QueuePrincipal.pendingPromise = false
|
||||||
@@ -98,9 +117,11 @@ class CoreClass {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 📄 Finalizar flujo
|
// 📄 Finalizar flujo
|
||||||
const endFlow = async () => {
|
const endFlow = async (message = null) => {
|
||||||
prevMsg = null
|
prevMsg = null
|
||||||
endFlowFlag = true
|
endFlowFlag = true
|
||||||
|
if (message)
|
||||||
|
this.sendProviderAndSave(from, createCtxMessage(message))
|
||||||
clearQueue()
|
clearQueue()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -144,35 +165,26 @@ class CoreClass {
|
|||||||
if (next) return continueFlow()
|
if (next) return continueFlow()
|
||||||
return this.sendProviderAndSave(from, {
|
return this.sendProviderAndSave(from, {
|
||||||
...prevMsg,
|
...prevMsg,
|
||||||
answer: message ?? prevMsg.answer,
|
answer:
|
||||||
|
typeof message === 'string'
|
||||||
|
? message
|
||||||
|
: message?.body ?? prevMsg.answer,
|
||||||
|
options: {
|
||||||
|
...prevMsg.options,
|
||||||
|
buttons: message?.buttons ?? prevMsg.options?.buttons,
|
||||||
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 📄 [options: flowDynamic]: esta funcion se encarga de responder un array de respuesta esta limitado a 5 mensajes
|
// 📄 [options: flowDynamic]: esta funcion se encarga de responder un array de respuesta esta limitado a 5 mensajes
|
||||||
// para evitar bloque de whatsapp
|
// para evitar bloque de whatsapp
|
||||||
|
|
||||||
const flowDynamic = async (
|
const flowDynamic = async (listMsg = []) => {
|
||||||
listMsg = [],
|
|
||||||
optListMsg = { limit: 5, fallback: false }
|
|
||||||
) => {
|
|
||||||
if (!Array.isArray(listMsg)) listMsg = [listMsg]
|
if (!Array.isArray(listMsg)) listMsg = [listMsg]
|
||||||
|
|
||||||
fallBackFlag = optListMsg.fallback
|
const parseListMsg = listMsg.map((opt, index) =>
|
||||||
const parseListMsg = listMsg
|
createCtxMessage(opt, index)
|
||||||
.map((opt, index) => {
|
)
|
||||||
const body = typeof opt === 'string' ? opt : opt.body
|
|
||||||
const media = opt?.media ?? null
|
|
||||||
const buttons = opt?.buttons ?? []
|
|
||||||
|
|
||||||
return toCtx({
|
|
||||||
body,
|
|
||||||
from,
|
|
||||||
keyword: null,
|
|
||||||
index,
|
|
||||||
options: { media, buttons },
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.slice(0, optListMsg.limit)
|
|
||||||
|
|
||||||
if (endFlowFlag) return
|
if (endFlowFlag) return
|
||||||
for (const msg of parseListMsg) {
|
for (const msg of parseListMsg) {
|
||||||
@@ -199,7 +211,7 @@ class CoreClass {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 📄🤘(tiene return) [options: nested(array)]: Si se tiene flujos hijos los implementa
|
// 📄🤘(tiene return) [options: nested(array)]: Si se tiene flujos hijos los implementa
|
||||||
if (prevMsg?.options?.nested?.length) {
|
if (!endFlowFlag && prevMsg?.options?.nested?.length) {
|
||||||
const nestedRef = prevMsg.options.nested
|
const nestedRef = prevMsg.options.nested
|
||||||
const flowStandalone = nestedRef.map((f) => ({
|
const flowStandalone = nestedRef.map((f) => ({
|
||||||
...nestedRef.find((r) => r.refSerialize === f.refSerialize),
|
...nestedRef.find((r) => r.refSerialize === f.refSerialize),
|
||||||
@@ -212,7 +224,7 @@ class CoreClass {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 📄🤘(tiene return) Si el mensaje previo implementa capture
|
// 📄🤘(tiene return) Si el mensaje previo implementa capture
|
||||||
if (!prevMsg?.options?.nested?.length) {
|
if (!endFlowFlag && !prevMsg?.options?.nested?.length) {
|
||||||
const typeCapture = typeof prevMsg?.options?.capture
|
const typeCapture = typeof prevMsg?.options?.capture
|
||||||
|
|
||||||
if (typeCapture === 'boolean' && fallBackFlag) {
|
if (typeCapture === 'boolean' && fallBackFlag) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@bot-whatsapp/bot",
|
"name": "@bot-whatsapp/bot",
|
||||||
"version": "0.0.84-alpha.0",
|
"version": "0.0.91-alpha.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "./lib/bundle.bot.cjs",
|
"main": "./lib/bundle.bot.cjs",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user