// ABOUTME: Playwright global teardown - stops PocketBase and cleans up temp data. // ABOUTME: Runs after all e2e tests to ensure clean shutdown. import * as fs from "node:fs"; import * as path from "node:path"; const STATE_FILE = path.join(__dirname, ".harness-state.json"); interface HarnessStateFile { dataDir: string; url: string; pid: number; } export default async function globalTeardown(): Promise { console.log("Stopping PocketBase..."); // Read the saved state if (!fs.existsSync(STATE_FILE)) { console.log("No harness state file found, nothing to clean up."); return; } const state: HarnessStateFile = JSON.parse( fs.readFileSync(STATE_FILE, "utf-8"), ); // Kill the PocketBase process if (state.pid) { try { process.kill(state.pid, "SIGTERM"); // Wait for graceful shutdown await new Promise((resolve) => setTimeout(resolve, 500)); // Force kill if still running try { process.kill(state.pid, "SIGKILL"); } catch { // Process already dead, which is fine } } catch { // Process might already be dead } } // Clean up the temporary data directory if (state.dataDir && fs.existsSync(state.dataDir)) { fs.rmSync(state.dataDir, { recursive: true, force: true }); console.log(`Cleaned up temp directory: ${state.dataDir}`); } // Remove the state file fs.unlinkSync(STATE_FILE); console.log("PocketBase stopped and cleaned up."); }