76 lines
2.8 KiB
JavaScript
76 lines
2.8 KiB
JavaScript
// Tests unitaires — gestion desktop des vaults & dossiers (#159)
|
|
// Helpers purs de frontend/js/desktop.js (module sans import, importable en Node).
|
|
//
|
|
// Usage: node tests/frontend/desktop-roots.test.mjs
|
|
import { strict as assert } from 'assert';
|
|
import {
|
|
resolveRootKind,
|
|
lastPathSegment,
|
|
removeRoot,
|
|
pickAndAddDir,
|
|
} from '../../frontend/js/desktop.js';
|
|
|
|
const vaults = [
|
|
{ name: 'Obsidian', path: 'C:/Users/x/voute_obsidian' },
|
|
{ name: 'Work', path: 'D:/work' },
|
|
];
|
|
const dirs = [{ name: 'Bruno-Download', path: 'C:/Users/x/Downloads' }];
|
|
|
|
function testResolveRootKind() {
|
|
assert.equal(resolveRootKind(vaults, dirs, 'Obsidian'), 'vault');
|
|
assert.equal(resolveRootKind(vaults, dirs, 'Bruno-Download'), 'dir');
|
|
assert.equal(resolveRootKind(vaults, dirs, 'inconnu'), null);
|
|
// Listes absentes (invoke en échec → undefined) : pas de crash.
|
|
assert.equal(resolveRootKind(undefined, undefined, 'Obsidian'), null);
|
|
// Un nom ne peut être que vault OU dir (les deux listes sont disjointes).
|
|
assert.equal(resolveRootKind(vaults, dirs, 'Work'), 'vault');
|
|
console.log(' ✓ resolveRootKind');
|
|
}
|
|
|
|
function testLastPathSegment() {
|
|
assert.equal(lastPathSegment('C:\\Obsidian_doc\\Obsidian-Sandbox'), 'Obsidian-Sandbox');
|
|
assert.equal(lastPathSegment('C:/vaults/perso/'), 'perso');
|
|
assert.equal(lastPathSegment('/home/bruno/vault'), 'vault');
|
|
assert.equal(lastPathSegment('/'), '');
|
|
assert.equal(lastPathSegment('C:\\'), 'C:');
|
|
console.log(' ✓ lastPathSegment');
|
|
}
|
|
|
|
async function testDesktopGating() {
|
|
// Hors desktop (pas de window.__TAURI__) : tout est un no-op silencieux —
|
|
// c'est ce qui protège l'app web (Docker) où la section est masquée.
|
|
assert.equal(await removeRoot('Obsidian', 'confirmer ?'), undefined);
|
|
assert.equal(await pickAndAddDir(), undefined);
|
|
assert.equal(await removeRoot('', 'x'), undefined);
|
|
console.log(' ✓ gating hors desktop');
|
|
}
|
|
|
|
// Le nom dérivé par pickAndAddDir/pickAndAddVault doit être un segment simple
|
|
// (jamais un chemin complet) : réutilisé telle quelle par add_dir/add_vault.
|
|
function testDerivedNameIsBasename() {
|
|
for (const p of ['C:\\Obsidian_doc\\Obsidian-Sandbox', '/home/bruno/Downloads/']) {
|
|
const name = lastPathSegment(p) || 'Dossier';
|
|
assert.ok(!name.includes('/') && !name.includes('\\'), `basename expected for ${p}`);
|
|
}
|
|
console.log(' ✓ derived name is a basename');
|
|
}
|
|
|
|
const tests = [
|
|
testResolveRootKind,
|
|
testLastPathSegment,
|
|
testDesktopGating,
|
|
testDerivedNameIsBasename,
|
|
];
|
|
|
|
let failed = 0;
|
|
for (const fn of tests) {
|
|
try {
|
|
await fn();
|
|
} catch (e) {
|
|
failed++;
|
|
console.error(` ✗ ${fn.name}: ${e.message}`);
|
|
}
|
|
}
|
|
console.log(failed === 0 ? `${tests.length} passed, 0 failed` : `${tests.length - failed} passed, ${failed} failed`);
|
|
process.exit(failed === 0 ? 0 : 1);
|