diff --git a/libs/research/test/feature-library.test.ts b/libs/research/test/feature-library.test.ts index 4200a10e..1fe1994f 100644 --- a/libs/research/test/feature-library.test.ts +++ b/libs/research/test/feature-library.test.ts @@ -1115,3 +1115,90 @@ describe('RET_1D_V1 economic reference check (planned SQL + hand-calculated fixt expect(targetYearRows[2]?.value).toBeCloseTo(0.04, 12); }); }); + +describe('QA: relation-source allow-list and Vault boundary edge cases', () => { + const positionalBypassSqls = [ + // Explicit alias, then DuckDB POSITIONAL JOIN to an undeclared catalog table. + 'SELECT b.listing_id, b.ts, b.ts AS known_at, s.v AS value FROM {{input:bars_1d}} AS b POSITIONAL JOIN staging_tbl AS s', + // Bare alias form. + 'SELECT b.listing_id, b.ts, b.ts AS known_at, s.v AS value FROM {{input:bars_1d}} b POSITIONAL JOIN staging_tbl s', + // Inside a CTE body. + 'WITH x AS (SELECT b.listing_id, b.ts, s.v FROM {{input:bars_1d}} AS b POSITIONAL JOIN staging_tbl AS s) SELECT listing_id, ts, ts AS known_at, v AS value FROM x', + ]; + + it('rejects POSITIONAL JOIN to an undeclared relation after an aliased input (registry and runner never execute it)', async () => { + for (const sql of positionalBypassSqls) { + const def = makeSimpleDef({ name: 'positional_leak', sql }); + let thrown: unknown; + try { + validateFeatureDefinition(def); + } catch (error) { + thrown = error; + } + expect(thrown).toBeInstanceOf(FeatureLibraryError); + expect(['EXTERNAL_DATA_ACCESS', 'INVALID_SQL_STATEMENT']).toContain( + (thrown as FeatureLibraryError).code + ); + + expect(() => createFeatureRegistry().registerSignal(def)).toThrow(FeatureLibraryError); + + const fake = createRecordingFakeExecutor(); + await expect( + runFeatureBatch( + def, + { curatedRoot: '/c', fromYear: 2021, toYear: 2021, vaultCutoffDate: '2022-06-30' }, + fake.executor + ) + ).rejects.toBeInstanceOf(FeatureLibraryError); + expect(fake.calls).toHaveLength(0); + } + }); + + it('still rejects a POSITIONAL JOIN to an undeclared relation chained after an ON predicate', () => { + expectFeatureErrorCode( + () => + validateFeatureDefinition( + makeSimpleDef({ + sql: 'SELECT b.listing_id, b.ts, b.ts AS known_at, b.close AS value FROM {{input:bars_1d}} AS b JOIN {{input:bars_1d}} AS c ON b.ts = c.ts POSITIONAL JOIN staging_tbl', + }) + ), + 'EXTERNAL_DATA_ACCESS' + ); + }); + + it('accepts a leap-day cutoff and clamps the cutoff year to the last microsecond of the prior day', () => { + const writes = planFeatureBatch(RET_1D_V1, { + curatedRoot: '/c', + fromYear: 2023, + toYear: 2025, + vaultCutoffDate: '2024-02-29', + }); + expect(writes.map(w => w.year)).toEqual([2023, 2024]); + const ceiling = '2024-02-28T23:59:59.999999Z'; + expect(writes[1]?.params).toEqual([ceiling, ceiling, 2024, ceiling, ceiling]); + expect(writes[0]?.params).toEqual([ + '2023-12-31T23:59:59.999999Z', + ceiling, + 2023, + '2023-12-31T23:59:59.999999Z', + ceiling, + ]); + }); + + it('plans the cutoff year when only its first day precedes a January 2 cutoff', () => { + const writes = planFeatureBatch(RET_1D_V1, { + curatedRoot: '/c', + fromYear: 2022, + toYear: 2022, + vaultCutoffDate: '2022-01-02', + }); + expect(writes.map(w => w.year)).toEqual([2022]); + const ceiling = '2022-01-01T23:59:59.999999Z'; + expect(writes[0]?.params).toEqual([ceiling, ceiling, 2022, ceiling, ceiling]); + for (const param of writes[0]?.params ?? []) { + if (typeof param === 'string') { + expect(param < '2022-01-02').toBe(true); + } + } + }); +});