More tests and debugging putRtept

This commit is contained in:
Eric van der Vlist 2023-02-09 21:07:09 +01:00
parent 3f5bb1633c
commit c30ceecbb1
7 changed files with 370 additions and 52 deletions

213
package-lock.json generated
View File

@ -49,6 +49,7 @@
"@types/pouchdb": "^6.4.0", "@types/pouchdb": "^6.4.0",
"@types/proj4": "^2.5.2", "@types/proj4": "^2.5.2",
"@types/shelljs": "^0.8.11", "@types/shelljs": "^0.8.11",
"@vitest/ui": "^0.28.4",
"jsdom": "^21.1.0", "jsdom": "^21.1.0",
"license-checker": "^25.0.1", "license-checker": "^25.0.1",
"license-compatibility-checker": "^0.3.5", "license-compatibility-checker": "^0.3.5",
@ -1408,11 +1409,52 @@
"resolved": "https://registry.npmjs.org/@mapbox/unitbezier/-/unitbezier-0.0.0.tgz", "resolved": "https://registry.npmjs.org/@mapbox/unitbezier/-/unitbezier-0.0.0.tgz",
"integrity": "sha512-HPnRdYO0WjFjRTSwO3frz1wKaU649OBFPX3Zo/2WZvuRi6zMiRGui8SnPQiQABgqCf8YikDe5t3HViTVw1WUzA==" "integrity": "sha512-HPnRdYO0WjFjRTSwO3frz1wKaU649OBFPX3Zo/2WZvuRi6zMiRGui8SnPQiQABgqCf8YikDe5t3HViTVw1WUzA=="
}, },
"node_modules/@nodelib/fs.scandir": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz",
"integrity": "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==",
"dev": true,
"dependencies": {
"@nodelib/fs.stat": "2.0.5",
"run-parallel": "^1.1.9"
},
"engines": {
"node": ">= 8"
}
},
"node_modules/@nodelib/fs.stat": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.5.tgz",
"integrity": "sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A==",
"dev": true,
"engines": {
"node": ">= 8"
}
},
"node_modules/@nodelib/fs.walk": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.8.tgz",
"integrity": "sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==",
"dev": true,
"dependencies": {
"@nodelib/fs.scandir": "2.1.5",
"fastq": "^1.6.0"
},
"engines": {
"node": ">= 8"
}
},
"node_modules/@petamoriken/float16": { "node_modules/@petamoriken/float16": {
"version": "3.7.1", "version": "3.7.1",
"resolved": "https://registry.npmjs.org/@petamoriken/float16/-/float16-3.7.1.tgz", "resolved": "https://registry.npmjs.org/@petamoriken/float16/-/float16-3.7.1.tgz",
"integrity": "sha512-oXZOc+aePd0FnhTWk15pyqK+Do87n0TyLV1nxdEougE95X/WXWDqmQobfhgnSY7QsWn5euZUWuDVeTQvoQ5VNw==" "integrity": "sha512-oXZOc+aePd0FnhTWk15pyqK+Do87n0TyLV1nxdEougE95X/WXWDqmQobfhgnSY7QsWn5euZUWuDVeTQvoQ5VNw=="
}, },
"node_modules/@polka/url": {
"version": "1.0.0-next.21",
"resolved": "https://registry.npmjs.org/@polka/url/-/url-1.0.0-next.21.tgz",
"integrity": "sha512-a5Sab1C4/icpTZVzZc5Ghpz88yQtGOyNqYXcZgOssB2uuAr+wF/MvN6bgtW32q7HHrvBki+BsZ0OuNv6EV3K9g==",
"dev": true
},
"node_modules/@popperjs/core": { "node_modules/@popperjs/core": {
"version": "2.11.6", "version": "2.11.6",
"resolved": "https://registry.npmjs.org/@popperjs/core/-/core-2.11.6.tgz", "resolved": "https://registry.npmjs.org/@popperjs/core/-/core-2.11.6.tgz",
@ -2234,6 +2276,19 @@
"tinyspy": "^1.0.2" "tinyspy": "^1.0.2"
} }
}, },
"node_modules/@vitest/ui": {
"version": "0.28.4",
"resolved": "https://registry.npmjs.org/@vitest/ui/-/ui-0.28.4.tgz",
"integrity": "sha512-LQfCCFc17n49mwtraV9/NAWl2DUqJS/9ZEa3fqJjoYO+HowdseQ5jvWflpzliCyfrIAh6cXVo1bNzHnDXe0cbw==",
"dev": true,
"dependencies": {
"fast-glob": "^3.2.12",
"flatted": "^3.2.7",
"pathe": "^1.1.0",
"picocolors": "^1.0.0",
"sirv": "^2.0.2"
}
},
"node_modules/@vitest/utils": { "node_modules/@vitest/utils": {
"version": "0.28.4", "version": "0.28.4",
"resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-0.28.4.tgz", "resolved": "https://registry.npmjs.org/@vitest/utils/-/utils-0.28.4.tgz",
@ -3947,12 +4002,37 @@
"resolved": "https://registry.npmjs.org/type/-/type-2.7.2.tgz", "resolved": "https://registry.npmjs.org/type/-/type-2.7.2.tgz",
"integrity": "sha512-dzlvlNlt6AXU7EBSfpAscydQ7gXB+pPGsPnfJnZpiNJBDj7IaJzQlBZYGdEi4R9HmPdBv2XmWJ6YUtoTa7lmCw==" "integrity": "sha512-dzlvlNlt6AXU7EBSfpAscydQ7gXB+pPGsPnfJnZpiNJBDj7IaJzQlBZYGdEi4R9HmPdBv2XmWJ6YUtoTa7lmCw=="
}, },
"node_modules/fast-glob": {
"version": "3.2.12",
"resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.2.12.tgz",
"integrity": "sha512-DVj4CQIYYow0BlaelwK1pHl5n5cRSJfM60UA0zK891sVInoPri2Ekj7+e1CT3/3qxXenpI+nBBmQAcJPJgaj4w==",
"dev": true,
"dependencies": {
"@nodelib/fs.stat": "^2.0.2",
"@nodelib/fs.walk": "^1.2.3",
"glob-parent": "^5.1.2",
"merge2": "^1.3.0",
"micromatch": "^4.0.4"
},
"engines": {
"node": ">=8.6.0"
}
},
"node_modules/fast-levenshtein": { "node_modules/fast-levenshtein": {
"version": "2.0.6", "version": "2.0.6",
"resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz", "resolved": "https://registry.npmjs.org/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz",
"integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==", "integrity": "sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==",
"dev": true "dev": true
}, },
"node_modules/fastq": {
"version": "1.15.0",
"resolved": "https://registry.npmjs.org/fastq/-/fastq-1.15.0.tgz",
"integrity": "sha512-wBrocU2LCXXa+lWBt8RoIRD89Fi8OdABODa/kEnyeyjS5aZO5/GNvI5sEINADqP/h8M29UHTHUb53sUu5Ihqdw==",
"dev": true,
"dependencies": {
"reusify": "^1.0.4"
}
},
"node_modules/fd-slicer": { "node_modules/fd-slicer": {
"version": "1.1.0", "version": "1.1.0",
"resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz", "resolved": "https://registry.npmjs.org/fd-slicer/-/fd-slicer-1.1.0.tgz",
@ -3992,6 +4072,12 @@
"micromatch": "^4.0.2" "micromatch": "^4.0.2"
} }
}, },
"node_modules/flatted": {
"version": "3.2.7",
"resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.7.tgz",
"integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ==",
"dev": true
},
"node_modules/for-each": { "node_modules/for-each": {
"version": "0.3.3", "version": "0.3.3",
"resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz",
@ -4194,6 +4280,18 @@
"url": "https://github.com/sponsors/isaacs" "url": "https://github.com/sponsors/isaacs"
} }
}, },
"node_modules/glob-parent": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz",
"integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==",
"dev": true,
"dependencies": {
"is-glob": "^4.0.1"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/global-dirs": { "node_modules/global-dirs": {
"version": "3.0.1", "version": "3.0.1",
"resolved": "https://registry.npmjs.org/global-dirs/-/global-dirs-3.0.1.tgz", "resolved": "https://registry.npmjs.org/global-dirs/-/global-dirs-3.0.1.tgz",
@ -4702,6 +4800,15 @@
"url": "https://github.com/sponsors/sindresorhus" "url": "https://github.com/sponsors/sindresorhus"
} }
}, },
"node_modules/is-extglob": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz",
"integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==",
"dev": true,
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/is-fullwidth-code-point": { "node_modules/is-fullwidth-code-point": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz",
@ -4711,6 +4818,18 @@
"node": ">=8" "node": ">=8"
} }
}, },
"node_modules/is-glob": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz",
"integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==",
"dev": true,
"dependencies": {
"is-extglob": "^2.1.1"
},
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/is-installed-globally": { "node_modules/is-installed-globally": {
"version": "0.4.0", "version": "0.4.0",
"resolved": "https://registry.npmjs.org/is-installed-globally/-/is-installed-globally-0.4.0.tgz", "resolved": "https://registry.npmjs.org/is-installed-globally/-/is-installed-globally-0.4.0.tgz",
@ -6052,6 +6171,15 @@
"url": "https://github.com/sponsors/mesqueeb" "url": "https://github.com/sponsors/mesqueeb"
} }
}, },
"node_modules/merge2": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz",
"integrity": "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==",
"dev": true,
"engines": {
"node": ">= 8"
}
},
"node_modules/mgrs": { "node_modules/mgrs": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/mgrs/-/mgrs-1.0.0.tgz", "resolved": "https://registry.npmjs.org/mgrs/-/mgrs-1.0.0.tgz",
@ -6188,6 +6316,15 @@
"ufo": "^1.0.1" "ufo": "^1.0.1"
} }
}, },
"node_modules/mrmime": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/mrmime/-/mrmime-1.0.1.tgz",
"integrity": "sha512-hzzEagAgDyoU1Q6yg5uI+AorQgdvMCur3FcKf7NhMKWsaYg+RnbTyHRa/9IlLF9rf455MOCtcqqrQQ83pPP7Uw==",
"dev": true,
"engines": {
"node": ">=10"
}
},
"node_modules/ms": { "node_modules/ms": {
"version": "2.1.2", "version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
@ -7145,6 +7282,26 @@
"resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz", "resolved": "https://registry.npmjs.org/querystringify/-/querystringify-2.2.0.tgz",
"integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ==" "integrity": "sha512-FIqgj2EUvTa7R50u0rGsyTftzjYmv/a3hO345bZNrqabNqjtgiDMgmo4mkUjd+nzU5oF3dClKqFIPUKybUyqoQ=="
}, },
"node_modules/queue-microtask": {
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz",
"integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==",
"dev": true,
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
]
},
"node_modules/quick-lru": { "node_modules/quick-lru": {
"version": "6.1.1", "version": "6.1.1",
"resolved": "https://registry.npmjs.org/quick-lru/-/quick-lru-6.1.1.tgz", "resolved": "https://registry.npmjs.org/quick-lru/-/quick-lru-6.1.1.tgz",
@ -7376,6 +7533,16 @@
"lowercase-keys": "^1.0.0" "lowercase-keys": "^1.0.0"
} }
}, },
"node_modules/reusify": {
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz",
"integrity": "sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==",
"dev": true,
"engines": {
"iojs": ">=1.0.0",
"node": ">=0.10.0"
}
},
"node_modules/rimraf": { "node_modules/rimraf": {
"version": "3.0.2", "version": "3.0.2",
"resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz", "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-3.0.2.tgz",
@ -7433,6 +7600,29 @@
"estree-walker": "^0.6.1" "estree-walker": "^0.6.1"
} }
}, },
"node_modules/run-parallel": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz",
"integrity": "sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==",
"dev": true,
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"dependencies": {
"queue-microtask": "^1.2.2"
}
},
"node_modules/rw": { "node_modules/rw": {
"version": "1.3.3", "version": "1.3.3",
"resolved": "https://registry.npmjs.org/rw/-/rw-1.3.3.tgz", "resolved": "https://registry.npmjs.org/rw/-/rw-1.3.3.tgz",
@ -7600,6 +7790,20 @@
"integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==", "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==",
"dev": true "dev": true
}, },
"node_modules/sirv": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/sirv/-/sirv-2.0.2.tgz",
"integrity": "sha512-4Qog6aE29nIjAOKe/wowFTxOdmbEZKb+3tsLljaBRzJwtqto0BChD2zzH0LhgCSXiI+V7X+Y45v14wBZQ1TK3w==",
"dev": true,
"dependencies": {
"@polka/url": "^1.0.0-next.20",
"mrmime": "^1.0.0",
"totalist": "^3.0.0"
},
"engines": {
"node": ">= 10"
}
},
"node_modules/sisteransi": { "node_modules/sisteransi": {
"version": "1.0.5", "version": "1.0.5",
"resolved": "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.5.tgz", "resolved": "https://registry.npmjs.org/sisteransi/-/sisteransi-1.0.5.tgz",
@ -8153,6 +8357,15 @@
"node": ">=8.0" "node": ">=8.0"
} }
}, },
"node_modules/totalist": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/totalist/-/totalist-3.0.0.tgz",
"integrity": "sha512-eM+pCBxXO/njtF7vdFsHuqb+ElbxqtI4r5EAvk6grfAFyJ6IvWlSkfZ5T9ozC6xWw3Fj1fGoSmrl0gUs46JVIw==",
"dev": true,
"engines": {
"node": ">=6"
}
},
"node_modules/tough-cookie": { "node_modules/tough-cookie": {
"version": "4.1.2", "version": "4.1.2",
"resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.1.2.tgz", "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-4.1.2.tgz",

View File

@ -21,6 +21,7 @@
"@types/pouchdb": "^6.4.0", "@types/pouchdb": "^6.4.0",
"@types/proj4": "^2.5.2", "@types/proj4": "^2.5.2",
"@types/shelljs": "^0.8.11", "@types/shelljs": "^0.8.11",
"@vitest/ui": "^0.28.4",
"jsdom": "^21.1.0", "jsdom": "^21.1.0",
"license-checker": "^25.0.1", "license-checker": "^25.0.1",
"license-compatibility-checker": "^0.3.5", "license-compatibility-checker": "^0.3.5",

View File

@ -1,7 +1,8 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import shell from 'shelljs'; import shell from 'shelljs';
import { initDb } from '.'; import PouchDB from 'pouchdb';
import { existsGpx, putNewGpx } from './gpx'; import { existsGpx, putNewGpx } from './gpx';
const test = it; const test = it;
@ -67,7 +68,9 @@ describe('putNewGpx', () => {
describe('The gpx module with a real db', () => { describe('The gpx module with a real db', () => {
beforeEach(async () => { beforeEach(async () => {
await initDb({}); globalThis.db = new PouchDB('dyomedea', {
auto_compaction: false,
});
globalThis.Date.now = () => 0; globalThis.Date.now = () => 0;
}); });
afterEach(async () => { afterEach(async () => {
@ -75,9 +78,10 @@ describe('The gpx module with a real db', () => {
await db.destroy(); await db.destroy();
} catch (err) { } catch (err) {
// console.error(err); // console.error(err);
shell.exec('rm -rf $*$'); await shell.exec('rm -rf $*$');
} }
db = undefined; globalThis.db = undefined;
globalThis.dbReady = false;
globalThis.Date.now = originalDateNow; globalThis.Date.now = originalDateNow;
}); });

View File

@ -1,9 +1,11 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import shell from 'shelljs'; import shell from 'shelljs';
import { initDb } from '.'; import PouchDB from 'pouchdb';
import { emptyRte, getRte, putNewRte, putRte } from './rte'; import { emptyRte, getRte, putNewRte, putRte } from './rte';
import { put } from './lib';
import { emptyWpt } from './wpt';
const test = it; const test = it;
const jest = vi; const jest = vi;
@ -56,7 +58,9 @@ describe('The rte module', () => {
describe('The rte module with a real db', () => { describe('The rte module with a real db', () => {
beforeEach(async () => { beforeEach(async () => {
await initDb({}); globalThis.db = new PouchDB('dyomedea', {
auto_compaction: false,
});
globalThis.Date.now = () => 0; globalThis.Date.now = () => 0;
}); });
afterEach(async () => { afterEach(async () => {
@ -64,9 +68,10 @@ describe('The rte module with a real db', () => {
await db.destroy(); await db.destroy();
} catch (err) { } catch (err) {
// console.error(err); // console.error(err);
shell.exec('rm -rf \$*\$'); await shell.exec('rm -rf $*$');
} }
db = undefined; globalThis.db = undefined;
globalThis.dbReady = false;
globalThis.Date.now = originalDateNow; globalThis.Date.now = originalDateNow;
}); });
it('can write and read a rte', async () => { it('can write and read a rte', async () => {
@ -97,4 +102,38 @@ describe('The rte module with a real db', () => {
const rteGet = await getRte({ id }); const rteGet = await getRte({ id });
expect(rteGet).toEqual(rte); expect(rteGet).toEqual(rte);
}); });
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);
});
}); });

View File

@ -1,4 +1,4 @@
import { debounce, memoize, property, wrap } from 'lodash'; import { cloneDeep, debounce, memoize, property, wrap } from 'lodash';
import { delay } from '../lib/delay'; import { delay } from '../lib/delay';
import getUri from '../lib/ids'; import getUri from '../lib/ids';
import { appendToArray, putNewGpx } from './gpx'; import { appendToArray, putNewGpx } from './gpx';
@ -75,32 +75,24 @@ export const getRteDocs: ({
const { id } = params; const { id } = params;
const docs = await getFamily(id, { include_docs: true }); const docs = await getFamily(id, { include_docs: true });
console.log({ caller: 'getRteDocs', id, docs }); console.log({ caller: 'getRteDocs', id, docs });
let target: any[]; let rte: Rte;
let rte: Rte | undefined = undefined; if (docs.rows.length === 1) {
docs.rows.every((row: any) => { rte = docs.rows[0].doc.doc;
// level 0 } else {
if (row.doc.type === 'rte') { rte = cloneDeep(docs.rows[0].doc.doc);
if (!!rte) { if (!rte.rtept) {
console.error({ rte.rtept = [];
caller: 'getRte', }
id, docs.rows.slice(1).forEach((row: any) => {
row, rte.rtept.push(row.doc.doc);
target,
rte,
}); });
return false; // Hack to stop if getFamily fails
} }
target = [row.doc.doc]; console.log({
rte = row.doc.doc; caller: 'getRteDocs',
} id,
//level 1 docs,
if (row.doc.type === 'rtept') { nbRteptIn: docs.rows[0].doc.rtept?.length,
target.splice(1); nbRteptTotal: rte?.rtept?.length,
// row.doc.doc.id = row.doc._id;
appendToArray(target.at(-1), row.doc.type, row.doc.doc);
target.push(row.doc.doc);
}
return true;
}); });
return { docs, rte }; return { docs, rte };
}; };

View File

@ -1,9 +1,11 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import shell from 'shelljs'; import shell from 'shelljs';
import { initDb } from '.'; import PouchDB from 'pouchdb';
import { getRte, putRte } from './rte'; import { getRte, compactRteOrTrksegDebounced, putRte } from './rte';
import { putRtept } from './rtept'; import { putRtept } from './rtept';
import { get, getFamily, put } from './lib';
import { emptyWpt } from './wpt';
const test = it; const test = it;
const jest = vi; const jest = vi;
@ -18,22 +20,28 @@ const originalDateNow = globalThis.Date.now;
describe('The rtept module with a real db', () => { describe('The rtept module with a real db', () => {
beforeEach(async () => { beforeEach(async () => {
await initDb({}); globalThis.db = new PouchDB('dyomedea', {
auto_compaction: false,
});
globalThis.Date.now = () => 0; globalThis.Date.now = () => 0;
vi.mock('compactRteOrTrksegDebounced', () => {
console.log({ caller: 'mocked compactRteOrTrksegDebounced' });
});
}); });
afterEach(async () => { afterEach(async () => {
try { try {
await db.destroy(); await db.destroy();
} catch (err) { } catch (err) {
// console.error(err); // console.error(err);
shell.exec('rm -rf \$*\$'); await shell.exec('rm -rf $*$');
} }
db = undefined; globalThis.db = undefined;
globalThis.dbReady = false;
globalThis.Date.now = originalDateNow; globalThis.Date.now = originalDateNow;
}); });
it('can write a rtept inside a rte', async () => { it('can write a rtept inside a rte', async () => {
const idRte = 'whatever'; const idRte = 'whatever';
const rtept: Rtept = { const rtept: Wpt = {
$: { lat: 2, lon: 2 }, $: { lat: 2, lon: 2 },
}; };
const rte0: Rte = { const rte0: Rte = {
@ -62,8 +70,65 @@ describe('The rtept module with a real db', () => {
const rtePut = await putRte({ id: idRte, rte: rte0 }); const rtePut = await putRte({ id: idRte, rte: rte0 });
expect(rtePut).toEqual(rte0); expect(rtePut).toEqual(rte0);
const rtePutRtept = await putRtept({ idRte, index: 1, rtept }); const rtePutRtept = await putRtept({ idRte, index: 1, rtept });
expect(rtePutRtept).toEqual(rte1);
const rteGetRtept = await getRte({ id: idRte }); const rteGetRtept = await getRte({ id: idRte });
expect(rteGetRtept).toEqual(rte1); expect(rteGetRtept).toEqual(rte1);
}); });
it('can write a rtept outside a rte', 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 });
expect(rtePut).toEqual(rte0);
await put(idRtept, 'rtept', () => rtept3, emptyWpt);
expect(await getRte({ id: idRte })).toEqual(rte1);
await putRtept({
idRte,
index: 2,
rtept: rtept4,
});
expect((await getFamily(idRte)).rows.length).toEqual(2);
expect(await getRte({ id: idRte })).toEqual(rte2);
expect((await get(idRtept)).doc).toEqual(rtept4);
});
}); });

View File

@ -26,22 +26,26 @@ export const emptyRtept: Wpt = {
}; };
export const putRtept = async (params: any) => { export const putRtept = async (params: any) => {
console.log({ caller: 'putRtept', params });
const { idRte, index, rtept } = params; const { idRte, index, rtept } = params;
const { docs, rte } = await getRteDocs({ id: idRte }); const { docs } = await getRteDocs({ id: idRte });
const rte = docs.rows[0].doc.doc;
const nbRteptInRte = rte && rte.rtept ? rte.rtept.length : 0; const nbRteptInRte = rte && rte.rtept ? rte.rtept.length : 0;
console.log({ caller: 'putRtept', nbRteptInRte });
const nbRteptOutRte = docs.rows.length - 1; const nbRteptOutRte = docs.rows.length - 1;
const nbRtept = nbRteptInRte + nbRteptOutRte; const nbRtept = nbRteptInRte + nbRteptOutRte;
const positiveIndex = index >= 0 ? index : nbRtept - index; const positiveIndex = index >= 0 ? index : nbRtept - index;
// console.log({
// caller: 'putRtept',
// nbRteptInRte,
// nbRteptOutRte,
// nbRtept,
// positiveIndex,
// });
if (positiveIndex < nbRteptInRte) { if (positiveIndex < nbRteptInRte) {
rte.rtept[positiveIndex] = rtept; rte.rtept[positiveIndex] = rtept;
await putRte({ id: idRte, rte }); await putRte({ id: idRte, rte });
} else { } else {
let rteptIdObj = getUri('rtept', docs.rows[1].id); const reptId = docs.rows[1 + positiveIndex - nbRteptInRte].doc._id;
rteptIdObj.rtept = rteptIdObj.rtept + positiveIndex - nbRteptInRte; await put(reptId, 'rtept', () => rtept, emptyRtept);
const rteptUri = getUri('rtept', rteptIdObj);
await put(rteptUri, 'rtept', () => rtept, emptyRtept);
} }
return rte; return rtept;
}; };