Files
Mind-o-Mat/tests/vault-path.test.ts
Jannik c717353037
CI / test (push) Canceled after 0s
CI / webapp (push) Canceled after 0s
fix: beheben von Lint- und Prettier-Fehlern in Tests und Config
2026-09-08 16:54:21 +02:00

119 lines
3.2 KiB
TypeScript

// Unit-Tests für 1.2 Vault-Pfad-Discovery
// Phase 1.2 Test
import { describe, it, expect } from 'vitest';
import { resolveVaultPath, assertVaultExists, VaultPathError } from '../src/vault-path.js';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
describe('resolveVaultPath', () => {
const cwd = process.cwd();
it('liest --vault <pfad> aus CLI-Args', () => {
const result = resolveVaultPath({
args: ['--vault', 'C:\\foo'],
env: {},
cwd,
});
expect(result.path).toBe('C:\\foo');
expect(result.source).toBe('cli');
});
it('liest -v <pfad> Kurzform', () => {
const result = resolveVaultPath({
args: ['-v', 'C:\\bar'],
env: {},
cwd,
});
expect(result.path).toBe('C:\\bar');
expect(result.source).toBe('cli');
});
it('liest --vault=<pfad> Gleichheits-Form', () => {
const result = resolveVaultPath({
args: ['--vault=C:\\baz'],
env: {},
cwd,
});
expect(result.path).toBe('C:\\baz');
expect(result.source).toBe('cli');
});
it('faellt zurück auf MINDOMAT_VAULT_PATH, wenn keine CLI-Arg', () => {
const result = resolveVaultPath({
args: [],
env: { MINDOMAT_VAULT_PATH: 'C:\\env-vault' },
cwd,
});
expect(result.path).toBe('C:\\env-vault');
expect(result.source).toBe('env');
});
it('CLI-Arg ueberschreibt Env-Var', () => {
const result = resolveVaultPath({
args: ['--vault', 'C:\\cli-wins'],
env: { MINDOMAT_VAULT_PATH: 'C:\\env-loses' },
cwd,
});
expect(result.path).toBe('C:\\cli-wins');
expect(result.source).toBe('cli');
});
it('wirft VaultPathError MISSING, wenn weder CLI noch Env', () => {
expect(() =>
resolveVaultPath({
args: [],
env: {},
cwd,
}),
).toThrow(VaultPathError);
try {
resolveVaultPath({ args: [], env: {}, cwd });
} catch (err) {
expect(err).toBeInstanceOf(VaultPathError);
expect((err as VaultPathError).code).toBe('MISSING');
}
});
it('wirft VaultPathError MISSING, wenn Env-Var leer', () => {
expect(() =>
resolveVaultPath({
args: [],
env: { MINDOMAT_VAULT_PATH: ' ' },
cwd,
}),
).toThrow(VaultPathError);
});
it('loest relative Pfade gegen cwd auf', () => {
const result = resolveVaultPath({
args: ['--vault', 'relativ'],
env: {},
cwd: 'C:\\base',
});
// Windows-spezifisch: join mit Backslashes
expect(result.path).toMatch(/relativ/);
expect(result.path).toMatch(/C:\\base/);
});
});
describe('assertVaultExists', () => {
let tmpDir: string;
it('laesst existierende Verzeichnisse durch', () => {
tmpDir = mkdtempSync(join(tmpdir(), 'mindomat-test-'));
expect(() => assertVaultExists(tmpDir)).not.toThrow();
rmSync(tmpDir, { recursive: true, force: true });
});
it('wirft NOT_FOUND fuer nicht existierende Pfade', () => {
try {
assertVaultExists('C:\\gibt-es-nicht\\garantiert\\nicht');
} catch (err) {
expect(err).toBeInstanceOf(VaultPathError);
expect((err as VaultPathError).code).toBe('NOT_FOUND');
}
});
});