import { describe, expect, it } from 'vitest' import { bounds, ordinate, ordered, shares, spread, stepped, trace, thickness, type Candle, } from './chart' /** * The arithmetic behind the drawings. * * A drawing is the easiest surface in a product to be wrong on and the hardest * to notice being wrong on. A line through rows in the order they arrived, a * path of `NaN` that renders as nothing, a flat line at zero standing in for a * read that failed, and a bar whose parts do not come to the whole all look * like charts. None of that is visible to an eye reviewing a screenshot, so it * is settled here. */ const MIDNIGHT = 1_731_628_800 const DAY = 86_400 const box = { width: 100, height: 40 } const at = (day: number, value: number) => ({ at: MIDNIGHT + day * DAY, value }) const session = (day: number, open: number, high: number, low: number, close: number): Candle => ({ at: MIDNIGHT + day * DAY, value: close, open, high, low, }) describe('the readings a chart will draw', () => { it('puts them in time order whatever order they arrived in', () => { expect(ordered([at(1, 20), at(0, 10)]).map((p) => p.value)).toEqual([10, 20]) }) it('leaves the caller its own array', () => { // A component that sorts in place has reordered a list the caller is still // holding, and the order it read in is gone. const given = [at(1, 20), at(0, 10)] ordered(given) expect(given.map((p) => p.value)).toEqual([20, 10]) }) it('drops a reading that is not a number', () => { // `Number('')` is zero, so an indexer's uncomputed day reaches a client // that converts before it checks as a token falling to nothing. What gets // this far instead is `NaN`, and one of those in the middle of a path // renders the whole line as nothing at all. expect(ordered([at(0, 10), at(1, Number.NaN), at(2, 20)])).toHaveLength(2) expect(ordered([{ at: Number.NaN, value: 10 }])).toHaveLength(0) }) it('drops a session missing any of its four prices', () => { const half = { ...session(0, 1, 2, 1, 2), high: Number.NaN } expect(ordered([half, session(1, 1, 2, 1, 2)])).toHaveLength(1) }) }) describe('what the box has to cover', () => { it('is the range the readings occupy, and not zero to the top of them', () => { // The whole of why a domain is computed: a day between these two numbers // fills the box, where a zero baseline draws it as a grey rule. expect(bounds([at(0, 0.000109), at(1, 0.000111)])).toEqual({ low: 0.000109, high: 0.000111, }) }) it("reaches a session's wicks and not only its ends", () => { // Scaled to the closes alone, a candle's high and low are drawn outside // the box they are meant to be inside. expect(bounds([session(0, 10, 30, 5, 20)])).toEqual({ low: 5, high: 30 }) }) it('scales to the readings rather than to one impossible day', () => { // A single trade against an empty pool prints a number with no relation to // the asset. Scaled to it, a hundred real days occupy less than a pixel and // the drawing is one vertical line — the outlier is the only thing visible, // and it is the one number that means nothing. const real = Array.from({ length: 100 }, (_, i) => at(i, 10 + (i % 5))) const seen = bounds([...real, at(100, 9.27e16)]) expect(seen.high).toBeLessThan(20) }) it('keeps its true extremes where there is no spread to trim', () => { expect(bounds([at(0, 7), at(1, 7)])).toEqual({ low: 7, high: 7 }) }) }) describe('placing a reading in the box', () => { it('spaces the readings along time, not along the rows', () => { // An indexer closes a day only where it saw something. Evenly spaced, a // fortnight of silence draws the same width as a busy Tuesday. expect(spread([at(0, 1), at(1, 2), at(10, 3)], 100)).toEqual([0, 10, 100]) }) it('falls back to even spacing where every reading shares a timestamp', () => { expect(spread([at(0, 1), at(0, 2), at(0, 3)], 100)).toEqual([0, 50, 100]) }) it('puts the low on the floor and the high at the ceiling', () => { // y counts from the top, so the high is the small number. expect(ordinate(10, { low: 10, high: 20 }, 40)).toBe(40) expect(ordinate(20, { low: 10, high: 20 }, 40)).toBe(0) expect(ordinate(15, { low: 10, high: 20 }, 40)).toBe(20) }) it('holds a reading outside the trimmed domain at the edge of the box', () => { // The scale is chosen for the middle ninety-eight per cent, so a single bad // print is outside it by construction. Placed where the arithmetic puts it // the mark lands far above the box and the root element clips it away — no // mark at all on the one day a reader came to ask about, and in a // cell-width line there is no cursor and no readout to reach it by. expect(ordinate(1e9, { low: 10, high: 20 }, 40)).toBe(0) expect(ordinate(-1e9, { low: 10, high: 20 }, 40)).toBe(40) }) it('puts a series that never moved through the middle', () => { // The obvious arithmetic divides by a range of zero here, and a path of // NaN renders as nothing — a drawing that disappears exactly when the price // held still. expect(ordinate(7, { low: 7, high: 7 }, 40)).toBe(20) }) }) describe('the line', () => { it('crosses the box and climbs its range', () => { const points = [at(0, 10), at(1, 20)] expect(trace(points, box, bounds(points))).toBe('M0 40 L100 0') }) it('is drawn where the readings happened', () => { const points = [at(0, 10), at(1, 20), at(10, 10)] expect(trace(points, box, bounds(points))).toBe('M0 40 L10 0 L100 40') }) it('runs flat through the middle where nothing moved', () => { const points = [at(0, 7), at(1, 7)] expect(trace(points, box, bounds(points))).toBe('M0 20 L100 20') }) }) describe('how wide a mark is drawn', () => { it('is decided by the closest pair, so two marks never overlap', () => { // A share of the average would draw the two readings an hour apart on top // of each other and report a session that was never there. // // Seven of the ten between the closest pair, less what the mark itself // takes out of the box: the readings are placed across a box inset by one // mark, so the room a mark is a share of is the room left after it. expect(thickness([0, 10, 100], box)).toBeCloseTo(6.54, 2) }) it('is never thinner than a hairline', () => { expect(thickness([0, 0.1, 100], box)).toBe(1) }) it('leaves room for itself, so the fewest readings a chart draws do not overlap', () => { // Two readings is the shortest series that is drawn at all, and it is the // case the arithmetic is worst at: the pair spans the whole box, so a mark // is a share of the whole box and there is nothing left to inset it into. // Taken at the full width and then inset, the two marks overlapped across // two fifths of the drawing. for (const n of [2, 3, 4, 8, 30]) { const points = Array.from({ length: n }, (_, i) => at(i, 100 + i)) const across = spread(points, box.width) const thick = thickness(across, box) const xs = spread(points, box.width - thick).map((x) => x + thick / 2) for (let i = 1; i < xs.length; i++) { expect(xs[i]! - xs[i - 1]!).toBeGreaterThanOrEqual(thick - 1e-9) } expect(xs[0]! - thick / 2).toBeGreaterThanOrEqual(-1e-9) expect(xs[xs.length - 1]! + thick / 2).toBeLessThanOrEqual(box.width + 1e-9) } }) }) describe('reading along the series with a keyboard', () => { it('steps either way and stops at both ends', () => { expect(stepped('ArrowRight', 0, 4)).toBe(1) expect(stepped('ArrowLeft', 2, 4)).toBe(1) // Time does not wrap. Landing on the oldest reading from the newest is a // jump that reads as a series looping. expect(stepped('ArrowRight', 3, 4)).toBe(3) expect(stepped('ArrowLeft', 0, 4)).toBe(0) }) it('reaches both ends outright', () => { expect(stepped('Home', 2, 4)).toBe(0) expect(stepped('End', 1, 4)).toBe(3) }) it('leaves every other key to the page', () => { expect(stepped('ArrowUp', 1, 4)).toBeNull() expect(stepped('Enter', 1, 4)).toBeNull() expect(stepped(' ', 1, 4)).toBeNull() }) }) describe('dividing a whole', () => { const votes = [ { name: 'for', value: 60 }, { name: 'against', value: 30 }, { name: 'abstain', value: 10 }, ] it('gives each part its share of what the parts come to', () => { expect(shares(votes)).toEqual({ whole: 100, each: [0.6, 0.3, 0.1] }) }) it('measures against a stated whole, so an unmet quorum is a bar with room left', () => { // The reason a whole can be stated at all: forty thousand of a hundred // thousand is two fifths of a bar, and the same forty thousand with no // whole stated is a bar entirely one colour. expect(shares([{ name: 'behind it', value: 40 }], 100)).toEqual({ whole: 100, each: [0.4], }) }) it('fills the bar once the parts pass the whole', () => { // A stated whole smaller than its parts is not a whole. Taken literally the // parts would run past the end of their own track. const { whole, each } = shares([{ name: 'behind it', value: 140 }], 100) expect(whole).toBe(140) expect(each).toEqual([1]) }) it('gives no share to a part that is not a number, or is less than none', () => { // The figure is still written under the bar. A length is a claim about a // proportion, and there is no proportion in a number nobody could read. expect(shares([{ name: 'read', value: 50 }, { name: 'lost', value: Number.NaN }])).toEqual({ whole: 50, each: [1, 0], }) expect(shares([{ name: 'read', value: 50 }, { name: 'owed', value: -10 }]).whole).toBe(50) }) it('answers nothing where nothing was divided', () => { // Every part at zero is the same picture as no parts at all, and a bar of // no length is a picture a reader takes for a reading. expect(shares([])).toEqual({ whole: 0, each: [] }) expect(shares([{ name: 'for', value: 0 }])).toEqual({ whole: 0, each: [] }) }) })