-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDirectoryBrowser.test.ts
More file actions
54 lines (46 loc) · 1.74 KB
/
Copy pathDirectoryBrowser.test.ts
File metadata and controls
54 lines (46 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import fs from 'fs';
import path from 'path';
import os from 'os';
import { DirectoryBrowser } from '../src/services/DirectoryBrowser.js';
describe('DirectoryBrowser', () => {
let tmpDir: string;
let browser: DirectoryBrowser;
beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dirbrowser-test-'));
browser = new DirectoryBrowser();
// Create test structure
fs.mkdirSync(path.join(tmpDir, 'subdir1'));
fs.mkdirSync(path.join(tmpDir, 'subdir2'));
fs.writeFileSync(path.join(tmpDir, 'file1.txt'), 'hello');
fs.writeFileSync(path.join(tmpDir, 'file2.js'), 'world');
fs.mkdirSync(path.join(tmpDir, '.hidden'));
});
afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});
it('lists directory contents', () => {
const entries = browser.listDirectory(tmpDir);
// Should not include hidden directories
const names = entries.map((e) => e.name);
expect(names).toContain('subdir1');
expect(names).toContain('subdir2');
expect(names).toContain('file1.txt');
expect(names).not.toContain('.hidden');
});
it('directories come before files', () => {
const entries = browser.listDirectory(tmpDir);
const firstFile = entries.findIndex((e) => !e.isDirectory);
const lastDir = entries.findLastIndex((e) => e.isDirectory);
if (firstFile !== -1 && lastDir !== -1) {
expect(lastDir).toBeLessThan(firstFile);
}
});
it('throws for nonexistent directory', () => {
expect(() => browser.listDirectory('/nonexistent/path')).toThrow();
});
it('returns parent directory', () => {
const parent = browser.getParent(tmpDir);
expect(parent).toBe(path.dirname(tmpDir));
});
});