Merge branch 'dev' into feat/trial-ledger-ddl

This commit is contained in:
Studio 2026-11-27 17:56:00 -05:00
commit 5a60778c62

View file

@ -1,4 +1,4 @@
import { mkdtempSync, rmSync } from 'node:fs';
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
@ -149,6 +149,8 @@ function runInitInIsolatedSubprocess(
mode: 'load-and-resolve' | 'load-only' = 'load-and-resolve'
): { exitCode: number; stdout: string; stderr: string } {
const isolatedDir = mkdtempSync(join(tmpdir(), 'stock-bot-db-migrate-env-'));
// Stop the config loader's upward .env search inside the temp directory.
writeFileSync(join(isolatedDir, '.env'), '', 'utf8');
const script =
mode === 'load-only'
? `import { loadMigrationConfig } from ${JSON.stringify(dbMigrateModuleUrl)};
@ -276,7 +278,114 @@ describe('loadMigrationConfig (real initialization path, isolated subprocess)',
});
});
describe('config loader .env isolation used by runInitInIsolatedSubprocess', () => {
// Arrange a parent dir whose .env would flip the dev target to QA PostgreSQL and leak a sentinel,
// then load config from a child dir with or without its own empty .env.
function loadFromChild(childHasEmptyEnv: boolean): {
exitCode: number;
stdout: string;
stderr: string;
} {
const parentDir = mkdtempSync(join(tmpdir(), 'stock-bot-db-migrate-parent-'));
const childDir = join(parentDir, 'child');
try {
mkdirSync(childDir);
writeFileSync(
join(parentDir, '.env'),
[
'DB_MIGRATE_ENV_LEAK_SENTINEL=leaked',
'POSTGRES_PORT=5432',
'POSTGRES_DATABASE=trading_bot',
'DATABASE_POSTGRES_PORT=5432',
'DATABASE_POSTGRES_DATABASE=trading_bot',
].join('\n'),
'utf8'
);
if (childHasEmptyEnv) {
writeFileSync(join(childDir, '.env'), '', 'utf8');
}
const script = `import { loadMigrationConfig, resolveMigrationTarget } from ${JSON.stringify(dbMigrateModuleUrl)};
const config = loadMigrationConfig();
const resolved = resolveMigrationTarget(config, process.env);
console.log(JSON.stringify({ sentinel: process.env.DB_MIGRATE_ENV_LEAK_SENTINEL ?? null, targetLabel: resolved.targetLabel }));`;
const result = Bun.spawnSync([process.execPath, '-e', script], {
cwd: childDir,
env: {
PATH: process.env.PATH ?? '',
HOME: childDir,
NODE_ENV: 'development',
POSTGRES_HOST: 'localhost',
DATABASE_POSTGRES_HOST: 'localhost',
POSTGRES_USERNAME: 'dev_user',
POSTGRES_PASSWORD: 'dev_pass',
},
});
return {
exitCode: result.exitCode,
stdout: new TextDecoder().decode(result.stdout).trim(),
stderr: new TextDecoder().decode(result.stderr).trim(),
};
} finally {
rmSync(parentDir, { recursive: true, force: true });
}
}
test('an empty .env in the working directory stops the loader from reading a parent .env', () => {
const result = loadFromChild(true);
expect(result.exitCode).toBe(0);
const parsed = JSON.parse(result.stdout) as { sentinel: string | null; targetLabel: string };
expect(parsed.sentinel).toBeNull();
// Defaults (no parent override) resolve to the isolated dev database, not QA.
expect(parsed.targetLabel).not.toBe('localhost:5432/trading_bot');
});
test('without the empty .env the parent .env leaks in and the QA target is rejected', () => {
const result = loadFromChild(false);
expect(result.exitCode).not.toBe(0);
expect(result.stderr).toMatch(/QA PostgreSQL/i);
});
});
describe('db-migrate CLI subprocess', () => {
test(
'exits non-zero with a clear message when the dev PostgreSQL target is unreachable',
() => {
const isolatedDir = mkdtempSync(join(tmpdir(), 'stock-bot-db-migrate-unreachable-'));
try {
writeFileSync(join(isolatedDir, '.env'), '', 'utf8');
const result = Bun.spawnSync(
[process.execPath, 'run', join(root, 'scripts/db-migrate.ts'), '--track', 'master'],
{
cwd: isolatedDir,
env: {
PATH: process.env.PATH ?? '',
HOME: isolatedDir,
NODE_ENV: 'development',
POSTGRES_HOST: '127.0.0.1',
POSTGRES_PORT: '1',
POSTGRES_DATABASE: 'trading_bot_dev',
DATABASE_POSTGRES_HOST: '127.0.0.1',
DATABASE_POSTGRES_PORT: '1',
DATABASE_POSTGRES_DATABASE: 'trading_bot_dev',
},
timeout: 30000,
}
);
const stdout = new TextDecoder().decode(result.stdout);
const stderr = new TextDecoder().decode(result.stderr);
expect(result.exitCode).toBe(1);
expect(stderr).toMatch(/db:migrate failed: .*connect/i);
expect(stdout).not.toMatch(/Applied \d+ migration/);
} finally {
rmSync(isolatedDir, { recursive: true, force: true });
}
},
40000
);
test('exits non-zero with valid tracks on stderr for --track bogus without connecting to DB', () => {
const result = Bun.spawnSync(
[process.execPath, 'run', 'scripts/db-migrate.ts', '--track', 'bogus'],