diff --git a/libs/research/test/mcp-postgres-copilot.test.ts b/libs/research/test/mcp-postgres-copilot.test.ts index 3070eab0..cdb0d3d8 100644 --- a/libs/research/test/mcp-postgres-copilot.test.ts +++ b/libs/research/test/mcp-postgres-copilot.test.ts @@ -991,3 +991,149 @@ describe('createPostgresResearchCopilotServer', () => { } }); }); + +describe('createPostgresResearchCopilotServer (QA edge cases)', () => { + test('clamps event_study session-date request to exactly the last pre-cutoff session', async () => { + const fixture = buildEventStudyFixture(); + const { options, sessionDateCalls, executor } = buildValidOptions({ fixture }); + const server = createPostgresResearchCopilotServer(options); + + const res = await callMcpTool(server, 1, 'event_study', { + listingIds: [...fixture.listingIds], + from: fixture.firstDate, + to: fixture.allDates[fixture.allDates.length - 1]!, + event: { kinds: ['filing'], subtypes: ['8-K'] }, + preDays: 2, + postDays: 3, + universeMarket: 'US_EQ', + horizonBucket: 'swing_1_10d', + benchmark: 'equal_weight', + }); + + expect(res.parsed?.status).toBe('ok'); + expect(sessionDateCalls).toHaveLength(1); + const requested = sessionDateCalls[0]!; + // Fixture pins lastPreCutoffDate (2023-02-27) as the calendar day before the cutoff + // (2023-02-28), so an off-by-one clamp that drops the final session fails here. + expect(fixture.lastPreCutoffDate).toBe('2023-02-27'); + expect(fixture.vaultCutoffDate).toBe('2023-02-28'); + expect(requested.toDate).toBe(fixture.lastPreCutoffDate); + const served = fixture.allDates.filter( + d => d >= requested.fromDate && d <= requested.toDate + ); + expect(served[served.length - 1]).toBe(fixture.lastPreCutoffDate); + expect(executor.ledgerRows[0]!.sample_end <= fixture.lastPreCutoffDate).toBe(true); + }); + + test('binds the UTC calendar date of an intraday vaultCutoffTs to trial-ref and symbol-search queries', async () => { + const intradayCutoff = Date.UTC(2024, 6, 1, 15, 30, 0, 0); // 2024-07-01T15:30Z + const executor = new RecordingPostgresExecutor({ trials: [{ id: '42', sampleEnd: '2024-06-30' }] }); + const { options } = buildValidOptions({ executor, vaultCutoffTs: intradayCutoff }); + const server = createPostgresResearchCopilotServer(options); + + await callMcpTool(server, 1, 'search_symbols', { query: 'ACME' }); + const fileRes = await callMcpTool(server, 2, 'file_finding', { + kind: 'finding', + title: 'Intraday cutoff', + summary: 'Cutoff date derivation check.', + trialRefs: ['42'], + }); + + expect(fileRes.parsed?.created).toBe(true); + expect(executor.calls[0]?.params?.[0]).toBe('2024-07-01'); + expect(executor.calls[1]?.params).toEqual(['42', '2024-07-01']); + }); + + test('rejects unknown and non-canonical trial refs without inserting a finding', async () => { + const executor = new RecordingPostgresExecutor(); + const { options } = buildValidOptions({ + executor, + vaultCutoffTs: Date.UTC(2024, 6, 1, 0, 0, 0, 0), + }); + const server = createPostgresResearchCopilotServer(options); + + for (const [i, ref] of ['999', '042', '-1', 'abc'].entries()) { + const res = await callMcpTool(server, i + 1, 'file_finding', { + kind: 'finding', + title: 'Bad ref', + summary: 'Must not be filed.', + trialRefs: [ref], + }); + expect(res.envelope.isError).toBe(true); + } + expect(executor.calls.some(c => c.text.includes('INSERT INTO'))).toBe(false); + }); + + test('ignores pre-built ports and a stray vaultCutoffTs smuggled onto the options object', async () => { + const executor = new RecordingPostgresExecutor({ trials: [{ id: '42', sampleEnd: '2024-06-01' }] }); + const { options } = buildValidOptions({ + executor, + vaultCutoffTs: Date.UTC(2024, 5, 1, 0, 0, 0, 0), // 2024-06-01: trial 42 is Vault-only + }); + let smuggledUsed = false; + const smuggled = { + ...options, + vaultCutoffTs: Date.UTC(2030, 0, 1), + ledger: { query: async () => { smuggledUsed = true; return { rows: [] }; } }, + fileFinding: { + trialLookup: { hasResearchVisibleTrial: async () => { smuggledUsed = true; return true; } }, + }, + } as unknown as PostgresResearchCopilotServerOptions; + const server = createPostgresResearchCopilotServer(smuggled); + + const res = await callMcpTool(server, 1, 'file_finding', { + kind: 'finding', + title: 'Smuggle attempt', + summary: 'Holdout-backed trial must stay invisible.', + trialRefs: ['42'], + }); + + expect(res.envelope.isError).toBe(true); + expect(smuggledUsed).toBe(false); + expect(executor.calls).toHaveLength(1); + expect(executor.calls[0]?.params).toEqual(['42', '2024-06-01']); + }); + + test('keeps two servers isolated to their own executors', async () => { + const a = buildValidOptions(); + const b = buildValidOptions(); + const serverA = createPostgresResearchCopilotServer(a.options); + createPostgresResearchCopilotServer(b.options); + + await callMcpTool(serverA, 1, 'search_symbols', { query: 'ACME' }); + + expect(a.executor.calls).toHaveLength(1); + expect(b.executor.calls).toHaveLength(0); + }); + + test('lists tools over JSON-RPC tools/list in RESEARCH_COPILOT_TOOL_NAMES order', async () => { + const { options, executor } = buildValidOptions(); + const server = createPostgresResearchCopilotServer(options); + + const raw = await server.handleMessage( + JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'tools/list' }) + ); + const rpc = JSON.parse(raw!) as { result: { tools: { name: string }[] } }; + + expect(rpc.result.tools.map(t => t.name)).toEqual([...RESEARCH_COPILOT_TOOL_NAMES]); + expect(executor.calls).toHaveLength(0); + }); + + test.each([ + ['uppercase schema', { schema: 'Research_X' }], + ['quoted schema injection', { schema: 'x"; DROP TABLE findings; --' }], + ['NaN vault cutoff', { vaultCutoffTs: Number.NaN }], + ] as const)('fails closed at construction with zero queries for %s', (_label, override) => { + const { options, executor } = buildValidOptions(); + const bad = { + ...options, + ...('schema' in override ? { schema: override.schema } : {}), + ...('vaultCutoffTs' in override + ? { vaultContext: { vaultCutoffTs: override.vaultCutoffTs } } + : {}), + }; + + expect(() => createPostgresResearchCopilotServer(bad)).toThrow(); + expect(executor.calls).toHaveLength(0); + }); +});