mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 09:22:15 +00:00
c5e2afc21b
The Atlas page showed 100 cities and 169 places where GET /api/v1/stats and the dashboard's passport card showed 66 and 164 for the same account. The two sides counted with their own code. The Atlas page took the address part right before the country as the city, which is often the region (Bavaria for Munich, Nuremberg and Regensburg alike), and counted a bare place name with no comma as a city. The stats side read its places with a DISTINCT over address, coordinates and region only, so two places that looked alike (the same hotel on two trips, places with neither address nor coordinates) counted once, and its city reader skipped every address part with a digit, so "10178 Berlin" never counted as Berlin. Both now take places and cities from one method: every place counts once, a city is read with cityFromAddress, which skips the region (#1115) and now reads a city written beside its postcode (German, French, US, Japanese, Dutch, British and Canadian forms) while a street with its house number still does not count, and two spellings that differ only in case are one city. The Atlas page, the passport card, /api/v1/stats and the MCP atlas tool follow it. The response shapes are unchanged. Closes #3137
82 lines
4.1 KiB
TypeScript
82 lines
4.1 KiB
TypeScript
/**
|
|
* The "cities visited" extractor (#1115). Every address below is either a real one
|
|
* from a trip or the shape a geocoder returns for that part of the world, because
|
|
* the bug being fixed here was precisely that the old rule looked plausible against
|
|
* invented examples and failed against actual data.
|
|
*/
|
|
import { cityFromAddress } from '../../../src/nest/atlas/city-from-address';
|
|
|
|
import { describe, it, expect } from 'vitest';
|
|
|
|
const COUNTRIES = new Set(['Japan', 'France', 'Germany', 'Italy', 'Nederland', '日本', 'Россия', 'Ελλάδα', '대한민국']);
|
|
const isCountry = (part: string) => COUNTRIES.has(part);
|
|
const city = (address: string | null | undefined, region?: string | null) =>
|
|
cityFromAddress(address, isCountry, region);
|
|
|
|
describe('cityFromAddress', () => {
|
|
it('takes the city, not the name of the place itself', () => {
|
|
// The old rule scanned left to right and returned "Shibuya Sky" for this.
|
|
expect(city('Shibuya Sky, 12, Shibuya 2, Shibuya, Tokyo, 150-0002, Japan', 'Tokyo')).toBe('Shibuya');
|
|
expect(city('Family Mart, Omotesandō, Kita-Aoyama 3, Kita-Aoyama, Minato, Tokyo, 107-0061, Japan', 'Tokyo')).toBe(
|
|
'Minato',
|
|
);
|
|
expect(city('BNP Paribas, Boulevard de Courcelles, Paris, Île-de-France, 75008, France', 'Île-de-France')).toBe(
|
|
'Paris',
|
|
);
|
|
});
|
|
|
|
it('skips the region so the city below it wins', () => {
|
|
// Without the region hint, walking back from the end stops at the region.
|
|
expect(city('Museum, Marienplatz, München, Bayern, 80331, Germany', 'Bayern')).toBe('München');
|
|
expect(city('Museum, Marienplatz, München, Bayern, 80331, Germany')).toBe('Bayern');
|
|
});
|
|
|
|
it('reads addresses that are not written in the latin alphabet', () => {
|
|
// Every one of these returned nothing before.
|
|
expect(city('渋谷スカイ, 渋谷区, 東京都, 日本', '東京都')).toBe('渋谷区');
|
|
expect(city('Красная площадь, Москва, Россия')).toBe('Москва');
|
|
expect(city('Πλάκα, Αθήνα, Ελλάδα')).toBe('Αθήνα');
|
|
expect(city('경복궁, 종로구, 서울특별시, 대한민국', '서울특별시')).toBe('종로구');
|
|
});
|
|
|
|
it('ignores postcodes, house numbers and numbered districts', () => {
|
|
expect(city('Trevi Fountain, Piazza di Trevi, Rione II Trevi, Roma, 00187, Italy', 'Lazio')).toBe('Roma');
|
|
expect(city('Cafe, 12, Paris 8e Arrondissement, Paris, 75008, France', 'Île-de-France')).toBe('Paris');
|
|
});
|
|
|
|
it('has nothing to offer for an address that is only a name', () => {
|
|
expect(city('Eiffel Tower')).toBeNull();
|
|
expect(city('')).toBeNull();
|
|
expect(city(null)).toBeNull();
|
|
expect(city(undefined)).toBeNull();
|
|
});
|
|
|
|
it('returns null rather than a country when everything else drops out', () => {
|
|
expect(city('Somewhere, France')).toBeNull();
|
|
expect(city('Somewhere, 75008, France')).toBeNull();
|
|
});
|
|
|
|
it('survives stray separators', () => {
|
|
expect(city('Place, , Lyon, France')).toBe('Lyon');
|
|
expect(city('Place, -, Lyon, France')).toBe('Lyon');
|
|
});
|
|
});
|
|
|
|
describe('cityFromAddress with the postcode in the same part as the city (#3137)', () => {
|
|
it('reads the city beside a postcode, the way a Google-style address writes it', () => {
|
|
expect(city('Alexanderplatz 1, 10178 Berlin, Germany')).toBe('Berlin');
|
|
expect(city('Rue de Rivoli 99, 75001 Paris, France')).toBe('Paris');
|
|
expect(city('Via del Corso 12, 00186 Roma RM, Italy')).toBe('Roma');
|
|
expect(city('Damrak 1, 1012 LG Amsterdam, Nederland')).toBe('Amsterdam');
|
|
expect(city('10 Downing St, London SW1A 2AA, United Kingdom')).toBe('London');
|
|
expect(city('Shibuya Crossing, Shibuya City, Tokyo 150-0002, Japan', 'Tokyo')).toBe('Shibuya City');
|
|
});
|
|
|
|
it('never takes a street with its house number, a numbered district or a state code for a city', () => {
|
|
expect(city('Hotel Adlon, Unter den Linden 77, Germany')).toBeNull();
|
|
expect(city('Shibuya Sky, 12, Shibuya 2, Japan')).toBeNull();
|
|
expect(city('350 5th Ave, New York, NY 10118, USA')).toBe('New York');
|
|
expect(city('CN Tower, Toronto, ON M5V 3L9, Canada')).toBe('Toronto');
|
|
});
|
|
});
|