|
| 1 | +import { describe, expect, it, mock } from 'bun:test' |
| 2 | + |
| 3 | +import { buildFileTree, computeProjectIndex } from '../impl/file-tree-builder' |
| 4 | + |
| 5 | +/** |
| 6 | + * These tests focus on DOMAIN LOGIC - the tree building algorithm, |
| 7 | + * sorting rules (directories before files, alphabetical), and hierarchy. |
| 8 | + * Low-value tests that just verify JavaScript built-in behavior have been removed. |
| 9 | + */ |
| 10 | + |
| 11 | +describe('buildFileTree', () => { |
| 12 | + describe('tree structure building', () => { |
| 13 | + it('should create nested directory structure from path', () => { |
| 14 | + const result = buildFileTree(['src/components/Button.tsx']) |
| 15 | + |
| 16 | + expect(result[0].name).toBe('src') |
| 17 | + expect(result[0].type).toBe('directory') |
| 18 | + expect(result[0].children![0].name).toBe('components') |
| 19 | + expect(result[0].children![0].children![0].name).toBe('Button.tsx') |
| 20 | + expect(result[0].children![0].children![0].type).toBe('file') |
| 21 | + }) |
| 22 | + |
| 23 | + it('should group multiple files in same directory', () => { |
| 24 | + const result = buildFileTree(['src/a.ts', 'src/b.ts', 'src/c.ts']) |
| 25 | + |
| 26 | + expect(result).toHaveLength(1) // single src directory |
| 27 | + expect(result[0].children).toHaveLength(3) // three files inside |
| 28 | + }) |
| 29 | + |
| 30 | + it('should create separate root directories', () => { |
| 31 | + const result = buildFileTree(['src/file.ts', 'lib/file.ts', 'tests/file.ts']) |
| 32 | + |
| 33 | + expect(result).toHaveLength(3) |
| 34 | + expect(result.map(n => n.name)).toContain('src') |
| 35 | + expect(result.map(n => n.name)).toContain('lib') |
| 36 | + expect(result.map(n => n.name)).toContain('tests') |
| 37 | + }) |
| 38 | + |
| 39 | + it('should handle mixed root files and directories', () => { |
| 40 | + const result = buildFileTree(['root.ts', 'src/nested.ts']) |
| 41 | + |
| 42 | + const rootFile = result.find(n => n.name === 'root.ts') |
| 43 | + const srcDir = result.find(n => n.name === 'src') |
| 44 | + |
| 45 | + expect(rootFile?.type).toBe('file') |
| 46 | + expect(srcDir?.type).toBe('directory') |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + describe('sorting: directories before files, then alphabetical', () => { |
| 51 | + it('should sort directories BEFORE files at same level', () => { |
| 52 | + const result = buildFileTree(['file.ts', 'src/file.ts', 'another.ts']) |
| 53 | + |
| 54 | + // Directory should come first |
| 55 | + expect(result[0].name).toBe('src') |
| 56 | + expect(result[0].type).toBe('directory') |
| 57 | + // Then files |
| 58 | + expect(result[1].type).toBe('file') |
| 59 | + expect(result[2].type).toBe('file') |
| 60 | + }) |
| 61 | + |
| 62 | + it('should sort alphabetically within same type', () => { |
| 63 | + const result = buildFileTree(['z.ts', 'a.ts', 'm.ts']) |
| 64 | + |
| 65 | + expect(result[0].name).toBe('a.ts') |
| 66 | + expect(result[1].name).toBe('m.ts') |
| 67 | + expect(result[2].name).toBe('z.ts') |
| 68 | + }) |
| 69 | + |
| 70 | + it('should sort children within directories', () => { |
| 71 | + const result = buildFileTree(['src/z.ts', 'src/a.ts', 'src/utils/helper.ts']) |
| 72 | + |
| 73 | + // utils directory should come before files |
| 74 | + expect(result[0].children![0].name).toBe('utils') |
| 75 | + expect(result[0].children![0].type).toBe('directory') |
| 76 | + // then files alphabetically |
| 77 | + expect(result[0].children![1].name).toBe('a.ts') |
| 78 | + expect(result[0].children![2].name).toBe('z.ts') |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + describe('filePath property', () => { |
| 83 | + it('should set correct filePath for nested items', () => { |
| 84 | + const result = buildFileTree(['src/components/Button.tsx']) |
| 85 | + |
| 86 | + expect(result[0].filePath).toBe('src') |
| 87 | + expect(result[0].children![0].filePath).toBe('src/components') |
| 88 | + expect(result[0].children![0].children![0].filePath).toBe('src/components/Button.tsx') |
| 89 | + }) |
| 90 | + }) |
| 91 | + |
| 92 | + describe('complex project structure', () => { |
| 93 | + it('should handle typical project layout', () => { |
| 94 | + const files = [ |
| 95 | + 'package.json', |
| 96 | + 'tsconfig.json', |
| 97 | + 'src/index.ts', |
| 98 | + 'src/components/Button.tsx', |
| 99 | + 'src/utils/helpers.ts', |
| 100 | + 'tests/unit/button.test.ts', |
| 101 | + ] |
| 102 | + const result = buildFileTree(files) |
| 103 | + |
| 104 | + // Directories first (src, tests), then files (package.json, tsconfig.json) |
| 105 | + expect(result[0].type).toBe('directory') |
| 106 | + expect(result[1].type).toBe('directory') |
| 107 | + expect(result.map(n => n.name)).toContain('src') |
| 108 | + expect(result.map(n => n.name)).toContain('tests') |
| 109 | + expect(result.map(n => n.name)).toContain('package.json') |
| 110 | + }) |
| 111 | + }) |
| 112 | +}) |
| 113 | + |
| 114 | +describe('computeProjectIndex', () => { |
| 115 | + it('should return file tree for project files', async () => { |
| 116 | + const projectFiles = { |
| 117 | + 'src/index.ts': 'export const hello = "world"', |
| 118 | + 'src/utils.ts': 'export const add = (a, b) => a + b', |
| 119 | + } |
| 120 | + |
| 121 | + const result = await computeProjectIndex('/mock/cwd', projectFiles) |
| 122 | + |
| 123 | + expect(result.fileTree).toHaveLength(1) |
| 124 | + expect(result.fileTree[0].name).toBe('src') |
| 125 | + expect(result.fileTree[0].children).toHaveLength(2) |
| 126 | + }) |
| 127 | + |
| 128 | + it('should sort file paths before building tree', async () => { |
| 129 | + const projectFiles = { |
| 130 | + 'z.ts': 'const z = 1', |
| 131 | + 'a.ts': 'const a = 1', |
| 132 | + 'm.ts': 'const m = 1', |
| 133 | + } |
| 134 | + |
| 135 | + const result = await computeProjectIndex('/mock/cwd', projectFiles) |
| 136 | + |
| 137 | + expect(result.fileTree[0].name).toBe('a.ts') |
| 138 | + expect(result.fileTree[1].name).toBe('m.ts') |
| 139 | + expect(result.fileTree[2].name).toBe('z.ts') |
| 140 | + }) |
| 141 | + |
| 142 | + it('should return correct structure shape', async () => { |
| 143 | + const result = await computeProjectIndex('/mock/cwd', { 'file.ts': 'export const x = 1' }) |
| 144 | + |
| 145 | + expect(result).toHaveProperty('fileTree') |
| 146 | + expect(result).toHaveProperty('fileTokenScores') |
| 147 | + expect(result).toHaveProperty('tokenCallers') |
| 148 | + expect(Array.isArray(result.fileTree)).toBe(true) |
| 149 | + }) |
| 150 | +}) |
| 151 | + |
| 152 | +/** |
| 153 | + * Mutation tests - verify our tests would catch real bugs |
| 154 | + */ |
| 155 | +describe('mutation detection', () => { |
| 156 | + it('REQUIRES directories to sort before files', () => { |
| 157 | + // If sorting was removed/broken, this would fail |
| 158 | + const result = buildFileTree(['z-file.ts', 'a-dir/file.ts']) |
| 159 | + expect(result[0].name).toBe('a-dir') // directory first, even though z < a alphabetically for files |
| 160 | + expect(result[0].type).toBe('directory') |
| 161 | + }) |
| 162 | + |
| 163 | + it('REQUIRES alphabetical sorting within type', () => { |
| 164 | + // If localeCompare was removed, this would fail |
| 165 | + const result = buildFileTree(['z.ts', 'a.ts']) |
| 166 | + expect(result[0].name).toBe('a.ts') |
| 167 | + }) |
| 168 | + |
| 169 | + it('REQUIRES recursive sorting of children', () => { |
| 170 | + // If sortNodes wasn't called recursively, this would fail |
| 171 | + const result = buildFileTree(['parent/z.ts', 'parent/a.ts']) |
| 172 | + expect(result[0].children![0].name).toBe('a.ts') |
| 173 | + }) |
| 174 | +}) |
0 commit comments