⚗️ Implement bot v2 MVP (#194)

Closes #190
This commit is contained in:
Baptiste Arnaud
2022-12-22 17:02:34 +01:00
committed by GitHub
parent e55823e011
commit 1a3869ae6d
202 changed files with 8060 additions and 1152 deletions

View File

@@ -1,7 +1,8 @@
import { getTestAsset } from '@/test/utils/playwright'
import test, { expect } from '@playwright/test'
import cuid from 'cuid'
import { HttpMethod } from 'models'
import prisma from '@/lib/prisma'
import { HttpMethod, SendMessageInput } from 'models'
import {
createWebhook,
deleteTypebots,
@@ -9,10 +10,14 @@ import {
importTypebotInDatabase,
} from 'utils/playwright/databaseActions'
const typebotId = cuid()
const publicId = `${typebotId}-public`
test.afterEach(async () => {
await deleteWebhooks(['chat-webhook-id'])
await deleteTypebots(['chat-sub-bot'])
})
test.beforeEach(async () => {
test('API chat execution should work on preview bot', async ({ request }) => {
const typebotId = cuid()
const publicId = `${typebotId}-public`
await importTypebotInDatabase(getTestAsset('typebots/chat/main.json'), {
id: typebotId,
publicId,
@@ -26,25 +31,64 @@ test.beforeEach(async () => {
method: HttpMethod.GET,
url: 'https://api.chucknorris.io/jokes/random',
})
await test.step('Start the chat', async () => {
const { sessionId, messages, input, resultId } = await (
await request.post(`/api/v1/sendMessage`, {
data: {
startParams: {
typebotId,
isPreview: true,
},
// TODO: replace with satisfies once compatible with playwright
} as SendMessageInput,
})
).json()
expect(resultId).toBeUndefined()
expect(sessionId).toBeDefined()
expect(messages[0].content.plainText).toBe('Hi there! 👋')
expect(messages[1].content.plainText).toBe("Welcome. What's your name?")
expect(input.type).toBe('text input')
})
})
test.afterEach(async () => {
await deleteWebhooks(['chat-webhook-id'])
await deleteTypebots(['chat-sub-bot'])
})
test('API chat execution should work', async ({ request }) => {
test('API chat execution should work on published bot', async ({ request }) => {
const typebotId = cuid()
const publicId = `${typebotId}-public`
await importTypebotInDatabase(getTestAsset('typebots/chat/main.json'), {
id: typebotId,
publicId,
})
await importTypebotInDatabase(getTestAsset('typebots/chat/linkedBot.json'), {
id: 'chat-sub-bot',
publicId: 'chat-sub-bot-public',
})
await createWebhook(typebotId, {
id: 'chat-webhook-id',
method: HttpMethod.GET,
url: 'https://api.chucknorris.io/jokes/random',
})
let chatSessionId: string
await test.step('Start the chat', async () => {
const { sessionId, messages, input } = await (
await request.post(`/api/v1/typebots/${typebotId}/sendMessage`, {
const { sessionId, messages, input, resultId } = await (
await request.post(`/api/v1/sendMessage`, {
data: {
message: 'Hi',
},
startParams: {
typebotId,
},
// TODO: replace with satisfies once compatible with playwright
} as SendMessageInput,
})
).json()
chatSessionId = sessionId
expect(resultId).toBeDefined()
const result = await prisma.result.findUnique({
where: {
id: resultId,
},
})
expect(result).toBeDefined()
expect(sessionId).toBeDefined()
expect(messages[0].content.plainText).toBe('Hi there! 👋')
expect(messages[1].content.plainText).toBe("Welcome. What's your name?")
@@ -53,7 +97,7 @@ test('API chat execution should work', async ({ request }) => {
await test.step('Answer Name question', async () => {
const { messages, input } = await (
await request.post(`/api/v1/typebots/${typebotId}/sendMessage`, {
await request.post(`/api/v1/sendMessage`, {
data: { message: 'John', sessionId: chatSessionId },
})
).json()
@@ -64,7 +108,7 @@ test('API chat execution should work', async ({ request }) => {
await test.step('Answer Age question', async () => {
const { messages, input } = await (
await request.post(`/api/v1/typebots/${typebotId}/sendMessage`, {
await request.post(`/api/v1/sendMessage`, {
data: { message: '24', sessionId: chatSessionId },
})
).json()
@@ -78,7 +122,7 @@ test('API chat execution should work', async ({ request }) => {
await test.step('Answer Rating question', async () => {
const { messages, input } = await (
await request.post(`/api/v1/typebots/${typebotId}/sendMessage`, {
await request.post(`/api/v1/sendMessage`, {
data: { message: '8', sessionId: chatSessionId },
})
).json()
@@ -90,7 +134,7 @@ test('API chat execution should work', async ({ request }) => {
await test.step('Answer Email question with wrong input', async () => {
const { messages, input } = await (
await request.post(`/api/v1/typebots/${typebotId}/sendMessage`, {
await request.post(`/api/v1/sendMessage`, {
data: { message: 'invalid email', sessionId: chatSessionId },
})
).json()
@@ -102,7 +146,7 @@ test('API chat execution should work', async ({ request }) => {
await test.step('Answer Email question with valid input', async () => {
const { messages, input } = await (
await request.post(`/api/v1/typebots/${typebotId}/sendMessage`, {
await request.post(`/api/v1/sendMessage`, {
data: { message: 'typebot@email.com', sessionId: chatSessionId },
})
).json()
@@ -112,7 +156,7 @@ test('API chat execution should work', async ({ request }) => {
await test.step('Answer URL question', async () => {
const { messages, input } = await (
await request.post(`/api/v1/typebots/${typebotId}/sendMessage`, {
await request.post(`/api/v1/sendMessage`, {
data: { message: 'https://typebot.io', sessionId: chatSessionId },
})
).json()
@@ -122,7 +166,7 @@ test('API chat execution should work', async ({ request }) => {
await test.step('Answer Buttons question with invalid choice', async () => {
const { messages, input } = await (
await request.post(`/api/v1/typebots/${typebotId}/sendMessage`, {
await request.post(`/api/v1/sendMessage`, {
data: { message: 'Yolo', sessionId: chatSessionId },
})
).json()
@@ -134,7 +178,7 @@ test('API chat execution should work', async ({ request }) => {
await test.step('Answer Buttons question with invalid choice', async () => {
const { messages } = await (
await request.post(`/api/v1/typebots/${typebotId}/sendMessage`, {
await request.post(`/api/v1/sendMessage`, {
data: { message: 'Yes', sessionId: chatSessionId },
})
).json()