import { dnTraverseTree, upTraverseTree } from "@core/util/tree_traversal"; import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture"; import { depth7Tree } from "@tests/helpers/tree_fixture"; import { describe, expect, test } from "vitest"; const testTree: FixtureNode = { inode: 0, name: "root", type: "Dir", children: [ { inode: 1, name: "hello.txt", type: "File", content: "Hello, world!", children: [], }, { inode: 2, name: "sub", type: "Dir", children: [ { inode: 3, name: "world.txt", type: "File", content: "Goodbye!", children: [], }, ], }, ], }; const fs = buildTestFS(testTree); const fsTwo = buildTestFS(depth7Tree); describe("dnTraverseTree", () => { test("successfully traverses down the tree in depth-first pre-order", () => { const result = dnTraverseTree(fs, 0); // This got changed from [0, 2, 3, 1] to [1, 2, 3] // A bug in dnTraverseTree resulted in the tree traversal not respecting order of nodes, and including root. // Including root in results caused further problems so it was decided that results wont include the passed root node; expect(result, `Checking the result ${result} to be [1, 2, 3]`).toEqual([1, 2, 3]); }); test("returns empty array for a non existent root inode", () => { expect(dnTraverseTree(fs, 999)).toEqual([]); }); }); describe("upTraverseTree", () => { test("successfully traverses up the tree", () => { const result = upTraverseTree(fsTwo, 7); expect(result, `Checking the result ${result} to be [7, 6, 5, 4, 3, 2, 1, 0]`).toEqual([ 7, 6, 5, 4, 3, 2, 1, 0, ]); }); test("returns empty array for a non existent inode", () => { expect(dnTraverseTree(fs, 999)).toEqual([]); }); });