- Cleaned up old tests

- Changed `node:buffer` and `node:streams` imports to native `WebWorkers` imports;
- Fixed tests that used old `toPtr` and `fromPtr` functions to use the new NodePointer class methods
- Exposed pointer resolution helper functions to the consumer
This commit is contained in:
2026-11-20 12:22:23 +01:00
parent 2cf84ccd2f
commit d825299e26
20 changed files with 152 additions and 738 deletions
+85 -154
View File
@@ -1,14 +1,12 @@
// @core/filesystem/filesystem.test.ts
import { FileSystem, type FileSystemConfig } from "@core/filesystem/filesystem";
import {
fromPtr,
NodePointer,
NodeType,
toPtr,
type DirectoryNode,
type FileNode,
type Node,
type NodeEntry,
type NodePointer,
type SymlinkNode,
} from "@core/filesystem/node";
import { DefaultVDiskConfig, VDisk, type IDataStore, type IMetaStore } from "@core/filesystem/vdisk";
@@ -25,10 +23,8 @@ import {
expectNotADirectoryError,
expectNotAFileError,
} from "@tests/helpers/error_matchers";
import fc from "fast-check";
import { Blob } from "node:buffer";
import { Readable } from "node:stream";
import { afterEach, describe, expect, it, vi } from "vitest";
("");
// ============================================================================
// Fixtures
@@ -89,7 +85,7 @@ function makeUser(overrides: Partial<UserInfo> = {}): UserInfo {
uid: 1000,
gid: 1000,
group: [],
home: 0,
home: NodePointer.parse("0:0"),
...overrides,
};
}
@@ -128,7 +124,7 @@ function buildReverseIndex(nodes: Map<number, Node>, deviceId: number): Map<Node
const [inode, pinode, name] = stack.pop()!;
const node = nodes.get(inode);
if (!node) continue;
const key = toPtr({ device_id: deviceId, inode });
const key = NodePointer.of(deviceId, inode);
const entry: NodeEntry = { pinode, pdevice_id: deviceId, name };
const arr = index.get(key);
if (arr) arr.push(entry);
@@ -161,7 +157,7 @@ function makeFakeDisk(id: number): FakeDisk {
unlinkNodeEntry: vi.fn(async () => undefined),
moveNodeEntry: vi.fn(async () => undefined),
writeContent: vi.fn(async () => undefined),
streamContent: vi.fn(() => Readable.from([Buffer.from("data")])),
streamContent: vi.fn(() => new Blob(["data"]).stream()),
writeNodeEntry: vi.fn(async () => undefined),
};
return disk;
@@ -214,7 +210,7 @@ async function makeFS(
vi.spyOn(VDisk, "open").mockImplementation(async config => disks.get(config.id) as unknown as VDisk);
const fs = FileSystem.init(vdiskConfigs, {
const fs = await FileSystem.init(vdiskConfigs, {
maxNameLength: 255,
maxPathLength: Infinity,
maxSymlinkDepth: 40,
@@ -306,9 +302,9 @@ describe("FileSystem.init", () => {
* Fix is non-trivial (making `init` async changes the public API), so this
* documents the current behavior. When patched, this test should be deleted.
*/
it("BUG: init returns before registerDisk resolves — no disk is available right after init", () => {
it("BUG: init returns before registerDisk resolves — no disk is available right after init", async () => {
vi.spyOn(VDisk, "open").mockResolvedValue({} as VDisk);
const fs = FileSystem.init([
const fs = await FileSystem.init([
{ dstore: {} as IDataStore, mstore: {} as IMetaStore, config: DefaultVDiskConfig(0) },
]);
expect(() => fs.getRoot()).toThrow(/system device not found/);
@@ -342,7 +338,7 @@ describe("FileSystem.init", () => {
const { fs } = await makeFS();
const longName = "a".repeat(256);
expectInvalidNodeNameError(() =>
fs.createNode(toPtr({ device_id: 0, inode: 0 }), longName, {
fs.createNode(NodePointer.of(0, 0), longName, {
type: NodeType.Directory,
mode: 0o755,
owner: 0,
@@ -357,7 +353,7 @@ describe("FileSystem.init", () => {
it("respects a custom maxNameLength", async () => {
const { fs } = await makeFS({ config: { maxNameLength: 5 } });
expectInvalidNodeNameError(() =>
fs.createNode(toPtr({ device_id: 0, inode: 0 }), "abcdef", {
fs.createNode(NodePointer.of(0, 0), "abcdef", {
type: NodeType.Directory,
mode: 0o755,
owner: 0,
@@ -375,7 +371,7 @@ describe("FileSystem.init", () => {
const { fs } = await makeFS({ config: { maxPathLength: 1, maxSymlinkDepth: 0 } });
// No API path exercises these limits today; the calls below succeed regardless.
expect(() =>
fs.createNode(toPtr({ device_id: 0, inode: 0 }), "a-very-long-name", {
fs.createNode(NodePointer.of(0, 0), "a-very-long-name", {
type: NodeType.Directory,
mode: 0o755,
owner: 0,
@@ -436,7 +432,7 @@ describe("FileSystem.getNode", () => {
const { fs } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "f", makeFileNode({ inode: 1 })) }],
});
expect(fs.getNode(toPtr({ device_id: 0, inode: 1 }))).toBeDefined();
expect(fs.getNode(NodePointer.of(0, 1))).toBeDefined();
});
/**
@@ -444,7 +440,7 @@ describe("FileSystem.getNode", () => {
*/
it("returns undefined when the inode is missing on an existing device", async () => {
const { fs } = await makeFS();
expect(fs.getNode(toPtr({ device_id: 0, inode: 99 }))).toBeUndefined();
expect(fs.getNode(NodePointer.of(0, 99))).toBeUndefined();
});
/**
@@ -452,7 +448,7 @@ describe("FileSystem.getNode", () => {
*/
it("returns undefined when the device does not exist", async () => {
const { fs } = await makeFS();
expect(fs.getNode(toPtr({ device_id: 99, inode: 0 }))).toBeUndefined();
expect(fs.getNode(NodePointer.of(99, 0))).toBeUndefined();
});
});
@@ -464,9 +460,9 @@ describe("FileSystem.exists", () => {
const { fs } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "f", makeFileNode({ inode: 1 })) }],
});
expect(fs.exists(toPtr({ device_id: 0, inode: 1 }))).toBe(true);
expect(fs.exists(toPtr({ device_id: 0, inode: 42 }))).toBe(false);
expect(fs.exists(toPtr({ device_id: 99, inode: 0 }))).toBe(false);
expect(fs.exists(NodePointer.of(0, 1))).toBe(true);
expect(fs.exists(NodePointer.of(0, 42))).toBe(false);
expect(fs.exists(NodePointer.of(99, 0))).toBe(false);
});
});
@@ -482,7 +478,7 @@ describe("FileSystem.getNodeEntries", () => {
const { fs } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "a", makeFileNode({ inode: 1 })) }],
});
const entries = fs.getNodeEntries(toPtr({ device_id: 0, inode: 1 }));
const entries = fs.getNodeEntries(NodePointer.of(0, 1));
expect(entries).toHaveLength(1);
expect(entries![0]).toMatchObject({ pinode: 0, pdevice_id: 0, name: "a" });
});
@@ -492,7 +488,7 @@ describe("FileSystem.getNodeEntries", () => {
*/
it("returns undefined when the pointer is not in the index", async () => {
const { fs } = await makeFS();
expect(fs.getNodeEntries(toPtr({ device_id: 0, inode: 99 }))).toBeUndefined();
expect(fs.getNodeEntries(NodePointer.of(0, 99))).toBeUndefined();
});
/**
@@ -502,7 +498,7 @@ describe("FileSystem.getNodeEntries", () => {
*/
it("should throw DeviceNotFoundError when the device is missing", async () => {
const { fs } = await makeFS();
await expectDeviceNotFoundError(() => fs.getNodeEntries(toPtr({ device_id: 99, inode: 0 })));
await expectDeviceNotFoundError(() => fs.getNodeEntries(NodePointer.of(99, 0)));
});
});
@@ -527,9 +523,9 @@ describe("FileSystem.isFile / isDirectory / isSymlink", () => {
},
],
});
const f = toPtr({ device_id: 0, inode: 1 });
const d = toPtr({ device_id: 0, inode: 2 });
const l = toPtr({ device_id: 0, inode: 3 });
const f = NodePointer.of(0, 1);
const d = NodePointer.of(0, 2);
const l = NodePointer.of(0, 3);
expect(fs.isFile(f)).toBe(true);
expect(fs.isDirectory(f)).toBe(false);
@@ -549,9 +545,9 @@ describe("FileSystem.isFile / isDirectory / isSymlink", () => {
*/
it("throws NodeNotFoundError when the node does not exist", async () => {
const { fs } = await makeFS();
expectNodeNotFoundError(() => fs.isFile(toPtr({ device_id: 0, inode: 99 })));
expectNodeNotFoundError(() => fs.isDirectory(toPtr({ device_id: 0, inode: 99 })));
expectNodeNotFoundError(() => fs.isSymlink(toPtr({ device_id: 0, inode: 99 })));
expectNodeNotFoundError(() => fs.isFile(NodePointer.of(0, 99)));
expectNodeNotFoundError(() => fs.isDirectory(NodePointer.of(0, 99)));
expectNodeNotFoundError(() => fs.isSymlink(NodePointer.of(0, 99)));
});
/**
@@ -560,9 +556,9 @@ describe("FileSystem.isFile / isDirectory / isSymlink", () => {
*/
it("should throw DeviceNotFoundError when the device is missing", async () => {
const { fs } = await makeFS();
await expectDeviceNotFoundError(() => fs.isFile(toPtr({ device_id: 99, inode: 0 })));
await expectDeviceNotFoundError(() => fs.isDirectory(toPtr({ device_id: 99, inode: 0 })));
await expectDeviceNotFoundError(() => fs.isSymlink(toPtr({ device_id: 99, inode: 0 })));
await expectDeviceNotFoundError(() => fs.isFile(NodePointer.of(99, 0)));
await expectDeviceNotFoundError(() => fs.isDirectory(NodePointer.of(99, 0)));
await expectDeviceNotFoundError(() => fs.isSymlink(NodePointer.of(99, 0)));
});
});
@@ -577,7 +573,7 @@ describe("FileSystem.createNode", () => {
it("creates a file by delegating to the parent device", async () => {
const { fs, disks } = await makeFS();
const data = { type: NodeType.File as const, mode: 0o644, owner: 1, group: 1, content: new Blob(), size: 0 };
fs.createNode(toPtr({ device_id: 0, inode: 0 }), "f", data);
fs.createNode(NodePointer.of(0, 0), "f", data);
expect(disks.get(0)!.createNodeEntry).toHaveBeenCalledWith(0, "f", data);
});
@@ -587,7 +583,7 @@ describe("FileSystem.createNode", () => {
it("creates a directory", async () => {
const { fs, disks } = await makeFS();
const data = { type: NodeType.Directory as const, mode: 0o755, owner: 1, group: 1 };
fs.createNode(toPtr({ device_id: 0, inode: 0 }), "d", data);
fs.createNode(NodePointer.of(0, 0), "d", data);
expect(disks.get(0)!.createNodeEntry).toHaveBeenCalledWith(0, "d", data);
});
@@ -597,7 +593,7 @@ describe("FileSystem.createNode", () => {
it("creates a symlink", async () => {
const { fs, disks } = await makeFS();
const data = { type: NodeType.Symlink as const, mode: 0o777, owner: 1, group: 1, target: "/x" };
fs.createNode(toPtr({ device_id: 0, inode: 0 }), "l", data);
fs.createNode(NodePointer.of(0, 0), "l", data);
expect(disks.get(0)!.createNodeEntry).toHaveBeenCalledWith(0, "l", data);
});
@@ -607,7 +603,7 @@ describe("FileSystem.createNode", () => {
it("throws DeviceNotFoundError when the parent's device is missing", async () => {
const { fs } = await makeFS();
await expectDeviceNotFoundError(() =>
fs.createNode(toPtr({ device_id: 99, inode: 0 }), "x", {
fs.createNode(NodePointer.of(99, 0), "x", {
type: NodeType.Directory,
mode: 0o755,
owner: 0,
@@ -622,7 +618,7 @@ describe("FileSystem.createNode", () => {
it("throws NodeNotFoundError when the parent inode does not exist", async () => {
const { fs } = await makeFS();
expectNodeNotFoundError(() =>
fs.createNode(toPtr({ device_id: 0, inode: 99 }), "x", {
fs.createNode(NodePointer.of(0, 99), "x", {
type: NodeType.Directory,
mode: 0o755,
owner: 0,
@@ -639,7 +635,7 @@ describe("FileSystem.createNode", () => {
devices: [{ id: 0, setup: d => addChild(d, 0, "f", makeFileNode({ inode: 1 })) }],
});
expectNotADirectoryError(() =>
fs.createNode(toPtr({ device_id: 0, inode: 1 }), "x", {
fs.createNode(NodePointer.of(0, 1), "x", {
type: NodeType.Directory,
mode: 0o755,
owner: 0,
@@ -656,7 +652,7 @@ describe("FileSystem.createNode", () => {
devices: [{ id: 0, setup: d => addChild(d, 0, "f", makeFileNode({ inode: 1 })) }],
});
expectAlreadyExistsError(() =>
fs.createNode(toPtr({ device_id: 0, inode: 0 }), "f", {
fs.createNode(NodePointer.of(0, 0), "f", {
type: NodeType.Directory,
mode: 0o755,
owner: 0,
@@ -671,7 +667,7 @@ describe("FileSystem.createNode", () => {
it("throws InvalidNodeNameError when the name exceeds maxNameLength", async () => {
const { fs } = await makeFS({ config: { maxNameLength: 3 } });
expectInvalidNodeNameError(() =>
fs.createNode(toPtr({ device_id: 0, inode: 0 }), "four", {
fs.createNode(NodePointer.of(0, 0), "four", {
type: NodeType.Directory,
mode: 0o755,
owner: 0,
@@ -685,7 +681,7 @@ describe("FileSystem.createNode", () => {
*/
it("accepts a name whose length equals maxNameLength exactly", async () => {
const { fs, disks } = await makeFS({ config: { maxNameLength: 4 } });
fs.createNode(toPtr({ device_id: 0, inode: 0 }), "abcd", {
fs.createNode(NodePointer.of(0, 0), "abcd", {
type: NodeType.Directory,
mode: 0o755,
owner: 0,
@@ -707,7 +703,7 @@ describe("FileSystem.deleteNode", () => {
const { fs, disks } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "a", makeFileNode({ inode: 1 })) }],
});
fs.deleteNode(toPtr({ device_id: 0, inode: 1 }));
fs.deleteNode(NodePointer.of(0, 1));
expect(disks.get(0)!.unlinkNodeEntry).toHaveBeenCalledWith(0, "a");
});
@@ -716,7 +712,7 @@ describe("FileSystem.deleteNode", () => {
*/
it("throws IllegalMutationError when deleting device 0's root", async () => {
const { fs } = await makeFS();
expectIllegalMutationError(() => fs.deleteNode(toPtr({ device_id: 0, inode: 0 })));
expectIllegalMutationError(() => fs.deleteNode(NodePointer.of(0, 0)));
});
/**
@@ -724,7 +720,7 @@ describe("FileSystem.deleteNode", () => {
*/
it("throws DeviceNotFoundError when the device does not exist", async () => {
const { fs } = await makeFS();
await expectDeviceNotFoundError(() => fs.deleteNode(toPtr({ device_id: 99, inode: 1 })));
await expectDeviceNotFoundError(() => fs.deleteNode(NodePointer.of(99, 1)));
});
});
@@ -740,7 +736,7 @@ describe("FileSystem.moveNode", () => {
const { fs, disks } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "a", makeFileNode({ inode: 1 })) }],
});
fs.moveNode(toPtr({ device_id: 0, inode: 1 }), toPtr({ device_id: 0, inode: 0 }), "b");
fs.moveNode(NodePointer.of(0, 1), NodePointer.of(0, 0), "b");
expect(disks.get(0)!.moveNodeEntry).toHaveBeenCalledWith(0, 0, "a", "b");
});
@@ -762,7 +758,7 @@ describe("FileSystem.moveNode", () => {
},
],
});
fs.moveNode(toPtr({ device_id: 0, inode: 3 }), toPtr({ device_id: 0, inode: 2 }));
fs.moveNode(NodePointer.of(0, 3), NodePointer.of(0, 2));
expect(disks.get(0)!.moveNodeEntry).toHaveBeenCalledWith(1, 2, "f", undefined);
});
@@ -782,7 +778,7 @@ describe("FileSystem.moveNode", () => {
},
],
});
fs.moveNode(toPtr({ device_id: 0, inode: 3 }), toPtr({ device_id: 0, inode: 2 }), "g");
fs.moveNode(NodePointer.of(0, 3), NodePointer.of(0, 2), "g");
expect(disks.get(0)!.moveNodeEntry).toHaveBeenCalledWith(1, 2, "f", "g");
});
@@ -791,9 +787,7 @@ describe("FileSystem.moveNode", () => {
*/
it("throws DeviceNotFoundError when the source device is missing", async () => {
const { fs } = await makeFS();
await expectDeviceNotFoundError(() =>
fs.moveNode(toPtr({ device_id: 99, inode: 1 }), toPtr({ device_id: 0, inode: 0 })),
);
await expectDeviceNotFoundError(() => fs.moveNode(NodePointer.of(99, 1), NodePointer.of(0, 0)));
});
/**
@@ -803,9 +797,7 @@ describe("FileSystem.moveNode", () => {
const { fs } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "a", makeFileNode({ inode: 1 })) }],
});
await expectDeviceNotFoundError(() =>
fs.moveNode(toPtr({ device_id: 0, inode: 1 }), toPtr({ device_id: 99, inode: 0 })),
);
await expectDeviceNotFoundError(() => fs.moveNode(NodePointer.of(0, 1), NodePointer.of(99, 0)));
});
/**
@@ -813,9 +805,7 @@ describe("FileSystem.moveNode", () => {
*/
it("throws NodeNotFoundError when the source node is missing", async () => {
const { fs } = await makeFS();
expectNodeNotFoundError(() =>
fs.moveNode(toPtr({ device_id: 0, inode: 99 }), toPtr({ device_id: 0, inode: 0 })),
);
expectNodeNotFoundError(() => fs.moveNode(NodePointer.of(0, 99), NodePointer.of(0, 0)));
});
/**
@@ -825,9 +815,7 @@ describe("FileSystem.moveNode", () => {
const { fs } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "a", makeDirectoryNode({ inode: 1 })) }],
});
expectIllegalMutationError(() =>
fs.moveNode(toPtr({ device_id: 0, inode: 0 }), toPtr({ device_id: 0, inode: 1 })),
);
expectIllegalMutationError(() => fs.moveNode(NodePointer.of(0, 0), NodePointer.of(0, 1)));
});
/**
@@ -837,9 +825,7 @@ describe("FileSystem.moveNode", () => {
const { fs } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "a", makeFileNode({ inode: 1 })) }],
});
expectNodeNotFoundError(() =>
fs.moveNode(toPtr({ device_id: 0, inode: 1 }), toPtr({ device_id: 0, inode: 99 })),
);
expectNodeNotFoundError(() => fs.moveNode(NodePointer.of(0, 1), NodePointer.of(0, 99)));
});
/**
@@ -857,9 +843,7 @@ describe("FileSystem.moveNode", () => {
},
],
});
expectNotADirectoryError(() =>
fs.moveNode(toPtr({ device_id: 0, inode: 1 }), toPtr({ device_id: 0, inode: 2 })),
);
expectNotADirectoryError(() => fs.moveNode(NodePointer.of(0, 1), NodePointer.of(0, 2)));
});
/**
@@ -879,9 +863,7 @@ describe("FileSystem.moveNode", () => {
},
],
});
expectAlreadyExistsError(() =>
fs.moveNode(toPtr({ device_id: 0, inode: 3 }), toPtr({ device_id: 0, inode: 2 }), "f"),
);
expectAlreadyExistsError(() => fs.moveNode(NodePointer.of(0, 3), NodePointer.of(0, 2), "f"));
});
/**
@@ -901,9 +883,7 @@ describe("FileSystem.moveNode", () => {
],
config: { maxNameLength: 2 },
});
expectInvalidNodeNameError(() =>
fs.moveNode(toPtr({ device_id: 0, inode: 3 }), toPtr({ device_id: 0, inode: 2 }), "abc"),
);
expectInvalidNodeNameError(() => fs.moveNode(NodePointer.of(0, 3), NodePointer.of(0, 2), "abc"));
});
/**
@@ -921,9 +901,7 @@ describe("FileSystem.moveNode", () => {
},
],
});
expectDirectoryLoopError(() =>
fs.moveNode(toPtr({ device_id: 0, inode: 1 }), toPtr({ device_id: 0, inode: 2 })),
);
expectDirectoryLoopError(() => fs.moveNode(NodePointer.of(0, 1), NodePointer.of(0, 2)));
});
/**
@@ -933,9 +911,7 @@ describe("FileSystem.moveNode", () => {
const { fs } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "a", makeDirectoryNode({ inode: 1 })) }],
});
expectDirectoryLoopError(() =>
fs.moveNode(toPtr({ device_id: 0, inode: 1 }), toPtr({ device_id: 0, inode: 1 })),
);
expectDirectoryLoopError(() => fs.moveNode(NodePointer.of(0, 1), NodePointer.of(0, 1)));
});
/**
@@ -948,9 +924,7 @@ describe("FileSystem.moveNode", () => {
const { fs } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "a", makeDirectoryNode({ inode: 1 })) }],
});
expectAlreadyExistsError(() =>
fs.moveNode(toPtr({ device_id: 0, inode: 1 }), toPtr({ device_id: 0, inode: 0 }), "a"),
);
expectAlreadyExistsError(() => fs.moveNode(NodePointer.of(0, 1), NodePointer.of(0, 0), "a"));
});
/**
@@ -962,7 +936,7 @@ describe("FileSystem.moveNode", () => {
devices: [{ id: 0, setup: d => addChild(d, 0, "a", makeFileNode({ inode: 1 })) }, { id: 1 }],
});
const log = vi.spyOn(console, "error").mockImplementation(() => {});
fs.moveNode(toPtr({ device_id: 0, inode: 1 }), toPtr({ device_id: 1, inode: 0 }));
fs.moveNode(NodePointer.of(0, 1), NodePointer.of(1, 0));
expect(log).toHaveBeenCalledWith(expect.stringMatching(/cross device/i));
expect(disks.get(0)!.moveNodeEntry).not.toHaveBeenCalled();
expect(disks.get(1)!.moveNodeEntry).not.toHaveBeenCalled();
@@ -987,7 +961,7 @@ describe("FileSystem.writeContent", () => {
],
});
const blob = new Blob(["hello"]);
fs.writeContent(toPtr({ device_id: 0, inode: 1 }), blob);
fs.writeContent(NodePointer.of(0, 1), blob);
expect(disks.get(0)!.writeContent).toHaveBeenCalledWith("uuid-123", blob);
});
@@ -996,7 +970,7 @@ describe("FileSystem.writeContent", () => {
*/
it("throws DeviceNotFoundError when the device is missing", async () => {
const { fs } = await makeFS();
await expectDeviceNotFoundError(() => fs.writeContent(toPtr({ device_id: 99, inode: 1 }), new Blob()));
await expectDeviceNotFoundError(() => fs.writeContent(NodePointer.of(99, 1), new Blob()));
});
/**
@@ -1004,7 +978,7 @@ describe("FileSystem.writeContent", () => {
*/
it("throws NodeNotFoundError when the node is missing", async () => {
const { fs } = await makeFS();
expectNodeNotFoundError(() => fs.writeContent(toPtr({ device_id: 0, inode: 99 }), new Blob()));
expectNodeNotFoundError(() => fs.writeContent(NodePointer.of(0, 99), new Blob()));
});
/**
@@ -1014,14 +988,14 @@ describe("FileSystem.writeContent", () => {
const { fs } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "d", makeDirectoryNode({ inode: 1 })) }],
});
await expectNotAFileError(() => fs.writeContent(toPtr({ device_id: 0, inode: 1 }), new Blob()));
await expectNotAFileError(() => fs.writeContent(NodePointer.of(0, 1), new Blob()));
});
it("rejects writing content to a symlink with NotAFileError", async () => {
const { fs } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "l", makeSymlinkNode({ inode: 1 })) }],
});
await expectNotAFileError(() => fs.writeContent(toPtr({ device_id: 0, inode: 1 }), new Blob()));
await expectNotAFileError(() => fs.writeContent(NodePointer.of(0, 1), new Blob()));
});
});
@@ -1042,9 +1016,9 @@ describe("FileSystem.streamContent", () => {
},
],
});
const stream = fs.streamContent(toPtr({ device_id: 0, inode: 1 }));
const stream = fs.streamContent(NodePointer.of(0, 1));
expect(disks.get(0)!.streamContent).toHaveBeenCalledWith("uuid-123");
expect(stream).toBeInstanceOf(Readable);
expect(stream).toBeInstanceOf(ReadableStream);
});
/**
@@ -1052,7 +1026,7 @@ describe("FileSystem.streamContent", () => {
*/
it("throws DeviceNotFoundError when the device is missing", async () => {
const { fs } = await makeFS();
await expectDeviceNotFoundError(() => fs.streamContent(toPtr({ device_id: 99, inode: 1 })));
await expectDeviceNotFoundError(() => fs.streamContent(NodePointer.of(99, 1)));
});
/**
@@ -1060,7 +1034,7 @@ describe("FileSystem.streamContent", () => {
*/
it("throws NodeNotFoundError when the node is missing", async () => {
const { fs } = await makeFS();
expectNodeNotFoundError(() => fs.streamContent(toPtr({ device_id: 0, inode: 99 })));
expectNodeNotFoundError(() => fs.streamContent(NodePointer.of(0, 99)));
});
/**
@@ -1070,7 +1044,7 @@ describe("FileSystem.streamContent", () => {
const { fs } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "d", makeDirectoryNode({ inode: 1 })) }],
});
await expectNotAFileError(() => fs.streamContent(toPtr({ device_id: 0, inode: 1 })));
await expectNotAFileError(() => fs.streamContent(NodePointer.of(0, 1)));
});
/**
@@ -1080,7 +1054,7 @@ describe("FileSystem.streamContent", () => {
const { fs } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "l", makeSymlinkNode({ inode: 1 })) }],
});
expectNotAFileError(() => fs.streamContent(toPtr({ device_id: 0, inode: 1 })));
expectNotAFileError(() => fs.streamContent(NodePointer.of(0, 1)));
});
});
@@ -1096,7 +1070,7 @@ describe("FileSystem.modifyNode", () => {
const { fs, disks } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "f", makeFileNode({ inode: 1 })) }],
});
fs.modifyNode(toPtr({ device_id: 0, inode: 1 }), { mode: 0o600, owner: 7, group: 8 });
fs.modifyNode(NodePointer.of(0, 1), { mode: 0o600, owner: 7, group: 8 });
expect(disks.get(0)!.writeNodeEntry).toHaveBeenCalledWith(1, { mode: 0o600, owner: 7, group: 8 });
});
@@ -1105,7 +1079,7 @@ describe("FileSystem.modifyNode", () => {
*/
it("throws DeviceNotFoundError when the device is missing", async () => {
const { fs } = await makeFS();
await expectDeviceNotFoundError(() => fs.modifyNode(toPtr({ device_id: 99, inode: 1 }), { mode: 0 }));
await expectDeviceNotFoundError(() => fs.modifyNode(NodePointer.of(99, 1), { mode: 0 }));
});
/**
@@ -1113,7 +1087,7 @@ describe("FileSystem.modifyNode", () => {
*/
it("throws NodeNotFoundError when the node is missing", async () => {
const { fs } = await makeFS();
expectNodeNotFoundError(() => fs.modifyNode(toPtr({ device_id: 0, inode: 99 }), { mode: 0 }));
expectNodeNotFoundError(() => fs.modifyNode(NodePointer.of(0, 99), { mode: 0 }));
});
/**
@@ -1125,7 +1099,7 @@ describe("FileSystem.modifyNode", () => {
const { fs } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "d", makeDirectoryNode({ inode: 1 })) }],
});
await expectNotAFileError(() => fs.modifyNode(toPtr({ device_id: 0, inode: 1 }), { mode: 0 }));
await expectNotAFileError(() => fs.modifyNode(NodePointer.of(0, 1), { mode: 0 }));
});
/**
@@ -1138,7 +1112,7 @@ describe("FileSystem.modifyNode", () => {
const { fs } = await makeFS({
devices: [{ id: 0, setup: d => addChild(d, 0, "l", makeSymlinkNode({ inode: 1 })) }],
});
await expectNotAFileError(() => fs.modifyNode(toPtr({ device_id: 0, inode: 1 }), { mode: 0o700 }));
await expectNotAFileError(() => fs.modifyNode(NodePointer.of(0, 1), { mode: 0o700 }));
});
});
@@ -1175,9 +1149,9 @@ describe("FileSystem.canRead / canWrite / canExecute", () => {
const root = makeUser({ uid: 0 });
for (const mode of [0, 0o777, 0o400, 0o040, 0o004]) {
disks.get(0)!._nodes.set(1, makeFileNode({ inode: 1, mode }));
expect(fs.canRead(toPtr({ device_id: 0, inode: 1 }), root)).toBe(true);
expect(fs.canWrite(toPtr({ device_id: 0, inode: 1 }), root)).toBe(true);
expect(fs.canExecute(toPtr({ device_id: 0, inode: 1 }), root)).toBe(true);
expect(fs.canRead(NodePointer.of(0, 1), root)).toBe(true);
expect(fs.canWrite(NodePointer.of(0, 1), root)).toBe(true);
expect(fs.canExecute(NodePointer.of(0, 1), root)).toBe(true);
}
});
@@ -1187,37 +1161,9 @@ describe("FileSystem.canRead / canWrite / canExecute", () => {
it("throws NodeNotFoundError when the node is missing", async () => {
const { fs } = await makeFS();
const user = makeUser();
await expectNodeNotFoundError(() => fs.canRead(toPtr({ device_id: 0, inode: 99 }), user));
await expectNodeNotFoundError(() => fs.canWrite(toPtr({ device_id: 0, inode: 99 }), user));
await expectNodeNotFoundError(() => fs.canExecute(toPtr({ device_id: 0, inode: 99 }), user));
});
/**
* Property: for every combination of (mode, owner, group, uid, gid) on a
* fresh file node, the three `can*` functions agree with a reference
* implementation of Unix permission bits. This is a regression guard against
* accidental bit-constant typos in the implementation.
*/
it("agrees with a reference Unix permission implementation for every mode/uid/gid combination", async () => {
const { fs, disks } = await makeFS();
const ptr = toPtr({ device_id: 0, inode: 1 });
fc.assert(
fc.property(
fc.integer({ min: 0, max: 0o777 }),
fc.integer({ min: 0, max: 1000 }),
fc.integer({ min: 0, max: 1000 }),
fc.integer({ min: 0, max: 1000 }),
fc.integer({ min: 0, max: 1000 }),
(mode, owner, group, uid, gid) => {
disks.get(0)!._nodes.set(1, makeFileNode({ inode: 1, mode, owner, group }));
const user = makeUser({ uid, gid });
expect(fs.canRead(ptr, user)).toBe(referenceCan("read", mode, { owner, group }, user));
expect(fs.canWrite(ptr, user)).toBe(referenceCan("write", mode, { owner, group }, user));
expect(fs.canExecute(ptr, user)).toBe(referenceCan("execute", mode, { owner, group }, user));
},
),
);
await expectNodeNotFoundError(() => fs.canRead(NodePointer.of(0, 99), user));
await expectNodeNotFoundError(() => fs.canWrite(NodePointer.of(0, 99), user));
await expectNodeNotFoundError(() => fs.canExecute(NodePointer.of(0, 99), user));
});
/**
@@ -1235,7 +1181,7 @@ describe("FileSystem.canRead / canWrite / canExecute", () => {
const user = makeUser({ uid: 1000, gid: 1000, group: [42, 99] });
// User is not owner, primary gid is 1000, but supplementary group contains 42.
// Correct Unix: `canRead` is true.
expect(fs.canRead(toPtr({ device_id: 0, inode: 1 }), user)).toBe(true);
expect(fs.canRead(NodePointer.of(0, 1), user)).toBe(true);
});
});
@@ -1244,21 +1190,6 @@ describe("FileSystem.canRead / canWrite / canExecute", () => {
// ============================================================================
describe("fromPtr / toPtr", () => {
/**
* Property: every valid pointer round-trips through toPtr → fromPtr.
*/
it("round-trips arbitrary device/inode pairs through toPtr/fromPtr", () => {
fc.assert(
fc.property(
fc.integer({ min: 0, max: 1_000_000 }),
fc.integer({ min: 0, max: 1_000_000 }),
(device_id, inode) => {
expect(fromPtr(toPtr({ device_id, inode }))).toEqual({ device_id, inode });
},
),
);
});
/**
* Invalid formats must throw. (Example-based because the failure mode is
* a specific regex, not a property over generated strings.)
@@ -1266,7 +1197,7 @@ describe("fromPtr / toPtr", () => {
it.each(["", ":", "abc", "1:", ":1", "1.5:2", "-1:2", "1 :2", "1: 2", "1:2abc", "1:2 ", " 1:2"])(
"throws on invalid pointer %j",
bad => {
expect(() => fromPtr(bad)).toThrow(/Invalid node pointer format/);
expect(() => NodePointer.parse(bad)).toThrow(/Invalid node pointer format/);
},
);
});
@@ -1283,7 +1214,7 @@ describe("FileSystem integration", () => {
it("drives the underlying device through a typical file lifecycle", async () => {
const { fs, disks } = await makeFS();
const disk = disks.get(0)!;
const root = toPtr({ device_id: 0, inode: 0 });
const root = NodePointer.of(0, 0);
fs.createNode(root, "hello.txt", {
type: NodeType.File,
@@ -1302,16 +1233,16 @@ describe("FileSystem integration", () => {
// Simulate the device having created the node.
addChild(disk, 0, "hello.txt", makeFileNode({ inode: 1, content: "uuid-1" }));
fs.modifyNode(toPtr({ device_id: 0, inode: 1 }), { mode: 0o600 });
fs.modifyNode(NodePointer.of(0, 1), { mode: 0o600 });
expect(disk.writeNodeEntry).toHaveBeenLastCalledWith(1, { mode: 0o600 });
fs.writeContent(toPtr({ device_id: 0, inode: 1 }), new Blob(["updated"]));
fs.writeContent(NodePointer.of(0, 1), new Blob(["updated"]));
expect(disk.writeContent).toHaveBeenLastCalledWith("uuid-1", expect.any(Blob));
fs.moveNode(toPtr({ device_id: 0, inode: 1 }), root, "renamed.txt");
fs.moveNode(NodePointer.of(0, 1), root, "renamed.txt");
expect(disk.moveNodeEntry).toHaveBeenLastCalledWith(0, 0, "hello.txt", "renamed.txt");
fs.deleteNode(toPtr({ device_id: 0, inode: 1 }));
fs.deleteNode(NodePointer.of(0, 1));
expect(disk.unlinkNodeEntry).toHaveBeenLastCalledWith(0, "hello.txt");
});
});