lecheminvert.tech/test/unit/polyline.spec.ts

43 lines
1.4 KiB
TypeScript
Raw Permalink Normal View History

import { describe, expect, it } from 'vitest'
import { decodePolyline, thin } from '#shared/polyline'
describe('decodePolyline', () => {
it('decode l exemple de reference de Google a la precision 5', () => {
// Exemple officiel : trois points en Californie.
const r = decodePolyline('_p~iF~ps|U_ulLnnqC_mqNvxq`@', 5)
expect(r).toHaveLength(3)
// Ordre GeoJSON : longitude d'abord.
expect(r[0]![1]).toBeCloseTo(38.5, 4)
expect(r[0]![0]).toBeCloseTo(-120.2, 4)
})
it('place le trace ailleurs si la precision est fausse', () => {
// C'est tout l'enjeu : MOTIS encode a 1e7, Google a 1e5.
const juste = decodePolyline('_p~iF~ps|U', 5)
const faux = decodePolyline('_p~iF~ps|U', 7)
expect(faux[0]![1]).not.toBeCloseTo(juste[0]![1], 2)
expect(faux[0]![1]).toBeCloseTo(juste[0]![1] / 100, 4)
})
it('renvoie un tableau vide sur une chaine vide', () => {
expect(decodePolyline('', 7)).toEqual([])
})
})
describe('thin', () => {
const trace = Array.from({ length: 1000 }, (_, i) => [i, i] as [number, number])
it('ne touche pas a un trace deja assez court', () => {
const court = trace.slice(0, 50)
expect(thin(court, 800)).toBe(court)
})
it('ramene un trace long sous la limite demandee', () => {
expect(thin(trace, 100).length).toBeLessThanOrEqual(101)
})
it('conserve toujours le terminus', () => {
const r = thin(trace, 100)
expect(r[r.length - 1]).toEqual(trace[trace.length - 1])
})
})