T-89: M1.1n — Security-master run CLI: read-only Mongo sources + bar spans → composeSecurityMasterRun → match report files, with opt-in persist to dev PostgreSQL (no new dependencies)

This commit is contained in:
Studio 2026-11-28 06:27:26 -05:00
parent d691d64260
commit d1057c41f3
4 changed files with 210 additions and 17 deletions

View file

@ -10,6 +10,7 @@ import {
type GroupedBarSpanRow,
} from '../../apps/stock/data-pipeline/src/normalization/bar-spans';
import {
assertSecurityMasterPersistSafety,
classifySourceExchanges,
compareAscii,
composeSecurityMasterRun,
@ -672,6 +673,150 @@ describe('runSecurityMaster', () => {
expect(stdoutChunks).toHaveLength(0);
});
test('assertSecurityMasterPersistSafety rejects QA profile and non-dev PostgreSQL targets before opening PostgreSQL', async () => {
const buildConfig = (overrides: {
environment: 'development' | 'qa';
host?: string;
port: number;
database: string;
}) =>
stockAppSchema.parse({
name: 'stock-bot',
version: '1.0.0',
environment: overrides.environment,
service: {
name: 'data-pipeline',
port: 2002,
},
log: {
level: 'info',
},
database: {
postgres: {
enabled: true,
host: overrides.host ?? 'localhost',
port: overrides.port,
database: overrides.database,
user: 'trading_user',
password: 'dev_password',
},
mongodb: {
enabled: true,
uri:
overrides.environment === 'qa'
? 'mongodb://localhost:27017/stock'
: 'mongodb://localhost:27018/stock_dev',
readUri: 'mongodb://studio_stock_bot_read:pass@localhost:27017/stock?authSource=stock',
readOnly: true,
database: overrides.environment === 'qa' ? 'stock' : 'stock_dev',
},
questdb: { enabled: false },
dragonfly: { enabled: false },
},
providers: {},
features: {},
});
// 1. QA profile targeting QA PostgreSQL (localhost:5432/trading_bot) must be rejected before PG factory runs
const qaConfig = buildConfig({
environment: 'qa',
port: 5432,
database: 'trading_bot',
});
const fakeMongo = createStrictReadOnlyFakeMongo(buildUsCaFixture());
let pgFactoryCalls = 0;
let mongoClosed = 0;
const stdoutChunks: string[] = [];
let caught: unknown = null;
try {
await runSecurityMaster(
{
mongoCollectionClient: fakeMongo.mongoCollectionClient,
barSpanReader: fakeMongo.barSpanReader,
closeMongo: () => {
mongoClosed += 1;
},
assertSafety: () => assertSecurityMasterPersistSafety(qaConfig, {}),
createPostgresClient: () => {
pgFactoryCalls += 1;
throw new Error('PostgreSQL factory must not be called for QA profile');
},
writeStdout: text => stdoutChunks.push(text),
},
{ asOfDate: '2026-01-15', outDir: createTempDir(), persist: true }
);
} catch (error) {
caught = error;
}
expect(caught).toBeInstanceOf(Error);
expect((caught as Error).message).toContain('env-safety');
expect((caught as Error).message).toContain('NODE_ENV=development');
expect(pgFactoryCalls).toBe(0);
expect(mongoClosed).toBe(1);
expect(stdoutChunks).toHaveLength(0);
// 2. Development profile targeting QA PostgreSQL (even with STOCK_BOT_ALLOW_DEV_ON_QA_DB=true) is rejected
const devQaPgConfig = buildConfig({
environment: 'development',
port: 5432,
database: 'trading_bot',
});
expect(() =>
assertSecurityMasterPersistSafety(devQaPgConfig, { STOCK_BOT_ALLOW_DEV_ON_QA_DB: 'true' })
).toThrow(/Safety guard: development environment cannot target QA PostgreSQL/);
// 3. Development profile on port 5433 with non-dev database name is rejected
const devNonDevDbConfig = buildConfig({
environment: 'development',
port: 5433,
database: 'trading_bot',
});
expect(() => assertSecurityMasterPersistSafety(devNonDevDbConfig, {})).toThrow(
/cannot target QA PostgreSQL/
);
// 4. Development profile targeting isolated dev PostgreSQL (localhost:5433/trading_bot_dev) passes
const devIsolatedConfig = buildConfig({
environment: 'development',
port: 5433,
database: 'trading_bot_dev',
});
expect(() => assertSecurityMasterPersistSafety(devIsolatedConfig, {})).not.toThrow();
});
test('closeMongo failure after successful writes rejects with close-mongo and leaves stdout empty', async () => {
const fakeMongo = createStrictReadOnlyFakeMongo(buildUsCaFixture());
const outDir = createTempDir();
const stdoutChunks: string[] = [];
const closeErr = new Error('Mongo disconnect timed out');
let caught: unknown = null;
try {
await runSecurityMaster(
{
mongoCollectionClient: fakeMongo.mongoCollectionClient,
barSpanReader: fakeMongo.barSpanReader,
closeMongo: () => {
throw closeErr;
},
writeStdout: text => {
stdoutChunks.push(text);
},
},
{ asOfDate: '2026-01-15', outDir }
);
} catch (error) {
caught = error;
}
expect(caught).toBeInstanceOf(Error);
expect((caught as Error).message).toContain('close-mongo');
expect((caught as Error).cause).toBe(closeErr);
expect(stdoutChunks).toHaveLength(0);
});
test('real assertEnvironmentSafety rejects development config targeting QA PostgreSQL 5432/trading_bot', () => {
const devQaPgConfig = stockAppSchema.parse({
name: 'stock-bot',