170 lines
6.5 KiB
TypeScript
170 lines
6.5 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
|
import {
|
|
resolveIsin,
|
|
type ResolveIsinInput,
|
|
} from '../../apps/stock/data-pipeline/src/normalization/identifiers';
|
|
import {
|
|
parseTicker,
|
|
parseTickerDetailed,
|
|
type TickerSource,
|
|
} from '../../apps/stock/data-pipeline/src/normalization/ticker';
|
|
|
|
const ALL_SOURCES: readonly TickerSource[] = ['eod', 'qm', 'ib', 'ceo'];
|
|
const NON_CEO_SOURCES: readonly TickerSource[] = ['eod', 'qm', 'ib'];
|
|
|
|
describe('parseTicker', () => {
|
|
test('canonicalizes separator variants across all sources', () => {
|
|
for (const source of ALL_SOURCES) {
|
|
expect(parseTicker('AAA-P', source)).toEqual({ tickerRoot: 'AAA', classSuffix: 'P' });
|
|
expect(parseTicker('AAA- P', source)).toEqual({ tickerRoot: 'AAA', classSuffix: 'P' });
|
|
expect(parseTicker('AAA.\tP', source)).toEqual({ tickerRoot: 'AAA', classSuffix: 'P' });
|
|
expect(parseTicker('AAA.P', source)).toEqual({ tickerRoot: 'AAA', classSuffix: 'P' });
|
|
expect(parseTicker('AACT U', source)).toEqual({ tickerRoot: 'AACT', classSuffix: 'U' });
|
|
expect(parseTicker('AACT U', source)).toEqual({ tickerRoot: 'AACT', classSuffix: 'U' });
|
|
expect(parseTicker('AACT.U', source)).toEqual({ tickerRoot: 'AACT', classSuffix: 'U' });
|
|
expect(parseTicker('SCU.UN', source)).toEqual({ tickerRoot: 'SCU', classSuffix: 'UN' });
|
|
expect(parseTicker('RY.PR.A', source)).toEqual({ tickerRoot: 'RY', classSuffix: 'PR.A' });
|
|
expect(parseTicker('RY-PR-A', source)).toEqual({ tickerRoot: 'RY', classSuffix: 'PR.A' });
|
|
expect(parseTicker('RY- PR- A', source)).toEqual({ tickerRoot: 'RY', classSuffix: 'PR.A' });
|
|
expect(parseTicker('RY PR A', source)).toEqual({ tickerRoot: 'RY', classSuffix: 'PR.A' });
|
|
}
|
|
});
|
|
|
|
test('parses plain tickers with empty classSuffix and uppercases trimmed input', () => {
|
|
expect(parseTicker('AAPL', 'eod')).toEqual({ tickerRoot: 'AAPL', classSuffix: '' });
|
|
expect(parseTicker(' shop ', 'qm')).toEqual({ tickerRoot: 'SHOP', classSuffix: '' });
|
|
expect(parseTicker('RY', 'ib')).toEqual({ tickerRoot: 'RY', classSuffix: '' });
|
|
});
|
|
|
|
test('strips embedded .TO and .V venue suffixes only for ceo source', () => {
|
|
expect(parseTicker('SHOP.TO', 'ceo')).toEqual({ tickerRoot: 'SHOP', classSuffix: '' });
|
|
expect(parseTicker('ABC.V', 'ceo')).toEqual({ tickerRoot: 'ABC', classSuffix: '' });
|
|
expect(parseTicker('SCU.UN.TO', 'ceo')).toEqual({ tickerRoot: 'SCU', classSuffix: 'UN' });
|
|
expect(parseTicker('XYZ.WT.V', 'ceo')).toEqual({ tickerRoot: 'XYZ', classSuffix: 'WT' });
|
|
expect(parseTicker('ry.pr.a.to', 'ceo')).toEqual({ tickerRoot: 'RY', classSuffix: 'PR.A' });
|
|
});
|
|
|
|
test('does not strip .TO or .V as venue suffixes for non-ceo sources', () => {
|
|
for (const source of NON_CEO_SOURCES) {
|
|
expect(parseTicker('ABC.V', source)).toEqual({ tickerRoot: 'ABC', classSuffix: 'V' });
|
|
expect(parseTicker('SHOP.TO', source)).toEqual({ tickerRoot: 'SHOP', classSuffix: 'TO' });
|
|
}
|
|
});
|
|
|
|
test('returns null for empty, whitespace, and malformed inputs without throwing', () => {
|
|
const invalidInputs: (string | null | undefined)[] = [
|
|
'',
|
|
' ',
|
|
'.',
|
|
'.AAPL',
|
|
'-A',
|
|
'AAPL.',
|
|
'AAPL-',
|
|
'A..B',
|
|
'A--B',
|
|
'AAA- -P',
|
|
'RY.PR.',
|
|
'RY-PR--A',
|
|
'RY-PR- -A',
|
|
null,
|
|
undefined,
|
|
];
|
|
for (const source of ALL_SOURCES) {
|
|
for (const raw of invalidInputs) {
|
|
expect(parseTicker(raw, source)).toBeNull();
|
|
}
|
|
}
|
|
expect(parseTicker('.TO', 'ceo')).toBeNull();
|
|
expect(parseTicker('.V', 'ceo')).toBeNull();
|
|
expect(parseTicker(' .TO', 'ceo')).toBeNull();
|
|
expect(parseTicker('..TO', 'ceo')).toBeNull();
|
|
});
|
|
|
|
test('enforces listing.class_suffix VARCHAR(12) max length', () => {
|
|
expect(parseTicker('RY.ABCDEFGHIJKL', 'eod')).toEqual({
|
|
tickerRoot: 'RY',
|
|
classSuffix: 'ABCDEFGHIJKL',
|
|
});
|
|
expect(parseTicker('RY.ABCDEFGHIJKLM', 'eod')).toBeNull();
|
|
expect(parseTicker('RY.PR.ABCDEFGHIJ', 'eod')).toBeNull();
|
|
|
|
expect(parseTickerDetailed('RY.ABCDEFGHIJKLM', 'eod')).toEqual({
|
|
ok: false,
|
|
reason: 'class_suffix_too_long',
|
|
tickerRoot: 'RY',
|
|
classSuffix: 'ABCDEFGHIJKLM',
|
|
});
|
|
expect(parseTickerDetailed('RY.PR.ABCDEFGHIJ.TO', 'ceo')).toEqual({
|
|
ok: false,
|
|
reason: 'class_suffix_too_long',
|
|
tickerRoot: 'RY',
|
|
classSuffix: 'PR.ABCDEFGHIJ',
|
|
});
|
|
expect(parseTickerDetailed('RY..A', 'eod')).toEqual({
|
|
ok: false,
|
|
reason: 'invalid_ticker',
|
|
});
|
|
});
|
|
|
|
test('completes 100,000 resolveIsin + parseTicker calls in under 500 ms', () => {
|
|
const isinCorpus: readonly ResolveIsinInput[] = [
|
|
{ isin: 'US0378331005' },
|
|
{ isin: 'CA7800871029' },
|
|
{ isin: 'CA82509L1070' },
|
|
{ isin: 'CATSETEST001' },
|
|
{ cusip: '037833100', country: 'US' },
|
|
{ isin: 'CA8131391062', cusip: '813137106', country: 'CA' },
|
|
{ isin: 'CA00149WBAD4' },
|
|
{ isin: ' us0378331005 ' },
|
|
];
|
|
const tickerCorpus: readonly { raw: string; source: TickerSource }[] = [
|
|
{ raw: 'AAPL', source: 'eod' },
|
|
{ raw: 'AAA-P', source: 'qm' },
|
|
{ raw: 'AACT U', source: 'ib' },
|
|
{ raw: 'SCU.UN.TO', source: 'ceo' },
|
|
{ raw: 'SHOP.TO', source: 'ceo' },
|
|
{ raw: 'ABC.V', source: 'eod' },
|
|
{ raw: 'RY-PR-A', source: 'qm' },
|
|
{ raw: ' shop ', source: 'ib' },
|
|
];
|
|
|
|
const corpusLen = isinCorpus.length;
|
|
let sink = 0;
|
|
|
|
// Warm up JIT
|
|
for (let i = 0; i < 1_000; i++) {
|
|
const idx = i & 7;
|
|
const isinResult = resolveIsin(isinCorpus[idx]);
|
|
const tickerEntry = tickerCorpus[idx]!;
|
|
const tickerResult = parseTicker(tickerEntry.raw, tickerEntry.source);
|
|
if (isinResult.isin !== null) {
|
|
sink++;
|
|
}
|
|
if (tickerResult !== null) {
|
|
sink += tickerResult.tickerRoot.length;
|
|
}
|
|
}
|
|
|
|
const iterations = 100_000;
|
|
const startMs = performance.now();
|
|
for (let i = 0; i < iterations; i++) {
|
|
const idx = i % corpusLen;
|
|
const isinResult = resolveIsin(isinCorpus[idx]);
|
|
const tickerEntry = tickerCorpus[idx]!;
|
|
const tickerResult = parseTicker(tickerEntry.raw, tickerEntry.source);
|
|
if (isinResult.isin !== null) {
|
|
sink++;
|
|
}
|
|
if (tickerResult !== null) {
|
|
sink += tickerResult.tickerRoot.length;
|
|
}
|
|
}
|
|
const elapsedMs = performance.now() - startMs;
|
|
|
|
process.stdout.write(
|
|
`[bench] 100,000 resolveIsin + parseTicker calls: ${elapsedMs.toFixed(2)} ms (sink=${sink})\n`
|
|
);
|
|
expect(sink).toBeGreaterThan(0);
|
|
expect(elapsedMs).toBeLessThan(500);
|
|
});
|
|
});
|