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.

18.9 KB
Raw Blame
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import {
  A2_TO_A3,
  visitedRegionCount,
  bucketTooltipHeight,
  bucketTooltipNeedsScroll,
  bucketTooltipPlacement,
  bucketTooltipWidth,
  countryStatus,
  visitMonth,
  findBucketDuplicate,
  isBucketDuplicateError,
  isCountryVisible,
  normalizeRegionName,
  regionCacheEvictions,
  withCountryMarkedVisited,
  wishlistA3Codes,
  wishlistRegionCodes,
  groupCountryPlaces,
  countryColor,
  COUNTRY_COLORS,
  REGION_CACHE_MAX,
  type AtlasData,
  type BucketItem,
} from './atlasModel';

describe('normalizeRegionName', () => {
  it('matches names that only differ by diacritics (Ile-de-France vs Île-de-France)', () => {
    expect(normalizeRegionName('Ile-de-France')).toBe(normalizeRegionName('Île-de-France'));
  });

  it('matches names that only differ by dash style and surrounding spaces', () => {
    expect(normalizeRegionName('Bourgogne – Franche-Comté')).toBe(normalizeRegionName('Bourgogne-Franche-Comté'));
  });

  it('is case-insensitive', () => {
    expect(normalizeRegionName('PROVENCE')).toBe(normalizeRegionName('provence'));
  });

  it('still distinguishes genuinely different names', () => {
    expect(normalizeRegionName('Bretagne')).not.toBe(normalizeRegionName('Brittany'));
  });
});

// Countries whose GeoJSON feature carries no usable ISO_A2 must be hardcoded in
// A2_TO_A3 (see the comment above the table) or they get no map handlers at all.
describe('A2_TO_A3 hardcoded entries (#1609)', () => {
  it('maps Kosovo (XK → XKX)', () => {
    expect(A2_TO_A3.XK).toBe('XKX');
  });

  it('resolves the shipped Kosovo feature (ADM0_A3=XKX, ISO_A2=null) to XK', () => {
    // Mirrors the onEachFeature fallback in useAtlas.ts: reverse lookup by A3,
    // then ISO_A2 — which is null for Kosovo in the bundled geoBoundaries data.
    const feature = { properties: { ADM0_A3: 'XKX', ISO_A2: null as string | null } };
    const a3 = feature.properties.ADM0_A3;
    const a3ToA2Entry = Object.entries(A2_TO_A3).find(([, v]) => v === a3);
    const isoA2 = feature.properties.ISO_A2;
    const countryCode = a3ToA2Entry ? a3ToA2Entry[0] : (isoA2 && isoA2 !== '-99' ? isoA2 : null);
    expect(countryCode).toBe('XK');
  });
});

// Trip-date driven visit status (#1048). The three helpers below are the only place
// the client decides what "been there" means, so every mark flow shares one answer.
describe('countryStatus', () => {
  it('treats a country without a status as visited (older server, no #1048 field)', () => {
    expect(countryStatus({})).toBe('visited');
  });

  it('passes an explicit status through', () => {
    expect(countryStatus({ status: 'visited' })).toBe('visited');
    expect(countryStatus({ status: 'planned' })).toBe('planned');
    expect(countryStatus({ status: 'idea' })).toBe('idea');
  });
});

describe('visitMonth (#1535)', () => {
  // West of Greenwich, where new Date('2024-06-01') is still the last evening of May.
  beforeEach(() => {
    vi.stubEnv('TZ', 'America/New_York');
  });

  afterEach(() => {
    vi.unstubAllEnvs();
  });

  it('reads the year and month as a local date, so the 1st stays in its own month', () => {
    const june = visitMonth('2024-06-01')!;
    expect([june.getFullYear(), june.getMonth(), june.getDate()]).toEqual([2024, 5, 1]);
    const january = visitMonth('2025-01-01')!;
    expect([january.getFullYear(), january.getMonth()]).toEqual([2025, 0]);
  });

  it('gives nothing for a missing or malformed date', () => {
    expect(visitMonth(null)).toBeNull();
    expect(visitMonth(undefined)).toBeNull();
    expect(visitMonth('')).toBeNull();
    expect(visitMonth('June 2024')).toBeNull();
  });
});

