moveNode tests written, 1 fails, need to check for bugs

This commit is contained in:
2026-07-21 04:26:19 +02:00
parent 46f9e05d63
commit 7cf60208e0
2 changed files with 194 additions and 28 deletions
+144 -4
View File
@@ -1,6 +1,12 @@
import { createImmutableBuffer, type ImmutableBuffer } from "@core/util/immutable_buffer"; import { createImmutableBuffer, type ImmutableBuffer } from "@core/util/immutable_buffer";
import { dnTraverseTree } from "@core/util/tree_traversal"; import { dnTraverseTree, upTraverseTree } from "@core/util/tree_traversal";
import type { AlreadyExistsError, IllegalMutationError, NodeNotFoundError, NotADirectoryError } from "./errors"; import type {
AlreadyExistsError,
DirectoryLoopError,
IllegalMutationError,
NodeNotFoundError,
NotADirectoryError,
} from "./errors";
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "./treenode"; import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "./treenode";
export class FileSystem { export class FileSystem {
@@ -88,9 +94,143 @@ export class FileSystem {
return node.type === TreeNodeType.Symlink ? true : false; return node.type === TreeNodeType.Symlink ? true : false;
} */ } */
/* public moveNode(sourceInode: number, destinationInode: number, name?: string): FileSystem { public moveNode(sourceInode: number, destinationInode: number, name?: string): FileSystem {
if (sourceInode === 0) {
throw {
type: "IllegalMutation",
message: `Attempt at illegal mutation of node under inode 0 (root)`,
timestamp: Date.now(),
} satisfies IllegalMutationError;
}
} */ const sourceNode = this.resolveNode(sourceInode);
const destinationNode = this.resolveNode(destinationInode);
if (sourceNode === undefined) {
throw {
type: "NodeNotFound",
message: `Source node under inode ${sourceInode} not found`,
timestamp: Date.now(),
inode: sourceInode,
} satisfies NodeNotFoundError;
}
const nodeName = name ?? sourceNode.name;
if (destinationNode === undefined) {
throw {
type: "NodeNotFound",
message: `Destination node under inode ${destinationInode} not found`,
timestamp: Date.now(),
inode: destinationInode,
} satisfies NodeNotFoundError;
}
if (destinationNode.type !== TreeNodeType.Directory) {
throw {
type: "NotADirectory",
message: `Destination node under inode ${destinationInode} is not a directory`,
inode: destinationInode,
timestamp: Date.now(),
nodeType: destinationNode.type,
} satisfies NotADirectoryError;
}
if (destinationNode.children[nodeName] !== undefined) {
throw {
type: "AlreadyExists",
message: `A node inside the destination inode ${destinationNode} under the name ${nodeName} already exists`,
timestamp: Date.now(),
parentInode: destinationInode,
name: nodeName,
} satisfies AlreadyExistsError;
}
if (upTraverseTree(this, destinationInode).includes(sourceInode)) {
throw {
type: "DirectoryLoop",
message: "Cannot move node to a descendant of itself",
timestamp: Date.now(),
descendantInode: destinationInode,
ancestorInode: sourceInode,
} satisfies DirectoryLoopError;
}
let newNode: TreeNode;
if (sourceNode.type === TreeNodeType.File) {
const fileNode: FileNode = {
type: sourceNode.type,
inode: sourceNode.inode,
name: nodeName,
parent: destinationInode,
mode: sourceNode.mode,
owner: sourceNode.owner,
group: sourceNode.group,
mtime: sourceNode.mtime,
atime: sourceNode.atime,
ctime: Date.now(),
content: sourceNode.content,
};
newNode = fileNode;
} else {
const directoryNode: DirectoryNode = {
type: sourceNode.type,
inode: sourceNode.inode,
name: nodeName,
parent: destinationInode,
mode: sourceNode.mode,
owner: sourceNode.owner,
group: sourceNode.group,
mtime: sourceNode.mtime,
atime: sourceNode.atime,
ctime: Date.now(),
children: sourceNode.children,
};
newNode = directoryNode;
}
const parentNode = this.resolveNode(sourceNode.parent) as DirectoryNode;
const { [sourceNode.name]: __, ...newChildren } = parentNode.children;
const newParentNode: DirectoryNode = {
type: parentNode.type,
inode: parentNode.inode,
name: parentNode.name,
parent: parentNode.parent,
mode: parentNode.mode,
owner: parentNode.owner,
group: parentNode.group,
mtime: Date.now(),
atime: parentNode.atime,
ctime: Date.now(),
children: newChildren,
};
const newDestinationNode: DirectoryNode = {
type: destinationNode.type,
inode: destinationInode,
name: destinationNode.name,
parent: destinationNode.parent,
mode: destinationNode.mode,
owner: destinationNode.owner,
group: destinationNode.group,
mtime: Date.now(),
atime: destinationNode.atime,
ctime: Date.now(),
children: { ...destinationNode.children, [nodeName]: sourceInode },
};
const map = new Map(this.Overlay);
map.set(sourceInode, Object.freeze(newNode));
map.set(destinationInode, Object.freeze(newDestinationNode));
map.set(sourceNode.parent, Object.freeze(newParentNode));
const newOverlay: ReadonlyMap<number, TreeNode | null> = map;
return FileSystem.init(this._BaseFS, newOverlay, this._INodeIndex);
}
public modifyNode( public modifyNode(
inode: number, inode: number,
+50 -24
View File
@@ -1,9 +1,8 @@
import { FileSystem } from "@core/filesystem/filesystem"; import { FileSystem } from "@core/filesystem/filesystem";
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/treenode"; import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/treenode";
import { createImmutableBuffer } from "@core/util/immutable_buffer"; import { createImmutableBuffer } from "@core/util/immutable_buffer";
import { dnTraverseTree } from "@core/util/tree_traversal";
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture"; import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
import { describe, expect, it, test } from "vitest"; import { describe, expect, it } from "vitest";
const testTree: FixtureNode = { const testTree: FixtureNode = {
inode: 0, inode: 0,
@@ -29,6 +28,12 @@ const testTree: FixtureNode = {
content: "Goodbye!", content: "Goodbye!",
children: [], children: [],
}, },
{
inode: 4,
name: "bye",
type: "Dir",
children: [],
},
], ],
}, },
], ],
@@ -36,28 +41,6 @@ const testTree: FixtureNode = {
const fs = buildTestFS(testTree); const fs = buildTestFS(testTree);
test("walks down the tree to check if every node has the parent set correctly", () => {
const result = dnTraverseTree(fs, 0);
const out: boolean[] = [];
let parentInode = 0;
for (const inode of result) {
const node = fs.getNode(inode) as TreeNode;
if (node.parent !== parentInode) {
console.log(node.inode);
out.push(false);
}
parentInode = node.parent;
}
console.log(out, result);
expect(out.includes(false)).toBe(false);
});
describe("FileSystem.init", () => { describe("FileSystem.init", () => {
const map: Map<number, TreeNode> = new Map(); const map: Map<number, TreeNode> = new Map();
const time = Date.now(); const time = Date.now();
@@ -314,3 +297,46 @@ describe("modifyNode", () => {
expect(() => fs.modifyNode(999, changes)).toThrow(); expect(() => fs.modifyNode(999, changes)).toThrow();
}); });
}); });
describe("moveNode", () => {
it("throws because of invalid destination node", () => {
expect(() => fs.moveNode(1, 999)).toThrow();
});
it("throws because of invalid source node", () => {
expect(() => fs.moveNode(999, 2)).toThrow();
});
it("throws because of invalid destination type", () => {
expect(() => fs.moveNode(3, 1)).toThrow();
});
it("throws because of an already exising child under destination with the same name", () => {
expect(() => fs.moveNode(1, 2, "world.txt")).toThrow();
});
it("throws because it tried to move a node to a descendant of itself", () => {
expect(() => fs.moveNode(2, 4)).toThrow();
});
it("throws because it tried to move root", () => {
expect(() => fs.moveNode(0, 1)).toThrow();
});
it("successfully moves a node to another directory", () => {
const newFS = fs.moveNode(1, 2);
const nodeSour = newFS.getNode(1) as FileNode;
const nodeDest = newFS.getNode(2) as DirectoryNode;
expect(nodeSour.parent).toBe(2);
expect(nodeDest.children[nodeSour.name]).not.toBeUndefined();
expect((newFS.getNode(0) as DirectoryNode).children[nodeSour.name]).toBeUndefined();
});
it("successfully renames a node", () => {
const newFS = fs.moveNode(3, 2, "foo.foo");
const nodeSour = newFS.getNode(1) as FileNode;
const nodeDest = newFS.getNode(2) as DirectoryNode;
expect(nodeSour.parent).toBe(2);
expect(nodeSour.name).toBe("foo.foo");
expect((fs.getNode(3) as FileNode).parent).toEqual(nodeSour.parent);
expect(nodeDest.children["world.txt"]).toBeUndefined();
expect(nodeDest.children["foo.foo"]).not.toBeUndefined();
});
});