diff --git a/src/db/rtept.test.ts b/src/db/rtept.test.ts index 0d91205..d28d982 100644 --- a/src/db/rtept.test.ts +++ b/src/db/rtept.test.ts @@ -131,4 +131,59 @@ describe('The rtept module with a real db', () => { expect(await getRte({ id: idRte })).toEqual(rte2); expect((await get(idRtept)).doc).toEqual(rtept4); }); + it('can write a rtept outside a rte with a negative index', async () => { + const idRte = 'gpx/4320836410265485/2rte/000034'; + const idRtept = 'gpx/4320836410265485/2rte/000034/000035'; + const rtept3: Wpt = { + $: { lat: 3, lon: 3 }, + }; + const rtept4: Wpt = { + $: { lat: 4, lon: 4 }, + }; + const rte0: Rte = { + name: 'A new route', + rtept: [ + { + $: { lat: 1, lon: 1 }, + }, + { + $: { lat: 2, lon: 2 }, + }, + ], + }; + const rte1: Rte = { + name: 'A new route', + rtept: [ + { + $: { lat: 1, lon: 1 }, + }, + { + $: { lat: 2, lon: 2 }, + }, + rtept3, + ], + }; + const rte2: Rte = { + name: 'A new route', + rtept: [ + { + $: { lat: 1, lon: 1 }, + }, + { + $: { lat: 2, lon: 2 }, + }, + rtept4, + ], + }; + + const rtePut = await putRte({ id: idRte, rte: rte0 }); + await put(idRtept, 'rtept', () => rtept3, emptyWpt); + await putRtept({ + idRte, + index: -1, + rtept: rtept4, + }); + expect(await getRte({ id: idRte })).toEqual(rte2); + expect((await get(idRtept)).doc).toEqual(rtept4); + }); }); diff --git a/src/db/rtept.ts b/src/db/rtept.ts index f1adddb..efb6410 100644 --- a/src/db/rtept.ts +++ b/src/db/rtept.ts @@ -32,7 +32,7 @@ export const putRtept = async (params: any) => { const nbRteptInRte = rte && rte.rtept ? rte.rtept.length : 0; const nbRteptOutRte = docs.rows.length - 1; const nbRtept = nbRteptInRte + nbRteptOutRte; - const positiveIndex = index >= 0 ? index : nbRtept - index; + const positiveIndex = index >= 0 ? index : nbRtept + index; // console.log({ // caller: 'putRtept', // nbRteptInRte,