- 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
+39 -91
View File
@@ -1,9 +1,8 @@
// @tests/core/util/path_resolver.test.ts (adjust)
import { toPtr, type NodePointer } from "@core/filesystem/node";
import { NodePointer } from "@core/filesystem/node";
import { resolvePath } from "@core/util/path_resolver";
import { expectNodeNotFoundError, expectNotADirectoryError } from "@tests/helpers/error_matchers";
import { makeFS, makeShellState, makeTree, makeUser } from "@tests/helpers/filesystems_fixture";
import fc from "fast-check";
import { describe, expect, it } from "vitest";
// ============================================================================
@@ -37,34 +36,9 @@ describe("resolvePath — path normalization", () => {
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 }) });
const state = makeShellState(fs, { cwd: NodePointer.of(0, work) });
expect(resolvePath(state, "")).toEqual({ device_id: 0, inode: work });
});
/**
* 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");
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);
},
),
);
});
});
// ============================================================================
@@ -174,7 +148,7 @@ describe("resolvePath — relative paths", () => {
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 }) });
const state = makeShellState(fs, { cwd: NodePointer.of(0, work) });
expect(resolvePath(state, "file")).toEqual({ device_id: 0, inode: file });
});
@@ -187,7 +161,7 @@ describe("resolvePath — relative paths", () => {
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 }) });
const state = makeShellState(fs, { cwd: NodePointer.of(0, work) });
expect(resolvePath(state, "sub/leaf")).toEqual({ device_id: 0, inode: leaf });
});
@@ -212,7 +186,7 @@ describe("resolvePath — '~' expansion", () => {
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 user = makeUser(NodePointer.of(0, home));
const state = makeShellState(fs, { user });
expect(resolvePath(state, "~")).toEqual({ device_id: 0, inode: home });
});
@@ -225,7 +199,7 @@ describe("resolvePath — '~' expansion", () => {
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 user = makeUser(NodePointer.of(0, home));
const state = makeShellState(fs, { user });
expect(resolvePath(state, "~/file")).toEqual({ device_id: 0, inode: file });
});
@@ -241,7 +215,7 @@ describe("resolvePath — '~' expansion", () => {
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 user = makeUser(NodePointer.of(1, home));
const state = makeShellState(fs, { user });
expect(resolvePath(state, "~/file")).toEqual({ device_id: 1, inode: file });
});
@@ -280,7 +254,7 @@ describe("resolvePath — mount points", () => {
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 }));
fs.mount(NodePointer.of(0, mnt), NodePointer.of(1, 0));
expect(resolvePath(makeShellState(fs), "/mnt")).toEqual({ device_id: 1, inode: 0 });
});
@@ -295,7 +269,7 @@ describe("resolvePath — mount points", () => {
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 }));
fs.mount(NodePointer.of(0, mnt), NodePointer.of(1, 0));
expect(resolvePath(makeShellState(fs), "/mnt/file")).toEqual({ device_id: 1, inode: file });
});
@@ -310,7 +284,7 @@ describe("resolvePath — mount points", () => {
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(NodePointer.of(0, mnt), NodePointer.of(1, 0));
expect(resolvePath(makeShellState(fs), "/mnt/sub/leaf")).toEqual({ device_id: 1, inode: leaf });
});
@@ -325,8 +299,8 @@ describe("resolvePath — mount points", () => {
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 }),
NodePointer.of(0, mnt), // "0:2" — the mountpoint
NodePointer.of(1, 0),
);
const state = makeShellState(fs);
@@ -349,7 +323,7 @@ describe("resolvePath — mount points", () => {
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 }));
fs.mount(NodePointer.of(0, b), NodePointer.of(1, sub));
expect(resolvePath(makeShellState(fs), "/a/b")).toEqual({ device_id: 1, inode: sub });
});
@@ -365,7 +339,7 @@ describe("resolvePath — mount points", () => {
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 }));
fs.mount(NodePointer.of(0, b), NodePointer.of(1, sub));
expect(resolvePath(makeShellState(fs), "/a/b/leaf")).toEqual({ device_id: 1, inode: leaf });
});
@@ -381,8 +355,8 @@ describe("resolvePath — mount points", () => {
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 }));
fs.mount(NodePointer.of(0, mnt), NodePointer.of(1, 0));
fs.mount(NodePointer.of(0, other), NodePointer.of(1, sub));
const state = makeShellState(fs);
expect(resolvePath(state, "/mnt/file")).toEqual({ device_id: 1, inode: file });
@@ -406,7 +380,7 @@ describe("resolvePath — symlinks across mount points", () => {
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 }));
fs.mount(NodePointer.of(0, mnt), NodePointer.of(1, 0));
expect(resolvePath(makeShellState(fs), "/link")).toEqual({ device_id: 1, inode: 0 });
});
@@ -423,7 +397,7 @@ describe("resolvePath — symlinks across mount points", () => {
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 }));
fs.mount(NodePointer.of(0, mnt), NodePointer.of(1, 0));
expect(resolvePath(makeShellState(fs), "/link/file")).toEqual({ device_id: 1, inode: file });
});
@@ -480,32 +454,6 @@ describe("resolvePath — known bugs", () => {
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", () => {
@@ -521,9 +469,9 @@ describe("resolvePath — '..' across mount boundaries", () => {
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 }));
await fs.mount(NodePointer.of(0, mnt), NodePointer.of(1, 0));
const atMountedRoot = makeShellState(fs, { cwd: toPtr({ device_id: 1, inode: 0 }) });
const atMountedRoot = makeShellState(fs, { cwd: NodePointer.of(1, 0) });
expect(resolvePath(atMountedRoot, "..")).toEqual({ device_id: 0, inode: 0 });
});
@@ -538,9 +486,9 @@ describe("resolvePath — '..' across mount boundaries", () => {
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 }));
await fs.mount(NodePointer.of(0, mnt), NodePointer.of(1, 0));
const atMountedRoot = makeShellState(fs, { cwd: toPtr({ device_id: 1, inode: 0 }) });
const atMountedRoot = makeShellState(fs, { cwd: NodePointer.of(1, 0) });
expect(resolvePath(atMountedRoot, "..")).toEqual({ device_id: 0, inode: user });
});
@@ -557,9 +505,9 @@ describe("resolvePath — '..' across mount boundaries", () => {
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 }));
await fs.mount(NodePointer.of(0, mnt), NodePointer.of(1, 0));
const atSub = makeShellState(fs, { cwd: toPtr({ device_id: 1, inode: sub }) });
const atSub = makeShellState(fs, { cwd: NodePointer.of(1, sub) });
expect(resolvePath(atSub, "..")).toEqual({ device_id: 1, inode: 0 });
});
@@ -576,21 +524,21 @@ describe("resolvePath — '..' across mount boundaries", () => {
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 }));
await fs.mount(NodePointer.of(0, mnt), NodePointer.of(1, 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 }) });
const atLeaf = makeShellState(fs, { cwd: NodePointer.of(1, 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 }) });
const atSub = makeShellState(fs, { cwd: NodePointer.of(1, 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 }) });
const atMountedRoot = makeShellState(fs, { cwd: NodePointer.of(1, 0) });
expect(resolvePath(atMountedRoot, "..")).toEqual({ device_id: 0, inode: 0 });
});
@@ -611,15 +559,15 @@ describe("resolvePath — '..' across mount boundaries", () => {
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
await fs.mount(NodePointer.of(0, a), NodePointer.of(1, 0)); // /a → dev1
await fs.mount(NodePointer.of(0, nested), NodePointer.of(2, 0)); // /b/nested → dev2
// `..` from dev1's root → parent of `/a` = `/`
const atDev1Root = makeShellState(fs, { cwd: toPtr({ device_id: 1, inode: 0 }) });
const atDev1Root = makeShellState(fs, { cwd: NodePointer.of(1, 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 }) });
const atDev2Root = makeShellState(fs, { cwd: NodePointer.of(2, 0) });
expect(resolvePath(atDev2Root, "..")).toEqual({ device_id: 0, inode: b });
});
@@ -652,18 +600,18 @@ describe("resolvePath — '..' across mount boundaries", () => {
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
await fs.mount(NodePointer.of(0, mnt), NodePointer.of(1, 0)); // /mnt → dev1
await fs.mount(NodePointer.of(1, b), NodePointer.of(2, 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 }) });
const atDev2Root = makeShellState(fs, { cwd: NodePointer.of(2, 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 }) });
const atDev1Root = makeShellState(fs, { cwd: NodePointer.of(1, 0) });
expect(resolvePath(atDev1Root, "..")).toEqual({ device_id: 0, inode: 0 });
});
@@ -677,18 +625,18 @@ describe("resolvePath — '..' across mount boundaries", () => {
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 }));
await fs.mount(NodePointer.of(0, mnt), NodePointer.of(1, 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 }) });
const atMnt = makeShellState(fs, { cwd: NodePointer.of(1, 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 }) });
const atRoot = makeShellState(fs, { cwd: NodePointer.of(0, 0) });
expect(resolvePath(atRoot, "mnt")).toEqual({ device_id: 1, inode: 0 });
});
});