mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-19 12:09:15 +00:00
test: a lot test
This commit is contained in:
@@ -1,24 +1,16 @@
|
||||
const { test } = require('uvu')
|
||||
const { suite } = 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')
|
||||
const { addKeyword, createBot, createFlow } = require('../packages/bot/index')
|
||||
const { setup, clear, delay } = require('../__mocks__/env')
|
||||
|
||||
/**
|
||||
* Falsear peticion async
|
||||
* @param {*} fakeData
|
||||
* @returns
|
||||
*/
|
||||
const fakeHTTP = async (fakeData = []) => {
|
||||
await delay(5)
|
||||
const data = fakeData.map((u, i) => ({ body: `${u}` }))
|
||||
return Promise.resolve(data)
|
||||
}
|
||||
const suiteCase = suite('Flujo: manejo de estado')
|
||||
|
||||
test(`[Caso - 05] Continuar Flujo (continueFlow)`, async () => {
|
||||
const MOCK_VALUES = ['¿CUal es tu email?', 'Continuamos....', '¿Cual es tu edad?']
|
||||
const provider = createProvider(PROVIDER_DB)
|
||||
const database = new MOCK_DB()
|
||||
suiteCase.before.each(setup)
|
||||
suiteCase.after.each(clear)
|
||||
|
||||
suiteCase(`Debe retornar un mensaje resumen`, async ({ database, provider }) => {
|
||||
let STATE_APP = {}
|
||||
const MOCK_VALUES = ['¿Cual es tu nombre?', '¿Cual es tu edad?', 'Tu datos son:']
|
||||
|
||||
const flujoPrincipal = addKeyword(['hola'])
|
||||
.addAnswer(
|
||||
@@ -26,25 +18,27 @@ test(`[Caso - 05] Continuar Flujo (continueFlow)`, async () => {
|
||||
{
|
||||
capture: true,
|
||||
},
|
||||
async (ctx, { flowDynamic, fallBack }) => {
|
||||
const validation = ctx.body.includes('@')
|
||||
async (ctx, { flowDynamic }) => {
|
||||
STATE_APP[ctx.from] = { ...STATE_APP[ctx.from], name: ctx.body }
|
||||
|
||||
if (validation) {
|
||||
const getDataFromApi = await fakeHTTP(['Gracias por tu email se ha validado de manera correcta'])
|
||||
return flowDynamic(getDataFromApi)
|
||||
}
|
||||
return fallBack()
|
||||
flowDynamic('Gracias por tu nombre!')
|
||||
}
|
||||
)
|
||||
.addAnswer(MOCK_VALUES[1])
|
||||
.addAnswer(MOCK_VALUES[2], { capture: true }, async (ctx, { flowDynamic, fallBack }) => {
|
||||
if (ctx.body !== '18') {
|
||||
await delay(20)
|
||||
return fallBack('Ups creo que no eres mayor de edad')
|
||||
.addAnswer(
|
||||
MOCK_VALUES[1],
|
||||
{
|
||||
capture: true,
|
||||
},
|
||||
async (ctx, { flowDynamic }) => {
|
||||
STATE_APP[ctx.from] = { ...STATE_APP[ctx.from], age: ctx.body }
|
||||
|
||||
await flowDynamic(`Gracias por tu edad! ${STATE_APP[ctx.from].name}`)
|
||||
}
|
||||
return flowDynamic('Bien tu edad es correcta!')
|
||||
)
|
||||
.addAnswer(MOCK_VALUES[2], null, async (ctx, { flowDynamic }) => {
|
||||
flowDynamic(`Nombre: ${STATE_APP[ctx.from].name} Edad: ${STATE_APP[ctx.from].age}`)
|
||||
})
|
||||
.addAnswer('Puedes pasar')
|
||||
.addAnswer('🤖🤖 Gracias por tu participacion')
|
||||
|
||||
createBot({
|
||||
database,
|
||||
@@ -57,44 +51,47 @@ test(`[Caso - 05] Continuar Flujo (continueFlow)`, async () => {
|
||||
body: 'hola',
|
||||
})
|
||||
|
||||
provider.delaySendMessage(5, 'message', {
|
||||
from: '001',
|
||||
body: 'hola',
|
||||
})
|
||||
|
||||
provider.delaySendMessage(10, 'message', {
|
||||
from: '000',
|
||||
body: 'this is not email value',
|
||||
body: 'Leifer',
|
||||
})
|
||||
|
||||
provider.delaySendMessage(15, 'message', {
|
||||
from: '000',
|
||||
body: '90',
|
||||
})
|
||||
|
||||
provider.delaySendMessage(20, 'message', {
|
||||
from: '000',
|
||||
body: 'test@test.com',
|
||||
from: '001',
|
||||
body: 'Maria',
|
||||
})
|
||||
|
||||
provider.delaySendMessage(90, 'message', {
|
||||
from: '000',
|
||||
body: '20',
|
||||
provider.delaySendMessage(25, 'message', {
|
||||
from: '001',
|
||||
body: '100',
|
||||
})
|
||||
|
||||
provider.delaySendMessage(200, 'message', {
|
||||
from: '000',
|
||||
body: '18',
|
||||
})
|
||||
|
||||
await delay(1200)
|
||||
await delay(500)
|
||||
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('Gracias por tu email se ha validado de manera correcta', getHistory[4])
|
||||
assert.is(MOCK_VALUES[1], getHistory[5])
|
||||
assert.is(MOCK_VALUES[2], getHistory[6])
|
||||
assert.is('20', getHistory[7])
|
||||
assert.is('Ups creo que no eres mayor de edad', getHistory[8])
|
||||
assert.is('18', getHistory[9])
|
||||
assert.is('Bien tu edad es correcta!', getHistory[10])
|
||||
assert.is('Puedes pasar', getHistory[11])
|
||||
assert.is('¿Cual es tu nombre?', getHistory[1])
|
||||
assert.is('Leifer', getHistory[2])
|
||||
assert.is('Gracias por tu nombre!', getHistory[3])
|
||||
assert.is('¿Cual es tu edad?', getHistory[4])
|
||||
assert.is('90', getHistory[5])
|
||||
assert.is('Gracias por tu edad! Leifer', getHistory[6])
|
||||
assert.is('Tu datos son:', getHistory[7])
|
||||
assert.is('Nombre: Leifer Edad: 90', getHistory[8])
|
||||
assert.is('🤖🤖 Gracias por tu participacion', getHistory[9])
|
||||
assert.is('Maria', getHistory[10])
|
||||
assert.is('Gracias por tu nombre!', getHistory[11])
|
||||
assert.is('100', getHistory[12])
|
||||
assert.is(undefined, getHistory[13])
|
||||
})
|
||||
|
||||
test.run()
|
||||
|
||||
function delay(ms) {
|
||||
return new Promise((res) => setTimeout(res, ms))
|
||||
}
|
||||
suiteCase.run()
|
||||
|
||||
Reference in New Issue
Block a user