mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-20 04:29:16 +00:00
fix(cli): ⚡ endflow
This commit is contained in:
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))
|
||||||
|
}
|
||||||
@@ -98,10 +98,15 @@ class CoreClass {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 📄 Finalizar flujo
|
// 📄 Finalizar flujo
|
||||||
const endFlow = async () => {
|
const endFlow = async (message = null) => {
|
||||||
prevMsg = null
|
prevMsg = null
|
||||||
endFlowFlag = true
|
endFlowFlag = true
|
||||||
clearQueue()
|
clearQueue()
|
||||||
|
if (message)
|
||||||
|
this.sendProviderAndSave(from, {
|
||||||
|
...prevMsg,
|
||||||
|
answer: message ?? prevMsg.answer,
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,7 +204,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 +217,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.86-alpha.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "./lib/bundle.bot.cjs",
|
"main": "./lib/bundle.bot.cjs",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
Reference in New Issue
Block a user