|
| 1 | +/** |
| 2 | + * Pre-test cleanup script |
| 3 | + * Runs BEFORE Playwright starts the dev server to ensure clean state |
| 4 | + */ |
| 5 | + |
| 6 | +const Redis = require("ioredis"); |
| 7 | +const mysql = require("mysql2/promise"); |
| 8 | +const dotenv = require("dotenv"); |
| 9 | +const path = require("path"); |
| 10 | + |
| 11 | +// Load test environment variables |
| 12 | +dotenv.config({ |
| 13 | + path: path.join(__dirname, "..", ".env.testing.testnet.local"), |
| 14 | + override: true, |
| 15 | +}); |
| 16 | + |
| 17 | +async function cleanup() { |
| 18 | + console.log("\n🧹 Pre-test cleanup: Clearing Redis and Database..."); |
| 19 | + |
| 20 | + // Clear Redis |
| 21 | + try { |
| 22 | + const redis = new Redis({ |
| 23 | + host: process.env.REDIS_HOST || "localhost", |
| 24 | + port: parseInt(process.env.REDIS_PORT || "6379"), |
| 25 | + retryStrategy: () => null, |
| 26 | + }); |
| 27 | + |
| 28 | + await redis.flushall(); |
| 29 | + console.log("✅ Redis cleared (all BullMQ jobs removed)"); |
| 30 | + await redis.quit(); |
| 31 | + } catch (error) { |
| 32 | + console.warn("⚠️ Redis cleanup failed:", error.message); |
| 33 | + process.exit(1); // Fail the test if Redis can't be cleared |
| 34 | + } |
| 35 | + |
| 36 | + // Clear MySQL |
| 37 | + try { |
| 38 | + const dbUrl = process.env.DKGP_DATABASE_URL || "mysql://root:@127.0.0.1:3306/dkg_publisher_db"; |
| 39 | + const match = dbUrl.match(/mysql:\/\/([^:]+):([^@]*)@([^:]+):(\d+)\/(.+)/); |
| 40 | + |
| 41 | + if (!match) { |
| 42 | + throw new Error("Invalid database URL format"); |
| 43 | + } |
| 44 | + |
| 45 | + const [, user, password, host, port, database] = match; |
| 46 | + |
| 47 | + const connection = await mysql.createConnection({ |
| 48 | + host, |
| 49 | + port: parseInt(port), |
| 50 | + user, |
| 51 | + password: password || undefined, |
| 52 | + database, |
| 53 | + }); |
| 54 | + |
| 55 | + await connection.execute("DELETE FROM publishing_attempts"); |
| 56 | + await connection.execute("DELETE FROM assets"); |
| 57 | + await connection.execute("UPDATE wallets SET is_locked = 0, locked_by = NULL, locked_at = NULL"); |
| 58 | + await connection.execute("ALTER TABLE assets AUTO_INCREMENT = 1"); |
| 59 | + |
| 60 | + console.log("✅ Database cleared (assets, attempts, wallets unlocked)"); |
| 61 | + console.log("✅ Asset ID counter reset to 1"); |
| 62 | + await connection.end(); |
| 63 | + } catch (error) { |
| 64 | + console.warn("⚠️ Database cleanup failed:", error.message); |
| 65 | + process.exit(1); // Fail the test if DB can't be cleared |
| 66 | + } |
| 67 | + |
| 68 | + console.log("🚀 Cleanup complete - environment ready for fresh test\n"); |
| 69 | +} |
| 70 | + |
| 71 | +cleanup().catch((error) => { |
| 72 | + console.error("💥 Cleanup script failed:", error); |
| 73 | + process.exit(1); |
| 74 | +}); |
0 commit comments