♻️ Re-organize workspace folders
This commit is contained in:
51
packages/lib/playwright/baseConfig.ts
Normal file
51
packages/lib/playwright/baseConfig.ts
Normal file
@ -0,0 +1,51 @@
|
||||
import { PlaywrightTestConfig } from '@playwright/test'
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
|
||||
const builderLocalEnvPath = path.join(
|
||||
__dirname,
|
||||
'../../../apps/builder/.env.local'
|
||||
)
|
||||
const localViewerEnvPath = path.join(
|
||||
__dirname,
|
||||
'../../../apps/viewer/.env.local'
|
||||
)
|
||||
if (fs.existsSync(builderLocalEnvPath))
|
||||
require('dotenv').config({
|
||||
path: builderLocalEnvPath,
|
||||
})
|
||||
|
||||
if (fs.existsSync(localViewerEnvPath))
|
||||
require('dotenv').config({
|
||||
path: localViewerEnvPath,
|
||||
})
|
||||
|
||||
export const playwrightBaseConfig: PlaywrightTestConfig = {
|
||||
globalSetup: require.resolve(path.join(__dirname, 'globalSetup')),
|
||||
timeout: process.env.CI ? 50 * 1000 : 40 * 1000,
|
||||
expect: {
|
||||
timeout: process.env.CI ? 10 * 1000 : 5 * 1000,
|
||||
},
|
||||
retries: process.env.NO_RETRIES ? 0 : 1,
|
||||
workers: process.env.CI ? 2 : 3,
|
||||
reporter: [
|
||||
[process.env.CI ? 'github' : 'list'],
|
||||
['html', { outputFolder: 'src/test/reporters' }],
|
||||
],
|
||||
maxFailures: process.env.CI ? 10 : undefined,
|
||||
webServer: process.env.CI
|
||||
? {
|
||||
command: 'pnpm run start',
|
||||
timeout: 60_000,
|
||||
reuseExistingServer: true,
|
||||
}
|
||||
: undefined,
|
||||
outputDir: './src/test/results',
|
||||
use: {
|
||||
trace: 'on-first-retry',
|
||||
video: 'retain-on-failure',
|
||||
locale: 'en-US',
|
||||
browserName: 'chromium',
|
||||
viewport: { width: 1400, height: 1000 },
|
||||
},
|
||||
}
|
231
packages/lib/playwright/databaseActions.ts
Normal file
231
packages/lib/playwright/databaseActions.ts
Normal file
@ -0,0 +1,231 @@
|
||||
import {
|
||||
Plan,
|
||||
Prisma,
|
||||
PrismaClient,
|
||||
User,
|
||||
Workspace,
|
||||
WorkspaceRole,
|
||||
} from '@typebot.io/prisma'
|
||||
import { createId } from '@paralleldrive/cuid2'
|
||||
import { Typebot, Webhook } from '@typebot.io/schemas'
|
||||
import { readFileSync } from 'fs'
|
||||
import { proWorkspaceId, userId } from './databaseSetup'
|
||||
import {
|
||||
parseTestTypebot,
|
||||
parseTypebotToPublicTypebot,
|
||||
} from './databaseHelpers'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
type CreateFakeResultsProps = {
|
||||
typebotId: string
|
||||
count: number
|
||||
customResultIdPrefix?: string
|
||||
isChronological?: boolean
|
||||
fakeStorage?: number
|
||||
}
|
||||
|
||||
export const injectFakeResults = async ({
|
||||
count,
|
||||
customResultIdPrefix,
|
||||
typebotId,
|
||||
isChronological,
|
||||
fakeStorage,
|
||||
}: CreateFakeResultsProps) => {
|
||||
const resultIdPrefix = customResultIdPrefix ?? createId()
|
||||
await prisma.result.createMany({
|
||||
data: [
|
||||
...Array.from(Array(count)).map((_, idx) => {
|
||||
const today = new Date()
|
||||
const rand = Math.random()
|
||||
return {
|
||||
id: `${resultIdPrefix}-result${idx}`,
|
||||
typebotId,
|
||||
createdAt: isChronological
|
||||
? new Date(
|
||||
today.setTime(today.getTime() + 1000 * 60 * 60 * 24 * idx)
|
||||
)
|
||||
: new Date(),
|
||||
isCompleted: rand > 0.5,
|
||||
hasStarted: true,
|
||||
variables: [],
|
||||
} satisfies Prisma.ResultCreateManyInput
|
||||
}),
|
||||
],
|
||||
})
|
||||
return createAnswers({ fakeStorage, resultIdPrefix, count })
|
||||
}
|
||||
|
||||
const createAnswers = ({
|
||||
count,
|
||||
resultIdPrefix,
|
||||
fakeStorage,
|
||||
}: { resultIdPrefix: string } & Pick<
|
||||
CreateFakeResultsProps,
|
||||
'fakeStorage' | 'count'
|
||||
>) => {
|
||||
return prisma.answer.createMany({
|
||||
data: [
|
||||
...Array.from(Array(count)).map((_, idx) => ({
|
||||
resultId: `${resultIdPrefix}-result${idx}`,
|
||||
content: `content${idx}`,
|
||||
blockId: 'block1',
|
||||
groupId: 'group1',
|
||||
storageUsed: fakeStorage ? Math.round(fakeStorage / count) : null,
|
||||
})),
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
export const importTypebotInDatabase = async (
|
||||
path: string,
|
||||
updates?: Partial<Typebot>
|
||||
) => {
|
||||
const typebot: Typebot = {
|
||||
...JSON.parse(readFileSync(path).toString()),
|
||||
workspaceId: proWorkspaceId,
|
||||
...updates,
|
||||
version: '3',
|
||||
}
|
||||
await prisma.typebot.create({
|
||||
data: parseCreateTypebot(typebot),
|
||||
})
|
||||
return prisma.publicTypebot.create({
|
||||
data: parseTypebotToPublicTypebot(
|
||||
updates?.id ? `${updates?.id}-public` : 'publicBot',
|
||||
typebot
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
export const deleteWorkspaces = async (workspaceIds: string[]) => {
|
||||
await prisma.workspace.deleteMany({
|
||||
where: { id: { in: workspaceIds } },
|
||||
})
|
||||
}
|
||||
|
||||
export const deleteTypebots = async (typebotIds: string[]) => {
|
||||
await prisma.typebot.deleteMany({
|
||||
where: { id: { in: typebotIds } },
|
||||
})
|
||||
}
|
||||
|
||||
export const deleteCredentials = async (credentialIds: string[]) => {
|
||||
await prisma.credentials.deleteMany({
|
||||
where: { id: { in: credentialIds } },
|
||||
})
|
||||
}
|
||||
|
||||
export const deleteWebhooks = async (webhookIds: string[]) => {
|
||||
await prisma.webhook.deleteMany({
|
||||
where: { id: { in: webhookIds } },
|
||||
})
|
||||
}
|
||||
|
||||
export const createWorkspaces = async (workspaces: Partial<Workspace>[]) => {
|
||||
const workspaceIds = workspaces.map((workspace) => workspace.id ?? createId())
|
||||
await prisma.workspace.createMany({
|
||||
data: workspaces.map((workspace, index) => ({
|
||||
id: workspaceIds[index],
|
||||
name: 'Free workspace',
|
||||
plan: Plan.FREE,
|
||||
...workspace,
|
||||
})),
|
||||
})
|
||||
await prisma.memberInWorkspace.createMany({
|
||||
data: workspaces.map((_, index) => ({
|
||||
userId,
|
||||
workspaceId: workspaceIds[index],
|
||||
role: WorkspaceRole.ADMIN,
|
||||
})),
|
||||
})
|
||||
return workspaceIds
|
||||
}
|
||||
|
||||
export const updateUser = (data: Partial<User>) =>
|
||||
prisma.user.update({
|
||||
data: {
|
||||
...data,
|
||||
onboardingCategories: data.onboardingCategories ?? [],
|
||||
},
|
||||
where: {
|
||||
id: userId,
|
||||
},
|
||||
})
|
||||
|
||||
export const createWebhook = async (
|
||||
typebotId: string,
|
||||
webhookProps?: Partial<Webhook>
|
||||
) => {
|
||||
try {
|
||||
await prisma.webhook.delete({ where: { id: 'webhook1' } })
|
||||
} catch {}
|
||||
return prisma.webhook.create({
|
||||
data: {
|
||||
method: 'GET',
|
||||
typebotId,
|
||||
id: 'webhook1',
|
||||
...webhookProps,
|
||||
queryParams: webhookProps?.queryParams ?? [],
|
||||
headers: webhookProps?.headers ?? [],
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export const createTypebots = async (partialTypebots: Partial<Typebot>[]) => {
|
||||
const typebotsWithId = partialTypebots.map((typebot) => {
|
||||
const typebotId = typebot.id ?? createId()
|
||||
return {
|
||||
...typebot,
|
||||
id: typebotId,
|
||||
publicId: typebot.publicId ?? typebotId + '-public',
|
||||
}
|
||||
})
|
||||
await prisma.typebot.createMany({
|
||||
data: typebotsWithId.map(parseTestTypebot).map(parseCreateTypebot),
|
||||
})
|
||||
return prisma.publicTypebot.createMany({
|
||||
data: typebotsWithId.map((t) =>
|
||||
parseTypebotToPublicTypebot(t.publicId, parseTestTypebot(t))
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
export const updateTypebot = async (
|
||||
partialTypebot: Partial<Typebot> & { id: string }
|
||||
) => {
|
||||
await prisma.typebot.updateMany({
|
||||
where: { id: partialTypebot.id },
|
||||
data: parseUpdateTypebot(partialTypebot),
|
||||
})
|
||||
return prisma.publicTypebot.updateMany({
|
||||
where: { typebotId: partialTypebot.id },
|
||||
data: partialTypebot,
|
||||
})
|
||||
}
|
||||
|
||||
export const updateWorkspace = async (
|
||||
id: string,
|
||||
data: Prisma.WorkspaceUncheckedUpdateManyInput
|
||||
) => {
|
||||
await prisma.workspace.updateMany({
|
||||
where: { id: proWorkspaceId },
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
export const parseCreateTypebot = (typebot: Typebot) => ({
|
||||
...typebot,
|
||||
resultsTablePreferences:
|
||||
typebot.resultsTablePreferences === null
|
||||
? Prisma.DbNull
|
||||
: typebot.resultsTablePreferences,
|
||||
})
|
||||
|
||||
const parseUpdateTypebot = (typebot: Partial<Typebot>) => ({
|
||||
...typebot,
|
||||
resultsTablePreferences:
|
||||
typebot.resultsTablePreferences === null
|
||||
? Prisma.DbNull
|
||||
: typebot.resultsTablePreferences,
|
||||
})
|
113
packages/lib/playwright/databaseHelpers.ts
Normal file
113
packages/lib/playwright/databaseHelpers.ts
Normal file
@ -0,0 +1,113 @@
|
||||
import { createId } from '@paralleldrive/cuid2'
|
||||
import {
|
||||
Block,
|
||||
defaultChoiceInputOptions,
|
||||
defaultSettings,
|
||||
defaultTheme,
|
||||
InputBlockType,
|
||||
ItemType,
|
||||
PublicTypebot,
|
||||
Typebot,
|
||||
} from '@typebot.io/schemas'
|
||||
import { isDefined } from '../utils'
|
||||
import { proWorkspaceId } from './databaseSetup'
|
||||
|
||||
export const parseTestTypebot = (
|
||||
partialTypebot: Partial<Typebot>
|
||||
): Typebot => ({
|
||||
id: createId(),
|
||||
version: '3',
|
||||
workspaceId: proWorkspaceId,
|
||||
folderId: null,
|
||||
name: 'My typebot',
|
||||
theme: defaultTheme,
|
||||
settings: defaultSettings,
|
||||
publicId: null,
|
||||
updatedAt: new Date(),
|
||||
createdAt: new Date(),
|
||||
customDomain: null,
|
||||
icon: null,
|
||||
isArchived: false,
|
||||
isClosed: false,
|
||||
resultsTablePreferences: null,
|
||||
variables: [{ id: 'var1', name: 'var1' }],
|
||||
...partialTypebot,
|
||||
edges: [
|
||||
{
|
||||
id: 'edge1',
|
||||
from: { groupId: 'group0', blockId: 'block0' },
|
||||
to: { groupId: 'group1' },
|
||||
},
|
||||
],
|
||||
groups: [
|
||||
{
|
||||
id: 'group0',
|
||||
title: 'Group #0',
|
||||
blocks: [
|
||||
{
|
||||
id: 'block0',
|
||||
type: 'start',
|
||||
groupId: 'group0',
|
||||
label: 'Start',
|
||||
outgoingEdgeId: 'edge1',
|
||||
},
|
||||
],
|
||||
graphCoordinates: { x: 0, y: 0 },
|
||||
},
|
||||
...(partialTypebot.groups ?? []),
|
||||
],
|
||||
})
|
||||
|
||||
export const parseTypebotToPublicTypebot = (
|
||||
id: string,
|
||||
typebot: Typebot
|
||||
): Omit<PublicTypebot, 'createdAt' | 'updatedAt'> => ({
|
||||
id,
|
||||
version: typebot.version,
|
||||
groups: typebot.groups,
|
||||
typebotId: typebot.id,
|
||||
theme: typebot.theme,
|
||||
settings: typebot.settings,
|
||||
variables: typebot.variables,
|
||||
edges: typebot.edges,
|
||||
})
|
||||
|
||||
type Options = {
|
||||
withGoButton?: boolean
|
||||
}
|
||||
|
||||
export const parseDefaultGroupWithBlock = (
|
||||
block: Partial<Block>,
|
||||
options?: Options
|
||||
): Pick<Typebot, 'groups'> => ({
|
||||
groups: [
|
||||
{
|
||||
graphCoordinates: { x: 200, y: 200 },
|
||||
id: 'group1',
|
||||
blocks: [
|
||||
options?.withGoButton
|
||||
? {
|
||||
id: 'block1',
|
||||
groupId: 'group1',
|
||||
type: InputBlockType.CHOICE,
|
||||
items: [
|
||||
{
|
||||
id: 'item1',
|
||||
blockId: 'block1',
|
||||
type: ItemType.BUTTON,
|
||||
content: 'Go',
|
||||
},
|
||||
],
|
||||
options: defaultChoiceInputOptions,
|
||||
}
|
||||
: undefined,
|
||||
{
|
||||
id: 'block2',
|
||||
groupId: 'group1',
|
||||
...block,
|
||||
} as Block,
|
||||
].filter(isDefined) as Block[],
|
||||
title: 'Group #1',
|
||||
},
|
||||
],
|
||||
})
|
168
packages/lib/playwright/databaseSetup.ts
Normal file
168
packages/lib/playwright/databaseSetup.ts
Normal file
@ -0,0 +1,168 @@
|
||||
import {
|
||||
GraphNavigation,
|
||||
Plan,
|
||||
PrismaClient,
|
||||
WorkspaceRole,
|
||||
} from '@typebot.io/prisma'
|
||||
import { encrypt } from '../api'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
export const apiToken = 'jirowjgrwGREHE'
|
||||
|
||||
export const userId = 'userId'
|
||||
export const otherUserId = 'otherUserId'
|
||||
|
||||
export const proWorkspaceId = 'proWorkspace'
|
||||
export const freeWorkspaceId = 'freeWorkspace'
|
||||
export const starterWorkspaceId = 'starterWorkspace'
|
||||
export const lifetimeWorkspaceId = 'lifetimeWorkspaceId'
|
||||
export const customWorkspaceId = 'customWorkspaceId'
|
||||
|
||||
const setupWorkspaces = async () => {
|
||||
await prisma.workspace.createMany({
|
||||
data: [
|
||||
{
|
||||
id: freeWorkspaceId,
|
||||
name: 'Free workspace',
|
||||
plan: Plan.FREE,
|
||||
},
|
||||
{
|
||||
id: starterWorkspaceId,
|
||||
name: 'Starter workspace',
|
||||
stripeId: 'cus_LnPDugJfa18N41',
|
||||
plan: Plan.STARTER,
|
||||
},
|
||||
{
|
||||
id: proWorkspaceId,
|
||||
name: 'Pro workspace',
|
||||
plan: Plan.PRO,
|
||||
},
|
||||
{
|
||||
id: lifetimeWorkspaceId,
|
||||
name: 'Lifetime workspace',
|
||||
plan: Plan.LIFETIME,
|
||||
},
|
||||
{
|
||||
id: customWorkspaceId,
|
||||
name: 'Custom workspace',
|
||||
plan: Plan.CUSTOM,
|
||||
customChatsLimit: 100000,
|
||||
customStorageLimit: 50,
|
||||
customSeatsLimit: 20,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
export const setupUsers = async () => {
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: userId,
|
||||
email: 'user@email.com',
|
||||
name: 'John Doe',
|
||||
graphNavigation: GraphNavigation.TRACKPAD,
|
||||
onboardingCategories: [],
|
||||
apiTokens: {
|
||||
createMany: {
|
||||
data: [
|
||||
{
|
||||
name: 'Token 1',
|
||||
token: apiToken,
|
||||
createdAt: new Date(2022, 1, 1),
|
||||
},
|
||||
{
|
||||
name: 'Github',
|
||||
token: 'jirowjgrwGREHEgdrgithub',
|
||||
createdAt: new Date(2022, 1, 2),
|
||||
},
|
||||
{
|
||||
name: 'N8n',
|
||||
token: 'jirowjgrwGREHrgwhrwn8n',
|
||||
createdAt: new Date(2022, 1, 3),
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
await prisma.user.create({
|
||||
data: {
|
||||
id: otherUserId,
|
||||
email: 'other-user@email.com',
|
||||
name: 'James Doe',
|
||||
onboardingCategories: [],
|
||||
},
|
||||
})
|
||||
return prisma.memberInWorkspace.createMany({
|
||||
data: [
|
||||
{
|
||||
role: WorkspaceRole.ADMIN,
|
||||
userId,
|
||||
workspaceId: freeWorkspaceId,
|
||||
},
|
||||
{
|
||||
role: WorkspaceRole.ADMIN,
|
||||
userId,
|
||||
workspaceId: starterWorkspaceId,
|
||||
},
|
||||
{
|
||||
role: WorkspaceRole.ADMIN,
|
||||
userId,
|
||||
workspaceId: proWorkspaceId,
|
||||
},
|
||||
{
|
||||
role: WorkspaceRole.ADMIN,
|
||||
userId,
|
||||
workspaceId: lifetimeWorkspaceId,
|
||||
},
|
||||
{
|
||||
role: WorkspaceRole.ADMIN,
|
||||
userId,
|
||||
workspaceId: customWorkspaceId,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
const setupCredentials = () => {
|
||||
const { encryptedData, iv } = encrypt({
|
||||
expiry_date: 1642441058842,
|
||||
access_token:
|
||||
'ya29.A0ARrdaM--PV_87ebjywDJpXKb77NBFJl16meVUapYdfNv6W6ZzqqC47fNaPaRjbDbOIIcp6f49cMaX5ndK9TAFnKwlVqz3nrK9nLKqgyDIhYsIq47smcAIZkK56SWPx3X3DwAFqRu2UPojpd2upWwo-3uJrod',
|
||||
// This token is linked to a test Google account (typebot.test.user@gmail.com)
|
||||
refresh_token:
|
||||
'1//039xWRt8YaYa3CgYIARAAGAMSNwF-L9Iru9FyuTrDSa7lkSceggPho83kJt2J29G69iEhT1C6XV1vmo6bQS9puL_R2t8FIwR3gek',
|
||||
})
|
||||
return prisma.credentials.createMany({
|
||||
data: [
|
||||
{
|
||||
name: 'pro-user@email.com',
|
||||
type: 'google sheets',
|
||||
data: encryptedData,
|
||||
workspaceId: proWorkspaceId,
|
||||
iv,
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
|
||||
export const setupDatabase = async () => {
|
||||
await setupWorkspaces()
|
||||
await setupUsers()
|
||||
return setupCredentials()
|
||||
}
|
||||
|
||||
export const teardownDatabase = async () => {
|
||||
await prisma.workspace.deleteMany({
|
||||
where: {
|
||||
members: {
|
||||
some: { userId: { in: [userId, otherUserId] } },
|
||||
},
|
||||
},
|
||||
})
|
||||
await prisma.user.deleteMany({
|
||||
where: { id: { in: [userId, otherUserId] } },
|
||||
})
|
||||
return prisma.webhook.deleteMany()
|
||||
}
|
11
packages/lib/playwright/globalSetup.ts
Normal file
11
packages/lib/playwright/globalSetup.ts
Normal file
@ -0,0 +1,11 @@
|
||||
import { FullConfig } from '@playwright/test'
|
||||
import { setupDatabase, teardownDatabase } from './databaseSetup'
|
||||
|
||||
async function globalSetup(config: FullConfig) {
|
||||
const { baseURL } = config.projects[0].use
|
||||
if (!baseURL) throw new Error('baseURL is missing')
|
||||
await teardownDatabase()
|
||||
await setupDatabase()
|
||||
}
|
||||
|
||||
export default globalSetup
|
30
packages/lib/playwright/testHelpers.ts
Normal file
30
packages/lib/playwright/testHelpers.ts
Normal file
@ -0,0 +1,30 @@
|
||||
import type { Page } from '@playwright/test'
|
||||
|
||||
export const mockSessionResponsesToOtherUser = async (page: Page) =>
|
||||
page.route('/api/auth/session', (route) => {
|
||||
if (route.request().method() === 'GET') {
|
||||
return route.fulfill({
|
||||
status: 200,
|
||||
body: '{"user":{"id":"otherUserId","name":"James Doe","email":"other-user@email.com","emailVerified":null,"image":"https://avatars.githubusercontent.com/u/16015833?v=4","stripeId":null,"graphNavigation": "TRACKPAD"}}',
|
||||
})
|
||||
}
|
||||
return route.continue()
|
||||
})
|
||||
|
||||
export const typebotViewer = (page: Page) =>
|
||||
page.frameLocator('#typebot-iframe')
|
||||
|
||||
export const waitForSuccessfulPutRequest = (page: Page) =>
|
||||
page.waitForResponse(
|
||||
(resp) => resp.request().method() === 'PUT' && resp.status() === 200
|
||||
)
|
||||
|
||||
export const waitForSuccessfulPostRequest = (page: Page) =>
|
||||
page.waitForResponse(
|
||||
(resp) => resp.request().method() === 'POST' && resp.status() === 200
|
||||
)
|
||||
|
||||
export const waitForSuccessfulDeleteRequest = (page: Page) =>
|
||||
page.waitForResponse(
|
||||
(resp) => resp.request().method() === 'DELETE' && resp.status() === 200
|
||||
)
|
Reference in New Issue
Block a user