moveNode tests written, 1 fails, need to check for bugs
This commit is contained in:
@@ -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<number, TreeNode> = 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();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user