import { resolveInode, resolvePath } from "@core/util/path_resolver"; import { expectNodeNotFoundError, expectNotADirectoryError } from "@tests/helpers/error_matchers"; import { Fixture7Depth } from "@tests/helpers/state_fixture"; import { describe, expect, test } from "vitest"; const state = Fixture7Depth.State; const fs = state.filesystem; describe("resolvePath", () => { test("returns an inode from an absolute path", () => { const path = "/d1/d2/d3/d4/"; const result = resolvePath(fs, 1, path); expect(result).toBeDefined(); expect(fs.getNode(result as number)!.name).toBe("d4"); }); test("returns an inode from a relative path", () => { const path = "./d2/d3/d4/"; const result = resolvePath(fs, 1, path); expect(result).toBeDefined(); expect(fs.getNode(result as number)!.name).toBe("d4"); }); test("returns an inode from a relative parent path", () => { const path = "../d1/d2/d3/d4/"; const result = resolvePath(fs, 1, path); expect(result).toBeDefined(); expect(fs.getNode(result as number)!.name).toBe("d4"); }); test("returns inode 0", () => { const path = "/"; const result = resolvePath(fs, 1, path); expect(result).toBeDefined(); expect(result).toBe(0); expect(fs.getNode(result as number)!.name).toBe("root"); }); test("returns inode 1 (cwd)", () => { const path = ""; const result = resolvePath(fs, 1, path); expect(result).toBeDefined(); expect(result).toBe(1); expect(fs.getNode(result as number)!.name).toBe("d1"); }); test("throws because of a nonexistent path", () => { expectNodeNotFoundError(() => resolvePath(fs, 1, "/d3")); }); test("throws because of file path component", () => { expectNotADirectoryError(() => resolvePath(fs, 6, "./deep_file.txt/d8")); }); test("throws because of a nonexistent cwd inode (technicaly should never happen)", () => { expectNodeNotFoundError(() => resolvePath(fs, 69, "/d3")); }); }); describe("resolveInode", () => { test("returns an the path from the d4 node", () => { expect(resolveInode(state, 4, true)).toBe("/d1/d2/d3/d4"); }); test("returns an empty string from a non existent inode", () => { expect(resolveInode(state, 999)).toBe(""); }); });