Files
lolympus-tournament-support/apps/api/test/run-mongo-tests.mjs
dahyeon f649490cd2 fix(auction): harden lifecycle and durable bid storage
Serialize lifecycle changes and make cancellation, deadlines, and match side effects recoverable and idempotent.

Archive accepted bids durably, quarantine invalid payloads, and preserve compatibility with empty round IDs. Synchronize HTTP polling and socket state with scoped resync events.

Add cancellation cleanup index migration and regression coverage. Validated with 744 API tests, 65 Redis integration tests, and 28 Mongo integration tests.
2026-11-09 23:24:11 +09:00

135 lines
3.8 KiB
JavaScript

import { spawn } from 'node:child_process';
import { createRequire } from 'node:module';
import { mkdtemp, mkdir, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join, relative, resolve } from 'node:path';
import { createServer } from 'node:net';
import { setTimeout as delay } from 'node:timers/promises';
import mongoose from 'mongoose';
const require = createRequire(import.meta.url);
const listener = createServer();
await new Promise((accept) => listener.listen(0, '127.0.0.1', accept));
const port = listener.address().port;
await new Promise((accept) => listener.close(accept));
const directory = await mkdtemp(join(tmpdir(), 'lolympus-mongo-test-'));
await mkdir(join(directory, 'data'));
const mongo = spawn(
process.env.MONGODB_TEST_SERVER || 'mongod',
[
'--bind_ip',
'127.0.0.1',
'--port',
String(port),
'--replSet',
'auction_review',
'--dbpath',
join(directory, 'data'),
'--logpath',
join(directory, 'mongod.log'),
'--wiredTigerCacheSizeGB',
'0.25',
],
{ windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] },
);
let startupError;
mongo.on('error', (error) => {
startupError = error;
});
const closed = new Promise((accept) => mongo.once('close', accept));
let output = '';
mongo.stdout.on('data', (chunk) => {
output += chunk;
});
mongo.stderr.on('data', (chunk) => {
output += chunk;
});
const uri = `mongodb://127.0.0.1:${port}/auction_review`;
const client = new mongoose.mongo.MongoClient(`${uri}?directConnection=true`, {
serverSelectionTimeoutMS: 500,
});
try {
let connected = false;
for (let attempt = 0; attempt < 60; attempt++) {
if (startupError) throw startupError;
if (mongo.exitCode !== null) throw new Error(`MongoDB exited: ${output}`);
try {
await client.connect();
connected = true;
break;
} catch {
await delay(100);
}
}
if (!connected) throw new Error(`MongoDB did not start: ${output}`);
await client
.db('admin')
.command({
replSetInitiate: {
_id: 'auction_review',
members: [{ _id: 0, host: `127.0.0.1:${port}` }],
},
});
let primary = false;
for (let attempt = 0; attempt < 100; attempt++) {
if ((await client.db('admin').command({ hello: 1 })).isWritablePrimary) {
primary = true;
break;
}
await delay(100);
}
if (!primary)
throw new Error('MongoDB replica set did not elect its primary.');
const build = await client.db('admin').command({ buildInfo: 1 });
console.log(
`Integration tests using isolated MongoDB ${build.version}, Mongoose ${mongoose.version}, loopback port ${port}`,
);
const jest = spawn(
process.execPath,
[
require.resolve('jest/bin/jest'),
'--config',
'test/jest-mongo.json',
'--runInBand',
...process.argv.slice(2),
],
{
cwd: resolve(import.meta.dirname, '..'),
windowsHide: true,
stdio: 'inherit',
env: {
...process.env,
NODE_ENV: 'test',
MONGO_TEST_ISOLATED: '1',
MONGO_TEST_URL: `${uri}?replicaSet=auction_review`,
},
},
);
process.exitCode = await new Promise((accept, reject) => {
jest.once('error', reject);
jest.once('exit', (code) => accept(code ?? 1));
});
} finally {
await client
.db('admin')
.command({ shutdown: 1, force: true })
.catch(() => {});
await client.close().catch(() => {});
if (mongo.pid && mongo.exitCode === null) mongo.kill();
await closed;
// Only remove the unique temporary directory created by this runner.
const target = relative(resolve(tmpdir()), resolve(directory));
if (
!target.startsWith('lolympus-mongo-test-') ||
target.includes('..') ||
/[/\\]/.test(target)
)
throw new Error('Unsafe MongoDB test directory.');
await rm(directory, {
recursive: true,
force: true,
maxRetries: 10,
retryDelay: 100,
});
}