describe('isCountryVisible', () => {
  it('always shows visited countries', () => {
    expect(isCountryVisible({ status: 'visited' }, false)).toBe(true);
    expect(isCountryVisible({ status: 'visited' }, true)).toBe(true);
  });

  it('hides planned and dateless countries until the layer is switched on', () => {
    expect(isCountryVisible({ status: 'planned' }, false)).toBe(false);
    expect(isCountryVisible({ status: 'idea' }, false)).toBe(false);
    expect(isCountryVisible({ status: 'planned' }, true)).toBe(true);
    expect(isCountryVisible({ status: 'idea' }, true)).toBe(true);
  });

  it('keeps a status-less country on the map with the toggle off', () => {
    expect(isCountryVisible({}, false)).toBe(true);
  });
});

describe('withCountryMarkedVisited', () => {
  const base = (over: Partial<AtlasData> = {}): AtlasData => ({
    countries: [{ code: 'FR', tripCount: 2, placeCount: 5, status: 'visited' }],
    stats: { totalTrips: 3, totalPlaces: 10, totalCountries: 1, totalDays: 14, totalCountriesPlanned: 0 },
    continents: { Europe: 1 },
    continentsPlanned: {},
    ...over,
  });

  it('appends an unknown country as visited and counts it once', () => {
    const next = withCountryMarkedVisited(base(), 'JP');

    expect(next.countries).toHaveLength(2);
    expect(next.countries[1]).toEqual({
      code: 'JP', placeCount: 0, tripCount: 0, firstVisit: null, lastVisit: null, status: 'visited',
    });
    expect(next.stats.totalCountries).toBe(2);
    expect(next.continents).toEqual({ Europe: 1, Asia: 1 });
    // Nothing was planned, so the planned tallies stay untouched.
    expect(next.stats.totalCountriesPlanned).toBe(0);
    expect(next.continentsPlanned).toEqual({});
  });

  it('returns the very same object when the country is already visited', () => {
    const prev = base();
    expect(withCountryMarkedVisited(prev, 'FR')).toBe(prev);
  });

  it('promotes a planned country instead of adding a second entry', () => {
    const prev = base({
      countries: [
        { code: 'FR', tripCount: 2, placeCount: 5, status: 'visited' },
        { code: 'JP', tripCount: 1, placeCount: 0, firstVisit: '2099-05-01', lastVisit: '2099-05-08', status: 'planned' },
      ],
      stats: { totalTrips: 3, totalPlaces: 10, totalCountries: 1, totalDays: 14, totalCountriesPlanned: 1 },
      continents: { Europe: 1 },
      continentsPlanned: { Asia: 1 },
    });

    const next = withCountryMarkedVisited(prev, 'JP');

    expect(next.countries).toHaveLength(2);
    // The planned trip's dates are not the dates of a visit, as the server agrees (#1535).
    expect(next.countries.find((c) => c.code === 'JP')).toMatchObject({ status: 'visited', firstVisit: null, lastVisit: null });
    expect(next.stats.totalCountries).toBe(2);
    expect(next.stats.totalCountriesPlanned).toBe(0);
    expect(next.continents).toEqual({ Europe: 1, Asia: 1 });
    expect(next.continentsPlanned).toEqual({ Asia: 0 });
  });

  it('promotes a dateless country the same way', () => {
    const prev = base({
      countries: [{ code: 'JP', tripCount: 1, placeCount: 0, status: 'idea' }],
      stats: { totalTrips: 3, totalPlaces: 10, totalCountries: 0, totalDays: 14, totalCountriesPlanned: 1 },
      continents: {},
      continentsPlanned: { Asia: 1 },
    });

    const next = withCountryMarkedVisited(prev, 'JP');

    expect(next.countries).toHaveLength(1);
    expect(next.countries[0].status).toBe('visited');
    expect(next.stats.totalCountries).toBe(1);
    expect(next.stats.totalCountriesPlanned).toBe(0);
  });

  it('never lets the planned tallies fall below zero', () => {
    const prev = base({
      countries: [{ code: 'JP', tripCount: 1, placeCount: 0, status: 'planned' }],
      stats: { totalTrips: 0, totalPlaces: 0, totalCountries: 0, totalDays: 0 },
      continents: {},
    });

    const next = withCountryMarkedVisited(prev, 'JP');

    expect(next.stats.totalCountriesPlanned).toBe(0);
    expect(next.continentsPlanned).toEqual({ Asia: 0 });
  });
});

