T-158: tests

This commit is contained in:
Studio 2026-12-02 09:02:09 -05:00
parent 90ab20c301
commit acfd6a92da
2 changed files with 180 additions and 25 deletions

View file

@ -1,10 +1,10 @@
import { describe, expect, test } from 'bun:test';
import { MarketDataStoreValidationError } from '../src/market-data-store';
import {
createPostgresSymbolSearchPort,
PostgresSymbolSearchError,
type SymbolSearchExecutor,
type SymbolSearchQuery,
createPostgresSymbolSearchPort,
} from '../src/mcp/index';
interface RecordedCall {
@ -70,13 +70,7 @@ describe('createPostgresSymbolSearchPort (fake executor)', () => {
expect(call.text).not.toContain(';');
expect(call.text).not.toContain(injection);
expect(call.text).not.toContain('DROP TABLE');
expect(call.params).toEqual([
'2024-06-30',
`%${injection}%`,
injection,
null,
true,
]);
expect(call.params).toEqual(['2024-06-30', `%${injection}%`, injection, null, true]);
});
test('escapes %, _, and backslash literals in the ILIKE pattern while preserving raw query for ISIN', async () => {
@ -91,13 +85,7 @@ describe('createPostgresSymbolSearchPort (fake executor)', () => {
expect(fake.calls).toHaveLength(1);
const call = fake.calls[0]!;
expect(call.text).toContain("ESCAPE '\\'");
expect(call.params).toEqual([
'2024-06-30',
'%A\\%B\\_C\\\\D%',
'A%B_C\\D',
null,
true,
]);
expect(call.params).toEqual(['2024-06-30', '%A\\%B\\_C\\\\D%', 'A%B_C\\D', null, true]);
});
test('converts vaultCutoffTs epoch-ms to UTC calendar date across midnight boundaries', async () => {
@ -173,10 +161,16 @@ describe('createPostgresSymbolSearchPort (fake executor)', () => {
expect(sql).toContain('h_past.valid_from <= $1::date');
// Both fallback resolution and root ILIKE predicate are gated by NOT root_state.is_future
expect(sql).toContain('WHEN NOT root_state.is_future THEN l.ticker_root');
expect(sql).toContain("OR (NOT root_state.is_future AND l.ticker_root ILIKE $2::text ESCAPE '\\')");
expect(sql).toContain(
"OR (NOT root_state.is_future AND l.ticker_root ILIKE $2::text ESCAPE '\\')"
);
// delisted_at derivation and $5 filter
expect(sql).toContain("WHEN l.delist_reason IS NOT NULL AND l.delist_reason <> 'active' THEN l.listed_to");
expect(sql).toContain('($5::boolean OR delisted.delisted_at IS NULL OR delisted.delisted_at > $1::date)');
expect(sql).toContain(
"WHEN l.delist_reason IS NOT NULL AND l.delist_reason <> 'active' THEN l.listed_to"
);
expect(sql).toContain(
'($5::boolean OR delisted.delisted_at IS NULL OR delisted.delisted_at > $1::date)'
);
});
test('quotes optional validated schema on all security-master tables and omits prefix when unset', async () => {
@ -302,7 +296,9 @@ describe('createPostgresSymbolSearchPort (fake executor)', () => {
for (const opts of invalidOptions) {
let caught: unknown;
try {
createPostgresSymbolSearchPort(opts as Parameters<typeof createPostgresSymbolSearchPort>[0]);
createPostgresSymbolSearchPort(
opts as Parameters<typeof createPostgresSymbolSearchPort>[0]
);
} catch (err) {
caught = err;
}
@ -411,3 +407,83 @@ describe('createPostgresSymbolSearchPort (fake executor)', () => {
);
});
});
describe('createPostgresSymbolSearchPort (QA edge cases)', () => {
test('accepts a pg.Pool-shaped executor whose query method lives on the prototype', async () => {
class PrototypeExecutor {
readonly calls: RecordedCall[] = [];
async query<Row = Record<string, unknown>>(
text: string,
params?: readonly unknown[]
): Promise<{ readonly rows: readonly Row[] }> {
this.calls.push({ text, params });
return { rows: [makeRawRow()] as unknown as readonly Row[] };
}
}
const executor = new PrototypeExecutor();
const port = createPostgresSymbolSearchPort({ executor });
const rows = await port.searchSymbols(BASE_QUERY);
expect(rows.map(r => r.listingId)).toEqual([101]);
expect(executor.calls).toHaveLength(1);
});
test('accepts minimal valid schema identifiers and quotes them', async () => {
for (const schema of ['_', 'a', 'stock_bot_safety_0abc']) {
const fake = createFakeExecutor([]);
const port = createPostgresSymbolSearchPort({ executor: fake.executor, schema });
await port.searchSymbols(BASE_QUERY);
expect(fake.calls[0]!.text).toContain(`FROM "${schema}".listing l`);
}
});
test('propagates executor failures unchanged without retrying', async () => {
const failure = new Error('connection terminated');
let attempts = 0;
const executor: SymbolSearchExecutor = {
async query() {
attempts += 1;
throw failure;
},
};
const port = createPostgresSymbolSearchPort({ executor });
await expect(port.searchSymbols(BASE_QUERY)).rejects.toBe(failure);
expect(attempts).toBe(1);
});
test('binds the UTC calendar date for an instant that is already the next day east of UTC', async () => {
const fake = createFakeExecutor([]);
const port = createPostgresSymbolSearchPort({ executor: fake.executor });
// 2024-02-29T23:30Z is 2024-03-01 in UTC+1 zones; the adapter must still bind the UTC date.
await port.searchSymbols({ ...BASE_QUERY, vaultCutoffTs: Date.UTC(2024, 1, 29, 23, 30) });
expect(fake.calls[0]!.params?.[0]).toBe('2024-02-29');
});
test('returns an empty array (not null) when the executor returns no rows', async () => {
const fake = createFakeExecutor([]);
const port = createPostgresSymbolSearchPort({ executor: fake.executor });
expect(await port.searchSymbols(BASE_QUERY)).toEqual([]);
});
test('rejects the whole result when any row in a batch is malformed', async () => {
const fake = createFakeExecutor([
makeRawRow(),
makeRawRow({ listing_id: 102, last_bar_date: '2024-04-31' }),
]);
const port = createPostgresSymbolSearchPort({ executor: fake.executor });
let caught: unknown;
try {
await port.searchSymbols(BASE_QUERY);
} catch (err) {
caught = err;
}
expect(caught).toBeInstanceOf(PostgresSymbolSearchError);
expect((caught as PostgresSymbolSearchError).code).toBe('INVALID_ROW');
});
});