Files
TREK/client/src/components/Map/reservationName.test.ts
T
Maurice c435033343 refactor: one endpoint-name cleaner for both map renderers
The scan that replaced the parenthetical-stripping regex went into the Leaflet
overlay and its Mapbox GL counterpart as two byte-identical copies, which is
what the duplication gate caught. trackColors.ts next door exists for exactly
this reason - both renderers read a track's colour through one function so it
cannot come out different on one map than the other - so the cleaner follows it.

Behaviour is unchanged. The test brute-forces the new function against the regex
it replaced over all 55,987 names up to six characters of "ab() \t", and pins the
32k-space input that used to take a second.
2026-08-25 22:08:32 +02:00

54 lines
1.9 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { cleanEndpointName } from './reservationName'
// The pattern this replaced. Kept here so the equivalence claim in the module
// is a test rather than a comment.
const LEGACY = /\s*\([^)]*\)/g
describe('cleanEndpointName', () => {
it('drops the parenthetical a booking confirmation appends', () => {
expect(cleanEndpointName('Wien Hbf (Vienna)')).toBe('Wien Hbf')
})
it('leaves an unclosed bracket alone', () => {
expect(cleanEndpointName('Gare de Lyon (Paris')).toBe('Gare de Lyon (Paris')
})
it('takes the whitespace directly before the bracket with it', () => {
expect(cleanEndpointName('Zurich HB (ZRH) ')).toBe('Zurich HB')
})
it('handles several brackets and an empty one', () => {
expect(cleanEndpointName('A (b) C () D')).toBe('A C D')
})
it('returns a name without brackets untouched', () => {
expect(cleanEndpointName('Amsterdam Centraal')).toBe('Amsterdam Centraal')
})
it('does not hang on the input that made the regex quadratic', () => {
// 32k spaces with no "(" after them: the shape that froze the map for
// every member of a trip once one member had typed it.
const started = performance.now()
expect(cleanEndpointName(' '.repeat(32_000) + 'x')).toBe('x')
expect(performance.now() - started).toBeLessThan(100)
})
it('agrees with the regex on every generated name', () => {
const alphabet = [...'ab() \t']
let checked = 0
for (let len = 0; len <= 6; len++) {
const total = alphabet.length ** len
for (let n = 0; n < total; n++) {
let s = ''
for (let k = n, i = 0; i < len; i++, k = Math.floor(k / alphabet.length)) {
s += alphabet[k % alphabet.length]
}
expect(cleanEndpointName(s)).toBe(s.replace(LEGACY, '').trim())
checked++
}
}
expect(checked).toBe(55987)
})
})