feat: revise fileGraph discover

This commit is contained in:
Chen, Tai-Lin committed 2023-08-28 17:53:52 +08:00
1 parent 3c41764f85
commit 02e1219228
2 files changed
+60 -17

No files matched your search

+6 -2
View File
@@ -30,9 +30,13 @@ export default class FileGraph {
// Overwrite the file graph with the provided files
// Remove file nodes that don't exist anymore and add new ones
eventFiles.forEach((file) => {
this.root.merge(file)
const newNode = new FileNode(eventFiles[0])
eventFiles.slice(1).forEach((file) => {
newNode.addChild(file, { makeParents: true })
})
this.root.merge(newNode)
}
handleEvent(event) {
+54 -15
View File
@@ -40,13 +40,23 @@ describe('FileGraph', () => {
const filesToAdd = [
{
path: '/home/user/file1.txt',
path: '/home/user/folder1',
type: 'folder',
discovered: true,
},
{
path: '/home/user/folder2',
type: 'folder',
discovered: true,
},
{
path: '/home/user/folder1/file1.txt',
type: 'file',
discovered: true,
},
{
path: '/home/user/folder1',
type: 'folder',
path: '/home/user/folder2/file2.txt',
type: 'file',
discovered: true,
},
]
@@ -55,7 +65,11 @@ describe('FileGraph', () => {
const filesToRemove = [
{
path: '/home/user/folder1',
path: '/home/user/folder1/file1.txt',
type: 'folder',
},
{
path: '/home/user/folder2',
type: 'folder',
},
]
@@ -64,6 +78,12 @@ describe('FileGraph', () => {
console.log(fileGraph.root.toString())
expect(fileGraph.root.children[0].children[0].children.length).toBe(1)
expect(fileGraph.root.children[0].children[0].children[0].name).toBe(
'folder1'
)
expect(
fileGraph.root.children[0].children[0].children[0].children.length
).toBe(0)
})
test('discover new files', () => {
@@ -71,28 +91,48 @@ describe('FileGraph', () => {
const filesToAdd = [
{
path: '/home/user/file1.txt',
path: '/home/user/folder1',
type: 'folder',
discovered: true,
},
{
path: '/home/user/folder2',
type: 'folder',
discovered: true,
},
{
path: '/home/user/folder1/file1.txt',
type: 'file',
discovered: true,
},
{
path: '/home/user/folder1',
type: 'folder',
path: '/home/user/folder2/file2.txt',
type: 'file',
discovered: true,
},
]
fileGraph.add(filesToAdd)
expect(fileGraph.root.children[0].children[0].children.length).toBe(2)
console.log(fileGraph.root.toString())
const newFiles = [
{
path: '/home/newfile.txt',
path: '/home/user/folder1',
type: 'folder',
discovered: true,
},
{
path: '/home/user/folder1/file1.txt',
type: 'file',
discovered: true,
},
{
path: '/home/user/file1.txt',
path: '/home/user/folder1/file2.txt',
type: 'file',
discovered: true,
},
{
path: '/home/user/folder1/file3.txt',
type: 'file',
discovered: true,
},
@@ -101,10 +141,9 @@ describe('FileGraph', () => {
fileGraph.discover(newFiles)
console.log(fileGraph.root.toString())
expect(fileGraph.root.children[0].children.length).toBe(2)
expect(fileGraph.root.children[0].children[0].name).toBe('newfile.txt')
expect(fileGraph.root.children[0].children[0].children[0].name).toBe(
'file1.txt'
)
expect(fileGraph.root.children[0].children[0].children.length).toBe(2)
expect(
fileGraph.root.children[0].children[0].children[0].children.length
).toBe(3)
})
})