// By default, node:test runs with in parallel, make sure each thread gets their own database export function getTestMongoUrl(dbName = "massive-test-models") { const urlString = process.env.NODE_MONGO_URL || ""; const workerId = process.env.NODE_TEST_WORKER_ID || "1"; // Legacy mode, when running only one worker id, keep the provided URL, otherwise append _1, _2, etc if (workerId === "1") { return urlString; } try { const url = new URL(urlString); let dbN = url.pathname.slice(1); if (!dbN) { dbN = dbName; } while (dbN.match(/-\d+$/)) { dbN = dbN.replace(/-\d+$/, ""); } url.pathname = `/${dbN}-${workerId}`; return url.toString(); } catch (e) { console.error("[Mocha Setup] Invalid NODE_MONGO_URL", e); return urlString; } } export function checkIfTestDatabase() { const url = getTestMongoUrl(); if (!/massive-test/i.exec(url)) { console.log(`Not a test database: ${url}? Expecting database: massive-test.`); process.exit(1); } } checkIfTestDatabase(); export const url = getTestMongoUrl();