Files
termos/test/core/path_resolver.test.ts
T

74 lines
2.4 KiB
TypeScript

import { resolveInode, resolvePath } from "@core/path_resolver";
import { expectNodeNotFoundError, expectNotADirectoryError } from "@tests/helpers/error_matchers";
import { buildTestFS } from "@tests/helpers/filesystems_fixture";
import { depth7Tree } from "@tests/helpers/tree_fixture";
import { describe, expect, test } from "vitest";
const fs = buildTestFS(depth7Tree);
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(fs, 4)).toBe("/d1/d2/d3/d4");
});
test("returns an empty string from a non existent inode", () => {
expect(resolveInode(fs, 999)).toBe("");
});
});