// @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 fc from "fast-check"; import { describe, expect, it } from "vitest"; // ============================================================================ // Node fixtures // ============================================================================ function makeDir(inode: number, children: Record = {}): 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)), }; } 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; } function makeTree(): TreeBuilder { const nodes = new Map([[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[]): Promise { 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(), 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 }); }); /** * 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: toPtr({ device_id: 0, inode: 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); }, ), ); }); }); // ============================================================================ // 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: 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 }); }); });