forked from codigoencasa/builderbot
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f201c50
commit 1c66f17
Showing
3 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters