|
|
@@ -9,24 +9,27 @@ beforeAll(async () => {
|
|
|
// Use external MongoDB if MONGO_URI is provided (e.g., in CI with GitHub Actions services)
|
|
|
if (process.env.MONGO_URI) {
|
|
|
// Generate unique database name for each test worker to avoid conflicts in parallel execution
|
|
|
- // VITEST_POOL_ID is provided by Vitest (e.g., "1", "2", "3"...)
|
|
|
- const workerId = process.env.VITEST_POOL_ID || '1';
|
|
|
+ // VITEST_WORKER_ID is provided by Vitest (e.g., "1", "2", "3"...)
|
|
|
+ const workerId = process.env.VITEST_WORKER_ID || '1';
|
|
|
const dbName = `growi_test_${workerId}`;
|
|
|
|
|
|
// Parse base URI and append database name
|
|
|
- // Handle both cases: with and without existing database name in URI
|
|
|
- let mongoUri: string;
|
|
|
- if (process.env.MONGO_URI.includes('?')) {
|
|
|
- // URI has query parameters: mongodb://host:port/dbname?params
|
|
|
- mongoUri = process.env.MONGO_URI.replace(/\/[^/?]*(\?|$)/, `/${dbName}$1`);
|
|
|
- } else if (process.env.MONGO_URI.match(/\/[^/]+$/)) {
|
|
|
- // URI has database name: mongodb://host:port/dbname
|
|
|
- mongoUri = process.env.MONGO_URI.replace(/\/[^/]+$/, `/${dbName}`);
|
|
|
+ // Extract base URI (protocol + host + port) and query parameters
|
|
|
+ const [uriWithoutQuery, queryString] = process.env.MONGO_URI.split('?');
|
|
|
+
|
|
|
+ // Find the last slash after the protocol (mongodb://)
|
|
|
+ // and replace everything after it with the new database name
|
|
|
+ let baseUri: string;
|
|
|
+ const protocolMatch = uriWithoutQuery.match(/^mongodb:\/\/[^/]+/);
|
|
|
+ if (protocolMatch) {
|
|
|
+ baseUri = protocolMatch[0];
|
|
|
} else {
|
|
|
- // URI has no database name: mongodb://host:port or mongodb://host:port/
|
|
|
- mongoUri = process.env.MONGO_URI.replace(/\/?$/, `/${dbName}`);
|
|
|
+ // Fallback: if no match, use the whole URI
|
|
|
+ baseUri = uriWithoutQuery;
|
|
|
}
|
|
|
|
|
|
+ const mongoUri = `${baseUri}/${dbName}${queryString ? '?' + queryString : ''}`;
|
|
|
+
|
|
|
// biome-ignore lint/suspicious/noConsole: Allow logging
|
|
|
console.log(`Using external MongoDB at ${mongoUri} (worker: ${workerId})`);
|
|
|
await mongoose.connect(mongoUri, mongoOptions);
|