- Fixed resolvePath
- Added symlink resolution - Added mount resolution - Fixed filesystem not awaiting for disks to be mounted (now `FileSystem.init()` needs to be called with `await`) - Removed old test fixture files - Added a feature-full `resolvePath`test suite *all tests passing green*
This commit is contained in:
+1261
-319
File diff suppressed because it is too large
Load Diff
+856
-55
@@ -1,73 +1,874 @@
|
||||
import { resolveInode, resolvePath } from "@core/util/path_resolver";
|
||||
// @tests/core/util/path_resolver.test.ts (adjust)
|
||||
import { FileSystem } from "@core/filesystem/filesystem";
|
||||
import {
|
||||
NodeType,
|
||||
toPtr,
|
||||
type DirectoryNode,
|
||||
type FileNode,
|
||||
type Node,
|
||||
type NodePointer,
|
||||
type SymlinkNode,
|
||||
} from "@core/filesystem/node";
|
||||
import { DefaultVDiskConfig } from "@core/filesystem/vdisk";
|
||||
import type { SessionState, UserInfo } from "@core/session_manager";
|
||||
import type { ShellState } from "@core/shell_state";
|
||||
import { resolvePath } from "@core/util/path_resolver";
|
||||
import { MemDataStore, MemMetaStore } from "@core/util/temp_mem_stores";
|
||||
import { expectNodeNotFoundError, expectNotADirectoryError } from "@tests/helpers/error_matchers";
|
||||
import { Fixture7Depth } from "@tests/helpers/state_fixture";
|
||||
import { describe, expect, test } from "vitest";
|
||||
import fc from "fast-check";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
const state = Fixture7Depth.State;
|
||||
const fs = state.filesystem;
|
||||
// ============================================================================
|
||||
// Node fixtures
|
||||
// ============================================================================
|
||||
|
||||
describe("resolvePath", () => {
|
||||
test("returns an inode from an absolute path", () => {
|
||||
const path = "/d1/d2/d3/d4/";
|
||||
const result = resolvePath(fs, 1, path);
|
||||
function makeDir(inode: number, children: Record<string, number> = {}): DirectoryNode {
|
||||
return {
|
||||
type: NodeType.Directory,
|
||||
inode,
|
||||
link_count: 2,
|
||||
mode: 0o755,
|
||||
owner: 0,
|
||||
group: 0,
|
||||
mtime: 0,
|
||||
ctime: 0,
|
||||
atime: 0,
|
||||
children: new Map(Object.entries(children)),
|
||||
};
|
||||
}
|
||||
|
||||
expect(result).toBeDefined();
|
||||
expect(fs.getNode(result as number)!.name).toBe("d4");
|
||||
function makeFile(inode: number): FileNode {
|
||||
return {
|
||||
type: NodeType.File,
|
||||
inode,
|
||||
link_count: 1,
|
||||
mode: 0o644,
|
||||
owner: 0,
|
||||
group: 0,
|
||||
mtime: 0,
|
||||
ctime: 0,
|
||||
atime: 0,
|
||||
content: `uuid-${inode}`,
|
||||
size: 0,
|
||||
};
|
||||
}
|
||||
|
||||
function makeSymlink(inode: number, target: string): SymlinkNode {
|
||||
return {
|
||||
type: NodeType.Symlink,
|
||||
inode,
|
||||
link_count: 1,
|
||||
mode: 0o777,
|
||||
owner: 0,
|
||||
group: 0,
|
||||
mtime: 0,
|
||||
ctime: 0,
|
||||
atime: 0,
|
||||
target,
|
||||
};
|
||||
}
|
||||
|
||||
function makeUser(home: NodePointer = toPtr({ device_id: 0, inode: 0 })): UserInfo {
|
||||
return {
|
||||
username: "test",
|
||||
uid: 1000,
|
||||
gid: 1000,
|
||||
group: [],
|
||||
home,
|
||||
};
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Tree builder — auto-assigns inodes, validates before use
|
||||
// ============================================================================
|
||||
|
||||
interface TreeBuilder {
|
||||
addDir(parentInode: number, name: string): number;
|
||||
addFile(parentInode: number, name: string): number;
|
||||
addSymlink(parentInode: number, name: string, target: string): number;
|
||||
build(): Map<number, Node>;
|
||||
}
|
||||
|
||||
function makeTree(): TreeBuilder {
|
||||
const nodes = new Map<number, Node>([[0, makeDir(0)]]);
|
||||
let nextInode = 1;
|
||||
|
||||
function attach(parentInode: number, name: string, childInode: number): void {
|
||||
const parent = nodes.get(parentInode);
|
||||
if (!parent || parent.type !== NodeType.Directory) {
|
||||
throw new Error(`Parent inode ${parentInode} is not a directory`);
|
||||
}
|
||||
const children = new Map(parent.children);
|
||||
children.set(name, childInode);
|
||||
nodes.set(parentInode, { ...parent, children });
|
||||
}
|
||||
|
||||
function add(node: Node, parentInode: number, name: string): number {
|
||||
attach(parentInode, name, node.inode);
|
||||
nodes.set(node.inode, node);
|
||||
return node.inode;
|
||||
}
|
||||
|
||||
return {
|
||||
addDir(parentInode, name) {
|
||||
return add(makeDir(nextInode++), parentInode, name);
|
||||
},
|
||||
addFile(parentInode, name) {
|
||||
return add(makeFile(nextInode++), parentInode, name);
|
||||
},
|
||||
addSymlink(parentInode, name, target) {
|
||||
return add(makeSymlink(nextInode++, target), parentInode, name);
|
||||
},
|
||||
build() {
|
||||
for (const [key, node] of nodes) {
|
||||
if (node.inode !== key) {
|
||||
throw new Error(`inode mismatch: key=${key}, node.inode=${node.inode}`);
|
||||
}
|
||||
if (node.type === NodeType.Directory) {
|
||||
for (const [cname, cinode] of node.children) {
|
||||
if (!nodes.has(cinode)) {
|
||||
throw new Error(`dangling child '${cname}' → inode ${cinode}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nodes;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// FileSystem builder — real VDisk stack, one device per base map
|
||||
// ============================================================================
|
||||
|
||||
async function makeFS(bases: Map<number, Node>[]): Promise<FileSystem> {
|
||||
const devices = bases.map((base, id) => ({
|
||||
dstore: new MemDataStore(),
|
||||
mstore: new MemMetaStore(),
|
||||
config: {
|
||||
...DefaultVDiskConfig(id),
|
||||
base,
|
||||
index: Math.max(0, ...base.keys()),
|
||||
},
|
||||
}));
|
||||
|
||||
const fs = FileSystem.init(devices);
|
||||
// `FileSystem.init` does not await `registerDisk` (see the init suite BUG);
|
||||
// flush a macrotask so every disk is registered before the first resolve.
|
||||
await new Promise(r => setImmediate(r));
|
||||
return fs;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Shell state fixture
|
||||
// ============================================================================
|
||||
|
||||
function makeShellState(
|
||||
fs: FileSystem,
|
||||
opts: { cwd?: NodePointer; home?: NodePointer; user?: UserInfo } = {},
|
||||
): ShellState {
|
||||
const cwd = opts.cwd ?? toPtr({ device_id: 0, inode: 0 });
|
||||
const user = opts.user ?? makeUser(opts.home);
|
||||
const session = {
|
||||
user,
|
||||
envVars: new Map<string, string>(),
|
||||
history: [],
|
||||
cwd,
|
||||
pwd: cwd,
|
||||
exitCode: 0,
|
||||
} as unknown as SessionState;
|
||||
|
||||
return {
|
||||
filesystem: fs,
|
||||
users: new Map(),
|
||||
groups: new Map(),
|
||||
sessionStack: [session],
|
||||
} as unknown as ShellState;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Path normalization
|
||||
// ============================================================================
|
||||
|
||||
describe("resolvePath — path normalization", () => {
|
||||
/**
|
||||
* `/` is the root of device 0; no traversal is needed.
|
||||
*/
|
||||
it("resolves '/' to the root of device 0", async () => {
|
||||
const fs = await makeFS([makeTree().build()]);
|
||||
expect(resolvePath(makeShellState(fs), "/")).toEqual({ device_id: 0, inode: 0 });
|
||||
});
|
||||
|
||||
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");
|
||||
/**
|
||||
* Redundant leading slashes are stripped by the normalizer.
|
||||
*/
|
||||
it("resolves '/', '//', and '///' identically", async () => {
|
||||
const fs = await makeFS([makeTree().build()]);
|
||||
const state = makeShellState(fs);
|
||||
for (const p of ["/", "//", "///"]) {
|
||||
expect(resolvePath(state, p)).toEqual({ device_id: 0, inode: 0 });
|
||||
}
|
||||
});
|
||||
|
||||
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");
|
||||
/**
|
||||
* Empty path returns the current working directory without traversal.
|
||||
*/
|
||||
it("resolves an empty string to the current working directory", async () => {
|
||||
const t = makeTree();
|
||||
const work = t.addDir(0, "work");
|
||||
const fs = await makeFS([t.build()]);
|
||||
const state = makeShellState(fs, { cwd: toPtr({ device_id: 0, inode: work }) });
|
||||
expect(resolvePath(state, "")).toEqual({ device_id: 0, inode: work });
|
||||
});
|
||||
|
||||
test("returns inode 0", () => {
|
||||
const path = "/";
|
||||
const result = resolvePath(fs, 1, path);
|
||||
/**
|
||||
* Property: leading, trailing, and interior redundant slashes never change
|
||||
* the resolved pointer.
|
||||
*/
|
||||
it("resolves identically regardless of redundant slashes", async () => {
|
||||
const t = makeTree();
|
||||
const a = t.addDir(0, "a");
|
||||
t.addFile(a, "b");
|
||||
const fs = await makeFS([t.build()]);
|
||||
const state = makeShellState(fs);
|
||||
const expected = resolvePath(state, "/a/b");
|
||||
|
||||
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"));
|
||||
fc.assert(
|
||||
fc.property(
|
||||
fc.integer({ min: 0, max: 5 }),
|
||||
fc.integer({ min: 0, max: 5 }),
|
||||
fc.integer({ min: 1, max: 4 }),
|
||||
(lead, trail, mid) => {
|
||||
const path = "/".repeat(lead) + "a" + "/".repeat(mid) + "b" + "/".repeat(trail);
|
||||
expect(resolvePath(state, path)).toEqual(expected);
|
||||
},
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveInode", () => {
|
||||
test("returns an the path from the d4 node", () => {
|
||||
expect(resolveInode(state, 4, true)).toBe("/d1/d2/d3/d4");
|
||||
// ============================================================================
|
||||
// Absolute paths
|
||||
// ============================================================================
|
||||
|
||||
describe("resolvePath — absolute paths", () => {
|
||||
/**
|
||||
* Single-component absolute path resolves to the child's pointer.
|
||||
*/
|
||||
it("resolves '/name' to the child node of root", async () => {
|
||||
const t = makeTree();
|
||||
const foo = t.addFile(0, "foo");
|
||||
const fs = await makeFS([t.build()]);
|
||||
expect(resolvePath(makeShellState(fs), "/foo")).toEqual({ device_id: 0, inode: foo });
|
||||
});
|
||||
|
||||
test("returns an empty string from a non existent inode", () => {
|
||||
expect(resolveInode(state, 999)).toBe("");
|
||||
/**
|
||||
* Nested paths traverse each directory in turn.
|
||||
*/
|
||||
it("resolves a nested path '/a/b' to the final child", async () => {
|
||||
const t = makeTree();
|
||||
const a = t.addDir(0, "a");
|
||||
const b = t.addFile(a, "b");
|
||||
const fs = await makeFS([t.build()]);
|
||||
expect(resolvePath(makeShellState(fs), "/a/b")).toEqual({ device_id: 0, inode: b });
|
||||
});
|
||||
|
||||
/**
|
||||
* Deeper paths traverse more levels.
|
||||
*/
|
||||
it("resolves a deep path '/a/b/c/d' to the final child", async () => {
|
||||
const t = makeTree();
|
||||
const a = t.addDir(0, "a");
|
||||
const b = t.addDir(a, "b");
|
||||
const c = t.addDir(b, "c");
|
||||
const d = t.addFile(c, "d");
|
||||
const fs = await makeFS([t.build()]);
|
||||
expect(resolvePath(makeShellState(fs), "/a/b/c/d")).toEqual({ device_id: 0, inode: d });
|
||||
});
|
||||
|
||||
/**
|
||||
* Trailing slash is stripped and resolves the same as without it.
|
||||
*/
|
||||
it("resolves '/a/b/' to the same pointer as '/a/b'", async () => {
|
||||
const t = makeTree();
|
||||
const a = t.addDir(0, "a");
|
||||
t.addFile(a, "b");
|
||||
const fs = await makeFS([t.build()]);
|
||||
const state = makeShellState(fs);
|
||||
expect(resolvePath(state, "/a/b/")).toEqual(resolvePath(state, "/a/b"));
|
||||
});
|
||||
|
||||
/**
|
||||
* Missing top-level component raises a typed NodeNotFound.
|
||||
*/
|
||||
it("throws NodeNotFound when the first component does not exist", async () => {
|
||||
const fs = await makeFS([makeTree().build()]);
|
||||
await expectNodeNotFoundError(() => resolvePath(makeShellState(fs), "/missing"));
|
||||
});
|
||||
|
||||
/**
|
||||
* Missing intermediate component raises a typed NodeNotFound.
|
||||
*/
|
||||
it("throws NodeNotFound when an intermediate component does not exist", async () => {
|
||||
const t = makeTree();
|
||||
t.addDir(0, "a");
|
||||
const fs = await makeFS([t.build()]);
|
||||
await expectNodeNotFoundError(() => resolvePath(makeShellState(fs), "/a/missing/b"));
|
||||
});
|
||||
|
||||
/**
|
||||
* Descending through a file at a non-final position is rejected with
|
||||
* NotADirectory (this one works — the isFile check fires because `a` is
|
||||
* not the last component).
|
||||
*/
|
||||
it("throws NotADirectory when traversing through a file at a non-final position", async () => {
|
||||
const t = makeTree();
|
||||
t.addFile(0, "a");
|
||||
const fs = await makeFS([t.build()]);
|
||||
await expectNotADirectoryError(() => resolvePath(makeShellState(fs), "/a/b/c"));
|
||||
});
|
||||
|
||||
/**
|
||||
* EXPECTED-TO-FAIL: descending through a file at the final position should
|
||||
* also throw NotADirectory. Current implementation skips the isFile check
|
||||
* on the last iteration and then crashes on `(file as Directory).children`.
|
||||
*/
|
||||
it("should throw NotADirectory when descending through a file at the final position", async () => {
|
||||
const t = makeTree();
|
||||
t.addFile(0, "a");
|
||||
const fs = await makeFS([t.build()]);
|
||||
await expectNotADirectoryError(() => resolvePath(makeShellState(fs), "/a/b"));
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// Relative paths
|
||||
// ============================================================================
|
||||
|
||||
describe("resolvePath — relative paths", () => {
|
||||
/**
|
||||
* A single relative component resolves against cwd.
|
||||
*/
|
||||
it("resolves a relative path from a device-0 cwd", async () => {
|
||||
const t = makeTree();
|
||||
const work = t.addDir(0, "work");
|
||||
const file = t.addFile(work, "file");
|
||||
const fs = await makeFS([t.build()]);
|
||||
const state = makeShellState(fs, { cwd: toPtr({ device_id: 0, inode: work }) });
|
||||
expect(resolvePath(state, "file")).toEqual({ device_id: 0, inode: file });
|
||||
});
|
||||
|
||||
/**
|
||||
* Multi-component relative paths traverse from cwd.
|
||||
*/
|
||||
it("resolves a multi-component relative path from cwd", async () => {
|
||||
const t = makeTree();
|
||||
const work = t.addDir(0, "work");
|
||||
const sub = t.addDir(work, "sub");
|
||||
const leaf = t.addFile(sub, "leaf");
|
||||
const fs = await makeFS([t.build()]);
|
||||
const state = makeShellState(fs, { cwd: toPtr({ device_id: 0, inode: work }) });
|
||||
expect(resolvePath(state, "sub/leaf")).toEqual({ device_id: 0, inode: leaf });
|
||||
});
|
||||
|
||||
/**
|
||||
* Missing relative component raises a typed NodeNotFound.
|
||||
*/
|
||||
it("throws NodeNotFound for a missing relative component", async () => {
|
||||
const fs = await makeFS([makeTree().build()]);
|
||||
await expectNodeNotFoundError(() => resolvePath(makeShellState(fs), "nope"));
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// ~ expansion
|
||||
// ============================================================================
|
||||
|
||||
describe("resolvePath — '~' expansion", () => {
|
||||
/**
|
||||
* `~` expands to the user's home directory (home on device 0).
|
||||
*/
|
||||
it("expands '~' to the user's home directory", async () => {
|
||||
const t = makeTree();
|
||||
const home = t.addDir(0, "home");
|
||||
const fs = await makeFS([t.build()]);
|
||||
const user = makeUser(toPtr({ device_id: 0, inode: home }));
|
||||
const state = makeShellState(fs, { user });
|
||||
expect(resolvePath(state, "~")).toEqual({ device_id: 0, inode: home });
|
||||
});
|
||||
|
||||
/**
|
||||
* `~/name` expands home first, then resolves `name` beneath it.
|
||||
*/
|
||||
it("expands '~/name' to a child of home", async () => {
|
||||
const t = makeTree();
|
||||
const home = t.addDir(0, "home");
|
||||
const file = t.addFile(home, "file");
|
||||
const fs = await makeFS([t.build()]);
|
||||
const user = makeUser(toPtr({ device_id: 0, inode: home }));
|
||||
const state = makeShellState(fs, { user });
|
||||
expect(resolvePath(state, "~/file")).toEqual({ device_id: 0, inode: file });
|
||||
});
|
||||
|
||||
/**
|
||||
* EXPECTED-TO-FAIL: `~` expansion never touches `currentVD`, so a home on
|
||||
* device 1 resolves the first component but then reads subsequent children
|
||||
* from device 0 — usually crashing on a missing inode.
|
||||
*/
|
||||
it("should resolve '~/file' when home lives on device 1", async () => {
|
||||
const t0 = makeTree();
|
||||
const t1 = makeTree();
|
||||
const home = t1.addDir(0, "home");
|
||||
const file = t1.addFile(home, "file");
|
||||
const fs = await makeFS([t0.build(), t1.build()]);
|
||||
const user = makeUser(toPtr({ device_id: 1, inode: home }));
|
||||
const state = makeShellState(fs, { user });
|
||||
expect(resolvePath(state, "~/file")).toEqual({ device_id: 1, inode: file });
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// Symlink resolution
|
||||
// ============================================================================
|
||||
|
||||
describe("resolvePath — symlink resolution", () => {
|
||||
/**
|
||||
* Absolute symlink targets resolve from device 0's root.
|
||||
*/
|
||||
it("follows an absolute symlink target", async () => {
|
||||
const t = makeTree();
|
||||
const real = t.addDir(0, "real");
|
||||
const leaf = t.addFile(real, "leaf");
|
||||
t.addSymlink(0, "link", "/real/leaf");
|
||||
const fs = await makeFS([t.build()]);
|
||||
expect(resolvePath(makeShellState(fs), "/link")).toEqual({ device_id: 0, inode: leaf });
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// Mount points
|
||||
// ============================================================================
|
||||
|
||||
describe("resolvePath — mount points", () => {
|
||||
/**
|
||||
* EXPECTED-TO-FAIL: mount redirect must fire on the child before descent.
|
||||
* `/mnt` alone should return the mounted device's root, not the mountpoint
|
||||
* directory on the parent device.
|
||||
*/
|
||||
it("should resolve '/mnt' to the mounted device's root", async () => {
|
||||
const t0 = makeTree();
|
||||
const mnt = t0.addDir(0, "mnt");
|
||||
const t1 = makeTree();
|
||||
const fs = await makeFS([t0.build(), t1.build()]);
|
||||
fs.mount(toPtr({ device_id: 0, inode: mnt }), toPtr({ device_id: 1, inode: 0 }));
|
||||
expect(resolvePath(makeShellState(fs), "/mnt")).toEqual({ device_id: 1, inode: 0 });
|
||||
});
|
||||
|
||||
/**
|
||||
* EXPECTED-TO-FAIL: `/mnt/file` should descend through the mount and land
|
||||
* on `/file` on device 1 — the trailing component must not be consumed by
|
||||
* the redirect.
|
||||
*/
|
||||
it("should resolve '/mnt/file' to '/file' on the mounted device", async () => {
|
||||
const t0 = makeTree();
|
||||
const mnt = t0.addDir(0, "mnt");
|
||||
const t1 = makeTree();
|
||||
const file = t1.addFile(0, "file");
|
||||
const fs = await makeFS([t0.build(), t1.build()]);
|
||||
fs.mount(toPtr({ device_id: 0, inode: mnt }), toPtr({ device_id: 1, inode: 0 }));
|
||||
expect(resolvePath(makeShellState(fs), "/mnt/file")).toEqual({ device_id: 1, inode: file });
|
||||
});
|
||||
|
||||
/**
|
||||
* EXPECTED-TO-FAIL: deep paths through a mount must traverse the mounted
|
||||
* device for every subsequent component.
|
||||
*/
|
||||
it("should resolve '/mnt/sub/leaf' to '/sub/leaf' on the mounted device", async () => {
|
||||
const t0 = makeTree();
|
||||
const mnt = t0.addDir(0, "mnt");
|
||||
const t1 = makeTree();
|
||||
const sub = t1.addDir(0, "sub");
|
||||
const leaf = t1.addFile(sub, "leaf");
|
||||
const fs = await makeFS([t0.build(), t1.build()]);
|
||||
fs.mount(toPtr({ device_id: 0, inode: mnt }), toPtr({ device_id: 1, inode: 0 }));
|
||||
expect(resolvePath(makeShellState(fs), "/mnt/sub/leaf")).toEqual({ device_id: 1, inode: leaf });
|
||||
});
|
||||
|
||||
/**
|
||||
* A non-mountpoint directory resolves to itself on the parent device —
|
||||
* `/a` is not a mount, so no redirect.
|
||||
*/
|
||||
it("resolves a non-mountpoint directory to itself on the parent device", async () => {
|
||||
const t0 = makeTree();
|
||||
const a = t0.addDir(0, "a"); // /a = device 0:1 (not a mountpoint)
|
||||
const mnt = t0.addDir(0, "mnt"); // /mnt = device 0:2 (the mountpoint)
|
||||
const t1 = makeTree();
|
||||
const fs = await makeFS([t0.build(), t1.build()]);
|
||||
fs.mount(
|
||||
toPtr({ device_id: 0, inode: mnt }), // "0:2" — the mountpoint
|
||||
toPtr({ device_id: 1, inode: 0 }),
|
||||
);
|
||||
|
||||
const state = makeShellState(fs);
|
||||
|
||||
// /a is not a mountpoint → resolves to itself on device 0
|
||||
expect(resolvePath(state, "/a")).toEqual({ device_id: 0, inode: a });
|
||||
|
||||
// /mnt IS a mountpoint → redirects to device 1's root
|
||||
// (this is already covered by the /mnt tests; include for contrast or drop)
|
||||
expect(resolvePath(state, "/mnt")).toEqual({ device_id: 1, inode: 0 });
|
||||
});
|
||||
|
||||
/**
|
||||
* EXPECTED-TO-FAIL: nested mount at `/a/b` should redirect to dev1's sub.
|
||||
*/
|
||||
it("should resolve '/a/b' (nested mount) to the mounted device's sub", async () => {
|
||||
const t0 = makeTree();
|
||||
const a = t0.addDir(0, "a");
|
||||
const b = t0.addDir(a, "b");
|
||||
const t1 = makeTree();
|
||||
const sub = t1.addDir(0, "sub");
|
||||
const fs = await makeFS([t0.build(), t1.build()]);
|
||||
fs.mount(toPtr({ device_id: 0, inode: b }), toPtr({ device_id: 1, inode: sub }));
|
||||
expect(resolvePath(makeShellState(fs), "/a/b")).toEqual({ device_id: 1, inode: sub });
|
||||
});
|
||||
|
||||
/**
|
||||
* EXPECTED-TO-FAIL: continuation of the nested case — `/a/b/leaf` should
|
||||
* land on dev1's leaf through the mounted sub.
|
||||
*/
|
||||
it("should resolve '/a/b/leaf' through a nested mount", async () => {
|
||||
const t0 = makeTree();
|
||||
const a = t0.addDir(0, "a");
|
||||
const b = t0.addDir(a, "b");
|
||||
const t1 = makeTree();
|
||||
const sub = t1.addDir(0, "sub");
|
||||
const leaf = t1.addFile(sub, "leaf");
|
||||
const fs = await makeFS([t0.build(), t1.build()]);
|
||||
fs.mount(toPtr({ device_id: 0, inode: b }), toPtr({ device_id: 1, inode: sub }));
|
||||
expect(resolvePath(makeShellState(fs), "/a/b/leaf")).toEqual({ device_id: 1, inode: leaf });
|
||||
});
|
||||
|
||||
/**
|
||||
* EXPECTED-TO-FAIL: two independent mounts must redirect independently.
|
||||
*/
|
||||
it("should resolve multiple independent mounts", async () => {
|
||||
const t0 = makeTree();
|
||||
const mnt = t0.addDir(0, "mnt");
|
||||
const other = t0.addDir(0, "other");
|
||||
const t1 = makeTree();
|
||||
const file = t1.addFile(0, "file");
|
||||
const sub = t1.addDir(0, "sub");
|
||||
const leaf = t1.addFile(sub, "leaf");
|
||||
const fs = await makeFS([t0.build(), t1.build()]);
|
||||
fs.mount(toPtr({ device_id: 0, inode: mnt }), toPtr({ device_id: 1, inode: 0 }));
|
||||
fs.mount(toPtr({ device_id: 0, inode: other }), toPtr({ device_id: 1, inode: sub }));
|
||||
|
||||
const state = makeShellState(fs);
|
||||
expect(resolvePath(state, "/mnt/file")).toEqual({ device_id: 1, inode: file });
|
||||
expect(resolvePath(state, "/other/leaf")).toEqual({ device_id: 1, inode: leaf });
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// Symlinks across mount points
|
||||
// ============================================================================
|
||||
|
||||
describe("resolvePath — symlinks across mount points", () => {
|
||||
/**
|
||||
* EXPECTED-TO-FAIL: symlink whose target lands on another device must
|
||||
* return the target's device id in the result. Without threading, the loop
|
||||
* terminates with currentNode from dev1 but currentVD still 0.
|
||||
*/
|
||||
it("should thread the target's device into currentVD after a symlink→mount hop", async () => {
|
||||
const t0 = makeTree();
|
||||
t0.addSymlink(0, "link", "/mnt");
|
||||
const mnt = t0.addDir(0, "mnt");
|
||||
const t1 = makeTree();
|
||||
const fs = await makeFS([t0.build(), t1.build()]);
|
||||
fs.mount(toPtr({ device_id: 0, inode: mnt }), toPtr({ device_id: 1, inode: 0 }));
|
||||
|
||||
expect(resolvePath(makeShellState(fs), "/link")).toEqual({ device_id: 1, inode: 0 });
|
||||
});
|
||||
|
||||
/**
|
||||
* EXPECTED-TO-FAIL: full traversal through the symlink and across the
|
||||
* mount. Verifies not only the final pointer is right, but that the loop
|
||||
* can continue descending on the mounted device.
|
||||
*/
|
||||
it("should resolve through a symlink→mount hop and continue on the mounted device", async () => {
|
||||
const t0 = makeTree();
|
||||
t0.addSymlink(0, "link", "/mnt");
|
||||
const mnt = t0.addDir(0, "mnt");
|
||||
const t1 = makeTree();
|
||||
const file = t1.addFile(0, "file");
|
||||
const fs = await makeFS([t0.build(), t1.build()]);
|
||||
fs.mount(toPtr({ device_id: 0, inode: mnt }), toPtr({ device_id: 1, inode: 0 }));
|
||||
|
||||
expect(resolvePath(makeShellState(fs), "/link/file")).toEqual({ device_id: 1, inode: file });
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// Integration
|
||||
// ============================================================================
|
||||
|
||||
describe("resolvePath — integration", () => {
|
||||
/**
|
||||
* Full traversal across multiple levels with a file terminus.
|
||||
*/
|
||||
it("resolves a multi-level path through directories to a file", async () => {
|
||||
const t = makeTree();
|
||||
const varInode = t.addDir(0, "var");
|
||||
const logInode = t.addDir(varInode, "log");
|
||||
const appLog = t.addFile(logInode, "app.log");
|
||||
const fs = await makeFS([t.build()]);
|
||||
expect(resolvePath(makeShellState(fs), "/var/log/app.log")).toEqual({ device_id: 0, inode: appLog });
|
||||
});
|
||||
});
|
||||
|
||||
// ============================================================================
|
||||
// Known bugs — each test asserts *current* behavior and goes red on the fix.
|
||||
// ============================================================================
|
||||
|
||||
describe("resolvePath — known bugs", () => {
|
||||
/**
|
||||
* BUG: symlink depth overflow throws a raw `Error`, not a typed error.
|
||||
* Same setup as above; asserts the thrown object isn't a discriminated
|
||||
* union member (no `type` field).
|
||||
*
|
||||
* Fix: throw a typed `SymlinkLoopError` (or similar) from `errors.ts`.
|
||||
*/
|
||||
it("BUG: symlink depth overflow throws a raw Error, not a typed one", async () => {
|
||||
const t = makeTree();
|
||||
const sNames = Array.from({ length: 41 }, (_, i) => `s${i + 1}`);
|
||||
t.addFile(0, "target");
|
||||
for (let i = 0; i < 40; i++) {
|
||||
t.addSymlink(0, sNames[i]!, `/${sNames[i + 1]}`);
|
||||
}
|
||||
t.addSymlink(0, sNames[40]!, "/target");
|
||||
const fs = await makeFS([t.build()]);
|
||||
|
||||
let thrown: unknown = undefined;
|
||||
try {
|
||||
resolvePath(makeShellState(fs), "/s1");
|
||||
} catch (e) {
|
||||
thrown = e;
|
||||
}
|
||||
|
||||
expect(thrown).toBeInstanceOf(Error);
|
||||
expect(thrown).not.toHaveProperty("type");
|
||||
// ^ BUG: no `type` discriminant. Correct: typed (e.g. SymlinkLoop).
|
||||
});
|
||||
|
||||
/**
|
||||
* BUG: a malformed cwd pointer is not validated; `fromPtr(session.cwd)`
|
||||
* throws a raw `Error("Invalid node pointer format")`, not a typed one.
|
||||
* Same "untyped" class as the symlink-depth bug.
|
||||
*
|
||||
* Fix: wrap `fromPtr` calls in a typed try/catch, or add a validating
|
||||
* parse (e.g. `tryFromPtr`) that returns `undefined` on failure.
|
||||
*/
|
||||
it("BUG: malformed cwd throws a raw Error from fromPtr", async () => {
|
||||
const fs = await makeFS([makeTree().build()]);
|
||||
const state = makeShellState(fs, {
|
||||
cwd: "garbage" as NodePointer,
|
||||
});
|
||||
expect(() => resolvePath(state, "foo")).toThrow(/Invalid node pointer format/);
|
||||
});
|
||||
|
||||
/**
|
||||
* BUG: same as above, for `session.user.home` when expanding `~`.
|
||||
*/
|
||||
it("BUG: malformed home pointer throws a raw Error from fromPtr", async () => {
|
||||
const fs = await makeFS([makeTree().build()]);
|
||||
const user = makeUser("garbage" as NodePointer);
|
||||
const state = makeShellState(fs, { user });
|
||||
expect(() => resolvePath(state, "~")).toThrow(/Invalid node pointer format/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolvePath — '..' across mount boundaries", () => {
|
||||
/**
|
||||
* BASIC CASE. Mount `/mnt` (device 0) → device 1's root. From inside the
|
||||
* mount (at `1:0`), `..` must exit through the reverse mount index to
|
||||
* `/mnt`'s parent (`/`) — not stay put via the `inode === 0` shortcut.
|
||||
*
|
||||
* This is the direct fix target: the `..` branch must check
|
||||
* `getMountSource(currentPtr)` *before* the `inode === 0` shortcut.
|
||||
*/
|
||||
it("'..' from a mount root returns to the mountpoint's parent", async () => {
|
||||
const t0 = makeTree();
|
||||
const mnt = t0.addDir(0, "mnt");
|
||||
const fs = await makeFS([t0.build(), makeTree().build()]);
|
||||
await fs.mount(toPtr({ device_id: 0, inode: mnt }), toPtr({ device_id: 1, inode: 0 }));
|
||||
|
||||
const atMountedRoot = makeShellState(fs, { cwd: toPtr({ device_id: 1, inode: 0 }) });
|
||||
expect(resolvePath(atMountedRoot, "..")).toEqual({ device_id: 0, inode: 0 });
|
||||
});
|
||||
|
||||
/**
|
||||
* NON-ROOT MOUNTPOINT. Mount `/home/user/mnt` → device 1's root. `..` from
|
||||
* the mounted root must land on `/home/user` (device 0), *not* `/`.
|
||||
* Catches a fix that hardcodes "go to root on mount exit".
|
||||
*/
|
||||
it("'..' from a non-root mount returns to the mountpoint's immediate parent", async () => {
|
||||
const t0 = makeTree();
|
||||
const home = t0.addDir(0, "home");
|
||||
const user = t0.addDir(home, "user");
|
||||
const mnt = t0.addDir(user, "mnt");
|
||||
const fs = await makeFS([t0.build(), makeTree().build()]);
|
||||
await fs.mount(toPtr({ device_id: 0, inode: mnt }), toPtr({ device_id: 1, inode: 0 }));
|
||||
|
||||
const atMountedRoot = makeShellState(fs, { cwd: toPtr({ device_id: 1, inode: 0 }) });
|
||||
expect(resolvePath(atMountedRoot, "..")).toEqual({ device_id: 0, inode: user });
|
||||
});
|
||||
|
||||
/**
|
||||
* INSIDE-MOUNT `..`. A subdirectory of the mounted device is *not* the
|
||||
* mount root, so `..` from `1:sub` must stay inside device 1 (regular
|
||||
* reverse-index walk), not exit the mount. Catches a fix that exits the
|
||||
* mount on every `..` inside the mounted device instead of only at its
|
||||
* root.
|
||||
*/
|
||||
it("'..' from inside a mount (not at its root) stays inside the mount", async () => {
|
||||
const t0 = makeTree();
|
||||
const mnt = t0.addDir(0, "mnt");
|
||||
const t1 = makeTree();
|
||||
const sub = t1.addDir(0, "sub");
|
||||
const fs = await makeFS([t0.build(), t1.build()]);
|
||||
await fs.mount(toPtr({ device_id: 0, inode: mnt }), toPtr({ device_id: 1, inode: 0 }));
|
||||
|
||||
const atSub = makeShellState(fs, { cwd: toPtr({ device_id: 1, inode: sub }) });
|
||||
expect(resolvePath(atSub, "..")).toEqual({ device_id: 1, inode: 0 });
|
||||
});
|
||||
|
||||
/**
|
||||
* MULTI-STEP `..`. From `/mnt/sub/leaf`, three consecutive `..` should
|
||||
* produce `/mnt/sub` → `/mnt` → `/`. Exercises both the in-mount walk and
|
||||
* the final mount exit in one flow. Catches a fix that only handles the
|
||||
* "already at mount root" case but breaks when coming from deeper.
|
||||
*/
|
||||
it("multi-step '..' walks from deep inside a mount out to the parent device", async () => {
|
||||
const t0 = makeTree();
|
||||
const mnt = t0.addDir(0, "mnt");
|
||||
const t1 = makeTree();
|
||||
const sub = t1.addDir(0, "sub");
|
||||
const leaf = t1.addFile(sub, "leaf");
|
||||
const fs = await makeFS([t0.build(), t1.build()]);
|
||||
await fs.mount(toPtr({ device_id: 0, inode: mnt }), toPtr({ device_id: 1, inode: 0 }));
|
||||
|
||||
// Sanity: full descent lands at `1:leaf`
|
||||
expect(resolvePath(makeShellState(fs), "/mnt/sub/leaf")).toEqual({ device_id: 1, inode: leaf });
|
||||
|
||||
// One `..` up: /mnt/sub
|
||||
const atLeaf = makeShellState(fs, { cwd: toPtr({ device_id: 1, inode: leaf }) });
|
||||
expect(resolvePath(atLeaf, "..")).toEqual({ device_id: 1, inode: sub });
|
||||
|
||||
// Another `..`: /mnt's content = dev1 root
|
||||
const atSub = makeShellState(fs, { cwd: toPtr({ device_id: 1, inode: sub }) });
|
||||
expect(resolvePath(atSub, "..")).toEqual({ device_id: 1, inode: 0 });
|
||||
|
||||
// Final `..`: exit the mount → /
|
||||
const atMountedRoot = makeShellState(fs, { cwd: toPtr({ device_id: 1, inode: 0 }) });
|
||||
expect(resolvePath(atMountedRoot, "..")).toEqual({ device_id: 0, inode: 0 });
|
||||
});
|
||||
|
||||
/**
|
||||
* INDEPENDENT MOUNTS AT DIFFERENT DEPTHS. Mount A at `/a` (dev0 → dev1)
|
||||
* and Mount B at `/b/nested` (dev0 → dev2). `..` from dev1's root must go
|
||||
* to `/` (parent of `/a`), while `..` from dev2's root must go to `/b`
|
||||
* (parent of `/b/nested`).
|
||||
*
|
||||
* Catches a fix that collapses the reverse index to a single entry — e.g.,
|
||||
* if `getMountSource` returns the *first* mount's mountpoint for any
|
||||
* target, or if `mount()` overwrites the wrong entry.
|
||||
*/
|
||||
it("'..' from different mounts returns to each mountpoint's own parent", async () => {
|
||||
const t0 = makeTree();
|
||||
const a = t0.addDir(0, "a");
|
||||
const b = t0.addDir(0, "b");
|
||||
const nested = t0.addDir(b, "nested");
|
||||
|
||||
const fs = await makeFS([t0.build(), makeTree().build(), makeTree().build()]);
|
||||
await fs.mount(toPtr({ device_id: 0, inode: a }), toPtr({ device_id: 1, inode: 0 })); // /a → dev1
|
||||
await fs.mount(toPtr({ device_id: 0, inode: nested }), toPtr({ device_id: 2, inode: 0 })); // /b/nested → dev2
|
||||
|
||||
// `..` from dev1's root → parent of `/a` = `/`
|
||||
const atDev1Root = makeShellState(fs, { cwd: toPtr({ device_id: 1, inode: 0 }) });
|
||||
expect(resolvePath(atDev1Root, "..")).toEqual({ device_id: 0, inode: 0 });
|
||||
|
||||
// `..` from dev2's root → parent of `/b/nested` = `/b`
|
||||
const atDev2Root = makeShellState(fs, { cwd: toPtr({ device_id: 2, inode: 0 }) });
|
||||
expect(resolvePath(atDev2Root, "..")).toEqual({ device_id: 0, inode: b });
|
||||
});
|
||||
|
||||
/**
|
||||
* MOUNT NESTED IN A MOUNTED DEVICE'S TREE. The most realistic cross-device
|
||||
* scenario:
|
||||
*
|
||||
* dev0: /mnt (0:1)
|
||||
* dev1: / (1:0)
|
||||
* /b (1:2) ← mountpoint of B
|
||||
* dev2: / (2:0)
|
||||
*
|
||||
* Mount A: 0:1 → 1:0 (/mnt shows dev1)
|
||||
* Mount B: 1:2 → 2:0 (/mnt/b shows dev2)
|
||||
*
|
||||
* From `/mnt/b` (dev2 root), `..` must exit B's mount to `/mnt/b`'s parent
|
||||
* in the *mounted* device 1 (`1:0`), not skip straight to device 0. A
|
||||
* second `..` then exits A's mount to `/`.
|
||||
*
|
||||
* This is the true "chained mount exit" case. Each `..` handles exactly
|
||||
* one mount; the reverse index for `1:2` (B's mountpoint) lands on `1:0`
|
||||
* (dev1 root), which is itself a mount target of A. The second `..` then
|
||||
* exits A. Both steps go through the same `getMountSource` branch.
|
||||
*/
|
||||
it("'..' walks out of a mount nested inside another mount's device tree", async () => {
|
||||
const t0 = makeTree();
|
||||
const mnt = t0.addDir(0, "mnt");
|
||||
|
||||
const t1 = makeTree();
|
||||
const b = t1.addDir(0, "b");
|
||||
|
||||
const fs = await makeFS([t0.build(), t1.build(), makeTree().build()]);
|
||||
await fs.mount(toPtr({ device_id: 0, inode: mnt }), toPtr({ device_id: 1, inode: 0 })); // /mnt → dev1
|
||||
await fs.mount(toPtr({ device_id: 1, inode: b }), toPtr({ device_id: 2, inode: 0 })); // /mnt/b → dev2
|
||||
|
||||
// Sanity: /mnt/b resolves to dev2 root
|
||||
expect(resolvePath(makeShellState(fs), "/mnt/b")).toEqual({ device_id: 2, inode: 0 });
|
||||
|
||||
// `..` from dev2 root → exit B's mount → parent of `1:b` = `1:0` (dev1 root)
|
||||
const atDev2Root = makeShellState(fs, { cwd: toPtr({ device_id: 2, inode: 0 }) });
|
||||
expect(resolvePath(atDev2Root, "..")).toEqual({ device_id: 1, inode: 0 });
|
||||
|
||||
// `..` from dev1 root → exit A's mount → parent of `0:mnt` = `/`
|
||||
const atDev1Root = makeShellState(fs, { cwd: toPtr({ device_id: 1, inode: 0 }) });
|
||||
expect(resolvePath(atDev1Root, "..")).toEqual({ device_id: 0, inode: 0 });
|
||||
});
|
||||
|
||||
/**
|
||||
* ROUND TRIP. `cd /mnt`, `cd ..`, `cd mnt` should land back at `{1:0}`.
|
||||
* Catches a fix that mutates mount state (e.g., a stale `currentDeviceId`
|
||||
* that leaks across calls) or that resolves `/mnt` differently after a
|
||||
* `..` exit.
|
||||
*/
|
||||
it("entering and exiting a mount via resolvePath round-trips", async () => {
|
||||
const t0 = makeTree();
|
||||
const mnt = t0.addDir(0, "mnt");
|
||||
const fs = await makeFS([t0.build(), makeTree().build()]);
|
||||
await fs.mount(toPtr({ device_id: 0, inode: mnt }), toPtr({ device_id: 1, inode: 0 }));
|
||||
|
||||
// Start at /, enter /mnt
|
||||
const fromRoot = makeShellState(fs);
|
||||
expect(resolvePath(fromRoot, "/mnt")).toEqual({ device_id: 1, inode: 0 });
|
||||
|
||||
// `..` from mounted root → /
|
||||
const atMnt = makeShellState(fs, { cwd: toPtr({ device_id: 1, inode: 0 }) });
|
||||
expect(resolvePath(atMnt, "..")).toEqual({ device_id: 0, inode: 0 });
|
||||
|
||||
// `mnt` again from / → back to mounted root
|
||||
const atRoot = makeShellState(fs, { cwd: toPtr({ device_id: 0, inode: 0 }) });
|
||||
expect(resolvePath(atRoot, "mnt")).toEqual({ device_id: 1, inode: 0 });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,17 +1,23 @@
|
||||
// @core/filesystem/errors.test-util.ts (adjust path to match yours)
|
||||
import type {
|
||||
AlreadyExistsError,
|
||||
DeviceNotFoundError,
|
||||
DirectoryLoopError,
|
||||
FileSystemCorruptionError,
|
||||
FileSystemInitializationError,
|
||||
IllegalMutationError,
|
||||
InvalidNodeNameError,
|
||||
NodeNotFoundError,
|
||||
NotADirectoryError,
|
||||
NotAFileError,
|
||||
} from "@core/filesystem/errors";
|
||||
import type { TreeNodeType } from "@core/filesystem/node";
|
||||
import type { NodeType } from "@core/filesystem/node";
|
||||
import { expect } from "vitest";
|
||||
|
||||
export function expectThrows(fn: () => unknown, expectedShape: Record<string, unknown>) {
|
||||
export async function expectThrows(fn: () => unknown, expectedShape: Record<string, unknown>) {
|
||||
let thrown: unknown;
|
||||
try {
|
||||
fn();
|
||||
await fn();
|
||||
} catch (e) {
|
||||
thrown = e;
|
||||
}
|
||||
@@ -20,19 +26,19 @@ export function expectThrows(fn: () => unknown, expectedShape: Record<string, un
|
||||
expect(thrown).toMatchObject(expectedShape);
|
||||
}
|
||||
|
||||
export function expectNotADirectoryError(fn: () => unknown, overrides?: Partial<NotADirectoryError>) {
|
||||
expectThrows(fn, {
|
||||
export async function expectNotADirectoryError(fn: () => unknown, overrides?: Partial<NotADirectoryError>) {
|
||||
return await expectThrows(fn, {
|
||||
type: "NotADirectory",
|
||||
inode: expect.any(Number) as number,
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
nodeType: expect.any(Number) as TreeNodeType,
|
||||
nodeType: expect.any(Number) as NodeType,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export function expectAlreadyExistsError(fn: () => unknown, overrides?: Partial<AlreadyExistsError>) {
|
||||
expectThrows(fn, {
|
||||
export async function expectAlreadyExistsError(fn: () => unknown, overrides?: Partial<AlreadyExistsError>) {
|
||||
return await expectThrows(fn, {
|
||||
type: "AlreadyExists",
|
||||
parentInode: expect.any(Number) as number,
|
||||
name: expect.any(String) as string,
|
||||
@@ -42,18 +48,23 @@ export function expectAlreadyExistsError(fn: () => unknown, overrides?: Partial<
|
||||
});
|
||||
}
|
||||
|
||||
export function expectNodeNotFoundError(fn: () => unknown, overrides?: Partial<NodeNotFoundError>) {
|
||||
expectThrows(fn, {
|
||||
/**
|
||||
* FIXED: `NodeNotFoundError` has `pointer: NodePointer` (a string), not `inode: number`.
|
||||
* The previous version asserted `inode: expect.any(Number)` and would have failed
|
||||
* against every real thrown `NodeNotFoundError`.
|
||||
*/
|
||||
export async function expectNodeNotFoundError(fn: () => unknown, overrides?: Partial<NodeNotFoundError>) {
|
||||
return await expectThrows(fn, {
|
||||
type: "NodeNotFound",
|
||||
inode: expect.any(Number) as number,
|
||||
pointer: expect.any(String) as string,
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export function expectIllegalMutationError(fn: () => unknown, overrides?: Partial<IllegalMutationError>) {
|
||||
expectThrows(fn, {
|
||||
export async function expectIllegalMutationError(fn: () => unknown, overrides?: Partial<IllegalMutationError>) {
|
||||
return await expectThrows(fn, {
|
||||
type: "IllegalMutation",
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
@@ -61,8 +72,8 @@ export function expectIllegalMutationError(fn: () => unknown, overrides?: Partia
|
||||
});
|
||||
}
|
||||
|
||||
export function expectDirectoryLoopError(fn: () => void, overrides?: Partial<DirectoryLoopError>) {
|
||||
expectThrows(fn, {
|
||||
export async function expectDirectoryLoopError(fn: () => void, overrides?: Partial<DirectoryLoopError>) {
|
||||
return await expectThrows(fn, {
|
||||
type: "DirectoryLoop",
|
||||
descendantInode: expect.any(Number) as number,
|
||||
ancestorInode: expect.any(Number) as number,
|
||||
@@ -71,3 +82,61 @@ export function expectDirectoryLoopError(fn: () => void, overrides?: Partial<Dir
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
// ---- NEW helpers below ----
|
||||
|
||||
export async function expectDeviceNotFoundError(fn: () => unknown, overrides?: Partial<DeviceNotFoundError>) {
|
||||
return await expectThrows(fn, {
|
||||
type: "DeviceNotFound",
|
||||
device_id: expect.any(Number) as number,
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export async function expectNotAFileError(fn: () => unknown, overrides?: Partial<NotAFileError>) {
|
||||
return await expectThrows(fn, {
|
||||
type: "NotAFile",
|
||||
inode: expect.any(Number) as number,
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
nodeType: expect.any(Number) as NodeType,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export async function expectFileSystemInitializationError(
|
||||
fn: () => unknown,
|
||||
overrides?: Partial<FileSystemInitializationError>,
|
||||
) {
|
||||
return await expectThrows(fn, {
|
||||
type: "FileSystemInitializationError",
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export async function expectInvalidNodeNameError(fn: () => unknown, overrides?: Partial<InvalidNodeNameError>) {
|
||||
return await expectThrows(fn, {
|
||||
type: "InvalidNodeName",
|
||||
pointer: expect.any(String) as string,
|
||||
attemptedName: expect.any(String) as string,
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
export async function expectFileSystemCorruptionError(
|
||||
fn: () => unknown,
|
||||
overrides?: Partial<FileSystemCorruptionError>,
|
||||
) {
|
||||
return await expectThrows(fn, {
|
||||
type: "FileSystemCorruption",
|
||||
message: expect.any(String) as string,
|
||||
timestamp: expect.any(Number) as number,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,91 +0,0 @@
|
||||
import { FileSystem } from "@core/filesystem/filesystem";
|
||||
import type { TreeNode } from "@core/filesystem/node";
|
||||
import type { GroupInfo, UserInfo } from "@core/session_manager";
|
||||
import { createInitialShellState, type ShellState } from "@core/shell_state";
|
||||
import { deserializeFileSystem } from "@shell/serializer";
|
||||
import { buildTestFS } from "./filesystems_fixture";
|
||||
import { depth7Tree } from "./tree_fixture";
|
||||
|
||||
export class Fixture {
|
||||
/*
|
||||
root (0)
|
||||
├── folder1 (1)
|
||||
│ └── subdir1 (2)
|
||||
├── folder2 (3)
|
||||
│ ├── subdir2 (4)
|
||||
│ └── subdir3 (5)
|
||||
│ └── empty-foo (6)
|
||||
└── some-file (7)
|
||||
*/
|
||||
|
||||
static rawJson = `{"atime": 1786341244,"children": [{"atime": 1786341264,"children": [{"atime": 1786341330,"children": [],"content": null,"ctime": 1786341330,"group": 1000,"inode": 2,"mode": 493,"mtime": 1786341330,"name": "subdir1","owner": 1000,"parent": 1,"type": "directory"}],"content": null,"ctime": 1786341330,"group": 1000,"inode": 1,"mode": 493,"mtime": 1786341330,"name": "folder1","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1786341269,"children": [{"atime": 1786341344,"children": [],"content": null,"ctime": 1786341344,"group": 1000,"inode": 4,"mode": 493,"mtime": 1786341344,"name": "subdir2","owner": 1000,"parent": 3,"type": "directory"},{"atime": 1786341345,"children": [{"atime": 1786341356,"children": [],"content": [],"ctime": 1786341356,"group": 1000,"inode": 6,"mode": 420,"mtime": 1786341356,"name": "empty-foo","owner": 1000,"parent": 5,"type": "regular"}],"content": null,"ctime": 1786341356,"group": 1000,"inode": 5,"mode": 493,"mtime": 1786341356,"name": "subdir3","owner": 1000,"parent": 3,"type": "directory"}],"content": null,"ctime": 1786341345,"group": 1000,"inode": 3,"mode": 493,"mtime": 1786341345,"name": "folder2","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1786341312,"children": [],"content": [72,101,108,108,111,44,32,87,111,114,108,100,33,10],"ctime": 1786341312,"group": 1000,"inode": 7,"mode": 420,"mtime": 1786341312,"name": "some-file","owner": 1000,"parent": 0,"type": "regular"}],"content": null,"ctime": 1786341312,"group": 1000,"inode": 0,"mode": 493,"mtime": 1786341312,"name": "root","owner": 1000,"parent": 0,"type": "directory"}`;
|
||||
static user: UserInfo = {
|
||||
username: "root",
|
||||
uid: 0,
|
||||
gid: 0, // <- PRIMARY GID
|
||||
group: [0], // <- GROUPS ARRAY
|
||||
home: 0,
|
||||
};
|
||||
static user2: UserInfo = {
|
||||
username: "user",
|
||||
uid: 1000,
|
||||
gid: 1000,
|
||||
group: [1000],
|
||||
home: 1,
|
||||
};
|
||||
|
||||
static nopass: UserInfo = {
|
||||
username: "no pass",
|
||||
uid: 1001,
|
||||
gid: 1000,
|
||||
group: [1000],
|
||||
home: 2,
|
||||
};
|
||||
|
||||
static group: GroupInfo = {
|
||||
groupname: "root",
|
||||
gid: 0,
|
||||
members: [0], // <- UID
|
||||
};
|
||||
static group2: GroupInfo = {
|
||||
groupname: "user",
|
||||
gid: 1000,
|
||||
members: [1000, 1001], // <- UID
|
||||
};
|
||||
|
||||
public static get State(): ShellState {
|
||||
const rawJson = `{"atime": 1786341244,"children": [{"atime": 1786341264,"children": [{"atime": 1786341330,"children": [],"content": null,"ctime": 1786341330,"group": 1000,"inode": 2,"mode": 493,"mtime": 1786341330,"name": "subdir1","owner": 1000,"parent": 1,"type": "directory"}],"content": null,"ctime": 1786341330,"group": 1000,"inode": 1,"mode": 493,"mtime": 1786341330,"name": "folder1","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1786341269,"children": [{"atime": 1786341344,"children": [],"content": null,"ctime": 1786341344,"group": 1000,"inode": 4,"mode": 493,"mtime": 1786341344,"name": "subdir2","owner": 1000,"parent": 3,"type": "directory"},{"atime": 1786341345,"children": [{"atime": 1786341356,"children": [],"content": [],"ctime": 1786341356,"group": 1000,"inode": 6,"mode": 420,"mtime": 1786341356,"name": "empty-foo","owner": 1000,"parent": 5,"type": "regular"}],"content": null,"ctime": 1786341356,"group": 1000,"inode": 5,"mode": 493,"mtime": 1786341356,"name": "subdir3","owner": 1000,"parent": 3,"type": "directory"}],"content": null,"ctime": 1786341345,"group": 1000,"inode": 3,"mode": 493,"mtime": 1786341345,"name": "folder2","owner": 1000,"parent": 0,"type": "directory"},{"atime": 1786341312,"children": [],"content": [72,101,108,108,111,44,32,87,111,114,108,100,33,10],"ctime": 1786341312,"group": 1000,"inode": 7,"mode": 420,"mtime": 1786341312,"name": "some-file","owner": 1000,"parent": 0,"type": "regular"}],"content": null,"ctime": 1786341312,"group": 1000,"inode": 0,"mode": 493,"mtime": 1786341312,"name": "root","owner": 1000,"parent": 0,"type": "directory"}`;
|
||||
const base = deserializeFileSystem(rawJson);
|
||||
|
||||
const overlay: ReadonlyMap<number, TreeNode | null> = new Map();
|
||||
|
||||
const fs = FileSystem.init(base, overlay, base.size - 1);
|
||||
|
||||
const users: Map<number, UserInfo> = new Map();
|
||||
const groups: Map<number, GroupInfo> = new Map();
|
||||
|
||||
users.set(0, Fixture.user);
|
||||
users.set(1000, Fixture.user2);
|
||||
users.set(1001, Fixture.nopass);
|
||||
groups.set(0, Fixture.group);
|
||||
groups.set(1000, Fixture.group2);
|
||||
|
||||
return createInitialShellState(fs, users, groups, Fixture.user);
|
||||
}
|
||||
}
|
||||
|
||||
export class Fixture7Depth extends Fixture {
|
||||
public static get State(): ShellState {
|
||||
const fs = buildTestFS(depth7Tree);
|
||||
|
||||
const users: Map<number, UserInfo> = new Map();
|
||||
const groups: Map<number, GroupInfo> = new Map();
|
||||
|
||||
users.set(0, Fixture7Depth.user);
|
||||
users.set(1000, Fixture7Depth.user2);
|
||||
groups.set(0, Fixture7Depth.group);
|
||||
groups.set(1000, Fixture7Depth.group2);
|
||||
|
||||
return createInitialShellState(fs, users, groups, Fixture7Depth.user);
|
||||
}
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
import type { FixtureNode } from "./filesystems_fixture";
|
||||
|
||||
export const depth7Tree: FixtureNode = {
|
||||
inode: 0,
|
||||
name: "root",
|
||||
type: "Dir",
|
||||
children: [
|
||||
{
|
||||
inode: 1,
|
||||
name: "d1",
|
||||
type: "Dir",
|
||||
children: [
|
||||
{
|
||||
inode: 2,
|
||||
name: "d2",
|
||||
type: "Dir",
|
||||
children: [
|
||||
{
|
||||
inode: 3,
|
||||
name: "d3",
|
||||
type: "Dir",
|
||||
children: [
|
||||
{
|
||||
inode: 4,
|
||||
name: "d4",
|
||||
type: "Dir",
|
||||
children: [
|
||||
{
|
||||
inode: 5,
|
||||
name: "d5",
|
||||
type: "Dir",
|
||||
children: [
|
||||
{
|
||||
inode: 6,
|
||||
name: "d6",
|
||||
type: "Dir",
|
||||
children: [
|
||||
{
|
||||
inode: 7,
|
||||
name: "deep_file.txt",
|
||||
type: "File",
|
||||
content: "Deepest node at depth 7",
|
||||
children: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
Reference in New Issue
Block a user