2022-11-29 10:02:40 +01:00
|
|
|
import { getTestAsset } from '@/test/utils/playwright'
|
|
|
|
import test, { expect } from '@playwright/test'
|
2023-02-10 15:06:02 +01:00
|
|
|
import { createId } from '@paralleldrive/cuid2'
|
2022-12-22 17:02:34 +01:00
|
|
|
import prisma from '@/lib/prisma'
|
2023-08-06 10:03:45 +02:00
|
|
|
import { SendMessageInput } from '@typebot.io/schemas'
|
2022-11-29 10:02:40 +01:00
|
|
|
import {
|
|
|
|
createWebhook,
|
|
|
|
deleteTypebots,
|
|
|
|
deleteWebhooks,
|
|
|
|
importTypebotInDatabase,
|
2023-03-15 08:35:16 +01:00
|
|
|
} from '@typebot.io/lib/playwright/databaseActions'
|
2023-08-06 10:03:45 +02:00
|
|
|
import { HttpMethod } from '@typebot.io/schemas/features/blocks/integrations/webhook/enums'
|
2022-11-29 10:02:40 +01:00
|
|
|
|
2022-12-22 17:02:34 +01:00
|
|
|
test.afterEach(async () => {
|
|
|
|
await deleteWebhooks(['chat-webhook-id'])
|
|
|
|
await deleteTypebots(['chat-sub-bot'])
|
|
|
|
})
|
2022-11-29 10:02:40 +01:00
|
|
|
|
2022-12-22 17:02:34 +01:00
|
|
|
test('API chat execution should work on preview bot', async ({ request }) => {
|
2023-02-10 15:06:02 +01:00
|
|
|
const typebotId = createId()
|
2022-12-22 17:02:34 +01:00
|
|
|
const publicId = `${typebotId}-public`
|
2022-11-29 10:02:40 +01:00
|
|
|
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',
|
|
|
|
})
|
|
|
|
|
2022-12-22 17:02:34 +01:00
|
|
|
await test.step('Start the chat', async () => {
|
|
|
|
const { sessionId, messages, input, resultId } = await (
|
|
|
|
await request.post(`/api/v1/sendMessage`, {
|
|
|
|
data: {
|
|
|
|
startParams: {
|
2023-01-16 12:13:21 +01:00
|
|
|
typebot: typebotId,
|
2022-12-22 17:02:34 +01:00
|
|
|
isPreview: true,
|
|
|
|
},
|
2023-01-16 12:13:21 +01:00
|
|
|
} satisfies SendMessageInput,
|
2022-12-22 17:02:34 +01:00
|
|
|
})
|
|
|
|
).json()
|
|
|
|
expect(resultId).toBeUndefined()
|
|
|
|
expect(sessionId).toBeDefined()
|
2023-04-13 17:04:21 +02:00
|
|
|
expect(messages[0].content.richText).toStrictEqual([
|
|
|
|
{ children: [{ text: 'Hi there! 👋' }], type: 'p' },
|
|
|
|
])
|
|
|
|
expect(messages[1].content.richText).toStrictEqual([
|
|
|
|
{ children: [{ text: "Welcome. What's your name?" }], type: 'p' },
|
|
|
|
])
|
2022-12-22 17:02:34 +01:00
|
|
|
expect(input.type).toBe('text input')
|
|
|
|
})
|
2022-11-29 10:02:40 +01:00
|
|
|
})
|
|
|
|
|
2022-12-22 17:02:34 +01:00
|
|
|
test('API chat execution should work on published bot', async ({ request }) => {
|
2023-02-10 15:06:02 +01:00
|
|
|
const typebotId = createId()
|
2022-12-22 17:02:34 +01:00
|
|
|
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',
|
|
|
|
})
|
2022-11-29 10:02:40 +01:00
|
|
|
let chatSessionId: string
|
|
|
|
|
|
|
|
await test.step('Start the chat', async () => {
|
2022-12-22 17:02:34 +01:00
|
|
|
const { sessionId, messages, input, resultId } = await (
|
|
|
|
await request.post(`/api/v1/sendMessage`, {
|
2022-11-29 10:02:40 +01:00
|
|
|
data: {
|
2022-12-22 17:02:34 +01:00
|
|
|
startParams: {
|
2023-01-16 12:13:21 +01:00
|
|
|
typebot: publicId,
|
2022-12-22 17:02:34 +01:00
|
|
|
},
|
2023-01-16 12:13:21 +01:00
|
|
|
} satisfies SendMessageInput,
|
2022-11-29 10:02:40 +01:00
|
|
|
})
|
|
|
|
).json()
|
|
|
|
chatSessionId = sessionId
|
2022-12-22 17:02:34 +01:00
|
|
|
expect(resultId).toBeDefined()
|
|
|
|
const result = await prisma.result.findUnique({
|
|
|
|
where: {
|
|
|
|
id: resultId,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
expect(result).toBeDefined()
|
2022-11-29 10:02:40 +01:00
|
|
|
expect(sessionId).toBeDefined()
|
2023-04-13 17:04:21 +02:00
|
|
|
expect(messages[0].content.richText).toStrictEqual([
|
|
|
|
{ children: [{ text: 'Hi there! 👋' }], type: 'p' },
|
|
|
|
])
|
|
|
|
expect(messages[1].content.richText).toStrictEqual([
|
|
|
|
{ children: [{ text: "Welcome. What's your name?" }], type: 'p' },
|
|
|
|
])
|
2022-11-29 10:02:40 +01:00
|
|
|
expect(input.type).toBe('text input')
|
|
|
|
})
|
|
|
|
|
|
|
|
await test.step('Answer Name question', async () => {
|
|
|
|
const { messages, input } = await (
|
2022-12-22 17:02:34 +01:00
|
|
|
await request.post(`/api/v1/sendMessage`, {
|
2022-11-29 10:02:40 +01:00
|
|
|
data: { message: 'John', sessionId: chatSessionId },
|
|
|
|
})
|
|
|
|
).json()
|
2023-04-13 17:04:21 +02:00
|
|
|
expect(messages[0].content.richText).toStrictEqual([
|
|
|
|
{ children: [{ text: 'Nice to meet you John' }], type: 'p' },
|
|
|
|
])
|
2022-11-29 10:02:40 +01:00
|
|
|
expect(messages[1].content.url).toMatch(new RegExp('giphy.com', 'gm'))
|
|
|
|
expect(input.type).toBe('number input')
|
|
|
|
})
|
|
|
|
|
|
|
|
await test.step('Answer Age question', async () => {
|
|
|
|
const { messages, input } = await (
|
2022-12-22 17:02:34 +01:00
|
|
|
await request.post(`/api/v1/sendMessage`, {
|
2022-11-29 10:02:40 +01:00
|
|
|
data: { message: '24', sessionId: chatSessionId },
|
|
|
|
})
|
|
|
|
).json()
|
2023-04-13 17:04:21 +02:00
|
|
|
expect(messages[0].content.richText).toStrictEqual([
|
|
|
|
{ children: [{ text: 'Ok, you are an adult then 😁' }], type: 'p' },
|
|
|
|
])
|
|
|
|
expect(messages[1].content.richText).toStrictEqual([
|
|
|
|
{ children: [{ text: 'My magic number is 42' }], type: 'p' },
|
|
|
|
])
|
|
|
|
expect(messages[2].content.richText).toStrictEqual([
|
|
|
|
{
|
|
|
|
children: [{ text: 'How would you rate the experience so far?' }],
|
|
|
|
type: 'p',
|
|
|
|
},
|
|
|
|
])
|
2022-11-29 10:02:40 +01:00
|
|
|
expect(input.type).toBe('rating input')
|
|
|
|
})
|
|
|
|
|
|
|
|
await test.step('Answer Rating question', async () => {
|
|
|
|
const { messages, input } = await (
|
2022-12-22 17:02:34 +01:00
|
|
|
await request.post(`/api/v1/sendMessage`, {
|
2022-11-29 10:02:40 +01:00
|
|
|
data: { message: '8', sessionId: chatSessionId },
|
|
|
|
})
|
|
|
|
).json()
|
2023-04-13 17:04:21 +02:00
|
|
|
expect(messages[0].content.richText).toStrictEqual([
|
|
|
|
{
|
|
|
|
children: [{ text: "I'm gonna shoot multiple inputs now..." }],
|
|
|
|
type: 'p',
|
|
|
|
},
|
|
|
|
])
|
2022-11-29 10:02:40 +01:00
|
|
|
expect(input.type).toBe('email input')
|
|
|
|
})
|
|
|
|
|
|
|
|
await test.step('Answer Email question with wrong input', async () => {
|
|
|
|
const { messages, input } = await (
|
2022-12-22 17:02:34 +01:00
|
|
|
await request.post(`/api/v1/sendMessage`, {
|
2022-11-29 10:02:40 +01:00
|
|
|
data: { message: 'invalid email', sessionId: chatSessionId },
|
|
|
|
})
|
|
|
|
).json()
|
2023-04-13 17:04:21 +02:00
|
|
|
expect(messages[0].content.richText).toStrictEqual([
|
|
|
|
{
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
text: "This email doesn't seem to be valid. Can you type it again?",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
type: 'p',
|
|
|
|
},
|
|
|
|
])
|
2022-11-29 10:02:40 +01:00
|
|
|
expect(input.type).toBe('email input')
|
|
|
|
})
|
|
|
|
|
|
|
|
await test.step('Answer Email question with valid input', async () => {
|
|
|
|
const { messages, input } = await (
|
2022-12-22 17:02:34 +01:00
|
|
|
await request.post(`/api/v1/sendMessage`, {
|
2022-11-29 10:02:40 +01:00
|
|
|
data: { message: 'typebot@email.com', sessionId: chatSessionId },
|
|
|
|
})
|
|
|
|
).json()
|
|
|
|
expect(messages.length).toBe(0)
|
|
|
|
expect(input.type).toBe('url input')
|
|
|
|
})
|
|
|
|
|
|
|
|
await test.step('Answer URL question', async () => {
|
|
|
|
const { messages, input } = await (
|
2022-12-22 17:02:34 +01:00
|
|
|
await request.post(`/api/v1/sendMessage`, {
|
2022-11-29 10:02:40 +01:00
|
|
|
data: { message: 'https://typebot.io', sessionId: chatSessionId },
|
|
|
|
})
|
|
|
|
).json()
|
|
|
|
expect(messages.length).toBe(0)
|
|
|
|
expect(input.type).toBe('choice input')
|
|
|
|
})
|
|
|
|
|
|
|
|
await test.step('Answer Buttons question with invalid choice', async () => {
|
|
|
|
const { messages } = await (
|
2022-12-22 17:02:34 +01:00
|
|
|
await request.post(`/api/v1/sendMessage`, {
|
2022-11-29 10:02:40 +01:00
|
|
|
data: { message: 'Yes', sessionId: chatSessionId },
|
|
|
|
})
|
|
|
|
).json()
|
2023-04-13 17:04:21 +02:00
|
|
|
expect(messages[0].content.richText).toStrictEqual([
|
|
|
|
{
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
text: 'Ok, you are solid 👏',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
type: 'p',
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(messages[1].content.richText).toStrictEqual([
|
|
|
|
{
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
text: "Let's trigger a webhook...",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
type: 'p',
|
|
|
|
},
|
|
|
|
])
|
|
|
|
expect(messages[2].content.richText.length).toBeGreaterThan(0)
|
2022-11-29 10:02:40 +01:00
|
|
|
})
|
|
|
|
})
|