diff --git a/libs/research/src/mcp/adapters/exchange-calendars.ts b/libs/research/src/mcp/adapters/exchange-calendars.ts index c9cf3872..801230f6 100644 --- a/libs/research/src/mcp/adapters/exchange-calendars.ts +++ b/libs/research/src/mcp/adapters/exchange-calendars.ts @@ -425,6 +425,14 @@ function parseResolverDayKey( ); } +function isPlainObjectRecord(value: unknown): value is Record { + if (typeof value !== 'object' || value === null) { + return false; + } + const proto: unknown = Object.getPrototypeOf(value); + return proto === Object.prototype || proto === null; +} + function parseEarlyCloses( raw: unknown ): Map> { @@ -432,7 +440,7 @@ function parseEarlyCloses( if (raw === undefined) { return result; } - if (!isPlainRecord(raw)) { + if (!isPlainObjectRecord(raw)) { throw new ExchangeCalendarResolverError( 'INVALID_OPTION', 'Invalid "earlyCloses": expected a plain object mapping calendarCode to { [YYYY-MM-DD]: minuteOfDay }.' @@ -448,7 +456,7 @@ function parseEarlyCloses( `Invalid "earlyCloses" calendar code "${calendarCode}": expected a non-blank trimmed string.` ); } - if (!isPlainRecord(rawDates)) { + if (!isPlainObjectRecord(rawDates)) { throw new ExchangeCalendarResolverError( 'INVALID_OPTION', `Invalid "earlyCloses.${calendarCode}": expected a plain object mapping YYYY-MM-DD dates to Eastern close minutes.` diff --git a/libs/research/test/mcp-exchange-calendars.test.ts b/libs/research/test/mcp-exchange-calendars.test.ts index 6526da0d..cb85f02c 100644 --- a/libs/research/test/mcp-exchange-calendars.test.ts +++ b/libs/research/test/mcp-exchange-calendars.test.ts @@ -289,29 +289,15 @@ describe('createPostgresListingCalendarPort', () => { }); describe('createExchangeCalendarResolver — multi-calendar, earlyCloses, and buildEventStudyInput integration', () => { - test('calls listSessionDates once per distinct calendar, anchors early-close vs 16:00 ET in buildEventStudyInput, and respects Vault cutoff', async () => { - const xnysDates = ['2024-11-27', '2024-11-29', '2024-12-02', '2024-12-03']; - const xtseDates = ['2024-11-27', '2024-11-28', '2024-11-29', '2024-12-02', '2024-12-03']; - - const makePorts = () => { - const sessionCalls: SessionDatesQuery[] = []; - const listingPort: ListingCalendarPort = { - async getListingCalendars() { - return [ - { listingId: 101, calendarCode: 'XNYS' }, - { listingId: 102, calendarCode: 'XNYS' }, - { listingId: 201, calendarCode: 'XTSE' }, - ]; - }, - }; - const sessionPort: SessionDatesPort = { - async listSessionDates(q) { - sessionCalls.push(q); - return q.calendarCode === 'XNYS' ? xnysDates : xtseDates; - }, - }; - return { listingPort, sessionPort, sessionCalls }; - }; + const createMultiCalendarFixture = () => { + const xnysDates = ['2024-11-27', '2024-11-29', '2024-12-02', '2024-12-03'] as const; + const xtseDates = [ + '2024-11-27', + '2024-11-28', + '2024-11-29', + '2024-12-02', + '2024-12-03', + ] as const; const bars: BarRecord[] = [ // Listing 101 (XNYS) — closed on US Thanksgiving 2024-11-28 @@ -342,6 +328,40 @@ describe('createExchangeCalendarResolver — multi-calendar, earlyCloses, and bu { listingId: 201, knownAt: eventTs, kind: 'filing', subtype: '8-K' }, ]; + return { + xnysDates, + xtseDates, + bars, + events, + fromDate: '2024-11-27', + toDate: '2024-12-03', + vaultCutoffDate: '2024-12-04', + }; + }; + + test('calls listSessionDates once per distinct calendar, anchors early-close vs 16:00 ET in buildEventStudyInput, and respects Vault cutoff', async () => { + const fixture = createMultiCalendarFixture(); + + const makePorts = () => { + const sessionCalls: SessionDatesQuery[] = []; + const listingPort: ListingCalendarPort = { + async getListingCalendars() { + return [ + { listingId: 101, calendarCode: 'XNYS' }, + { listingId: 102, calendarCode: 'XNYS' }, + { listingId: 201, calendarCode: 'XTSE' }, + ]; + }, + }; + const sessionPort: SessionDatesPort = { + async listSessionDates(q) { + sessionCalls.push(q); + return q.calendarCode === 'XNYS' ? fixture.xnysDates : fixture.xtseDates; + }, + }; + return { listingPort, sessionPort, sessionCalls }; + }; + // 1) Baseline: no earlyCloses (all sessions close at 16:00 ET = 21:00Z in EST) const baselinePorts = makePorts(); const baselineResolver = createExchangeCalendarResolver({ @@ -350,13 +370,13 @@ describe('createExchangeCalendarResolver — multi-calendar, earlyCloses, and bu }); const baselineCal = await baselineResolver({ listingIds: [101, 102, 201], - fromDate: '2024-11-27', - toDate: '2024-12-03', + fromDate: fixture.fromDate, + toDate: fixture.toDate, }); expect(baselinePorts.sessionCalls).toEqual([ - { calendarCode: 'XNYS', fromDate: '2024-11-27', toDate: '2024-12-03' }, - { calendarCode: 'XTSE', fromDate: '2024-11-27', toDate: '2024-12-03' }, + { calendarCode: 'XNYS', fromDate: fixture.fromDate, toDate: fixture.toDate }, + { calendarCode: 'XTSE', fromDate: fixture.fromDate, toDate: fixture.toDate }, ]); expect(baselineCal.listingCalendarId).toEqual({ 101: 'XNYS', @@ -365,9 +385,9 @@ describe('createExchangeCalendarResolver — multi-calendar, earlyCloses, and bu }); const baselinePanel = buildEventStudyInput({ - bars, - events, - vaultCutoffDate: '2024-12-04', + bars: fixture.bars, + events: fixture.events, + vaultCutoffDate: fixture.vaultCutoffDate, benchmark: 'equal_weight', exchangeCalendars: baselineCal, }); @@ -408,8 +428,8 @@ describe('createExchangeCalendarResolver — multi-calendar, earlyCloses, and bu const overrideCal = await overrideResolver({ listingIds: [101, 102, 201], - fromDate: '2024-11-27', - toDate: '2024-12-03', + fromDate: fixture.fromDate, + toDate: fixture.toDate, }); const xnysSessions = ( @@ -430,9 +450,9 @@ describe('createExchangeCalendarResolver — multi-calendar, earlyCloses, and bu ); const overridePanel = buildEventStudyInput({ - bars, - events, - vaultCutoffDate: '2024-12-04', + bars: fixture.bars, + events: fixture.events, + vaultCutoffDate: fixture.vaultCutoffDate, benchmark: 'equal_weight', exchangeCalendars: overrideCal, }); @@ -445,8 +465,7 @@ describe('createExchangeCalendarResolver — multi-calendar, earlyCloses, and bu }); test('is deterministic under permuted listingIds and reversed listing rows without mutating caller inputs', async () => { - const xnysDates = ['2024-11-29', '2024-12-02']; - const xtseDates = ['2024-11-29', '2024-12-02']; + const fixture = createMultiCalendarFixture(); const runWithOrder = async ( listingIds: number[], @@ -460,7 +479,7 @@ describe('createExchangeCalendarResolver — multi-calendar, earlyCloses, and bu }, sessionDates: { listSessionDates: (q) => { - const dates = q.calendarCode === 'XNYS' ? xnysDates : xtseDates; + const dates = q.calendarCode === 'XNYS' ? fixture.xnysDates : fixture.xtseDates; return asyncSessionPort ? Promise.resolve(dates) : dates; }, }, @@ -469,8 +488,8 @@ describe('createExchangeCalendarResolver — multi-calendar, earlyCloses, and bu const resolved = await resolver({ listingIds, - fromDate: '2024-11-29', - toDate: '2024-12-02', + fromDate: fixture.fromDate, + toDate: fixture.toDate, }); expect(listingIds).toEqual(snapshot); return resolved; @@ -503,6 +522,43 @@ describe('createExchangeCalendarResolver — multi-calendar, earlyCloses, and bu expect(Object.keys(permuted.listingCalendarId)).toEqual( Object.keys(forward.listingCalendarId) ); + + // Replay both resolved calendars through buildEventStudyInput on the same pinned bars, + // events, and Vault cutoff, and assert identical anchors, return series, and diagnostics. + const forwardPanel = buildEventStudyInput({ + bars: fixture.bars, + events: fixture.events, + vaultCutoffDate: fixture.vaultCutoffDate, + benchmark: 'equal_weight', + exchangeCalendars: forward, + }); + const permutedPanel = buildEventStudyInput({ + bars: fixture.bars, + events: fixture.events, + vaultCutoffDate: fixture.vaultCutoffDate, + benchmark: 'equal_weight', + exchangeCalendars: permuted, + }); + + expect(permutedPanel.input.sessionDates).toEqual(forwardPanel.input.sessionDates); + expect(permutedPanel.input.events).toEqual(forwardPanel.input.events); + expect(permutedPanel.input.events).toEqual([ + { entityId: 201, dayIndex: 2 }, + { entityId: 101, dayIndex: 3 }, + ]); + expect(Array.from(permutedPanel.input.entityReturns.keys())).toEqual( + Array.from(forwardPanel.input.entityReturns.keys()) + ); + for (const [entityId, forwardSeries] of forwardPanel.input.entityReturns.entries()) { + const permutedSeries = permutedPanel.input.entityReturns.get(entityId); + expect(permutedSeries).toBeDefined(); + expect(Array.from(permutedSeries as Float64Array)).toEqual(Array.from(forwardSeries)); + } + expect(Array.from(permutedPanel.input.benchmarkReturns)).toEqual( + Array.from(forwardPanel.input.benchmarkReturns) + ); + expect(permutedPanel.diagnostics).toEqual(forwardPanel.diagnostics); + expect(permutedPanel.diagnostics.vaultExcludedDates).toBe(1); }); }); @@ -656,6 +712,16 @@ describe('createExchangeCalendarResolver — fail-closed validation & edge cases { earlyCloses: null, expectedCode: 'INVALID_OPTION' }, { earlyCloses: [], expectedCode: 'INVALID_OPTION' }, { earlyCloses: 'XNYS', expectedCode: 'INVALID_OPTION' }, + { + earlyCloses: new Map([['XNYS', { '2024-11-29': 780 }]]), + expectedCode: 'INVALID_OPTION', + }, + { + earlyCloses: { XNYS: new Map([['2024-11-29', 780]]) }, + expectedCode: 'INVALID_OPTION', + }, + { earlyCloses: new Set(['XNYS']), expectedCode: 'INVALID_OPTION' }, + { earlyCloses: { XNYS: new Date('2024-11-29T00:00:00Z') }, expectedCode: 'INVALID_OPTION' }, { earlyCloses: { '': { '2024-11-29': 780 } }, expectedCode: 'INVALID_OPTION' }, { earlyCloses: { ' XNYS ': { '2024-11-29': 780 } }, expectedCode: 'INVALID_OPTION' }, { earlyCloses: { XNYS: null }, expectedCode: 'INVALID_OPTION' }, @@ -690,4 +756,34 @@ describe('createExchangeCalendarResolver — fail-closed validation & edge cases expect(listingCalls).toBe(0); expect(sessionCalls).toBe(0); }); + + test('accepts null-prototype records at both levels of earlyCloses', async () => { + const xnysOverrides = Object.create(null) as Record; + xnysOverrides['2024-11-29'] = 780; + const earlyCloses = Object.create(null) as Record>; + earlyCloses.XNYS = xnysOverrides; + + const resolver = createExchangeCalendarResolver({ + listingCalendars: { + getListingCalendars: () => [{ listingId: 101, calendarCode: 'XNYS' }], + }, + sessionDates: { + listSessionDates: () => ['2024-11-29', '2024-12-02'], + }, + earlyCloses, + }); + + const result = await resolver({ + listingIds: [101], + fromDate: '2024-11-29', + toDate: '2024-12-02', + }); + + expect(result.calendars).toEqual({ + XNYS: [ + { date: '2024-11-29', closeTs: utcMs(2024, 11, 29, 18, 0, 0) }, + { date: '2024-12-02', closeTs: utcMs(2024, 12, 2, 21, 0, 0) }, + ], + }); + }); });