From 95e00c2f26040033a508490962615d291af87164 Mon Sep 17 00:00:00 2001 From: Studio Date: Fri, 4 Dec 2026 15:41:41 -0500 Subject: [PATCH] T-210: tests --- .../test/duckdb-market-data-store.test.ts | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) diff --git a/libs/data/duckdb/test/duckdb-market-data-store.test.ts b/libs/data/duckdb/test/duckdb-market-data-store.test.ts index f10c9b2b..c57976a3 100644 --- a/libs/data/duckdb/test/duckdb-market-data-store.test.ts +++ b/libs/data/duckdb/test/duckdb-market-data-store.test.ts @@ -1540,3 +1540,177 @@ describe('macroAsOf and crossSection via real DuckDB', () => { } }); }); + +describe('macroAsOf and crossSection point-in-time edge cases via real DuckDB (QA)', () => { + const budgetEnv = (duckTempDir: string) => ({ + STOCK_BOT_DUCKDB_MEMORY_LIMIT: '2GB', + STOCK_BOT_DUCKDB_THREADS: '2', + STOCK_BOT_DUCKDB_TEMP_DIR: duckTempDir, + }); + + test('crossSection keeps recently delisted listings, picks latest pre-asOf restatement, and never leaks post-cutoff bars or feature revisions', async () => { + // Arrange + const root = await mkdtemp(join(tmpdir(), 'stockbot-duckdb-xsec-pit-')); + tempDirs.push(root); + const curatedRoot = join(root, 'curated'); + const duckTempDir = join(root, 'duckdb-tmp'); + await mkdir(join(curatedRoot, 'bars_1d', 'year=2025'), { recursive: true }); + await mkdir(join(curatedRoot, 'features', 'mom_5d', 'v2', 'year=2025'), { recursive: true }); + await mkdir(duckTempDir, { recursive: true }); + + const vaultCutoffTs = Date.UTC(2025, 4, 15, 0, 0, 0, 0); // 2025-05-15T00:00:00Z + const writer = await createNodeDuckDbExecutor(); + try { + // 301: last bar 2025-05-08 (inside 7-day lookback from 2025-05-14) -> recently delisted, kept. + // 302: last bar 2025-05-06 (outside lookback) -> excluded. + // 303: first bar 2025-05-15 (post-cutoff session) -> excluded. + // 304: 2025-05-14 bar whose known_at lands after the cutoff -> excluded even with a late asOf. + // 305: active 2025-05-14. + await writer.run(` + COPY ( + SELECT * FROM (VALUES + (301::INTEGER, DATE '2025-05-08', TIMESTAMPTZ '2025-05-08 20:00:00+00', 1.0::DOUBLE, 1.0::DOUBLE, 1.0::DOUBLE, 1.0::DOUBLE, 10.0::DOUBLE, NULL::DOUBLE, 'USD'::VARCHAR, TIMESTAMPTZ '2025-05-08 20:00:00+00'), + (302::INTEGER, DATE '2025-05-06', TIMESTAMPTZ '2025-05-06 20:00:00+00', 1.0::DOUBLE, 1.0::DOUBLE, 1.0::DOUBLE, 1.0::DOUBLE, 10.0::DOUBLE, NULL::DOUBLE, 'USD'::VARCHAR, TIMESTAMPTZ '2025-05-06 20:00:00+00'), + (303::INTEGER, DATE '2025-05-15', TIMESTAMPTZ '2025-05-15 20:00:00+00', 1.0::DOUBLE, 1.0::DOUBLE, 1.0::DOUBLE, 1.0::DOUBLE, 10.0::DOUBLE, NULL::DOUBLE, 'USD'::VARCHAR, TIMESTAMPTZ '2025-05-15 20:00:00+00'), + (304::INTEGER, DATE '2025-05-14', TIMESTAMPTZ '2025-05-14 20:00:00+00', 1.0::DOUBLE, 1.0::DOUBLE, 1.0::DOUBLE, 1.0::DOUBLE, 10.0::DOUBLE, NULL::DOUBLE, 'USD'::VARCHAR, TIMESTAMPTZ '2025-05-15 01:00:00+00'), + (305::INTEGER, DATE '2025-05-14', TIMESTAMPTZ '2025-05-14 20:00:00+00', 1.0::DOUBLE, 1.0::DOUBLE, 1.0::DOUBLE, 1.0::DOUBLE, 10.0::DOUBLE, NULL::DOUBLE, 'USD'::VARCHAR, TIMESTAMPTZ '2025-05-14 20:00:00+00') + ) AS t(listing_id, session_date, ts, open, high, low, close, volume, vwap, currency, known_at) + ) TO '${join(curatedRoot, 'bars_1d', 'year=2025', 'part.parquet')}' (FORMAT PARQUET) + `); + + // mom_5d@v2: + // - 305: original (known 20:00, 1.0), restatement (known 22:00, 1.5), post-cutoff restatement (9.9). + // - 301: prior-day and next-day rows only -> no same-day value. + // - 304: same-day value, but listing is outside the PIT universe. + await writer.run(` + COPY ( + SELECT * FROM (VALUES + (305::INTEGER, TIMESTAMPTZ '2025-05-14 20:00:00+00', TIMESTAMPTZ '2025-05-14 20:00:00+00', 1.0::DOUBLE), + (305::INTEGER, TIMESTAMPTZ '2025-05-14 20:00:00+00', TIMESTAMPTZ '2025-05-14 22:00:00+00', 1.5::DOUBLE), + (305::INTEGER, TIMESTAMPTZ '2025-05-14 20:00:00+00', TIMESTAMPTZ '2025-05-15 01:00:00+00', 9.9::DOUBLE), + (301::INTEGER, TIMESTAMPTZ '2025-05-13 20:00:00+00', TIMESTAMPTZ '2025-05-13 20:00:00+00', 2.0::DOUBLE), + (301::INTEGER, TIMESTAMPTZ '2025-05-15 20:00:00+00', TIMESTAMPTZ '2025-05-15 20:00:00+00', 3.0::DOUBLE), + (304::INTEGER, TIMESTAMPTZ '2025-05-14 20:00:00+00', TIMESTAMPTZ '2025-05-14 20:00:00+00', 4.0::DOUBLE) + ) AS t(listing_id, ts, known_at, value) + ) TO '${join(curatedRoot, 'features', 'mom_5d', 'v2', 'year=2025', 'part.parquet')}' (FORMAT PARQUET) + `); + } finally { + await writer.close(); + } + + const opened = await openDuckDbMarketDataStore({ + curatedRoot, + vaultCutoffTs, + env: budgetEnv(duckTempDir), + }); + try { + // Act: asOf requested past the cutoff must clamp strictly below it. + const late = await opened.store.crossSection({ + features: ['mom_5d@v2'], + date: '2025-05-14', + universe: 'US_EQ', + asOf: '2025-06-30', + }); + // Act: asOf between original and restatement sees only the original. + const early = await opened.store.crossSection({ + features: ['mom_5d@v2'], + date: '2025-05-14', + universe: 'US_EQ', + asOf: '2025-05-14T21:00:00Z', + }); + // Act: CA universe has no CAD listings at all. + const ca = await opened.store.crossSection({ + features: ['mom_5d@v2'], + date: '2025-05-14', + universe: 'CA_EQ', + }); + + // Assert + expect(late.columns).toEqual(['listing_id', 'mom_5d@v2']); + expect(late.rows).toEqual([ + { listing_id: 301, 'mom_5d@v2': null }, + { listing_id: 305, 'mom_5d@v2': 1.5 }, + ]); + expect(early.rows).toEqual([ + { listing_id: 301, 'mom_5d@v2': null }, + { listing_id: 305, 'mom_5d@v2': 1.0 }, + ]); + expect(ca.rows).toEqual([]); + + // Path-traversal refs are rejected by validation. + await expect( + opened.store.crossSection({ + features: ['../mom_5d@v2'], + date: '2025-05-14', + universe: 'US_EQ', + }) + ).rejects.toBeInstanceOf(MarketDataStoreValidationError); + } finally { + await opened.close(); + } + }); + + test('macroAsOf treats known_at == asOf as visible and honours the period_date from bound and country filter', async () => { + // Arrange + const root = await mkdtemp(join(tmpdir(), 'stockbot-duckdb-macro-pit-')); + tempDirs.push(root); + const curatedRoot = join(root, 'curated'); + const duckTempDir = join(root, 'duckdb-tmp'); + await mkdir(join(curatedRoot, 'macro'), { recursive: true }); + await mkdir(duckTempDir, { recursive: true }); + + const vaultCutoffTs = Date.UTC(2025, 4, 1, 0, 0, 0, 0); + const writer = await createNodeDuckDbExecutor(); + try { + await writer.run(` + COPY ( + SELECT * FROM (VALUES + ('US'::VARCHAR, 'policy_rate'::VARCHAR, DATE '2024-12-31', TIMESTAMPTZ '2025-01-01 00:00:00+00', 'publication_lag_estimate'::VARCHAR, 4.75::DOUBLE), + ('US'::VARCHAR, 'policy_rate'::VARCHAR, DATE '2025-01-31', TIMESTAMPTZ '2025-02-01 00:00:00+00', 'publication_lag_estimate'::VARCHAR, 4.50::DOUBLE), + ('GB'::VARCHAR, 'policy_rate'::VARCHAR, DATE '2025-01-31', TIMESTAMPTZ '2025-02-01 00:00:00+00', 'publication_lag_estimate'::VARCHAR, 4.00::DOUBLE) + ) AS t(country_iso, indicator, period_date, known_at, known_at_source, value) + ) TO '${join(curatedRoot, 'macro', 'part.parquet')}' (FORMAT PARQUET) + `); + } finally { + await writer.close(); + } + + const opened = await openDuckDbMarketDataStore({ + curatedRoot, + vaultCutoffTs, + env: budgetEnv(duckTempDir), + }); + try { + // Act + const atBoundary = await opened.store.macroAsOf({ + countries: ['US', 'CA'], + indicators: ['policy_rate'], + from: '2025-01-01', + to: '2025-03-01', + asOf: '2025-02-01T00:00:00Z', + }); + const justBefore = await opened.store.macroAsOf({ + countries: ['US', 'CA'], + indicators: ['policy_rate'], + from: '2025-01-01', + to: '2025-03-01', + asOf: '2025-01-31T23:59:59.999Z', + }); + + // Assert + expect(atBoundary.rows).toEqual([ + { + country_iso: 'US', + indicator: 'policy_rate', + period_date: '2025-01-31', + known_at: Date.UTC(2025, 1, 1), + known_at_source: 'publication_lag_estimate', + value: 4.5, + }, + ]); + expect(justBefore.rows).toEqual([]); + } finally { + await opened.close(); + } + }); +});