// @tests/core/util/path_resolver.test.ts (adjust) 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 { describe, expect, it } from "vitest"; // ============================================================================ // 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 }); }); /** * 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 }); } }); /** * 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: NodePointer.of(0, work) }); expect(resolvePath(state, "")).toEqual({ device_id: 0, inode: work }); }); }); // ============================================================================ // 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 }); }); /** * 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: NodePointer.of(0, 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: NodePointer.of(0, 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(NodePointer.of(0, 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(NodePointer.of(0, 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(NodePointer.of(1, 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(NodePointer.of(0, mnt), NodePointer.of(1, 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(NodePointer.of(0, mnt), NodePointer.of(1, 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(NodePointer.of(0, mnt), NodePointer.of(1, 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( NodePointer.of(0, mnt), // "0:2" — the mountpoint NodePointer.of(1, 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(NodePointer.of(0, b), NodePointer.of(1, 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(NodePointer.of(0, b), NodePointer.of(1, 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(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 }); 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(NodePointer.of(0, mnt), NodePointer.of(1, 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(NodePointer.of(0, mnt), NodePointer.of(1, 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). }); }); 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(NodePointer.of(0, mnt), NodePointer.of(1, 0)); const atMountedRoot = makeShellState(fs, { cwd: NodePointer.of(1, 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(NodePointer.of(0, mnt), NodePointer.of(1, 0)); const atMountedRoot = makeShellState(fs, { cwd: NodePointer.of(1, 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(NodePointer.of(0, mnt), NodePointer.of(1, 0)); const atSub = makeShellState(fs, { cwd: NodePointer.of(1, 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(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: NodePointer.of(1, leaf) }); expect(resolvePath(atLeaf, "..")).toEqual({ device_id: 1, inode: sub }); // Another `..`: /mnt's content = dev1 root 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: NodePointer.of(1, 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(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: 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: NodePointer.of(2, 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(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: 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: NodePointer.of(1, 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(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: 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: NodePointer.of(0, 0) }); expect(resolvePath(atRoot, "mnt")).toEqual({ device_id: 1, inode: 0 }); }); });