90 lines
3.5 KiB
TypeScript
90 lines
3.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
MAP_LOCATION_CLASSIFICATIONS,
|
|
groupMapLocations,
|
|
parseUserLocation,
|
|
parseUserNetwork,
|
|
projectWorldPoint,
|
|
} from "./user-location-map";
|
|
|
|
describe("user location map", () => {
|
|
it("allows only clear and hosting observations as map locations", () => {
|
|
expect(MAP_LOCATION_CLASSIFICATIONS).toEqual(["clear", "hosting"]);
|
|
expect(MAP_LOCATION_CLASSIFICATIONS).not.toContain("vpn");
|
|
expect(MAP_LOCATION_CLASSIFICATIONS).not.toContain("proxy");
|
|
expect(MAP_LOCATION_CLASSIFICATIONS).not.toContain("tor");
|
|
});
|
|
|
|
it("extracts a valid approximate location from cached IP intelligence", () => {
|
|
expect(parseUserLocation({
|
|
classification: "clear",
|
|
location: {
|
|
city: "Mountain View",
|
|
region: "California",
|
|
countryCode: "US",
|
|
latitude: 37.4056,
|
|
longitude: -122.0775,
|
|
},
|
|
})).toEqual({
|
|
latitude: 37.4056,
|
|
longitude: -122.0775,
|
|
label: "Mountain View, California, US",
|
|
});
|
|
});
|
|
|
|
it("extracts enriched network fields from existing ProxyCheck cache entries", () => {
|
|
expect(parseUserNetwork({
|
|
network: { asn: "AS7922", provider: "Comcast Cable Communications, LLC" },
|
|
rawResponse: {
|
|
status: "ok",
|
|
"203.0.113.10": { type: "Residential", proxy: "no" },
|
|
},
|
|
})).toEqual({
|
|
asn: "AS7922",
|
|
provider: "Comcast Cable Communications, LLC",
|
|
connectionType: "Residential",
|
|
proxy: false,
|
|
});
|
|
});
|
|
|
|
it("prefers normalized network fields and preserves unavailable values", () => {
|
|
expect(parseUserNetwork({
|
|
network: { asn: "AS62371", provider: "Proton AG", connectionType: "VPN", proxy: true },
|
|
rawResponse: { "198.51.100.5": { type: "Residential", proxy: "no" } },
|
|
})).toEqual({ asn: "AS62371", provider: "Proton AG", connectionType: "VPN", proxy: true });
|
|
expect(parseUserNetwork({ network: {} })).toEqual({ asn: null, provider: null, connectionType: null, proxy: null });
|
|
});
|
|
|
|
it("rejects missing and out-of-range coordinates", () => {
|
|
expect(parseUserLocation({ location: { latitude: 91, longitude: 0 } })).toBeNull();
|
|
expect(parseUserLocation({ location: { city: "Unknown" } })).toBeNull();
|
|
});
|
|
|
|
it("groups users sharing approximate coordinates without hiding their identities", () => {
|
|
const groups = groupMapLocations([
|
|
{ userId: "one", nickname: "Dani (Steve)", latitude: 37.4056, longitude: -122.0775 },
|
|
{ userId: "two", nickname: "Alex (AlexMC)", latitude: 37.4057, longitude: -122.0774 },
|
|
{ userId: "three", nickname: "Sam (Notch)", latitude: 51.5, longitude: -0.12 },
|
|
]);
|
|
|
|
expect(groups).toHaveLength(2);
|
|
expect(groups[0]).toMatchObject({ count: 2, nicknames: ["Alex (AlexMC)", "Dani (Steve)"] });
|
|
expect(groups[0]?.locations.map((location) => location.userId)).toEqual(["one", "two"]);
|
|
expect(groups[1]).toMatchObject({ count: 1, nicknames: ["Sam (Notch)"] });
|
|
});
|
|
|
|
it("normalizes signed zero and the antimeridian before grouping", () => {
|
|
const groups = groupMapLocations([
|
|
{ nickname: "West", latitude: -0.004, longitude: 180 },
|
|
{ nickname: "East", latitude: 0.004, longitude: -180 },
|
|
]);
|
|
expect(groups).toHaveLength(1);
|
|
expect(groups[0]).toMatchObject({ count: 2, key: "0:-180", latitude: 0, longitude: -180 });
|
|
});
|
|
|
|
it("projects longitude and latitude into an equirectangular SVG", () => {
|
|
expect(projectWorldPoint(0, 0, 800, 400)).toEqual({ x: 400, y: 200 });
|
|
expect(projectWorldPoint(90, 180, 800, 400)).toEqual({ x: 800, y: 0 });
|
|
});
|
|
});
|