function bucketItem(overrides: Partial<BucketItem>): BucketItem {
  return { id: 1, name: 'Somewhere', lat: null, lng: null, country_code: null, notes: null, target_date: null, ...overrides };
}

describe('countryColor', () => {
  it('always returns a color from the shared palette', () => {
    expect(COUNTRY_COLORS).toContain(countryColor('FRA'));
  });

  it('is stable for the same code across repeated calls', () => {
    expect(countryColor('JPN')).toBe(countryColor('JPN'));
  });

  it('is independent of how many other codes have been resolved before it (#reshuffle-bug)', () => {
    // Regression guard: an earlier order-index-based scheme reassigned every
    // country's color whenever one more country was visited/wishlisted.
    const before = countryColor('JPN');
    countryColor('FRA'); countryColor('DEU'); countryColor('ESP'); countryColor('ITA');
    expect(countryColor('JPN')).toBe(before);
  });

  it('gives different codes different colors in the common case', () => {
    expect(countryColor('FRA')).not.toBe(countryColor('JPN'));
  });
});

describe('wishlistA3Codes', () => {
  it('leaves a country alone when only one of its regions is wished for (#1901)', () => {
    const result = wishlistA3Codes([bucketItem({ country_code: 'DE', region_code: 'DE-BY' })], new Set());
    expect(result.size).toBe(0);
    expect(wishlistRegionCodes([bucketItem({ region_code: 'de-by' }), bucketItem({ region_code: null })])).toEqual(new Set(['DE-BY']));
  });

  it('resolves a bucket-list country to its A3 code', () => {
    const result = wishlistA3Codes([bucketItem({ country_code: 'JP' })], new Set());
    expect(result).toEqual(new Set(['JPN']));
  });

  it('excludes a bucket-list country that is already visited', () => {
    const result = wishlistA3Codes([bucketItem({ country_code: 'JP' })], new Set(['JPN']));
    expect(result.size).toBe(0);
  });

  it('ignores bucket-list items with no country_code (POI-only entries)', () => {
    const result = wishlistA3Codes([bucketItem({ country_code: null, lat: 35.6, lng: 139.7 })], new Set());
    expect(result.size).toBe(0);
  });

  it('dedupes multiple bucket-list items in the same country', () => {
    const result = wishlistA3Codes(
      [bucketItem({ id: 1, country_code: 'JP' }), bucketItem({ id: 2, country_code: 'JP' })],
      new Set(),
    );
    expect(result).toEqual(new Set(['JPN']));
  });
});

// The client half of #1898 — the same identity the server enforces, checked
// before the request so the refusal arrives translated and instantly.
describe('findBucketDuplicate (#1898)', () => {
  const candidate = { name: 'Japan', country_code: 'JP', target_date: null, lat: null, lng: null };

  it('finds an entry with the same name, country and target date', () => {
    const existing = bucketItem({ id: 5, name: 'Japan', country_code: 'JP' });
    expect(findBucketDuplicate([existing], candidate)).toBe(existing);
  });

  it('lets the same place through for a different target date', () => {
    const existing = bucketItem({ name: 'Japan', country_code: 'JP', target_date: '2027-05' });
    expect(findBucketDuplicate([existing], candidate)).toBeUndefined();
    expect(findBucketDuplicate([existing], { ...candidate, target_date: '2028-09' })).toBeUndefined();
    expect(findBucketDuplicate([existing], { ...candidate, target_date: '2027-05' })).toBe(existing);
  });

  it('treats an empty string and null as the same "not set"', () => {
    const undated = bucketItem({ name: 'Japan', country_code: 'JP', target_date: null });
    expect(findBucketDuplicate([undated], { ...candidate, target_date: '' })).toBe(undated);
    const blankCountry = bucketItem({ name: 'Japan', country_code: '' });
    expect(findBucketDuplicate([blankCountry], { ...candidate, country_code: null })).toBe(blankCountry);
  });

  it('ignores surrounding whitespace and ASCII case in the name', () => {
    const existing = bucketItem({ name: 'Kyoto', country_code: 'JP' });
    expect(findBucketDuplicate([existing], { ...candidate, name: '  kyoto ' })).toBe(existing);
  });

  it('keeps non-ASCII case apart, matching SQLite lower()', () => {
    // The server folds ASCII only, so folding more here would block a name the
    // server would have accepted.
    const existing = bucketItem({ name: 'Ísland', country_code: 'IS' });
    expect(findBucketDuplicate([existing], { ...candidate, name: 'ísland', country_code: 'IS' })).toBeUndefined();
  });

  it('separates the same name in another country or at other coordinates', () => {
    const existing = bucketItem({ name: 'Altstadt', country_code: 'DE', lat: 48.13, lng: 11.57 });
    expect(findBucketDuplicate([existing], { ...candidate, name: 'Altstadt', country_code: 'AT', lat: 48.13, lng: 11.57 }))
      .toBeUndefined();
    expect(findBucketDuplicate([existing], { ...candidate, name: 'Altstadt', country_code: 'DE', lat: 50.94, lng: 6.96 }))
      .toBeUndefined();
    expect(findBucketDuplicate([existing], { ...candidate, name: 'Altstadt', country_code: 'DE', lat: 48.13, lng: 11.57 }))
      .toBe(existing);
  });
});

