'use strict'; /** * Unit tests for server/config.js * Tests path de-duplication and child-path suppression. */ const path = require('path'); const { getEffectivePaths, getAllowedRoot } = require('../server/config'); // Normalise separator so tests pass on both Windows and Linux const sep = path.sep; const join = (...parts) => parts.join(sep); describe('getEffectivePaths', () => { test('returns empty array for empty input', () => { expect(getEffectivePaths([])).toEqual([]); }); test('returns empty array for non-array input', () => { expect(getEffectivePaths(null)).toEqual([]); expect(getEffectivePaths(undefined)).toEqual([]); }); test('de-duplicates identical paths', () => { const dir = path.resolve('/mnt/data'); const result = getEffectivePaths(['/mnt/data', '/mnt/data']); expect(result).toHaveLength(1); expect(result[0]).toBe(dir); }); test('drops child paths when parent is also configured', () => { const parent = '/mnt/data'; const child = '/mnt/data/photos'; const result = getEffectivePaths([parent, child]); // child should be dropped expect(result).toHaveLength(1); expect(result[0]).toBe(path.resolve(parent)); }); test('keeps sibling paths that share a common prefix but are not children', () => { const a = '/mnt/data'; const b = '/mnt/documents'; const result = getEffectivePaths([a, b]); expect(result).toHaveLength(2); }); test('handles deeply nested child suppression', () => { const root = '/mnt/storage'; const deep = '/mnt/storage/a/b/c/d'; const result = getEffectivePaths([root, deep]); expect(result).toHaveLength(1); expect(result[0]).toBe(path.resolve(root)); }); test('keeps multiple independent roots', () => { const paths = ['/mnt/ssd', '/home/user/shared', '/tmp/uploads']; const result = getEffectivePaths(paths); expect(result).toHaveLength(3); }); }); describe('getAllowedRoot', () => { const roots = ['/mnt/data', '/mnt/docs']; test('returns the root when file is directly inside it', () => { const file = path.join('/mnt/data', 'file.txt'); const result = getAllowedRoot(file, roots); expect(result).toBe(path.resolve('/mnt/data')); }); test('returns null when file is not inside any root', () => { const result = getAllowedRoot('/etc/passwd', roots); expect(result).toBeNull(); }); test('path traversal attempt returns null', () => { const traversal = path.join('/mnt/data', '..', '..', 'etc', 'passwd'); const result = getAllowedRoot(traversal, roots); expect(result).toBeNull(); }); test('returns root when path equals root exactly', () => { const result = getAllowedRoot('/mnt/docs', roots); expect(result).toBe(path.resolve('/mnt/docs')); }); test('returns correct root for nested file', () => { const file = path.join('/mnt/docs', 'sub', 'report.pdf'); const result = getAllowedRoot(file, roots); expect(result).toBe(path.resolve('/mnt/docs')); }); });