spb/cancerindex
Public
TypeScript 97.2%
SQL 1.5%
CSS 0.6%
JavaScript 0.5%
1import { describe, expect, it } from 'vitest';2import { rankEntities, percentiles, inputsHash } from '../src/rank.js';34describe('rankEntities', () => {5 it('is deterministic and handles ties with competition ranking', () => {6 const items = [7 { id: 'b', value: 10 },8 { id: 'a', value: 10 },9 { id: 'c', value: 5 },10 { id: 'd', value: 1 },11 ];12 const r = rankEntities(items);13 expect(r.map((x) => [x.id, x.rank])).toEqual([14 ['a', 1],15 ['b', 1],16 ['c', 3],17 ['d', 4],18 ]);19 expect(r[0]!.percentile).toBe(100);20 expect(r[3]!.percentile).toBe(0);21 expect(rankEntities([...items].reverse())).toEqual(r);22 });23 it('drops non-finite values and ranks ascending when requested', () => {24 const r = rankEntities([{ id: 'x', value: NaN }, { id: 'y', value: 2 }, { id: 'z', value: 1 }], { descending: false });25 expect(r.map((x) => x.id)).toEqual(['z', 'y']);26 expect(r[0]!.eligible).toBe(2);27 });28 it('percentiles and hash are stable', async () => {29 const items = [{ id: 'a', value: 3 }, { id: 'b', value: 1 }];30 expect(percentiles(items).get('a')).toBe(100);31 expect(await inputsHash(items)).toBe(await inputsHash([...items].reverse()));32 });33});34