2023-04-17 16:47:17 +02:00
|
|
|
import { AbTestBlock, SessionState } from '@typebot.io/schemas'
|
2023-09-20 15:26:52 +02:00
|
|
|
import { ExecuteLogicResponse } from '../../../types'
|
2023-11-08 15:34:16 +01:00
|
|
|
import { defaultAbTestOptions } from '@typebot.io/schemas/features/blocks/logic/abTest/constants'
|
2023-04-17 16:47:17 +02:00
|
|
|
|
|
|
|
|
export const executeAbTest = (
|
|
|
|
|
_: SessionState,
|
|
|
|
|
block: AbTestBlock
|
|
|
|
|
): ExecuteLogicResponse => {
|
|
|
|
|
const aEdgeId = block.items[0].outgoingEdgeId
|
|
|
|
|
const random = Math.random() * 100
|
2023-11-08 15:34:16 +01:00
|
|
|
if (
|
|
|
|
|
random < (block.options?.aPercent ?? defaultAbTestOptions.aPercent) &&
|
|
|
|
|
aEdgeId
|
|
|
|
|
) {
|
2023-04-17 16:47:17 +02:00
|
|
|
return { outgoingEdgeId: aEdgeId }
|
|
|
|
|
}
|
|
|
|
|
const bEdgeId = block.items[1].outgoingEdgeId
|
|
|
|
|
if (bEdgeId) return { outgoingEdgeId: bEdgeId }
|
|
|
|
|
return { outgoingEdgeId: block.outgoingEdgeId }
|
|
|
|
|
}
|