refactor(e2e): migrate database reset logic to standard Playwright custom fixture

This commit is contained in:
yiling
2026-07-10 16:18:45 +08:00
parent cd96015b9d
commit 0aba2ee557
5 changed files with 77 additions and 17 deletions
+55
View File
@@ -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'
+2 -3
View File
@@ -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'))
})
+3
View File
@@ -13,5 +13,8 @@
"@shumai/transcode": "workspace:*",
"@shumai/webui": "workspace:*",
"hono": "4.12.23"
},
"devDependencies": {
"@types/pg": "8.20.0"
}
}
+16 -13
View File
@@ -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:*",
+1 -1
View File
@@ -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 },