mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
tests/setup/per-fork-env.ts hardcoded SYNC_WAIT_MS=30000 on every fork, overriding whatever the container set, so the docker test image could never grant heavy ops a wider sync window. A 12MP stress-image enhance takes ~34s on the macOS Docker VM (Sharp runs 2-3x slower there), just past the 30s window, so the factory returned 202 and three sync-asserting image-enhancement tests failed. Honor a higher SYNC_WAIT_MS when provided (30s floor preserved for host/CI), raise it to 120s in docker-compose.test.yml, and make the vitest test/hook timeouts env-overridable so a slow-but-correct job returns 200 rather than tripping a framework timeout. Host and CI behavior is unchanged.
55 lines
2.3 KiB
TypeScript
55 lines
2.3 KiB
TypeScript
import crypto from "node:crypto";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import pg from "pg";
|
|
|
|
// Each test file (forks pool, isolated) gets its own Postgres database cloned
|
|
// from the migrated template built in tests/global-setup.ts, plus its own
|
|
// workspace dir. setupFiles run before any app module loads, so
|
|
// apps/api/src/config.ts captures the per-file DATABASE_URL.
|
|
const suffix = `${process.pid}_${crypto.randomUUID().slice(0, 8).replace(/-/g, "")}`;
|
|
const forkDir = path.join(os.tmpdir(), `SnapOtter-test-${suffix}`);
|
|
process.env.WORKSPACE_PATH = path.join(forkDir, "workspace");
|
|
|
|
const baseUrl = process.env.TEST_PG_BASE_URL;
|
|
if (!baseUrl) {
|
|
throw new Error("TEST_PG_BASE_URL missing; tests/global-setup.ts did not run");
|
|
}
|
|
|
|
const redisBaseUrl = process.env.TEST_REDIS_BASE_URL;
|
|
if (!redisBaseUrl) {
|
|
throw new Error("TEST_REDIS_BASE_URL missing; tests/global-setup.ts did not run");
|
|
}
|
|
process.env.REDIS_URL = redisBaseUrl;
|
|
process.env.BULLMQ_PREFIX = `snapotter_test_${suffix}`;
|
|
|
|
// Heavy format conversions can exceed the 8s production default under parallel
|
|
// test forks; 30s keeps tool routes synchronous (200) in tests while production
|
|
// stays at 8s. The constrained docker test image (macOS Docker VM, where Sharp
|
|
// and FFmpeg run ~2-3x slower) can request a larger window via SYNC_WAIT_MS;
|
|
// honor it rather than clobbering, but never drop below the 30s test floor.
|
|
const requestedSyncWait = Number(process.env.SYNC_WAIT_MS);
|
|
process.env.SYNC_WAIT_MS = String(
|
|
Number.isFinite(requestedSyncWait) && requestedSyncWait > 30000 ? requestedSyncWait : 30000,
|
|
);
|
|
const dbName = `snapotter_test_${suffix}`; // pid digits + uuid hex: identifier-safe
|
|
const admin = new pg.Client({ connectionString: baseUrl });
|
|
await admin.connect();
|
|
// Concurrent CREATE DATABASE ... TEMPLATE from parallel forks can transiently
|
|
// conflict; retry briefly.
|
|
let created = false;
|
|
for (let attempt = 0; attempt < 5 && !created; attempt++) {
|
|
try {
|
|
await admin.query(`CREATE DATABASE ${dbName} TEMPLATE snapotter_template`);
|
|
created = true;
|
|
} catch (err) {
|
|
if (attempt === 4) throw err;
|
|
await new Promise((r) => setTimeout(r, 150 + Math.floor(150 * attempt)));
|
|
}
|
|
}
|
|
await admin.end();
|
|
|
|
const forkUrl = new URL(baseUrl);
|
|
forkUrl.pathname = `/${dbName}`;
|
|
process.env.DATABASE_URL = forkUrl.toString();
|