diff --git a/src/db/rte.test.ts b/src/db/rte.test.ts index 9e06646..4a19087 100644 --- a/src/db/rte.test.ts +++ b/src/db/rte.test.ts @@ -3,7 +3,7 @@ import shell from 'shelljs'; import PouchDB from 'pouchdb'; -import { emptyRte, getRte, putNewRte, putRte } from './rte'; +import { getRte, putNewRte, putRte } from './rte'; import { put } from './lib'; import { emptyWpt } from './wpt'; diff --git a/src/db/trkseg.test.ts b/src/db/trkseg.test.ts new file mode 100644 index 0000000..5910839 --- /dev/null +++ b/src/db/trkseg.test.ts @@ -0,0 +1,128 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; +import shell from 'shelljs'; + +import PouchDB from 'pouchdb'; + +import { appendTrkpt, getTrkseg, putTrkseg } from './trkseg'; +import getUri from '../lib/ids'; + +import { put } from './lib'; +import { emptyWpt } from './wpt'; + +const test = it; +const jest = vi; + +declare global { + var db: any; + var dbReady: boolean; +} + +const originalDb = globalThis.db; +const originalDateNow = globalThis.Date.now; + +describe('The trkseg module with a real db', () => { + beforeEach(async () => { + globalThis.db = new PouchDB('dyomedea', { + auto_compaction: false, + }); + globalThis.Date.now = () => 0; + }); + afterEach(async () => { + try { + await db.destroy(); + } catch (err) { + // console.error(err); + await shell.exec('rm -rf $*$'); + } + globalThis.db = undefined; + globalThis.dbReady = false; + globalThis.Date.now = originalDateNow; + }); + it('can write and read a trkseg', async () => { + const id = 'gpx/4320836036089189/3trk/000000/000000'; + const trkseg: Trkseg = {}; + const trksegPut = await putTrkseg(id, trkseg); + const trksegGet = await getTrkseg({ id }); + expect(trksegGet).toEqual(trkseg); + }); + it('can write and read a trkseg with trkpts', async () => { + const id = 'whatever'; + const trkseg: Trkseg = { + trkpt: [ + { + $: { lat: 0, lon: 0 }, + }, + { + $: { lat: 1, lon: 1 }, + }, + ], + }; + const trksegPut = await putTrkseg(id, trkseg); + const trksegGet = await getTrkseg({ id }); + expect(trksegGet).toEqual(trkseg); + }); + it('can read external trkpts ', async () => { + const idTrkseg = 'gpx/4320836036089189/3trk/000000/000000'; + const idTrksegpt = 'gpx/4320836036089189/3trk/000000/000000/000035'; + const trkpt3: Wpt = { + $: { lat: 3, lon: 3 }, + }; + const trkseg0: Trkseg = { + trkpt: [ + { + $: { lat: 1, lon: 1 }, + }, + { + $: { lat: 2, lon: 2 }, + }, + ], + }; + const trkseg1: Trkseg = { + trkpt: [ + { + $: { lat: 1, lon: 1 }, + }, + { + $: { lat: 2, lon: 2 }, + }, + trkpt3, + ], + }; + const trksegPut = await putTrkseg(idTrkseg, trkseg0); + await put(idTrksegpt, 'trkpt', () => trkpt3, emptyWpt); + expect(await getTrkseg({ id: idTrkseg })).toEqual(trkseg1); + }); + it('can can append a new trkpt ', async () => { + const idTrkseg = 'gpx/4320836036089189/3trk/000000/000000'; + const trkpt3: Wpt = { + $: { lat: 3, lon: 3 }, + }; + const trkseg0: Trkseg = { + trkpt: [ + { + $: { lat: 1, lon: 1 }, + }, + { + $: { lat: 2, lon: 2 }, + }, + ], + }; + const trkseg1: Trkseg = { + trkpt: [ + { + $: { lat: 1, lon: 1 }, + }, + { + $: { lat: 2, lon: 2 }, + }, + trkpt3, + ], + }; + await putTrkseg(idTrkseg, trkseg0); + await appendTrkpt({ + trksegIdObj: getUri('trkseg', idTrkseg), + trkpt: trkpt3, + }); + expect(await getTrkseg({ id: idTrkseg })).toEqual(trkseg1); + }); +});