Starting to reimplement GPX db functions.

This commit is contained in:
Eric van der Vlist 2022-11-07 16:38:44 +01:00
parent 56b36ea063
commit 510736a33a
4 changed files with 139 additions and 2 deletions

59
src/db/gpx.test.ts Normal file
View File

@ -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',
});
});
});

44
src/db/gpx.ts Normal file
View File

@ -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 });
};

34
src/db/types.d.ts vendored Normal file
View File

@ -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 {}

View File

@ -16,8 +16,8 @@ describe('The dispatcher-worker ', () => {
expect(port.onmessage).toBeDefined(); expect(port.onmessage).toBeDefined();
expect(port.postMessage).not.toBeCalled(); expect(port.postMessage).not.toBeCalled();
}); });
test('receives a ping and sends back an unknownAction', () => { test('receives a ping and sends back an unknownAction', async () => {
port.onmessage({ data: { id: 5, payload: { action: 'ping' } } }); await port.onmessage({ data: { id: 5, payload: { action: 'ping' } } });
expect(port.postMessage).toBeCalledWith({ expect(port.postMessage).toBeCalledWith({
id: 5, id: 5,
payload: 'unknownAction', payload: 'unknownAction',