diff --git a/test/postgres/trial-ledger-schema.test.ts b/test/postgres/trial-ledger-schema.test.ts index 0f34c03d..68a810f6 100644 --- a/test/postgres/trial-ledger-schema.test.ts +++ b/test/postgres/trial-ledger-schema.test.ts @@ -906,3 +906,135 @@ describe.skipIf(!url)( }); } ); + +describe.skipIf(!url)( + 'trial_ledger and vault_evaluations PostgreSQL schema (QA re-run edge cases)', + () => { + test('raw re-execution of 36-trial-ledger.sql keeps exactly one copy of each trigger and enforcement still holds', async () => { + const f = await createIsolatedFixture(); + const triggers = async () => + ( + await f.pool.query( + `SELECT c.relname || ':' || t.tgname || ':' || p.proname AS t + FROM pg_trigger t + JOIN pg_class c ON c.oid = t.tgrelid + JOIN pg_proc p ON p.oid = t.tgfoid + WHERE NOT t.tgisinternal AND c.relnamespace = $1::regnamespace + AND c.relname IN ('trial_ledger', 'vault_evaluations') + ORDER BY 1`, + [f.schema] + ) + ).rows.map(r => r.t as string); + const expected = [ + 'trial_ledger:trial_ledger_no_truncate:reject_research_evidence_mutation', + 'trial_ledger:trial_ledger_retained:reject_research_evidence_mutation', + 'vault_evaluations:vault_evaluations_cap:enforce_vault_evaluation_cap', + 'vault_evaluations:vault_evaluations_cap_after:verify_vault_evaluation_cap_after', + 'vault_evaluations:vault_evaluations_no_truncate:reject_research_evidence_mutation', + 'vault_evaluations:vault_evaluations_retained:reject_research_evidence_mutation', + ]; + expect(await triggers()).toEqual(expected); + + await insertTrial(f.pool); + for (let i = 1; i <= 10; i++) { + await insertVaultEvaluation(f.pool, { strategyRef: `strat:rerun-${i}` }); + } + const rawSql = readFileSync(trialLedgerSqlPath, 'utf8'); + await f.pool.query(rawSql); + await f.pool.query(rawSql); + + expect(await triggers()).toEqual(expected); + await expectPgErrorCode( + f.pool.query('UPDATE trial_ledger SET n_obs = 3'), + '23514', + /Retained research evidence/ + ); + await expectPgErrorCode(f.pool.query('TRUNCATE vault_evaluations'), '23514'); + await expectPgErrorCode(insertVaultEvaluation(f.pool), '23514', /at most 10 evaluations/i); + expect(await countRows(f.pool, 'trial_ledger')).toBe(1); + expect(await countRows(f.pool, 'vault_evaluations')).toBe(10); + }); + + test('a session defaulting to REPEATABLE READ cannot insert a vault evaluation in autocommit mode', async () => { + const f = await createIsolatedFixture(); + const client = await f.pool.connect(); + try { + await client.query("SET default_transaction_isolation = 'repeatable read'"); + await expectPgErrorCode( + client.query( + `INSERT INTO vault_evaluations (${VAULT_COLUMNS_SQL}) VALUES (${VAULT_VALUES_SQL})` + ), + '25000', + /READ COMMITTED isolation/i + ); + await client.query('RESET default_transaction_isolation'); + } finally { + client.release(); + } + expect(await countRows(f.pool, 'vault_evaluations')).toBe(0); + }); + + test('ten single-row inserts inside one transaction fill the cap and the eleventh fails in that transaction', async () => { + const f = await createIsolatedFixture(); + const client = await f.pool.connect(); + try { + await client.query('BEGIN'); + for (let i = 1; i <= 10; i++) { + await client.query( + `INSERT INTO vault_evaluations (${VAULT_COLUMNS_SQL}) VALUES (${VAULT_VALUES_SQL})` + ); + } + await expectPgErrorCode( + client.query( + `INSERT INTO vault_evaluations (${VAULT_COLUMNS_SQL}) VALUES (${VAULT_VALUES_SQL})` + ), + '23514', + /at most 10 evaluations/i + ); + await client.query('ROLLBACK'); + } finally { + client.release(); + } + expect(await countRows(f.pool, 'vault_evaluations')).toBe(0); + }); + + test('agent principal accepts UUID versions 1-8 with RFC variants and rejects other versions and variants', async () => { + const f = await createIsolatedFixture(); + for (const ok of [ + 'agent:11111111-2222-1333-8444-555555555555', + 'agent:11111111-2222-7333-9444-555555555555', + 'agent:11111111-2222-8333-a444-555555555555', + 'agent:11111111-2222-4333-b444-555555555555', + ]) { + await insertTrial(f.pool, { principalId: ok }); + } + for (const bad of [ + 'agent:11111111-2222-0333-8444-555555555555', + 'agent:11111111-2222-9333-8444-555555555555', + 'agent:11111111-2222-4333-c444-555555555555', + 'agent:11111111-2222-4333-7444-555555555555', + 'agent:111111112222433384445555555555555', + 'agent:', + ]) { + await expectPgErrorCode(insertTrial(f.pool, { principalId: bad }), '23514'); + } + expect(await countRows(f.pool, 'trial_ledger')).toBe(4); + }); + + test('trial_ledger keeps daily_series values exactly as REAL and stores created_at from the server clock', async () => { + const f = await createIsolatedFixture(); + const row = ( + await f.pool.query( + `INSERT INTO trial_ledger (principal_id, stage, universe_market, horizon_bucket, + expression_hash, sample_start, sample_end, n_obs, sharpe_unann, skewness, kurtosis, daily_series) + VALUES ('operator', 'screen', 'FX', 'intraday', $1, '2021-01-04', '2021-01-05', 2, 0, 0, 3, + ARRAY[0.5::real, -0.25::real, 0::real]) + RETURNING daily_series::text AS series, created_at = transaction_timestamp() AS server_ts`, + [VALID_HASH] + ) + ).rows[0]; + expect(row.series).toBe('{0.5,-0.25,0}'); + expect(row.server_ts).toBe(true); + }); + } +);