Files
website/src/lib/worldtodo/cluster.test.ts
2026-10-26 08:38:51 +00:00

41 lines
1.4 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { clusterPoints, coordKey, ringOffsets } from './cluster';
const pt = (x: number, y: number, weight = 1) => ({ x, y, weight });
describe('clusterPoints', () => {
it('groups points within the radius and keeps far ones apart', () => {
const a = pt(0, 0);
const b = pt(10, 10);
const c = pt(200, 0);
const groups = clusterPoints([a, b, c], 40);
expect(groups).toHaveLength(2);
expect(groups.find((g) => g.includes(a))).toContain(b);
expect(groups.find((g) => g.includes(c))).toEqual([c]);
});
it('lets the heaviest point anchor the cluster', () => {
const small = pt(0, 0, 1);
const big = pt(30, 0, 5);
const far = pt(65, 0, 1);
const groups = clusterPoints([small, big, far], 40);
expect(groups[0][0]).toBe(big);
expect(groups[0]).toHaveLength(3);
});
it('radius 0 keeps every point alone', () => {
expect(clusterPoints([pt(0, 0), pt(0, 0)], 0)).toHaveLength(2);
});
});
describe('ringOffsets', () => {
it('is a no-op for one pin and spreads several pins apart', () => {
expect(ringOffsets(1)).toEqual([{ dx: 0, dy: 0 }]);
const ring = ringOffsets(4);
expect(ring).toHaveLength(4);
const keys = new Set(ring.map((o) => `${o.dx},${o.dy}`));
expect(keys.size).toBe(4);
});
it('rounds coordinates for grouping', () => {
expect(coordKey(52.2, 5.3)).toBe(coordKey(52.20001, 5.30002));
});
});