Skip to content

Commit

Permalink
Fix folder squash bug
Browse files Browse the repository at this point in the history
  • Loading branch information
gabotechs committed Jun 22, 2024
1 parent a69c834 commit e8607e5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
34 changes: 32 additions & 2 deletions web/src/Explorer/FolderState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
{
Expand Down Expand Up @@ -86,11 +114,12 @@ function it (
name: string,
input: {
nodes: string[][],
modify: (folderState: FolderState<object>) => void
modify: (folderState: FolderState<object>) => void,
squash?: boolean
},
expected: {
render: string,
events: Message[]
events: Message[],
}
) {
let id = 0
Expand All @@ -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))
Expand Down
10 changes: 9 additions & 1 deletion web/src/FileTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}
3 changes: 3 additions & 0 deletions web/src/FileTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,12 @@ export class FileTree<T = object> {
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
Expand Down

0 comments on commit e8607e5

Please sign in to comment.