From e8607e582d5b7eb904261950fae9ed5cf6d219a0 Mon Sep 17 00:00:00 2001 From: Gabriel Musat Date: Sat, 22 Jun 2024 08:07:41 +0200 Subject: [PATCH] Fix folder squash bug --- web/src/Explorer/FolderState.test.ts | 34 ++++++++++++++++++++++++++-- web/src/FileTree.test.ts | 10 +++++++- web/src/FileTree.ts | 3 +++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/web/src/Explorer/FolderState.test.ts b/web/src/Explorer/FolderState.test.ts index 8168864..016d8c7 100644 --- a/web/src/Explorer/FolderState.test.ts +++ b/web/src/Explorer/FolderState.test.ts @@ -28,6 +28,34 @@ describe('FolderState', () => { } ); + it( + 'should work with squashed folders', + { + nodes: [ + ['foo', 'bar', 'a.ts'], + ['foo', 'bar', 'b.ts'], + ['bar', 'c.ts'], + ['d.ts'] + ], + squash: true, + modify: folderState => { + folderState.tagAllFolders('expanded', 'true') + } + }, + { + render: `\ +> bar {"expanded":"true"} + c.ts undefined +> foo/bar {"expanded":"true"} + a.ts undefined + b.ts undefined +d.ts undefined`, + events: [ + ['tagged', 'expanded', 'true'] + ] + } + ) + it( 'Multiple operations', { @@ -86,11 +114,12 @@ function it ( name: string, input: { nodes: string[][], - modify: (folderState: FolderState) => void + modify: (folderState: FolderState) => void, + squash?: boolean }, expected: { render: string, - events: Message[] + events: Message[], } ) { let id = 0 @@ -104,6 +133,7 @@ function it ( for (const node of input.nodes) { fileTree.pushNode(newNode(node)) } + if (input.squash) fileTree.squash() const evs: Message[] = [] const folderState = FolderState.fromFileTree(fileTree) folderState.registerListener('test', (m) => evs.push(m)) diff --git a/web/src/FileTree.test.ts b/web/src/FileTree.test.ts index 0101be1..5c561e3 100644 --- a/web/src/FileTree.test.ts +++ b/web/src/FileTree.test.ts @@ -121,8 +121,9 @@ function it ( } if (input.squash) fileTree.squash() - // ensure children parent consistency + // ensure tree integrity ensureChildrenParents(fileTree) + ensureFolderNamesMatchKeys(fileTree) // expect the serialization to be equal expect(fileTree.toString()).to.equal(expected.render) @@ -155,3 +156,10 @@ function ensureChildrenParents (tree: FileTree): void { if (child.__parent !== tree) throw new Error(`${name} is a child of ${tree.name}, but ${name}'s parent is ${child.__parent?.name}`) } } + +function ensureFolderNamesMatchKeys (tree: FileTree): void { + for (const [name, child] of tree.subTrees.entries()) { + if (child.name !== name) throw new Error(`sub-tree ${tree.name} has a child named ${child.name} under the key ${name}. They key should match the child's name`) + ensureFolderNamesMatchKeys(child) + } +} diff --git a/web/src/FileTree.ts b/web/src/FileTree.ts index 68bad2c..3166210 100644 --- a/web/src/FileTree.ts +++ b/web/src/FileTree.ts @@ -90,9 +90,12 @@ export class FileTree { squash (): void { if (this.subTrees.size === 1 && this.leafs.size === 0) { const child = [...this.subTrees.values()][0] + const oldName = this.name this.name += '/' + child.name this.subTrees = child.subTrees this.leafs = child.leafs + this.__parent?.subTrees.delete(oldName) + this.__parent?.subTrees.set(this.name, this) this.squash() for (const tree of this.subTrees.values()) { tree.__parent = this