Files
dawnline/web/tests/map.test.ts
T
Aemeath 5768856d76
Build packages / Version (push) Waiting to run
Dawnline 0.5.1
- The map places everyone within their city: the IP gives the city
  alone, so each person gets a fixed spot of their own in it (1.5-5 km
  from its centre, clear of the server and of each other)
- People who would cover each other share one plate with their avatars
  and the city once, and people on top of the server ride in the
  server's plate (no more pointers, repeated labels or counts); zooming
  in they part to their own spots
- Avatars in a plate keep their device badges (round for one device);
  a click on an avatar opens that person's card, flying to them first
  when they are elsewhere, and a click on a plate brings its people into
  view, at city level when they share one, close enough to part
- Plates follow the zoom: compact far out (small avatars, no badges or
  place names), small badges at country level, full size closer in
- Plates slide, grow and shrink smoothly: avatars glide to their places,
  "+N" changes in place, and the plate's width follows
- Fixed: a hovered person could stay highlighted (everyone else faded)
  after their plate split under the pointer
- Overview's Online tile: small avatars light up with a halo round their
  ring, and the device badge no longer cuts a dark square into the hover
2026-10-17 03:55:24 +03:00

181 lines
5.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest';
import { arc, cityPlaces, devicePlaces, separate, type MapData } from '../src/admin/map';
describe('arc (mapcn buildArcCoordinates)', () => {
it('runs from one point to the other in 64 steps', () => {
const pts = arc([-0.1276, 51.5074], [-74.006, 40.7128]);
expect(pts).toHaveLength(65);
expect(pts[0]).toEqual([-0.1276, 51.5074]);
expect(pts[64][0]).toBeCloseTo(-74.006);
expect(pts[64][1]).toBeCloseTo(40.7128);
});
it('bends to the left of travel by a fifth of the distance', () => {
// Due east 10°: the control point sits 2° north of the middle, the curve's peak half of that.
const mid = arc([0, 0], [10, 0])[32];
expect(mid[0]).toBeCloseTo(5);
expect(mid[1]).toBeCloseTo(1);
});
it('takes the short way across the antimeridian', () => {
const pts = arc([139.69, 35.69], [-122.42, 37.77]);
expect(pts[pts.length - 1][0]).toBeCloseTo(237.58);
expect(Math.max(...pts.map((p) => Math.abs(p[0] - 139.69)))).toBeLessThan(100);
});
it('is a single point pair when both ends meet', () => {
expect(arc([10, 10], [10, 10])).toEqual([
[10, 10],
[10, 10],
]);
});
});
describe('devicePlaces', () => {
const pt = (city: string, ts: number, online = false, device = 'lap') => ({
person: 'my',
device,
ip: `ip-${city}-${ts}`,
ts,
online,
lat: 0,
lon: 0,
city,
country: '',
cc: 'RU',
asn: 0,
org: '',
});
const now = 100 * 86400;
const data = (points: ReturnType<typeof pt>[]): MapData => ({
geo: { enabled: true, home: null, points } as unknown as MapData['geo'],
people: [],
now,
person: 'my',
device: 'lap',
});
it('puts the live place first and numbers the rest newest first, one per city', () => {
const list = devicePlaces(
data([
pt('Kazan', now - 25 * 86400),
pt('Moscow', now - 7 * 86400),
pt('Moscow', now - 9 * 86400),
pt('Sochi', now - 86400),
pt('Krasnodar', now, true),
pt('Berlin', now - 86400, false, 'phone'),
]),
'30d',
);
expect(list.map((x) => [x.city, x.n])).toEqual([
['Krasnodar', 0],
['Sochi', 1],
['Moscow', 2],
['Kazan', 3],
]);
});
it('keeps to the period', () => {
expect(devicePlaces(data([pt('Kazan', now - 25 * 86400), pt('Sochi', now - 86400)]), '7d').map((x) => x.city)).toEqual(['Sochi']);
});
});
describe('separate', () => {
it('leaves markers that do not touch alone', () => {
expect(
separate([
{ x: 0, y: 0, r: 10 },
{ x: 100, y: 0, r: 10 },
]),
).toEqual([
[0, 0],
[0, 0],
]);
});
it('moves only the free one away from a fixed one, just clear', () => {
const [s, p] = separate([
{ x: 0, y: 0, r: 20, fixed: true },
{ x: 30, y: 0, r: 14 },
]);
expect(s).toEqual([0, 0]);
expect(p[0]).toBeCloseTo(20 + 14 + 6 - 30);
expect(p[1]).toBeCloseTo(0);
});
it('fades to nothing as markers part (no jump)', () => {
const at = (x: number) =>
separate([
{ x: 0, y: 0, r: 20, fixed: true },
{ x, y: 0, r: 14 },
])[1][0];
expect(at(39)).toBeCloseTo(1);
expect(at(39.9)).toBeCloseTo(0.1);
expect(at(40)).toBe(0);
});
it('splits two free markers half each and parts ones on the same spot', () => {
const [a, b] = separate(
[
{ x: 0, y: 0, r: 10 },
{ x: 10, y: 0, r: 10 },
],
0,
);
expect(a[0]).toBeCloseTo(-5);
expect(b[0]).toBeCloseTo(5);
const same = separate(
[
{ x: 0, y: 0, r: 10 },
{ x: 0, y: 0, r: 10 },
{ x: 0, y: 0, r: 10 },
],
0,
);
for (let i = 0; i < 3; i++)
for (let j = i + 1; j < 3; j++) expect(Math.hypot(same[i][0] - same[j][0], same[i][1] - same[j][1])).toBeGreaterThan(19);
});
});
describe('cityPlaces', () => {
const km = (a: [number, number], b: [number, number]) => {
const r = Math.PI / 180;
const h = Math.sin(((b[1] - a[1]) * r) / 2) ** 2 + Math.cos(a[1] * r) * Math.cos(b[1] * r) * Math.sin(((b[0] - a[0]) * r) / 2) ** 2;
return 12742 * Math.asin(Math.sqrt(h));
};
const krd = { lat: 45.0355, lon: 38.9753 };
const msk = { lat: 55.7558, lon: 37.6173 };
it('gives someone alone in their city a spot of their own in it, the same every time', () => {
const a = cityPlaces([{ key: 'a', ...krd }], msk).get('a')!;
const d = km(a, [krd.lon, krd.lat]);
expect(d).toBeGreaterThan(1.4);
expect(d).toBeLessThan(5.1);
expect(
cityPlaces(
[
{ key: 'a', ...krd },
{ key: 'b', ...krd },
],
msk,
).get('a'),
).toEqual(a);
});
it('spreads people sharing a city, apart enough to part at zoom 12', () => {
const keys = ['a', 'b', 'c', 'd', 'e'];
const at = cityPlaces(
keys.map((key) => ({ key, ...krd })),
msk,
);
const pts = keys.map((k) => at.get(k)!);
for (let i = 0; i < pts.length; i++) {
expect(km(pts[i], [krd.lon, krd.lat])).toBeLessThan(6);
for (let j = i + 1; j < pts.length; j++) expect(km(pts[i], pts[j])).toBeGreaterThan(1.25);
}
});
it('moves anyone in the server’s city off its centre, the same way every time', () => {
const a = cityPlaces([{ key: 'a', ...msk }], msk).get('a')!;
expect(km(a, [msk.lon, msk.lat])).toBeGreaterThan(2.4);
expect(cityPlaces([{ key: 'a', ...msk }], msk).get('a')).toEqual(a);
});
});