diff --git a/packages/web/src/features/git/utils.test.ts b/packages/web/src/features/git/utils.test.ts index e4aa5f8e2..4f86f0615 100644 --- a/packages/web/src/features/git/utils.test.ts +++ b/packages/web/src/features/git/utils.test.ts @@ -103,3 +103,28 @@ test('buildFileTree builds a sorted tree from a flat list', () => { }); }); +test('buildFileTree correctly sets intermediate directory paths', () => { + const flatList: { type: string, path: string }[] = [ + { type: 'blob', path: 'src/components/buttons/PrimaryButton.tsx' }, + { type: 'blob', path: 'src/index.ts' }, + ]; + + const tree = buildFileTree(flatList); + + const srcNode = tree.children.find((c) => c.name === 'src'); + expect(srcNode?.path).toBe('src'); + expect(srcNode?.type).toBe('tree'); + + const compNode = srcNode?.children.find((c) => c.name === 'components'); + expect(compNode?.path).toBe('src/components'); + expect(compNode?.type).toBe('tree'); + + const btnDir = compNode?.children.find((c) => c.name === 'buttons'); + expect(btnDir?.path).toBe('src/components/buttons'); + expect(btnDir?.type).toBe('tree'); + + const btnFile = btnDir?.children.find((c) => c.name === 'PrimaryButton.tsx'); + expect(btnFile?.path).toBe('src/components/buttons/PrimaryButton.tsx'); + expect(btnFile?.type).toBe('blob'); +}); + diff --git a/packages/web/src/features/git/utils.ts b/packages/web/src/features/git/utils.ts index f9b1a71e7..126281012 100644 --- a/packages/web/src/features/git/utils.ts +++ b/packages/web/src/features/git/utils.ts @@ -67,7 +67,7 @@ export const buildFileTree = (flatList: { type: string, path: string }[]): FileT if (!next) { next = { name: part, - path: item.path, + path: parts.slice(0, i + 1).join('/'), type: nodeType, children: [], };