// The region cache used to grow for the whole session (#1950). The eviction order is
// pure arithmetic, so it is decided here rather than inside the Leaflet effect.
describe('regionCacheEvictions (#1950)', () => {
  it('drops nothing while the cache is within its cap', () => {
    expect(regionCacheEvictions(['FR', 'IT'], new Set(), REGION_CACHE_MAX)).toEqual([]);
    expect(regionCacheEvictions(['FR', 'IT'], new Set(), 2)).toEqual([]);
  });

  it('drops the least recently viewed countries first, and only as many as needed', () => {
    expect(regionCacheEvictions(['FR', 'IT', 'ES', 'PT'], new Set(), 2)).toEqual(['FR', 'IT']);
    expect(regionCacheEvictions(['FR', 'IT', 'ES', 'PT'], new Set(), 3)).toEqual(['FR']);
  });

  it('never drops a country that has to stay', () => {
    // FR is the oldest entry but still on screen, so the next-oldest ones go instead.
    expect(regionCacheEvictions(['FR', 'IT', 'ES', 'PT'], new Set(['FR']), 2)).toEqual(['IT', 'ES']);
  });

  it('stays over the cap rather than dropping what is in view', () => {
    // Three countries on screen with room for two: evicting one would only make the
    // next pan fetch it straight back.
    expect(regionCacheEvictions(['FR', 'IT', 'ES'], new Set(['FR', 'IT', 'ES']), 2)).toEqual([]);
    expect(regionCacheEvictions(['FR', 'IT', 'ES', 'PT'], new Set(['IT', 'ES', 'PT']), 2)).toEqual(['FR']);
  });
});

describe('isBucketDuplicateError (#1898)', () => {
  it('recognises the 409 and nothing else', () => {
    expect(isBucketDuplicateError({ response: { status: 409 } })).toBe(true);
    expect(isBucketDuplicateError({ response: { status: 500 } })).toBe(false);
    expect(isBucketDuplicateError(new Error('offline'))).toBe(false);
    expect(isBucketDuplicateError(null)).toBe(false);
  });
});

describe('bucketTooltipWidth (#2153)', () => {
  it('caps at 480px on a wide viewport', () => {
    expect(bucketTooltipWidth(1400)).toBe(480);
  });

  it('shrinks to fit a narrow viewport, minus 32px of margin', () => {
    expect(bucketTooltipWidth(390)).toBe(358);
  });
});

describe('bucketTooltipHeight (#2153)', () => {
  it('is the 200px content cap plus the tooltip chrome, offset and arrow margin', () => {
    expect(bucketTooltipHeight(1080)).toBe(242);
  });

  it('follows the 40vh cap down on a short viewport', () => {
    expect(bucketTooltipHeight(400)).toBe(202);
  });
});

