diff --git a/apps/web/e2e/global-teardown.ts b/apps/web/e2e/global-teardown.ts new file mode 100644 index 00000000..6d68fa0e --- /dev/null +++ b/apps/web/e2e/global-teardown.ts @@ -0,0 +1,36 @@ +import { teardown as dbTeardown } from '../../../packages/db/src/test-global-setup' +import * as fs from 'fs' +import * as path from 'path' +import { fileURLToPath } from 'url' + +const currentDir = path.dirname(fileURLToPath(import.meta.url)) + +export default async function globalTeardown() { + const envPath = path.resolve(currentDir, '.env.e2e') + if (fs.existsSync(envPath)) { + const content = fs.readFileSync(envPath, 'utf8') + let bucketName: string | undefined + + content.split('\n').forEach((line) => { + const parts = line.split('=') + if (parts.length >= 2 && parts[0].trim() === 'S3_BUCKET') { + bucketName = parts.slice(1).join('=').trim() + } + }) + + if (bucketName) { + const bucketDir = path.resolve('data', bucketName) + if (fs.existsSync(bucketDir)) { + console.log(`Cleaning up E2E local storage bucket: ${bucketDir}`) + fs.rmSync(bucketDir, { recursive: true, force: true }) + } + } + + // Delete the temp .env.e2e file + fs.unlinkSync(envPath) + } + + // Stop pgvector container + await dbTeardown() + console.log('Global teardown complete.') +} diff --git a/apps/web/e2e/serve.ts b/apps/web/e2e/serve.ts new file mode 100644 index 00000000..41a3e4ed --- /dev/null +++ b/apps/web/e2e/serve.ts @@ -0,0 +1,34 @@ +import { globalTestSetup } from '../../../packages/db/src/test-global-setup' +import { randomBytes } from 'crypto' +import * as fs from 'fs' +import * as path from 'path' +import { fileURLToPath } from 'url' + +const currentDir = path.dirname(fileURLToPath(import.meta.url)) + +async function start() { + console.log('Starting E2E test database container...') + await globalTestSetup() + + // Generate a random S3 bucket name + const randomSuffix = randomBytes(4).toString('hex') + const bucketName = `shumai-e2e-${randomSuffix}` + + process.env.S3_BUCKET = bucketName + process.env.SHUMAI_SERVER_PORT = '5200' + process.env.BETTER_AUTH_URL = 'http://localhost:5200' + process.env.NODE_ENV = 'development' + + // Write variables to .env.e2e file so the teardown can load them + const envContent = [`DATABASE_URL=${process.env.DATABASE_URL}`, `S3_BUCKET=${bucketName}`].join( + '\n', + ) + + fs.writeFileSync(path.resolve(currentDir, '.env.e2e'), envContent) + console.log(`E2E env file written. Starting Web App...`) + + // Now run the web app + await import('../src/index.ts') +} + +start().catch(console.error) diff --git a/apps/web/e2e/signup-project.spec.ts b/apps/web/e2e/signup-project.spec.ts new file mode 100644 index 00000000..7a7ec0b8 --- /dev/null +++ b/apps/web/e2e/signup-project.spec.ts @@ -0,0 +1,75 @@ +import { expect, test } from '@playwright/test' +import * as fs from 'fs' +import * as path from 'path' +import { fileURLToPath } from 'url' + +const currentDir = path.dirname(fileURLToPath(import.meta.url)) +const tempImgPath = path.resolve(currentDir, 'temp-cover.png') + +test.beforeAll(async () => { + // Create a 1x1 transparent PNG file + const pngBase64 = + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=' + fs.writeFileSync(tempImgPath, Buffer.from(pngBase64, 'base64')) +}) + +test.afterAll(async () => { + if (fs.existsSync(tempImgPath)) { + fs.unlinkSync(tempImgPath) + } +}) + +test('should sign up and create a project with a cover image', async ({ page }) => { + const email = `test-${Date.now()}@shumai.local` + const password = 'Password123!' + + // 1. Sign Up Flow + await page.goto('/signup') + await expect(page).toHaveURL(/\/signup/) + + // Fill credentials + await page.fill('#email', email) + await page.fill('#password', password) + + // Submit sign up + await page.click('button[type="submit"]') + + // Wait for the automatic redirect to the team page (since only one team exists) + await expect(page).toHaveURL(/\/teams\/[^/]+/) + + // 2. Create Project Flow + // Click the "Create Project" button on the dashboard + await page.click('button:has-text("Create Project")') + + const dialog = page.locator('[role="dialog"]') + await expect(dialog).toBeVisible() + + // Fill project name + const projectName = `My Project ${Date.now()}` + await dialog.locator('#name').fill(projectName) + + // Set cover image + await dialog.locator('input[type="file"]').setInputFiles(tempImgPath) + + // Wait for image upload completion and preview + await expect(dialog.locator('img[alt="Cover Preview"]')).toBeVisible({ timeout: 15_000 }) + + // Click submit in dialog + await dialog.locator('button[type="submit"]').click() + + // Wait for navigation to the project page + await expect(page).toHaveURL(/\/projects\/[^/]+/) + + // Verify project name appears in the project view + await expect(page.locator(`text=${projectName}`).first()).toBeVisible() + + // 3. Verification on Dashboard + // Navigate back to the dashboard via sidebar + await page.click('button[aria-label="Dashboard"]') + + // Wait for dashboard URL + await expect(page).toHaveURL(/\/teams\/[^/]+/) + + // Verify the project card is visible and displays the cover image with project name alt text + await expect(page.locator(`img[alt="${projectName}"]`)).toBeVisible() +}) diff --git a/apps/web/src/index.ts b/apps/web/src/index.ts index 12049077..2e97505a 100644 --- a/apps/web/src/index.ts +++ b/apps/web/src/index.ts @@ -96,7 +96,6 @@ async function run() { .replaceAll('content="./', 'content="/') const serveMainHtml = () => new Response(mainHtmlText, { headers: mainHtmlHeaders }) - routes['/'] = serveMainHtml routes['/*'] = serveMainHtml } else { @@ -136,7 +135,14 @@ async function run() { process.on('SIGTERM', shutdown) } -handleDaemonCommands('shumai', run).catch((err) => { - console.error(err) - process.exit(1) -}) +if (process.env.S3_BUCKET && process.env.S3_BUCKET.startsWith('shumai-e2e')) { + run().catch((err) => { + console.error(err) + process.exit(1) + }) +} else { + handleDaemonCommands('shumai', run).catch((err) => { + console.error(err) + process.exit(1) + }) +} diff --git a/playwright.config.ts b/playwright.config.ts index f68e569f..ce0878bf 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -1,19 +1,13 @@ import { defineConfig, devices } from '@playwright/test' /** - * Playwright configuration for the webui UI integration tests. + * Playwright configuration for the monorepo integration and E2E tests. * - * The suite runs against a backend-free harness (packages/webui/e2e) served by - * Bun's native HTML bundler, so no database, auth or transcode pipeline is - * required. - * - * Browser selection: pass `--project=` to pick an engine, e.g. - * bun run test:e2e --project=webkit # WebKit (Safari engine), used on macOS CI - * bun run test:e2e --project=chromium - * bun run test:e2e --project=firefox + * We support two test projects: + * 1. harness: pure frontend UI integration tests served on port 5199. + * 2. app: fullstack end-to-end tests with real backend, pgvector container, and S3 local storage on port 5200. */ export default defineConfig({ - testDir: './packages/webui/e2e/tests', testMatch: '**/*.spec.ts', fullyParallel: true, forbidOnly: !!process.env.CI, @@ -23,35 +17,48 @@ export default defineConfig({ timeout: 60_000, expect: { timeout: 10_000 }, + globalTeardown: './apps/web/e2e/global-teardown.ts', + use: { - // `baseURL` is Playwright's required option key and cannot be renamed. - // eslint-disable-next-line @typescript-eslint/naming-convention - baseURL: 'http://localhost:5199', trace: 'on-first-retry', video: 'retain-on-failure', }, projects: [ { - name: 'chromium', - use: { ...devices['Desktop Chrome'] }, + name: 'harness', + testDir: './packages/webui/e2e/tests', + use: { + ...devices['Desktop Chrome'], + // eslint-disable-next-line @typescript-eslint/naming-convention + baseURL: 'http://localhost:5199', + }, }, { - name: 'firefox', - use: { ...devices['Desktop Firefox'] }, - }, - { - name: 'webkit', - use: { ...devices['Desktop Safari'] }, + name: 'app', + testDir: './apps/web/e2e', + fullyParallel: false, // run sequentially to avoid DB and storage conflicts + use: { + ...devices['Desktop Chrome'], + // eslint-disable-next-line @typescript-eslint/naming-convention + baseURL: 'http://localhost:5200', + }, }, ], - // Launch the isolated harness dev server (Bun's native HTML bundler) before - // the tests. - webServer: { - command: 'bun run packages/webui/e2e/harness/serve.ts', - url: 'http://localhost:5199', - reuseExistingServer: !process.env.CI, - timeout: 120_000, - }, + // Launch both the isolated harness server and the fullstack web app server + webServer: [ + { + command: 'bun run packages/webui/e2e/harness/serve.ts', + url: 'http://localhost:5199', + reuseExistingServer: !process.env.CI, + timeout: 120_000, + }, + { + command: 'bun run apps/web/e2e/serve.ts', + url: 'http://localhost:5200', + reuseExistingServer: !process.env.CI, + timeout: 120_000, + }, + ], }) diff --git a/vitest.config.ts b/vitest.config.ts index 53a5dd79..03923092 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -9,7 +9,7 @@ export default defineConfig({ pool: 'forks', // Playwright e2e specs live under packages/webui/e2e and are run by the // Playwright runner (bun run test:e2e), not Vitest. - exclude: [...configDefaults.exclude, 'packages/webui/e2e/**'], + exclude: [...configDefaults.exclude, 'packages/webui/e2e/**', 'apps/web/e2e/**'], server: { deps: { inline: ['zod'],