From 1eda39aa7756d7a8f62050775ded20709ad5faad Mon Sep 17 00:00:00 2001 From: Leifer Mendez Date: Fri, 17 Nov 2023 11:57:59 +0100 Subject: [PATCH] fix: :zap: delay after gotoFlow #877 --- __test__/0.0.2-goto-flow.test.js | 28 ++++++++++++++++++++++++++++ package.json | 2 +- packages/bot/core/core.class.js | 4 ++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/__test__/0.0.2-goto-flow.test.js b/__test__/0.0.2-goto-flow.test.js index d3d7dc73b..c1875dd01 100644 --- a/__test__/0.0.2-goto-flow.test.js +++ b/__test__/0.0.2-goto-flow.test.js @@ -238,4 +238,32 @@ testSuite(`Debe de continuar el el encadenamiento con procesos async`, async ({ assert.is(undefined, history[96]) }) +//Issue https://github.com/codigoencasa/bot-whatsapp/issues/877 +testSuite(`Debe respectar el delay del node previo`, async ({ database, provider }) => { + const flowPing = addKeyword(['hi']).addAction(async (_, { flowDynamic, gotoFlow }) => { + await flowDynamic('Buenas ping debe espera 1segundo') + return gotoFlow(flowBye) + }) + + const flowBye = addKeyword('ping').addAnswer(`Pong con delay 1 segundo`, { delay: 1000 }) + + await createBot({ + database, + flow: createFlow([flowPing, flowBye]), + provider, + }) + + await provider.delaySendMessage(0, 'message', { + from: '000', + body: 'hi', + }) + + await delay(2000) + const history = database.listHistory.map((item) => item.answer) + assert.is('__call_action__', history[0]) + assert.is('Buenas ping debe espera 1segundo', history[1]) + assert.is('Pong con delay 1 segundo', history[2]) + assert.is(undefined, history[3]) +}) + testSuite.run() diff --git a/package.json b/package.json index c93907430..a1a00b790 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "build": "pnpm run cli:rollup && pnpm run bot:rollup && pnpm run provider:rollup && pnpm run database:rollup && pnpm run contexts:rollup && pnpm run create-bot-whatsapp:rollup && pnpm run portal:rollup && pnpm run eslint-plugin:rollup", "copy.lib": "node ./scripts/move.js", "test.unit": "node ./node_modules/uvu/bin.js packages test", - "test.e2e": "node ./node_modules/uvu/bin.js __test__", + "test.e2e": "node ./node_modules/uvu/bin.js __test__ ", "test.coverage": "node ./node_modules/c8/bin/c8.js npm run test.unit", "test": "npm run test.coverage", "cli": "node ./packages/cli/bin/cli.js", diff --git a/packages/bot/core/core.class.js b/packages/bot/core/core.class.js index 5ae068d07..8cf266cdc 100644 --- a/packages/bot/core/core.class.js +++ b/packages/bot/core/core.class.js @@ -292,6 +292,7 @@ class CoreClass extends EventEmitter { async (flowInstance, step = 0) => { const promises = [] flag.gotoFlow = true + if (!flowInstance?.toJson) { printer([ `[POSSIBLE_CIRCULAR_DEPENDENCY]: Se ha detectado una dependencia circular.`, @@ -302,6 +303,8 @@ class CoreClass extends EventEmitter { return } + await delay(flowInstance?.ctx?.options?.delay ?? 0) + const flowTree = flowInstance.toJson() const flowParentId = flowTree[step] @@ -316,6 +319,7 @@ class CoreClass extends EventEmitter { // Enviar el mensaje al proveedor y guardarlo await this.sendProviderAndSave(from, ctxMessage).then(() => promises.push(ctxMessage)) } + await endFlow(flag)(promises) return