import { describe, expect, it } from 'vitest' import { compare, order, type Rank } from './order' /** * The ordering rules the table promises. * * Sorting looks obvious enough that nobody checks it, and the two ways it goes * wrong both read as data rather than as a bug: a hole shown as the biggest * number in the venue, and a caller's array quietly reordered so the order it * read in is gone. Both are checked here. */ type Row = { id: string; price: Rank } const rows: Row[] = [ { id: 'c', price: 3 }, { id: 'a', price: 1 }, { id: 'gone', price: null }, { id: 'b', price: 2 }, ] const price = (row: Row) => row.price const ids = (rows: Row[]) => rows.map((r) => r.id) describe('compare', () => { it('puts a missing reading after a present one', () => { expect(compare(null, 0)).toBe(1) expect(compare(0, null)).toBe(-1) expect(compare(null, null)).toBe(0) }) it('compares bigints beyond what a double can hold', () => { const big = 2n ** 64n expect(compare(big, big + 1n)).toBe(-1) // The same two values through Number() are equal, which is the bug this // avoids: 18446744073709551616 and ...617 both round to the same double. expect(Number(big) === Number(big + 1n)).toBe(true) }) it('compares mixed numbers and bigints as quantities', () => { expect(compare(2, 10n)).toBe(-1) expect(compare(10n, 2)).toBe(1) }) it('compares strings as text', () => { expect(compare('a', 'b')).toBe(-1) expect(compare('b', 'a')).toBe(1) }) }) describe('order', () => { it('sorts ascending', () => { expect(ids(order(rows, price, 'ascending'))).toEqual(['a', 'b', 'c', 'gone']) }) it('sorts descending', () => { expect(ids(order(rows, price, 'descending'))).toEqual(['c', 'b', 'a', 'gone']) }) it('keeps a missing reading last in both directions', () => { // The one that matters. Reversed naively, `gone` leads the descending // column and a reader takes an unknown price for the highest one. expect(ids(order(rows, price, 'ascending')).slice(-1)[0]).toBe('gone') expect(ids(order(rows, price, 'descending')).slice(-1)[0]).toBe('gone') }) it('leaves the rows it was given alone', () => { const given = [...rows] order(given, price, 'descending') expect(ids(given)).toEqual(ids(rows)) }) it('keeps ties in the order they arrived', () => { const tied: Row[] = [ { id: 'first', price: 1 }, { id: 'second', price: 1 }, { id: 'third', price: 1 }, ] expect(ids(order(tied, price, 'ascending'))).toEqual(['first', 'second', 'third']) expect(ids(order(tied, price, 'descending'))).toEqual(['first', 'second', 'third']) }) }) /** * The same quantity written two ways. * * `Rank` admits `bigint` and `number` because a chain states a quantity as one * and a cache holds it as the other, which is the pairing the union is here * for. `1n === 1` is false, so a comparator that decides equality by identity * alone answers "greater" for the pair whichever way it is asked — and a * browser handed that shuffles the rows it cannot separate. */ describe('a quantity read two ways is one quantity', () => { it('separates neither of the pair from the other', () => { expect(compare(1n, 1)).toBe(0) expect(compare(1, 1n)).toBe(0) }) it('still orders the pair against a different quantity', () => { expect(compare(1n, 2)).toBe(-1) expect(compare(2, 1n)).toBe(1) }) it('leaves rows it cannot separate in the order they arrived, either way', () => { type Held = { id: string; held: Rank } const held: Held[] = [ { id: 'A', held: 1n }, { id: 'B', held: 1 }, { id: 'C', held: 1n }, { id: 'D', held: 1 }, ] const of = (rows: Held[]) => rows.map((r) => r.id).join('') expect(of(order(held, (r) => r.held, 'ascending'))).toBe('ABCD') expect(of(order(held, (r) => r.held, 'descending'))).toBe('ABCD') }) })