Virtual FileSystem fixed - (Needs unit testing)!
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import { FileSystem } from "@core/filesystem/filesystem";
|
||||
import { TreeNodeType, type DirectoryNode, type FileNode, type TreeNode } from "@core/filesystem/node";
|
||||
import { NodeType, type FileNode } from "@core/filesystem/node";
|
||||
import type { IDataStore, IMetaStore, VDiskConfig } from "@core/filesystem/vdisk";
|
||||
import type { UserInfo } from "@core/session_manager";
|
||||
import { createImmutableBuffer } from "@core/util/immutable_buffer";
|
||||
import { MemDataStore, MemMetaStore } from "@core/util/temp_mem_stores";
|
||||
import { deserializeFileSystem } from "@shell/serializer";
|
||||
import {
|
||||
expectAlreadyExistsError,
|
||||
expectDirectoryLoopError,
|
||||
@@ -9,83 +12,55 @@ import {
|
||||
expectNodeNotFoundError,
|
||||
expectNotADirectoryError,
|
||||
} from "@tests/helpers/error_matchers";
|
||||
import { buildTestFS, type FixtureNode } from "@tests/helpers/filesystems_fixture";
|
||||
import { buildTestFS } from "@tests/helpers/filesystems_fixture";
|
||||
import { Blob } from "node:buffer";
|
||||
import { describe, expect, it } 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: [],
|
||||
},
|
||||
{
|
||||
inode: 4,
|
||||
name: "bye",
|
||||
type: "Dir",
|
||||
children: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const fs = buildTestFS(testTree);
|
||||
const fs = buildTestFS();
|
||||
|
||||
describe("FileSystem.init", () => {
|
||||
const map: Map<number, TreeNode> = new Map();
|
||||
const time = Date.now();
|
||||
const rawJson = `{"dev": [{"id": 0, "tree": {"type": "directory", "inode": 0, "mode": 443, "owner": 0, "group": 0, "parent": 0, "atime": 0, "mtime": 0, "ctime": 0, "children": []}}]}`;
|
||||
const arr = deserializeFileSystem(rawJson);
|
||||
const devices: { dstore: IDataStore; mstore: IMetaStore; config: VDiskConfig }[] = [];
|
||||
|
||||
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: {},
|
||||
const data = new MemDataStore();
|
||||
const meta = new MemMetaStore();
|
||||
const config: VDiskConfig = {
|
||||
id: 0,
|
||||
label: `disk-0`,
|
||||
index: 0,
|
||||
readonly: false,
|
||||
size: 1024 * 1024 * 1024 * 8, //8 GiB
|
||||
base: arr[0]!.map,
|
||||
dataC: {
|
||||
chunkSize: 1024 * 8, //8 KiB
|
||||
cacheSize: 1024 * 1024 * 64, //64 MiB
|
||||
cacheTTL: 1000 * 60, // 1 min
|
||||
},
|
||||
};
|
||||
map.set(0, root);
|
||||
|
||||
const base: ReadonlyMap<number, TreeNode> = map;
|
||||
const overlay: ReadonlyMap<number, TreeNode | null> = new Map();
|
||||
devices.push({ dstore: data, mstore: meta, config });
|
||||
|
||||
it("initializes correctly", () => {
|
||||
const fs = FileSystem.init(base, overlay, 0);
|
||||
const fs = FileSystem.init(devices);
|
||||
expect(fs).toBeInstanceOf(FileSystem);
|
||||
expect(fs.INodeIndex).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("createNode", () => {
|
||||
const content = createImmutableBuffer(new TextEncoder().encode("Lorem ipsum"));
|
||||
const content = new Blob([new TextEncoder().encode("Lorem ipsum")]);
|
||||
|
||||
it("creates a file and updates parent", () => {
|
||||
const newFS = fs.createNode(0, "readme", TreeNodeType.File, 0o644, 0, 0, content);
|
||||
fs.createNode("0:0", "readme", {
|
||||
type: NodeType.File,
|
||||
mode: 0o644,
|
||||
owner: 0,
|
||||
group: 0,
|
||||
content: content,
|
||||
size: content.size,
|
||||
});
|
||||
|
||||
expect(newFS).toBeInstanceOf(FileSystem);
|
||||
const newNode = newFS.getNode(5) as FileNode;
|
||||
expect(fs).toBeInstanceOf(FileSystem);
|
||||
const newNode = fs.getNode("0") as FileNode;
|
||||
|
||||
expect(newNode.type).toBe(TreeNodeType.File);
|
||||
expect(newNode.name).toBe("readme");
|
||||
|
||||
Reference in New Issue
Block a user