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.

4.6 KB
Raw Blame
import { describe, it, expect } from 'vitest'
import { roadtripStopTypeSchema } from '@trek/shared'
import { SERVICE_STOP_TYPES, SERVICE_COLORS } from './roadtripModel'
import {
  STOP_KINDS, STOP_KIND_BY_KEY, CORRIDOR_CATEGORIES, CORRIDOR_CATEGORY_BY_KEY, CORRIDOR_CATEGORY_KEYS,
  SERVICE_KIND_KEYS, REFUELLING_STOP_TYPES, isOvernightCategory, manualStopKindFor,
} from './stopKinds'

/**
 * The table exists to stop nine lists from drifting apart, and these are the assertions
 * that make that true rather than intended. Each one pins the table against a list that
 * cannot import it: the Zod enum lives in `@trek/shared` and must not depend on the
 * client, and `SERVICE_STOP_TYPES` lives in the React-free model.
 */
describe('stop kinds', () => {
  it('FE-STOPKIND-001: covers exactly the kinds the shared enum allows', () => {
    const fromSchema = [...roadtripStopTypeSchema.options].sort()
    const fromTable = STOP_KINDS.map(k => k.key).sort()
    expect(fromTable).toEqual(fromSchema)
  })

  it('FE-STOPKIND-002: agrees with the model about which kinds interrupt a drive', () => {
    expect([...SERVICE_KIND_KEYS].sort()).toEqual([...SERVICE_STOP_TYPES].sort())
  })

  it('FE-STOPKIND-003: every kind carries the colour the model gives it', () => {
    for (const kind of STOP_KINDS) expect(kind.color).toBe(SERVICE_COLORS[kind.key])
  })

  it('FE-STOPKIND-004: a refuelling kind is a kind, so a range budget cannot reset on a typo', () => {
    for (const key of REFUELLING_STOP_TYPES) expect(STOP_KIND_BY_KEY[key]).toBeDefined()
  })

  it('FE-STOPKIND-005: the corridor offers every category in a stable order', () => {
    // Sleeping sits next to sleeping: hotel follows campsite rather than landing wherever
    // the stop-kind table happens to end.
    expect(CORRIDOR_CATEGORY_KEYS).toEqual(['fuel', 'charging', 'rest_area', 'campsite', 'hotel', 'restaurant', 'sights'])
  })

  it('FE-STOPKIND-008: accommodation is a service stop as well as an overnight category', () => {
    expect(CORRIDOR_CATEGORY_BY_KEY.hotel.stopKind).toBe('hotel')
    expect(STOP_KIND_BY_KEY.hotel).toBeDefined()
    expect(SERVICE_KIND_KEYS).toContain('hotel')
  })

  it('FE-STOPKIND-009: a night can be booked at a campsite too, so the popup asks', () => {
    expect(isOvernightCategory('hotel')).toBe(true)
    expect(isOvernightCategory('campsite')).toBe(true)
    expect(isOvernightCategory('fuel')).toBe(false)
    expect(isOvernightCategory(null)).toBe(false)
  })

  it('FE-STOPKIND-010: every category that is a stop kind names the kind it creates', () => {
    for (const category of CORRIDOR_CATEGORIES) {
      if (category.stopKind === null) continue
      expect(category.stopKind).toBe(category.key)
      expect(STOP_KIND_BY_KEY[category.key]).toBeDefined()
    }
  })

  it('FE-STOPKIND-006: two kinds are named under a key of their own, and that is on purpose', () => {
    // Spelled out rather than derived: the corridor calls a rest area rest_area and names
    // it under rest, a restaurant restaurant and names it under food. A derived label key
    // would silently ask for two translations that do not exist.
    expect(STOP_KIND_BY_KEY.rest_area.labelKey).toBe('roadtrip.poi.rest')
    expect(STOP_KIND_BY_KEY.restaurant.labelKey).toBe('roadtrip.poi.food')
    for (const kind of STOP_KINDS) {
      if (kind.key !== 'rest_area' && kind.key !== 'restaurant' && kind.key !== 'hotel') {
        expect(kind.labelKey).toBe(`roadtrip.poi.${kind.key}`)
      }
    }
  })

  it('FE-STOPKIND-007: every kind offers a dwell the popup can pre-fill', () => {
    for (const kind of STOP_KINDS) {
      expect(kind.defaultMinutes).toBeGreaterThan(0)
      expect(Number.isInteger(kind.defaultMinutes)).toBe(true)
    }
  })

  it('FE-STOPKIND-008: the manual add opens on what the corridor is looking for', () => {
    // A stop added by hand used to open on fuel whatever the panel above it said, which
    // on an electric car meant a picker reading Charging over a form reading Fuel.
    for (const category of CORRIDOR_CATEGORIES) {
      expect(manualStopKindFor([category.key])).toBe(category.stopKind)
    }
    // Multi-select, so the first the panel LISTS wins: falling back would open on a kind
    // that is not among the ones switched on, which is a worse answer than any of them.
    const [first, second] = CORRIDOR_CATEGORY_KEYS
    expect(manualStopKindFor([second, first])).toBe(CORRIDOR_CATEGORY_BY_KEY[first].stopKind)
    // Nothing selected is no answer at all, and the caller decides what to do with that.
    expect(manualStopKindFor([])).toBeNull()
    expect(manualStopKindFor(['not-a-category'])).toBeNull()
  })
})