import { expect, test } from 'bun:test'; import { bollingerBands, macd, stochastic } from '@stock-bot/calculations/indicators'; test('population bands use the unrounded mean and preserve source offsets', () => { const result = bollingerBands([1, 2, 3, 4], 3, 2); expect(result.startIndex).toBe(2); expect(result.middle).toEqual([2, 3]); expect(result.upper[0]).toBeCloseTo(2 + 2 * Math.sqrt(2 / 3), 12); expect(result.lower[1]).toBeCloseTo(3 - 2 * Math.sqrt(2 / 3), 12); expect(bollingerBands([1, 2], 3).middle).toEqual([]); expect(bollingerBands([1, 2, 3], 2, 0).upper).toEqual([1.5, 2.5]); expect(() => bollingerBands([1, 2], 2, -1)).toThrow(); expect(() => bollingerBands([1, NaN], 2)).toThrow(); }); test('MACD aligns both EMA seeds, signal warm-up and histogram without time lookups', () => { const result = macd([1, 2, 3, 4, 5, 6], 2, 3, 2); expect(result.macd.startIndex).toBe(2); expect(result.signal.startIndex).toBe(3); expect(result.histogram.startIndex).toBe(3); result.macd.values.forEach(value => expect(value).toBeCloseTo(0.5, 12)); result.signal.values.forEach(value => expect(value).toBeCloseTo(0.5, 12)); result.histogram.values.forEach(value => expect(value).toBeCloseTo(0, 12)); const first = macd([1, 2, 3], 2, 3, 2, { seed: 'first' }); expect(first.macd.startIndex).toBe(0); expect(first.signal.startIndex).toBe(0); expect(first.macd.values[2]).toBeCloseTo(11 / 36, 12); expect(first.signal.values[2]).toBeCloseTo(13 / 54, 12); expect(first.histogram.values[2]).toBeCloseTo(7 / 108, 12); expect(macd([1, 2, 3], 2, 3, 5).signal.values).toEqual([]); expect(() => macd([1, 2, 3], 3, 2, 1)).toThrow(); expect(() => macd([1, 2], 1, 2, 0)).toThrow(); expect(() => macd([], 1, Number.MAX_SAFE_INTEGER - 1, Number.MAX_SAFE_INTEGER - 1)).toThrow(); }); test('stochastic smooths unrounded values and explicitly handles flat windows', () => { const bars = [1, 2, 3, 4, 5, 6].map(close => ({ high: 10, low: 0, close })); const result = stochastic(bars, 2, 2, 2); expect(result.k).toMatchObject({ startIndex: 2, values: [25, 35, 45, 55] }); expect(result.d).toMatchObject({ startIndex: 3, values: [30, 40, 50] }); expect(stochastic(Array(5).fill({ high: 1, low: 1, close: 1 }), 2, 1, 1).k.values).toEqual([ 50, 50, 50, 50, ]); expect(stochastic(bars.slice(0, 1), 2).k.values).toEqual([]); expect(() => stochastic(bars, 0)).toThrow(); expect(() => stochastic([{ high: 1, low: 0, close: 2 }], 1)).toThrow(); expect(() => stochastic([], Number.MAX_SAFE_INTEGER - 1, Number.MAX_SAFE_INTEGER - 1)).toThrow(); }); test('composed features are prefix-invariant and do not mutate source inputs', () => { const values = Array.from({ length: 50 }, (_, i) => 10 + Math.sin(i / 2)); const bars = values.map(close => ({ high: close + 1, low: close - 1, close })); const original = structuredClone(bars); const fullMacd = macd(values, 3, 7, 4), fullBands = bollingerBands(values, 5), fullStochastic = stochastic(bars, 5, 2, 3); for (let end = 0; end <= values.length; end++) { const prefix = values.slice(0, end); const m = macd(prefix, 3, 7, 4), b = bollingerBands(prefix, 5), s = stochastic(bars.slice(0, end), 5, 2, 3); for (const key of ['macd', 'signal', 'histogram'] as const) { expect(fullMacd[key].values.slice(0, m[key].values.length)).toEqual(m[key].values); } expect(fullBands.lower.slice(0, b.lower.length)).toEqual(b.lower); expect(fullStochastic.d.values.slice(0, s.d.values.length)).toEqual(s.d.values); } expect(bars).toEqual(original); });