From 0aba2ee557bb343fffd1c0266357e97adc8eed06 Mon Sep 17 00:00:00 2001 From: yiling Date: Fri, 10 Jul 2026 16:18:45 +0800 Subject: [PATCH] refactor(e2e): migrate database reset logic to standard Playwright custom fixture --- apps/web/e2e/fixtures.ts | 55 +++++++++++++++++++++++++++++ apps/web/e2e/signup-project.spec.ts | 5 ++- apps/web/package.json | 3 ++ bun.lock | 29 ++++++++------- playwright.config.ts | 2 +- 5 files changed, 77 insertions(+), 17 deletions(-) create mode 100644 apps/web/e2e/fixtures.ts diff --git a/apps/web/e2e/fixtures.ts b/apps/web/e2e/fixtures.ts new file mode 100644 index 00000000..880cf361 --- /dev/null +++ b/apps/web/e2e/fixtures.ts @@ -0,0 +1,55 @@ +import { test as base } from '@playwright/test' +import { PrismaClient } from '../../../packages/db/src/generated/prisma/client' +import { PrismaPg } from '@prisma/adapter-pg' +import { Pool } from 'pg' +import * as fs from 'fs' +import * as path from 'path' +import { fileURLToPath } from 'url' + +const currentDir = path.dirname(fileURLToPath(import.meta.url)) +const envPath = path.resolve(currentDir, '.env.e2e') + +// Load E2E environment variables if present +if (fs.existsSync(envPath)) { + const content = fs.readFileSync(envPath, 'utf8') + content.split('\n').forEach((line) => { + const parts = line.split('=') + if (parts.length >= 2) { + const key = parts[0].trim() + const value = parts.slice(1).join('=').trim() + process.env[key] = value + } + }) +} + +export const test = base.extend<{ prisma: PrismaClient }>({ + prisma: [ + async (_, use) => { + const connectionString = process.env.DATABASE_URL + const pool = new Pool({ connectionString }) + const adapter = new PrismaPg(pool) + const prisma = new PrismaClient({ adapter }) + + // Reset database state before the test runs + await prisma.$executeRawUnsafe(` + DO $$ DECLARE + r RECORD; + BEGIN + FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public' AND tablename <> '_prisma_migrations') LOOP + EXECUTE 'TRUNCATE TABLE ' || quote_ident(r.tablename) || ' CASCADE;'; + END LOOP; + END $$; + `) + + // Provide the clean prisma instance to the test + await use(prisma) + + // Disconnect and clean up resources after the test finishes + await prisma.$disconnect() + await pool.end() + }, + { auto: true }, + ], +}) + +export { expect } from '@playwright/test' diff --git a/apps/web/e2e/signup-project.spec.ts b/apps/web/e2e/signup-project.spec.ts index 7a7ec0b8..806e196b 100644 --- a/apps/web/e2e/signup-project.spec.ts +++ b/apps/web/e2e/signup-project.spec.ts @@ -1,4 +1,4 @@ -import { expect, test } from '@playwright/test' +import { expect, test } from './fixtures' import * as fs from 'fs' import * as path from 'path' import { fileURLToPath } from 'url' @@ -8,8 +8,7 @@ const tempImgPath = path.resolve(currentDir, 'temp-cover.png') test.beforeAll(async () => { // Create a 1x1 transparent PNG file - const pngBase64 = - 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=' + const pngBase64 = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=' fs.writeFileSync(tempImgPath, Buffer.from(pngBase64, 'base64')) }) diff --git a/apps/web/package.json b/apps/web/package.json index bfa76f98..4c40834f 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -13,5 +13,8 @@ "@shumai/transcode": "workspace:*", "@shumai/webui": "workspace:*", "hono": "4.12.23" + }, + "devDependencies": { + "@types/pg": "8.20.0" } } diff --git a/bun.lock b/bun.lock index 72d6d422..41ed4b85 100644 --- a/bun.lock +++ b/bun.lock @@ -37,7 +37,7 @@ }, "apps/agent": { "name": "@shumai/agent-app", - "version": "0.1.7", + "version": "0.1.8", "bin": { "shumai-agent": "./src/index.ts", }, @@ -49,7 +49,7 @@ }, "apps/cli": { "name": "@shumai/cli", - "version": "0.1.7", + "version": "0.1.8", "bin": { "shumai-cli": "./src/index.ts", }, @@ -64,7 +64,7 @@ }, "apps/transcode": { "name": "@shumai/transcode-app", - "version": "0.1.7", + "version": "0.1.8", "bin": { "shumai-transcode": "./src/index.ts", }, @@ -76,7 +76,7 @@ }, "apps/web": { "name": "@shumai/web-app", - "version": "0.1.7", + "version": "0.1.8", "dependencies": { "@shumai/agent": "workspace:*", "@shumai/api": "workspace:*", @@ -88,10 +88,13 @@ "@shumai/workflow-core": "workspace:*", "hono": "4.12.23", }, + "devDependencies": { + "@types/pg": "8.20.0", + }, }, "packages/agent": { "name": "@shumai/agent", - "version": "0.1.7", + "version": "0.1.8", "dependencies": { "@anthropic-ai/sandbox-runtime": "0.0.52", "@earendil-works/pi-agent-core": "0.78.0", @@ -115,7 +118,7 @@ }, "packages/api": { "name": "@shumai/api", - "version": "0.1.7", + "version": "0.1.8", "dependencies": { "@hono/zod-validator": "0.7.6", "@shumai/core": "workspace:*", @@ -129,7 +132,7 @@ }, "packages/core": { "name": "@shumai/core", - "version": "0.1.7", + "version": "0.1.8", "dependencies": { "@earendil-works/pi-agent-core": "0.78.0", "@shumai/db": "workspace:*", @@ -157,7 +160,7 @@ }, "packages/db": { "name": "@shumai/db", - "version": "0.1.7", + "version": "0.1.8", "dependencies": { "@earendil-works/pi-agent-core": "0.78.0", "@prisma/adapter-pg": "7.8.0", @@ -173,7 +176,7 @@ }, "packages/dtos": { "name": "@shumai/dtos", - "version": "0.1.7", + "version": "0.1.8", "dependencies": { "@earendil-works/pi-agent-core": "0.78.0", "zod": "4.4.3", @@ -181,11 +184,11 @@ }, "packages/tableprinter": { "name": "@shumai/tableprinter", - "version": "0.1.7", + "version": "0.1.8", }, "packages/transcode": { "name": "@shumai/transcode", - "version": "0.1.7", + "version": "0.1.8", "dependencies": { "@shumai/core": "workspace:*", "@shumai/db": "workspace:*", @@ -199,7 +202,7 @@ }, "packages/webui": { "name": "@shumai/webui", - "version": "0.1.7", + "version": "0.1.8", "dependencies": { "@base-ui/react": "1.5.0", "@dnd-kit/abstract": "0.4.0", @@ -296,7 +299,7 @@ }, "packages/workflow-core": { "name": "@shumai/workflow-core", - "version": "0.1.7", + "version": "0.1.8", "dependencies": { "@shumai/core": "workspace:*", "@shumai/db": "workspace:*", diff --git a/playwright.config.ts b/playwright.config.ts index e5793256..79d77c07 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -12,7 +12,7 @@ export default defineConfig({ fullyParallel: true, forbidOnly: !!process.env.CI, retries: process.env.CI ? 2 : 0, - workers: process.env.CI ? 1 : undefined, + workers: 1, reporter: process.env.CI ? [['list'], ['html', { open: 'never' }]] : 'list', timeout: 60_000, expect: { timeout: 10_000 },