From 7cf60208e0fb36f843f50c4558aa6ebfaa5975dd Mon Sep 17 00:00:00 2001 From: orion Date: Tue, 21 Jul 2026 04:26:19 +0200 Subject: [PATCH] moveNode tests written, 1 fails, need to check for bugs --- src/core/filesystem/filesystem.ts | 148 +++++++++++++++++++++++- test/core/filesystem/filesystem.test.ts | 74 ++++++++---- 2 files changed, 194 insertions(+), 28 deletions(-) diff --git a/src/core/filesystem/filesystem.ts b/src/core/filesystem/filesystem.ts index 30744dd..1cb0cd2 100644 --- a/src/core/filesystem/filesystem.ts +++ b/src/core/filesystem/filesystem.ts @@ -1,6 +1,12 @@ import { createImmutableBuffer, type ImmutableBuffer } from "@core/util/immutable_buffer"; -import { dnTraverseTree } from "@core/util/tree_traversal"; -import type { AlreadyExistsError, IllegalMutationError, NodeNotFoundError, NotADirectoryError } from "./errors"; +import { dnTraverseTree, upTraverseTree } from "@core/util/tree_traversal"; +import type { + AlreadyExistsError, + DirectoryLoopError, + IllegalMutationError, + NodeNotFoundError, + NotADirectoryError, +} from "./errors"; import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "./treenode"; export class FileSystem { @@ -88,9 +94,143 @@ export class FileSystem { 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 = map; + return FileSystem.init(this._BaseFS, newOverlay, this._INodeIndex); + } public modifyNode( inode: number, diff --git a/test/core/filesystem/filesystem.test.ts b/test/core/filesystem/filesystem.test.ts index d7f4f18..40868f8 100644 --- a/test/core/filesystem/filesystem.test.ts +++ b/test/core/filesystem/filesystem.test.ts @@ -1,9 +1,8 @@ import { FileSystem } from "@core/filesystem/filesystem"; import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/treenode"; import { createImmutableBuffer } from "@core/util/immutable_buffer"; -import { dnTraverseTree } from "@core/util/tree_traversal"; 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 = { inode: 0, @@ -29,6 +28,12 @@ const testTree: FixtureNode = { content: "Goodbye!", children: [], }, + { + inode: 4, + name: "bye", + type: "Dir", + children: [], + }, ], }, ], @@ -36,28 +41,6 @@ const testTree: FixtureNode = { 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", () => { const map: Map = new Map(); const time = Date.now(); @@ -314,3 +297,46 @@ describe("modifyNode", () => { 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(); + }); +});