Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

7.2 KB
Raw Blame
/**
 * 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 { isCountryName } from '../../../src/nest/atlas/known-countries';

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');
  });
});

// #3200: a Maldives traveller saw more regions than cities. Each of these addresses
// lost its city, or counted something that is none, before.
describe('cityFromAddress for the cities the count used to miss (#3200)', () => {
  const real = (address: string, region?: string | null) => cityFromAddress(address, isCountryName, region);

  it('reads the locality of the line the TREK index writes, which ends without a country', () => {
    expect(city('Pariser Platz 1, 10117, Berlin', 'Berlin')).toBe('Berlin');
    expect(city('Boduthakurufaanu Magu, 20026, Malé', 'Malé')).toBe('Malé');
    expect(city('Rue de Rivoli 99, 75001, Paris', 'Île-de-France')).toBe('Paris');
  });

  it('counts a city that is its own region when its postcode stands beside it', () => {
    expect(city('Pariser Platz, 10117 Berlin, Germany', 'Berlin')).toBe('Berlin');
    expect(city('Hotel Adlon Kempinski, Unter den Linden 77, 10117 Berlin, Germany', 'Berlin')).toBe('Berlin');
    expect(real('Michaelerkuppel, 1010 Wien, Österreich', 'Wien')).toBe('Wien');
    expect(real('Boduthakurufaanu Magu, Malé 20026, Maldives', 'Malé')).toBe('Malé');
    // A city below the region still wins, as before.
    expect(city('Shibuya Crossing, Shibuya City, Tokyo 150-0002, Japan', 'Tokyo')).toBe('Shibuya City');
  });

  it('never counts a region named without a postcode, so a mountain or an atoll is no city', () => {
    expect(real('Zugspitze, Bayern, Deutschland', 'Bayern')).toBeNull();
    expect(real('Resort, Kaafu, Maldives', 'Kaafu')).toBeNull();
  });

  it('reads the island below an atoll the geocoder writes with its generic word', () => {
    expect(real('Kurumba Maldives, Vihamanaafushi, Kaafu Atoll, Maldives', 'Kaafu')).toBe('Vihamanaafushi');
    expect(real('Maafushi Beach, Maafushi, Kaafu Atoll, 08090, Maldives', 'Kaafu')).toBe('Maafushi');
  });

  it('never counts a country as the city, in any language TREK speaks', () => {
    expect(real('Malé, Maldives', 'Malé')).toBeNull();
    expect(real('Maafushi, Maldives', 'Kaafu')).toBeNull();
    expect(real('Somewhere, 08090, Maldives')).toBeNull();
    expect(real('Piazza Navona, Roma, Italia', 'Lazio')).toBe('Roma');
  });

  it('keeps the lines it already read right', () => {
    expect(real('350 5th Ave, New York, NY 10118, USA')).toBe('New York');
    expect(real('CN Tower, Toronto, ON M5V 3L9, Canada')).toBe('Toronto');
    expect(real('Museum, Marienplatz, München, Bayern, 80331, Germany', 'Bayern')).toBe('München');
    expect(real('Marienplatz 1, München')).toBe('München');
  });
});

describe('isCountryName', () => {
  it('knows a country by its name in the languages TREK speaks, ignoring case', () => {
    for (const name of [
      'Maldives',
      'Malediven',
      'Österreich',
      'Italia',
      'Sri Lanka',
      'maldives',
      'Deutschland',
      'Japan',
    ]) {
      expect(isCountryName(name)).toBe(true);
    }
  });

  it('does not take a city or a region for a country', () => {
    for (const name of ['Malé', 'Berlin', 'Kaafu Atoll', 'München', 'Bayern', 'New York']) {
      expect(isCountryName(name)).toBe(false);
    }
  });
});