diff --git a/test/postgres/trial-ledger-schema.test.ts b/test/postgres/trial-ledger-schema.test.ts index 68a810f6..e5e93f27 100644 --- a/test/postgres/trial-ledger-schema.test.ts +++ b/test/postgres/trial-ledger-schema.test.ts @@ -1038,3 +1038,130 @@ describe.skipIf(!url)( }); } ); + +describe.skipIf(!url)( + 'trial_ledger and vault_evaluations PostgreSQL schema (QA round 3 edge cases)', + () => { + test('the statement-level AFTER INSERT check alone rejects an 11-row batch when the row trigger is off', async () => { + const f = await createIsolatedFixture(); + const client = await f.pool.connect(); + try { + // Isolates verify_vault_evaluation_cap_after(): with the BEFORE row trigger disabled, only the + // AFTER statement trigger can stop the batch. evaluated_at defaults to now(), so all 11 count. + await client.query('BEGIN'); + await client.query('ALTER TABLE vault_evaluations DISABLE TRIGGER vault_evaluations_cap'); + await expectPgErrorCode( + client.query( + `INSERT INTO vault_evaluations (${VAULT_COLUMNS_SQL}) + SELECT ${VAULT_VALUES_SQL} FROM generate_series(1, 11)` + ), + '23514', + /at most 10 evaluations/i + ); + await client.query('ROLLBACK'); + } finally { + client.release(); + } + expect(await countRows(f.pool, 'vault_evaluations')).toBe(0); + }); + + test('failed burns (passed = false, negative Sharpe, NULL notes) still count toward the yearly cap', async () => { + const f = await createIsolatedFixture(); + for (let i = 1; i <= 10; i++) { + await insertVaultEvaluation(f.pool, { + strategyRef: `strat:failed-${i}`, + passed: false, + sharpePostCost: -1.25, + notes: null, + }); + } + const stored = ( + await f.pool.query( + `SELECT count(*)::int AS c, bool_or(passed) AS any_passed, max(sharpe_post_cost) AS max_sharpe, + count(notes)::int AS with_notes + FROM vault_evaluations` + ) + ).rows[0]; + expect(stored).toEqual({ c: 10, any_passed: false, max_sharpe: -1.25, with_notes: 0 }); + await expectPgErrorCode( + insertVaultEvaluation(f.pool, { strategyRef: 'strat:after-failures' }), + '23514', + /at most 10 evaluations/i + ); + expect(await countRows(f.pool, 'vault_evaluations')).toBe(10); + }); + + test('trial_ledger keeps every repeated trial: duplicate expression_hash rows, negative and extreme finite stats', async () => { + const f = await createIsolatedFixture(); + // N-hat counting needs every attempt recorded, so there must be no uniqueness on the hash. + for (let i = 0; i < 3; i++) { + await insertTrial(f.pool); + } + await insertTrial(f.pool, { + sharpeUnann: -0.5, + skewness: '-1.7976931348623157e308', + kurtosis: '1.7976931348623157e308', + dailySeries: ['3.4e38', '-3.4e38'], + }); + const hashes = ( + await f.pool.query( + `SELECT expression_hash, count(*)::int AS c FROM trial_ledger GROUP BY expression_hash` + ) + ).rows; + expect(hashes).toEqual([{ expression_hash: VALID_HASH, c: 4 }]); + }); + + test('MERGE cannot update or delete retained trial_ledger and vault_evaluations rows', async () => { + const f = await createIsolatedFixture(); + const version = Number( + (await f.pool.query('SHOW server_version_num')).rows[0].server_version_num + ); + await insertTrial(f.pool); + await insertVaultEvaluation(f.pool); + if (version < 150000) { + // MERGE does not exist before PostgreSQL 15, so it is not a mutation path there. + await expectPgErrorCode( + f.pool.query( + 'MERGE INTO trial_ledger t USING (SELECT 1) s ON true WHEN MATCHED THEN DELETE' + ), + '42601' + ); + return; + } + const expectedMsg = /Retained research evidence cannot be rewritten or deleted/; + for (const table of ['trial_ledger', 'vault_evaluations']) { + await expectPgErrorCode( + f.pool.query(`MERGE INTO ${table} t USING (SELECT 1) s ON true WHEN MATCHED THEN DELETE`), + '23514', + expectedMsg + ); + await expectPgErrorCode( + f.pool.query( + `MERGE INTO ${table} t USING (SELECT 1) s ON true WHEN MATCHED THEN UPDATE SET n_obs = 999` + ), + '23514', + expectedMsg + ); + } + expect( + (await f.pool.query('SELECT n_obs FROM trial_ledger')).rows.map(r => r.n_obs as number) + ).toEqual([252]); + expect( + (await f.pool.query('SELECT n_obs FROM vault_evaluations')).rows.map(r => r.n_obs as number) + ).toEqual([126]); + }); + + test('a writable CTE cannot delete trial_ledger rows', async () => { + const f = await createIsolatedFixture(); + await insertTrial(f.pool); + await expectPgErrorCode( + f.pool.query( + 'WITH gone AS (DELETE FROM trial_ledger RETURNING trial_id) SELECT count(*) FROM gone' + ), + '23514', + /Retained research evidence/ + ); + expect(await countRows(f.pool, 'trial_ledger')).toBe(1); + }); + } +);