|
| 1 | +import { mkdir, mkdtemp, writeFile } from 'node:fs/promises'; |
| 2 | +import os from 'node:os'; |
| 3 | +import path from 'node:path'; |
| 4 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 5 | +import { walkWorkspaceFiles } from './files-ipc'; |
| 6 | + |
| 7 | +describe('walkWorkspaceFiles', () => { |
| 8 | + let workspaceDir: string; |
| 9 | + |
| 10 | + beforeEach(async () => { |
| 11 | + workspaceDir = await mkdtemp(path.join(os.tmpdir(), 'oc-files-ipc-')); |
| 12 | + }); |
| 13 | + |
| 14 | + it('returns empty array for an empty workspace', async () => { |
| 15 | + const files = await walkWorkspaceFiles(workspaceDir); |
| 16 | + expect(files).toEqual([]); |
| 17 | + }); |
| 18 | + |
| 19 | + it('lists html and asset files at the root', async () => { |
| 20 | + await writeFile(path.join(workspaceDir, 'index.html'), '<html></html>'); |
| 21 | + await writeFile(path.join(workspaceDir, 'styles.css'), 'body{}'); |
| 22 | + await writeFile(path.join(workspaceDir, 'app.js'), ''); |
| 23 | + const files = await walkWorkspaceFiles(workspaceDir); |
| 24 | + const paths = files.map((f) => f.path); |
| 25 | + expect(paths).toContain('index.html'); |
| 26 | + expect(paths).toContain('styles.css'); |
| 27 | + expect(paths).toContain('app.js'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('marks .html and .htm as kind=html', async () => { |
| 31 | + await writeFile(path.join(workspaceDir, 'a.html'), ''); |
| 32 | + await writeFile(path.join(workspaceDir, 'b.htm'), ''); |
| 33 | + await writeFile(path.join(workspaceDir, 'c.css'), ''); |
| 34 | + const files = await walkWorkspaceFiles(workspaceDir); |
| 35 | + const byPath = Object.fromEntries(files.map((f) => [f.path, f.kind])); |
| 36 | + expect(byPath['a.html']).toBe('html'); |
| 37 | + expect(byPath['b.htm']).toBe('html'); |
| 38 | + expect(byPath['c.css']).toBe('asset'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('sorts html files first, then assets, both alphabetical', async () => { |
| 42 | + await writeFile(path.join(workspaceDir, 'zzz.html'), ''); |
| 43 | + await writeFile(path.join(workspaceDir, 'aaa.css'), ''); |
| 44 | + await writeFile(path.join(workspaceDir, 'bbb.html'), ''); |
| 45 | + const files = await walkWorkspaceFiles(workspaceDir); |
| 46 | + expect(files.map((f) => f.path)).toEqual(['bbb.html', 'zzz.html', 'aaa.css']); |
| 47 | + }); |
| 48 | + |
| 49 | + it('walks nested directories', async () => { |
| 50 | + await mkdir(path.join(workspaceDir, 'sub')); |
| 51 | + await writeFile(path.join(workspaceDir, 'sub', 'page.html'), ''); |
| 52 | + await writeFile(path.join(workspaceDir, 'sub', 'logo.svg'), ''); |
| 53 | + const files = await walkWorkspaceFiles(workspaceDir); |
| 54 | + const paths = files.map((f) => f.path); |
| 55 | + expect(paths).toContain('sub/page.html'); |
| 56 | + expect(paths).toContain('sub/logo.svg'); |
| 57 | + }); |
| 58 | + |
| 59 | + it('skips standard heavy/build directories', async () => { |
| 60 | + for (const dir of ['node_modules', '.git', 'dist', 'build', '.next']) { |
| 61 | + await mkdir(path.join(workspaceDir, dir)); |
| 62 | + await writeFile(path.join(workspaceDir, dir, 'inside.html'), ''); |
| 63 | + } |
| 64 | + await writeFile(path.join(workspaceDir, 'visible.html'), ''); |
| 65 | + const files = await walkWorkspaceFiles(workspaceDir); |
| 66 | + const paths = files.map((f) => f.path); |
| 67 | + expect(paths).toEqual(['visible.html']); |
| 68 | + }); |
| 69 | + |
| 70 | + it('skips hidden (dotfile) entries', async () => { |
| 71 | + await writeFile(path.join(workspaceDir, '.env'), 'SECRET=1'); |
| 72 | + await writeFile(path.join(workspaceDir, '.hidden.html'), ''); |
| 73 | + await writeFile(path.join(workspaceDir, 'visible.html'), ''); |
| 74 | + const files = await walkWorkspaceFiles(workspaceDir); |
| 75 | + expect(files.map((f) => f.path)).toEqual(['visible.html']); |
| 76 | + }); |
| 77 | + |
| 78 | + it('skips files without an allowed extension', async () => { |
| 79 | + await writeFile(path.join(workspaceDir, 'binary.exe'), ''); |
| 80 | + await writeFile(path.join(workspaceDir, 'Makefile'), ''); |
| 81 | + await writeFile(path.join(workspaceDir, 'page.html'), ''); |
| 82 | + const files = await walkWorkspaceFiles(workspaceDir); |
| 83 | + expect(files.map((f) => f.path)).toEqual(['page.html']); |
| 84 | + }); |
| 85 | + |
| 86 | + it('returns size and ISO mtime per entry', async () => { |
| 87 | + const content = 'hello world'; |
| 88 | + await writeFile(path.join(workspaceDir, 'page.html'), content); |
| 89 | + const files = await walkWorkspaceFiles(workspaceDir); |
| 90 | + expect(files).toHaveLength(1); |
| 91 | + expect(files[0]?.size).toBe(content.length); |
| 92 | + expect(files[0]?.updatedAt).toMatch(/^\d{4}-\d{2}-\d{2}T/); |
| 93 | + }); |
| 94 | + |
| 95 | + it('respects the max-files cap', async () => { |
| 96 | + for (let i = 0; i < 10; i++) { |
| 97 | + await writeFile(path.join(workspaceDir, `file-${i}.html`), ''); |
| 98 | + } |
| 99 | + const files = await walkWorkspaceFiles(workspaceDir, 3); |
| 100 | + expect(files).toHaveLength(3); |
| 101 | + }); |
| 102 | + |
| 103 | + it('handles unreadable directories gracefully', async () => { |
| 104 | + // Walking a non-existent path returns [] rather than throwing -- the |
| 105 | + // workspace folder might have been deleted between the bind and the |
| 106 | + // list call and the panel should just go empty. |
| 107 | + const files = await walkWorkspaceFiles(path.join(workspaceDir, 'does-not-exist')); |
| 108 | + expect(files).toEqual([]); |
| 109 | + }); |
| 110 | +}); |
0 commit comments