From 510736a33abd166c2c863b2c9147969e9fdc8851 Mon Sep 17 00:00:00 2001 From: evlist Date: Mon, 7 Nov 2022 16:38:44 +0100 Subject: [PATCH] Starting to reimplement GPX db functions. --- src/db/gpx.test.ts | 59 +++++++++++++++++++++++++++ src/db/gpx.ts | 44 ++++++++++++++++++++ src/db/types.d.ts | 34 +++++++++++++++ src/workers/dispatcher-worker.test.js | 4 +- 4 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 src/db/gpx.test.ts create mode 100644 src/db/gpx.ts create mode 100644 src/db/types.d.ts diff --git a/src/db/gpx.test.ts b/src/db/gpx.test.ts new file mode 100644 index 0000000..e6ce094 --- /dev/null +++ b/src/db/gpx.test.ts @@ -0,0 +1,59 @@ +import { putNewGpx } from './gpx'; +declare global { + var db: any; + var dbReady: boolean; +} + +const originalDb = globalThis.db; +const originalDateNow = globalThis.Date.now; + +beforeEach(() => { + globalThis.db = { put: jest.fn() }; + globalThis.Date.now = () => 0; +}); + +afterEach(() => { + globalThis.db = originalDb; + globalThis.Date.now = originalDateNow; +}); + +describe('The gpx module', () => { + test('db.put() a new Gpx when required', () => { + putNewGpx('whatever'); + expect(globalThis.db.put).toBeCalledWith({ + _id: 'gpx/whatever', + doc: { + $: { + creator: 'dyomedea version 0.000002', + version: '1.1', + xmlns: 'http://www.topografix.com/GPX/1/1', + 'xmlns:dyo': 'http://xmlns.dyomedea.com/', + 'xmlns:gpxtpx': + 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1', + 'xmlns:gpxx': 'http://www.garmin.com/xmlschemas/GpxExtensions/v3', + 'xmlns:wptx1': + 'http://www.garmin.com/xmlschemas/WaypointExtension/v1', + 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', + 'xsi:schemaLocation': + 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/WaypointExtension/v1 http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd', + }, + extensions: undefined, + metadata: { + author: undefined, + bounds: undefined, + copyright: undefined, + desc: undefined, + extensions: undefined, + keywords: undefined, + link: undefined, + name: undefined, + time: '1970-01-01T00:00:00.000Z', + }, + rte: undefined, + trk: undefined, + wpt: undefined, + }, + type: 'gpx', + }); + }); +}); diff --git a/src/db/gpx.ts b/src/db/gpx.ts new file mode 100644 index 0000000..ffd2957 --- /dev/null +++ b/src/db/gpx.ts @@ -0,0 +1,44 @@ +import { cloneDeep } from 'lodash'; +import getUri from '../lib/ids'; + +declare global { + var db: any; + var dbReady: boolean; +} + +const emptyGpx: Gpx = { + $: { + version: '1.1', + creator: 'dyomedea version 0.000002', + xmlns: 'http://www.topografix.com/GPX/1/1', + 'xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance', + 'xsi:schemaLocation': + 'http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www8.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/WaypointExtension/v1 http://www8.garmin.com/xmlschemas/WaypointExtensionv1.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd', + 'xmlns:gpxx': 'http://www.garmin.com/xmlschemas/GpxExtensions/v3', + 'xmlns:wptx1': 'http://www.garmin.com/xmlschemas/WaypointExtension/v1', + 'xmlns:gpxtpx': 'http://www.garmin.com/xmlschemas/TrackPointExtension/v1', + 'xmlns:dyo': 'http://xmlns.dyomedea.com/', + }, + metadata: { + name: undefined, + desc: undefined, + author: undefined, + copyright: undefined, + link: undefined, + time: undefined, + keywords: undefined, + bounds: undefined, + extensions: undefined, + }, + wpt: undefined, + rte: undefined, + trk: undefined, + extensions: undefined, +}; + +export const putNewGpx = (id: string) => { + const uri = getUri('gpx', { gpx: id }); + const gpx = cloneDeep(emptyGpx); + gpx.metadata!.time = new Date(Date.now()).toISOString(); + db.put({ _id: uri, type: 'gpx', doc: gpx }); +}; diff --git a/src/db/types.d.ts b/src/db/types.d.ts new file mode 100644 index 0000000..78d22cf --- /dev/null +++ b/src/db/types.d.ts @@ -0,0 +1,34 @@ +interface Gpx { + $: Gpx_; + metadata?: Metadata; + wpt?: any[]; + rte?: any[]; + trk?: any[]; + extensions?: Extensions; +} + +interface Gpx_ { + version: '1.1'; + creator: string; + xmlns: 'http://www.topografix.com/GPX/1/1'; + 'xmlns:xsi'?: 'http://www.w3.org/2001/XMLSchema-instance'; + 'xsi:schemaLocation'?: string; + 'xmlns:gpxx'?: string; + 'xmlns:wptx1'?: string; + 'xmlns:gpxtpx'?: string; + 'xmlns:dyo'?: 'http://xmlns.dyomedea.com/'; +} + +interface Metadata { + name?: string; + desc?: string; + author?: string; + copyright?: string; + link?: string; + time?: string; + keywords?: string; + bounds?: string; + extensions?: Extensions; +} + +interface Extensions {} diff --git a/src/workers/dispatcher-worker.test.js b/src/workers/dispatcher-worker.test.js index 416fb9a..7db3e71 100644 --- a/src/workers/dispatcher-worker.test.js +++ b/src/workers/dispatcher-worker.test.js @@ -16,8 +16,8 @@ describe('The dispatcher-worker ', () => { expect(port.onmessage).toBeDefined(); expect(port.postMessage).not.toBeCalled(); }); - test('receives a ping and sends back an unknownAction', () => { - port.onmessage({ data: { id: 5, payload: { action: 'ping' } } }); + test('receives a ping and sends back an unknownAction', async () => { + await port.onmessage({ data: { id: 5, payload: { action: 'ping' } } }); expect(port.postMessage).toBeCalledWith({ id: 5, payload: 'unknownAction',