chore: heygermany customer project
This commit is contained in:
148
e2e/tests/support/hooks.ts
Normal file
148
e2e/tests/support/hooks.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
import {
|
||||
Before,
|
||||
After,
|
||||
BeforeAll,
|
||||
AfterAll,
|
||||
setDefaultTimeout,
|
||||
} from '@cucumber/cucumber'
|
||||
import { readFileSync } from 'fs'
|
||||
import { spawn, execFileSync, type ChildProcess } from 'child_process'
|
||||
import { resolve, dirname, join } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
import type { AppWorld } from './world.js'
|
||||
import { config } from './types.js'
|
||||
|
||||
setDefaultTimeout(Math.max(config.responseTimeout, 120_000))
|
||||
|
||||
let backendProcess: ChildProcess | undefined
|
||||
let frontendProcess: ChildProcess | undefined
|
||||
|
||||
async function waitForUrl(url: string, label: string, timeoutMs = 60_000) {
|
||||
const deadline = Date.now() + timeoutMs
|
||||
while (Date.now() < deadline) {
|
||||
try {
|
||||
const res = await fetch(url)
|
||||
if (res.status < 500) return
|
||||
} catch {
|
||||
// not ready yet
|
||||
}
|
||||
await new Promise((r) => setTimeout(r, 500))
|
||||
}
|
||||
throw new Error(`[e2e] ${label} did not become ready at ${url} within ${timeoutMs}ms`)
|
||||
}
|
||||
|
||||
function startProcess({
|
||||
cmd,
|
||||
cwd,
|
||||
env,
|
||||
label,
|
||||
}: {
|
||||
cmd: string
|
||||
cwd: string
|
||||
env: NodeJS.ProcessEnv
|
||||
label: string
|
||||
}) {
|
||||
const p = spawn(cmd, {
|
||||
cwd,
|
||||
env,
|
||||
stdio: 'pipe',
|
||||
shell: true,
|
||||
detached: true,
|
||||
})
|
||||
p.stderr?.on('data', (d: Buffer) => process.stderr.write(`[${label}] ${d}`))
|
||||
p.stdout?.on('data', (d: Buffer) => process.stdout.write(`[${label}] ${d}`))
|
||||
return p
|
||||
}
|
||||
|
||||
async function stopProcess(p: ChildProcess | undefined) {
|
||||
if (!p?.pid) return
|
||||
try {
|
||||
process.kill(-p.pid, 'SIGTERM')
|
||||
} catch {
|
||||
try {
|
||||
p.kill('SIGTERM')
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
await new Promise((r) => setTimeout(r, 1_000))
|
||||
}
|
||||
|
||||
function killPort(port: number) {
|
||||
try {
|
||||
const out = execFileSync('lsof', ['-ti', `tcp:${port}`], {
|
||||
encoding: 'utf8',
|
||||
}).trim()
|
||||
if (!out) return
|
||||
for (const pid of out.split(/\s+/)) {
|
||||
if (!pid) continue
|
||||
try {
|
||||
process.kill(Number(pid), 'SIGTERM')
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const repoRoot = resolve(__dirname, '../../..')
|
||||
const seedFile = join(repoRoot, 'db', 'sqlite-seed.sql')
|
||||
const sqliteDb = join(repoRoot, '.pikku-runtime', 'dev.db')
|
||||
|
||||
function prepareDatabase() {
|
||||
execFileSync('sqlite3', [sqliteDb], {
|
||||
cwd: repoRoot,
|
||||
input: readFileSync(seedFile),
|
||||
stdio: ['pipe', 'inherit', 'inherit'],
|
||||
})
|
||||
}
|
||||
|
||||
BeforeAll(async function () {
|
||||
if (!config.manageServers) return
|
||||
|
||||
if (process.env.E2E_PREPARE_DB !== '0' && process.env.E2E_PREPARE_DB !== 'false') {
|
||||
prepareDatabase()
|
||||
}
|
||||
|
||||
killPort(3000)
|
||||
killPort(3001)
|
||||
|
||||
backendProcess = startProcess({
|
||||
cmd: process.env.E2E_BACKEND_CMD ?? 'bun run dev:backend',
|
||||
cwd: repoRoot,
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_ENV: 'test',
|
||||
},
|
||||
label: 'backend',
|
||||
})
|
||||
await waitForUrl(`${config.apiUrl}/api/auth/get-session`, 'backend')
|
||||
|
||||
frontendProcess = startProcess({
|
||||
cmd: process.env.E2E_FRONTEND_CMD ?? 'bun run dev:frontend',
|
||||
cwd: repoRoot,
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_ENV: 'test',
|
||||
VITE_API_BASE_URL: config.apiUrl,
|
||||
},
|
||||
label: 'frontend',
|
||||
})
|
||||
await waitForUrl(`${config.appUrl}/en`, 'frontend')
|
||||
})
|
||||
|
||||
AfterAll(async function () {
|
||||
await stopProcess(frontendProcess)
|
||||
await stopProcess(backendProcess)
|
||||
})
|
||||
|
||||
Before(async function (this: AppWorld) {
|
||||
await this.openBrowser()
|
||||
})
|
||||
|
||||
After(async function (this: AppWorld) {
|
||||
await this.closeBrowser()
|
||||
})
|
||||
14
e2e/tests/support/types.ts
Normal file
14
e2e/tests/support/types.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
export const config = {
|
||||
get appUrl() {
|
||||
return process.env.APP_URL ?? 'http://localhost:3001'
|
||||
},
|
||||
get apiUrl() {
|
||||
return process.env.API_URL ?? 'http://localhost:3000'
|
||||
},
|
||||
get responseTimeout() {
|
||||
return Number(process.env.E2E_TIMEOUT ?? 30_000)
|
||||
},
|
||||
get manageServers() {
|
||||
return process.env.E2E_MANAGE_SERVERS === '1'
|
||||
},
|
||||
} as const
|
||||
134
e2e/tests/support/world.ts
Normal file
134
e2e/tests/support/world.ts
Normal file
@@ -0,0 +1,134 @@
|
||||
import { World, setWorldConstructor } from '@cucumber/cucumber'
|
||||
import {
|
||||
chromium,
|
||||
type Browser,
|
||||
type BrowserContext,
|
||||
type Page,
|
||||
} from '@playwright/test'
|
||||
import { config } from './types.js'
|
||||
|
||||
type ActorSession = {
|
||||
context: BrowserContext
|
||||
page: Page
|
||||
}
|
||||
|
||||
export class AppWorld extends World {
|
||||
browser!: Browser
|
||||
private actorSessions = new Map<string, ActorSession>()
|
||||
private currentActor = 'default'
|
||||
candidateName?: string
|
||||
candidateEmail?: string
|
||||
candidateId?: string
|
||||
|
||||
async openBrowser() {
|
||||
const headed = process.env.HEADED === '1' || process.env.HEADED === 'true'
|
||||
this.browser = await chromium.launch({
|
||||
headless: !headed,
|
||||
slowMo: headed ? 150 : 0,
|
||||
})
|
||||
await this.useActor('default')
|
||||
}
|
||||
|
||||
async closeBrowser() {
|
||||
for (const { context } of this.actorSessions.values()) {
|
||||
await context.close().catch(() => {})
|
||||
}
|
||||
this.actorSessions.clear()
|
||||
await this.browser?.close()
|
||||
}
|
||||
|
||||
get context() {
|
||||
return this.getActorSession(this.currentActor).context
|
||||
}
|
||||
|
||||
get page() {
|
||||
return this.getActorSession(this.currentActor).page
|
||||
}
|
||||
|
||||
get activeActor() {
|
||||
return this.currentActor
|
||||
}
|
||||
|
||||
async useActor(name: string) {
|
||||
if (!this.actorSessions.has(name)) {
|
||||
const context = await this.browser.newContext({
|
||||
locale: process.env.E2E_LOCALE ?? 'en-US',
|
||||
})
|
||||
const page = await context.newPage()
|
||||
page.setDefaultTimeout(config.responseTimeout)
|
||||
this.actorSessions.set(name, { context, page })
|
||||
}
|
||||
this.currentActor = name
|
||||
}
|
||||
|
||||
setActorPage(name: string, page: Page) {
|
||||
const session = this.getActorSession(name)
|
||||
page.setDefaultTimeout(config.responseTimeout)
|
||||
this.actorSessions.set(name, { ...session, page })
|
||||
}
|
||||
|
||||
private getActorSession(name: string) {
|
||||
const session = this.actorSessions.get(name)
|
||||
if (!session) {
|
||||
throw new Error(`Actor session "${name}" has not been initialized`)
|
||||
}
|
||||
return session
|
||||
}
|
||||
|
||||
async gotoApp(path: string) {
|
||||
const url = path.startsWith('http')
|
||||
? path
|
||||
: `${config.appUrl}${path.startsWith('/') ? path : `/${path}`}`
|
||||
await this.page.goto(url, { waitUntil: 'domcontentloaded' })
|
||||
await this.page.waitForLoadState('networkidle', { timeout: 2_500 }).catch(() => {})
|
||||
await this.page.waitForTimeout(400)
|
||||
}
|
||||
|
||||
async login(email: string, password: string) {
|
||||
const startingUrl = this.page.url()
|
||||
await this.page.locator('input[name="email"]').fill(email)
|
||||
await this.page.locator('input[name="password"]').fill(password)
|
||||
await this.page.getByRole('button', { name: /^login$/i }).click({
|
||||
noWaitAfter: true,
|
||||
})
|
||||
await Promise.race([
|
||||
this.page.waitForURL(
|
||||
(url) => url.toString() !== startingUrl && !url.toString().includes('/auth/login'),
|
||||
{ timeout: 5_000 },
|
||||
),
|
||||
this.page
|
||||
.getByText(/invalid email or password/i, { exact: false })
|
||||
.first()
|
||||
.waitFor({ state: 'visible', timeout: 5_000 }),
|
||||
]).catch(() => {})
|
||||
await this.page.waitForLoadState('networkidle', { timeout: 3_000 }).catch(() => {})
|
||||
await this.page.waitForTimeout(400)
|
||||
|
||||
const invalidCredentials = await this.page
|
||||
.getByText(/invalid email or password/i, { exact: false })
|
||||
.first()
|
||||
.isVisible()
|
||||
.catch(() => false)
|
||||
|
||||
if (invalidCredentials) {
|
||||
throw new Error(`Login failed for ${email}: invalid email or password`)
|
||||
}
|
||||
}
|
||||
|
||||
async expectText(text: string) {
|
||||
const locator = this.page.getByText(text, { exact: false }).first()
|
||||
try {
|
||||
await locator.waitFor({ state: 'visible', timeout: config.responseTimeout })
|
||||
} catch (error) {
|
||||
const bodyText = (await this.page.locator('body').innerText().catch(() => ''))
|
||||
.replace(/\s+/g, ' ')
|
||||
.slice(0, 1000)
|
||||
throw new Error(
|
||||
`Expected to see "${text}" but it was not visible. Body excerpt: ${bodyText}`,
|
||||
{ cause: error },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setWorldConstructor(AppWorld)
|
||||
Reference in New Issue
Block a user