Files
termos/test/core/filesystem/filesystem.test.ts
T
2026-07-20 18:15:50 +02:00

317 lines
10 KiB
TypeScript

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";
const testTree: FixtureNode = {
inode: 0,
name: "root",
type: "Dir",
children: [
{
inode: 1,
name: "hello.txt",
type: "File",
content: "Goodbye World!",
children: [],
},
{
inode: 2,
name: "sub",
type: "Dir",
children: [
{
inode: 3,
name: "world.txt",
type: "File",
content: "Goodbye!",
children: [],
},
],
},
],
};
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();
const root: DirectoryNode = {
type: TreeNodeType.Directory,
inode: 0,
name: "root",
parent: 0,
mode: 0o555,
owner: 0,
group: 0,
mtime: time,
atime: time,
ctime: time,
children: {},
};
map.set(0, root);
const base: ReadonlyMap<number, TreeNode> = map;
const overlay: ReadonlyMap<number, TreeNode | null> = new Map();
it("initializes correctly", () => {
const fs = FileSystem.init(base, overlay, 0);
expect(fs).toBeInstanceOf(FileSystem);
expect(fs.INodeIndex).toBe(0);
});
});
describe("createNode", () => {
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
it("creates a file and updates parent", () => {
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
expect(newFS).toBeInstanceOf(FileSystem);
const newNode = newFS.getNode(4) as FileNode;
expect(newNode.type).toBe(TreeNodeType.File);
expect(newNode.name).toBe("readme");
expect(new TextDecoder().decode(newNode.content.toArray())).toBe("Lorem ipsum");
expect(newNode.parent).toBe(0);
expect(newNode.mode).toBe(0o644);
const root = newFS.getNode(0) as DirectoryNode;
expect(root.children["readme"]).toBe(4);
expect(newFS.INodeIndex).toBe(4);
expect(newFS.Overlay.size).toBe(2);
expect(fs.getNode(4)).toBeUndefined();
const oldRoot = fs.getNode(0) as DirectoryNode;
expect(oldRoot.children).toEqual({ "hello.txt": 1, sub: 2 });
});
it("fails to create a file as it alread exists", () => {
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
expect(() => newFS.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content)).toThrow();
});
it("fails to create a file because the parent is not a directory", () => {
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
expect(() => newFS.createNode(1, "test", TreeNodeType.File, 0o644, 0, 0, content)).toThrow();
});
it("fails to create a file because the specified parent doesnt exist", () => {
expect(() => fs.createNode(999, "readme", TreeNodeType.File, 0o644, 0, 0, content)).toThrow();
});
});
describe("permission method group", () => {
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
it("returns true for user read permissions", () => {
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o400, 1000, 1000, content);
const perms = newFS.canRead(4, { uid: 1000, gid: 1000 });
expect(perms).toBe(true);
});
it("returns false for user read permissions", () => {
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o200, 1000, 1000, content);
const perms = newFS.canRead(4, { uid: 1000, gid: 1000 });
expect(perms).toBe(false);
});
it("returns true for group read permissions", () => {
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o040, 1000, 2000, content);
const perms = newFS.canRead(4, { uid: 999, gid: 2000 });
expect(perms).toBe(true);
});
it("returns false for group read permissions", () => {
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o020, 1000, 2000, content);
const perms = newFS.canRead(4, { uid: 999, gid: 2000 });
expect(perms).toBe(false);
});
it("returns true for other read permissions", () => {
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o004, 1000, 2000, content);
const perms = newFS.canRead(4, { uid: 999, gid: 3000 });
expect(perms).toBe(true);
});
it("returns false for other read permissions", () => {
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o002, 1000, 2000, content);
const perms = newFS.canRead(4, { uid: 999, gid: 3000 });
expect(perms).toBe(false);
});
it("returns true for user execute permissions", () => {
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o100, 1000, 2000, content);
const perms = newFS.canExecute(4, { uid: 1000, gid: 2000 });
expect(perms).toBe(true);
});
});
describe("deleteNode", () => {
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
const newNode = newFS.getNode(1) as FileNode;
it("fails to delete the root node", () => {
expect(() => fs.deleteNode(0)).toThrow();
});
it("fails to delete a non existant node", () => {
expect(() => fs.deleteNode(900)).toThrow();
});
it("successfully deletes a single child node", () => {
const newnewFS = newFS.deleteNode(1);
const overlay = newnewFS.Overlay;
const parent = newnewFS.getNode(0) as DirectoryNode;
expect(overlay.get(1)).toBe(null);
expect(parent.children).not.toHaveProperty(newNode.name);
});
it("successfully deletes a directory recursively", () => {
const newFS = fs.deleteNode(2);
const parent = newFS.getNode(0) as DirectoryNode;
expect(newFS.getNode(2)).toBe(undefined);
expect(newFS.getNode(3)).toBe(undefined);
expect(newFS.Overlay.size).toBeGreaterThanOrEqual(3);
expect(newFS.Overlay.get(2)).toBe(null);
expect(newFS.Overlay.get(3)).toBe(null);
expect(parent.children).not.toHaveProperty("sub");
expect(fs.getNode(2)).not.toBe(undefined);
expect(fs.Overlay.size).toBe(0);
});
});
describe("modifyNode", () => {
it("modifies content", () => {
const changes = {
content: createImmutableBuffer(new TextEncoder().encode("Hello, World!")),
};
const before = Date.now();
const newFS = fs.modifyNode(1, changes);
const node = newFS.getNode(1) as FileNode;
const after = Date.now();
expect(new TextDecoder().decode(node.content.toArray())).toBe("Hello, World!");
expect(newFS.Overlay.size).toBe(1);
expect(node.ctime).toBeGreaterThanOrEqual(before);
expect(node.ctime).toBeLessThanOrEqual(after);
expect(node.mtime).toBeGreaterThanOrEqual(before);
expect(node.mtime).toBeLessThanOrEqual(after);
});
it("modifies mode", () => {
const changes = {
mode: 0o222,
};
const before = Date.now();
const newFS = fs.modifyNode(1, changes);
const node = newFS.getNode(1) as FileNode;
const oldNode = fs.getNode(1) as FileNode;
const after = Date.now();
expect(node.mode).toEqual(0o222);
expect(newFS.Overlay.size).toBe(1);
expect(node.ctime).toBeGreaterThanOrEqual(before);
expect(node.ctime).toBeLessThanOrEqual(after);
expect(node.mtime).toEqual(oldNode.mtime);
});
it("modifies owner and group", () => {
const changes = {
owner: 1,
group: 2,
};
const before = Date.now();
const newFS = fs.modifyNode(1, changes);
const node = newFS.getNode(1) as FileNode;
const oldNode = fs.getNode(1) as FileNode;
const after = Date.now();
expect(node.owner).toBe(1);
expect(node.group).toBe(2);
expect(newFS.Overlay.size).toBe(1);
expect(node.ctime).toBeGreaterThanOrEqual(before);
expect(node.ctime).toBeLessThanOrEqual(after);
expect(node.mtime).toEqual(oldNode.mtime);
});
it("calls modify node with empty changes", () => {
const changes = {};
const before = Date.now();
const newFS = fs.modifyNode(1, changes);
const node = newFS.getNode(1) as FileNode;
const oldNode = fs.getNode(1) as FileNode;
const after = Date.now();
expect(node.content).toEqual(oldNode.content);
expect(node.mode).toEqual(oldNode.mode);
expect(node.owner).toEqual(oldNode.owner);
expect(node.group).toEqual(oldNode.group);
expect(node.content).toEqual(oldNode.content);
expect(node.ctime).toBeGreaterThanOrEqual(before);
expect(node.ctime).toBeLessThanOrEqual(after);
expect(node.mtime).toEqual(oldNode.mtime);
});
it("fails", () => {
const changes = {
owner: 1,
group: 2,
};
expect(() => fs.modifyNode(999, changes)).toThrow();
});
});