import { test } from 'node:test';
import assert from 'node:assert/strict';

const { safeRel, normAuthors, hasAccess, urlFor } = await import('../src/files.js');

test('safeRel: gültiger relativer .md-Pfad bleibt erhalten', () => {
  assert.equal(safeRel('library/software/stack.md'), 'library/software/stack.md');
  assert.equal(safeRel('a/./b.md'), 'a/b.md');
});

test('safeRel: Path-Traversal wird abgelehnt', () => {
  assert.throws(() => safeRel('../etc/passwd.md'));
  assert.throws(() => safeRel('a/../../b.md'));
  assert.throws(() => safeRel('/absolut.md'));
});

test('safeRel: nur .md erlaubt, leer/falsch wirft', () => {
  assert.throws(() => safeRel('note.txt'));
  assert.throws(() => safeRel(''));
  assert.throws(() => safeRel(null));
});

test('normAuthors: String/Array/Leer normalisieren', () => {
  assert.deepEqual(normAuthors('a@x.ch'), ['a@x.ch']);
  assert.deepEqual(normAuthors(['a@x.ch', 'b@y.ch']), ['a@x.ch', 'b@y.ch']);
  assert.deepEqual(normAuthors(null), []);
  assert.deepEqual(normAuthors([]), []);
});

test('hasAccess: case-insensitive Mitgliedschaft', () => {
  assert.equal(hasAccess(['Karim@x.ch'], 'karim@x.ch'), true);
  assert.equal(hasAccess(['a@x.ch'], 'b@y.ch'), false);
  assert.equal(hasAccess([], 'a@x.ch'), false);
});

test('urlFor: Hugo-URLs aus relativem Pfad', () => {
  assert.equal(urlFor('_index.md'), '/');
  assert.equal(urlFor('manifest.md'), '/manifest/');
  assert.equal(urlFor('library/software/stack.md'), '/library/software/stack/');
  assert.equal(urlFor('software/_index.md'), '/software/');
});
