2023-02-09 13:36:14 +00:00
|
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
2023-02-09 15:37:46 +00:00
|
|
|
import shell from 'shelljs';
|
|
|
|
|
2023-02-09 20:07:09 +00:00
|
|
|
import PouchDB from 'pouchdb';
|
2023-02-09 13:36:14 +00:00
|
|
|
|
2023-02-09 15:37:46 +00:00
|
|
|
import { emptyRte, getRte, putNewRte, putRte } from './rte';
|
2023-02-09 20:07:09 +00:00
|
|
|
import { put } from './lib';
|
|
|
|
import { emptyWpt } from './wpt';
|
2023-02-09 13:36:14 +00:00
|
|
|
|
|
|
|
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 rte module', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
globalThis.db = { put: jest.fn() };
|
|
|
|
globalThis.Date.now = () => 0;
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
globalThis.db = originalDb;
|
|
|
|
globalThis.Date.now = originalDateNow;
|
|
|
|
});
|
|
|
|
test('db.put() a new rte when required', async () => {
|
|
|
|
putNewRte({ gpx: 4320000000000000, rte: 25 });
|
|
|
|
await expect(globalThis.db.put).toBeCalledWith({
|
|
|
|
_id: 'gpx/4320000000000000/2rte/000025',
|
|
|
|
_rev: undefined,
|
|
|
|
doc: {
|
|
|
|
cmt: undefined,
|
|
|
|
desc: undefined,
|
|
|
|
extensions: undefined,
|
|
|
|
link: undefined,
|
|
|
|
name: undefined,
|
|
|
|
number: 0,
|
|
|
|
rtept: undefined,
|
|
|
|
src: undefined,
|
|
|
|
type: undefined,
|
|
|
|
},
|
|
|
|
type: 'rte',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
test('db.put() generates an id for the trk if needed', async () => {
|
|
|
|
const id = await putNewRte({ gpx: 0 });
|
|
|
|
expect(id).toEqual({ gpx: 0, rte: 0 });
|
|
|
|
});
|
|
|
|
test('db.put() generates ids for both gpx and trk if needed', async () => {
|
|
|
|
const id = await putNewRte();
|
|
|
|
expect(id).toEqual({ gpx: 4320000000000000, rte: 0 });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('The rte module with a real db', () => {
|
|
|
|
beforeEach(async () => {
|
2023-02-09 20:07:09 +00:00
|
|
|
globalThis.db = new PouchDB('dyomedea', {
|
|
|
|
auto_compaction: false,
|
|
|
|
});
|
2023-02-09 13:36:14 +00:00
|
|
|
globalThis.Date.now = () => 0;
|
|
|
|
});
|
|
|
|
afterEach(async () => {
|
2023-02-09 15:37:46 +00:00
|
|
|
try {
|
|
|
|
await db.destroy();
|
|
|
|
} catch (err) {
|
2023-02-09 20:07:09 +00:00
|
|
|
// console.error(err);
|
|
|
|
await shell.exec('rm -rf $*$');
|
2023-02-09 15:37:46 +00:00
|
|
|
}
|
2023-02-09 20:07:09 +00:00
|
|
|
globalThis.db = undefined;
|
|
|
|
globalThis.dbReady = false;
|
2023-02-09 13:36:14 +00:00
|
|
|
globalThis.Date.now = originalDateNow;
|
|
|
|
});
|
2023-02-09 15:37:46 +00:00
|
|
|
it('can write and read a rte', async () => {
|
|
|
|
const id = 'whatever';
|
|
|
|
const rte: Rte = {
|
|
|
|
name: 'A new route',
|
|
|
|
};
|
|
|
|
const rtePut = await putRte({ id, rte });
|
|
|
|
expect(rtePut).toEqual(rte);
|
|
|
|
const rteGet = await getRte({ id });
|
|
|
|
expect(rteGet).toEqual(rte);
|
|
|
|
});
|
|
|
|
it('can write and read a rte with rtepts', async () => {
|
|
|
|
const id = 'whatever';
|
|
|
|
const rte: Rte = {
|
|
|
|
name: 'A new route',
|
|
|
|
rtept: [
|
|
|
|
{
|
|
|
|
$: { lat: 0, lon: 0 },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
$: { lat: 1, lon: 1 },
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
const rtePut = await putRte({ id, rte });
|
|
|
|
expect(rtePut).toEqual(rte);
|
|
|
|
const rteGet = await getRte({ id });
|
|
|
|
expect(rteGet).toEqual(rte);
|
|
|
|
});
|
2023-02-09 20:07:09 +00:00
|
|
|
it('can read external rtepts ', async () => {
|
|
|
|
const idRte = 'gpx/4320836410265485/2rte/000034';
|
|
|
|
const idRtept = 'gpx/4320836410265485/2rte/000034/000035';
|
|
|
|
const rtept3: Wpt = {
|
|
|
|
$: { lat: 3, lon: 3 },
|
|
|
|
};
|
|
|
|
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 rtePut = await putRte({ id: idRte, rte: rte0 });
|
|
|
|
expect(rtePut).toEqual(rte0);
|
|
|
|
await put(idRtept, 'rtept', () => rtept3, emptyWpt);
|
|
|
|
expect(await getRte({ id: idRte })).toEqual(rte1);
|
|
|
|
});
|
2023-02-09 13:36:14 +00:00
|
|
|
});
|