describe('bucketTooltipPlacement (#2153)', () => {
  const viewport = { width: 1000, height: 1080 };

  it('opens above a marker with room on all sides, centred with no horizontal nudge', () => {
    const placement = bucketTooltipPlacement({ x: 500, y: 400 }, viewport, 480);
    expect(placement.direction).toBe('top');
    expect(placement.offset).toEqual([0, -14]);
  });

  it('flips to open below the marker when there is no room above it', () => {
    const placement = bucketTooltipPlacement({ x: 500, y: 100 }, viewport, 480);
    expect(placement.direction).toBe('bottom');
    expect(placement.offset[1]).toBe(14);
  });

  it('still flips for a marker that clears 220px but not the tooltip itself', () => {
    // 225px of room used to read as "fits above", which clipped the top 17px away.
    const placement = bucketTooltipPlacement({ x: 500, y: 225 }, viewport, 480);
    expect(placement.direction).toBe('bottom');
  });

  it('keeps the tooltip above when neither side fits but there is more room above', () => {
    // Landscape phone, 300px tall: the tooltip wants 162px and gets 152 above, 132 below.
    const placement = bucketTooltipPlacement({ x: 500, y: 160 }, { width: 1000, height: 300 }, 480);
    expect(placement.direction).toBe('top');
  });

  it('nudges right when centring the tooltip would clip the left edge', () => {
    // Marker at x=50 with a 480px-wide tooltip centred on it would start at
    // x=-190; the offset needs to push the tooltip's left edge to the margin (8px).
    const placement = bucketTooltipPlacement({ x: 50, y: 400 }, viewport, 480);
    const defaultLeft = 50 - 480 / 2;
    expect(placement.offset[0]).toBe(8 - defaultLeft);
  });

  it('nudges left when centring the tooltip would clip the right edge', () => {
    const placement = bucketTooltipPlacement({ x: 950, y: 400 }, viewport, 480);
    const defaultLeft = 950 - 480 / 2;
    const clampedLeft = 1000 - 8 - 480;
    expect(placement.offset[0]).toBe(clampedLeft - defaultLeft);
  });

  it('does not nudge horizontally when the tooltip already fits', () => {
    const placement = bucketTooltipPlacement({ x: 500, y: 400 }, viewport, 480);
    expect(placement.offset[0]).toBe(0);
  });
});

describe('bucketTooltipNeedsScroll (#2153)', () => {
  it('is false when content fits within the height cap', () => {
    expect(bucketTooltipNeedsScroll(180, 200)).toBe(false);
    expect(bucketTooltipNeedsScroll(200, 200)).toBe(false);
  });

  it('is true once content actually overflows the cap', () => {
    expect(bucketTooltipNeedsScroll(260, 200)).toBe(true);
  });

  it('tolerates a 1px measurement rounding difference without enabling scroll', () => {
    expect(bucketTooltipNeedsScroll(201, 200)).toBe(false);
  });
});

describe('visitedRegionCount (#1639)', () => {
  const regions = {
    US: [{ status: 'visited' as const }, { status: 'planned' as const }, {}],
    DE: [{ status: 'visited' as const }],
  }
  it('counts visited regions over every country, and in one of them', () => {
    expect(visitedRegionCount(regions)).toBe(3)
    expect(visitedRegionCount(regions, 'US')).toBe(2)
    expect(visitedRegionCount(regions, 'FR')).toBe(0)
    expect(visitedRegionCount({})).toBe(0)
  })
})

describe('groupCountryPlaces (#2174)', () => {
  const detail = {
    trips: [{ id: 1, title: 'Berlin' }, { id: 2, title: 'Munich' }, { id: 3, title: 'Empty' }],
    places: [
      { id: 10, name: 'Reichstag', lat: 0, lng: 0, trip_id: 1, address: 'Platz der Republik' },
      { id: 11, name: 'Brandenburg Gate', lat: 0, lng: 0, trip_id: 1, address: 'Pariser Platz' },
      { id: 12, name: 'Marienplatz', lat: 0, lng: 0, trip_id: 2, address: null },
    ],
  }
  it('groups by trip in trip order, places by name, and drops empty trips', () => {
    const groups = groupCountryPlaces(detail)
    expect(groups.map(g => g.trip.title)).toEqual(['Berlin', 'Munich'])
    expect(groups[0].places.map(p => p.name)).toEqual(['Brandenburg Gate', 'Reichstag'])
  })
  it('searches name and address', () => {
    expect(groupCountryPlaces(detail, 'platz').flatMap(g => g.places.map(p => p.id))).toEqual([11, 10, 12])
    expect(groupCountryPlaces(detail, 'nothing')).toEqual([])
  })
})