mirror of
https://github.com/amitwh/markdown-converter.git
synced 2026-08-03 02:11:07 +05:30
The list-directory handler returned { path, entries } while the
renderer typed result.data as FileEntry[]; the file-store had to fall
back to raw.entries via Array.isArray. Extract the entries builder into
src/main/files/list-directory.js so it can be unit-tested, return a
plain array, and skip entries that can't be stat'd (broken symlinks,
permission errors) instead of throwing.
Also tighten jest config so dist/*.snap electron-builder artifacts are
not matched as test suites.
64 lines
2.2 KiB
JavaScript
64 lines
2.2 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const os = require('os');
|
|
|
|
const { listDirectoryEntries } = require('../../../../src/main/files/list-directory');
|
|
|
|
describe('listDirectoryEntries', () => {
|
|
let tmpDir;
|
|
|
|
beforeEach(() => {
|
|
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'list-dir-'));
|
|
});
|
|
afterEach(() => {
|
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
});
|
|
|
|
test('returns a flat array (not wrapped in { entries })', () => {
|
|
fs.writeFileSync(path.join(tmpDir, 'a.md'), 'hello');
|
|
const result = listDirectoryEntries(tmpDir);
|
|
expect(Array.isArray(result)).toBe(true);
|
|
expect(result.length).toBe(1);
|
|
expect(result[0]).toMatchObject({
|
|
name: 'a.md',
|
|
isDirectory: false,
|
|
size: 5,
|
|
path: path.join(tmpDir, 'a.md'),
|
|
});
|
|
});
|
|
|
|
test('excludes dotfiles', () => {
|
|
fs.writeFileSync(path.join(tmpDir, '.hidden'), 'x');
|
|
fs.writeFileSync(path.join(tmpDir, 'visible.md'), 'y');
|
|
const result = listDirectoryEntries(tmpDir);
|
|
expect(result.map((e) => e.name)).toEqual(['visible.md']);
|
|
});
|
|
|
|
test('sorts directories first then alphabetically', () => {
|
|
fs.writeFileSync(path.join(tmpDir, 'zebra.md'), 'z');
|
|
fs.mkdirSync(path.join(tmpDir, 'aaa'));
|
|
fs.mkdirSync(path.join(tmpDir, 'bbb'));
|
|
fs.writeFileSync(path.join(tmpDir, 'apple.md'), 'a');
|
|
const result = listDirectoryEntries(tmpDir);
|
|
expect(result.map((e) => e.name)).toEqual(['aaa', 'bbb', 'apple.md', 'zebra.md']);
|
|
});
|
|
|
|
test('marks directories with isDirectory: true and size: 0', () => {
|
|
fs.mkdirSync(path.join(tmpDir, 'sub'));
|
|
const result = listDirectoryEntries(tmpDir);
|
|
expect(result[0]).toMatchObject({ name: 'sub', isDirectory: true, size: 0 });
|
|
});
|
|
|
|
test('skips entries that cannot be stat\'d (broken symlinks)', () => {
|
|
fs.writeFileSync(path.join(tmpDir, 'good.md'), 'ok');
|
|
try {
|
|
fs.symlinkSync(path.join(tmpDir, 'nonexistent'), path.join(tmpDir, 'broken-link'));
|
|
} catch (_) {
|
|
// symlinks not supported in env — skip
|
|
}
|
|
const result = listDirectoryEntries(tmpDir);
|
|
expect(result.find((e) => e.name === 'good.md')).toBeTruthy();
|
|
expect(result.find((e) => e.name === 'broken-link')).toBeUndefined();
|
|
});
|
|
});
|