|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { coerceFsPath } from "../../src/util/coerce-path" |
| 3 | +import path from "path" |
| 4 | + |
| 5 | +describe("coerceFsPath", () => { |
| 6 | + describe("accepts valid inputs", () => { |
| 7 | + test("returns empty string for undefined", () => { |
| 8 | + expect(coerceFsPath(undefined)).toBe("") |
| 9 | + }) |
| 10 | + |
| 11 | + test("returns empty string for null", () => { |
| 12 | + expect(coerceFsPath(null)).toBe("") |
| 13 | + }) |
| 14 | + |
| 15 | + test("returns string as-is", () => { |
| 16 | + expect(coerceFsPath("/path/to/file")).toBe("/path/to/file") |
| 17 | + expect(coerceFsPath("relative/path")).toBe("relative/path") |
| 18 | + expect(coerceFsPath("")).toBe("") |
| 19 | + }) |
| 20 | + |
| 21 | + test("extracts pathname from URL instance", () => { |
| 22 | + const url = new URL("file:///path/to/file.txt") |
| 23 | + expect(coerceFsPath(url)).toBe("/path/to/file.txt") |
| 24 | + }) |
| 25 | + |
| 26 | + test("extracts path from object with path property", () => { |
| 27 | + expect(coerceFsPath({ path: "/some/path" })).toBe("/some/path") |
| 28 | + }) |
| 29 | + |
| 30 | + test("extracts filePath from object with filePath property", () => { |
| 31 | + expect(coerceFsPath({ filePath: "/some/file.ts" })).toBe("/some/file.ts") |
| 32 | + }) |
| 33 | + |
| 34 | + test("extracts cwd from object with cwd property", () => { |
| 35 | + expect(coerceFsPath({ cwd: "/working/dir" })).toBe("/working/dir") |
| 36 | + }) |
| 37 | + |
| 38 | + test("extracts value from object with value property", () => { |
| 39 | + expect(coerceFsPath({ value: "/value/path" })).toBe("/value/path") |
| 40 | + }) |
| 41 | + |
| 42 | + test("returns empty string for object with null path property", () => { |
| 43 | + expect(coerceFsPath({ path: null })).toBe("") |
| 44 | + expect(coerceFsPath({ filePath: undefined })).toBe("") |
| 45 | + }) |
| 46 | + |
| 47 | + test("prioritizes path over other properties", () => { |
| 48 | + expect(coerceFsPath({ path: "/a", cwd: "/b", value: "/c" })).toBe("/a") |
| 49 | + }) |
| 50 | + |
| 51 | + test("falls back to filePath when path is missing", () => { |
| 52 | + expect(coerceFsPath({ filePath: "/a", cwd: "/b" })).toBe("/a") |
| 53 | + }) |
| 54 | + |
| 55 | + test("joins string array with path separator", () => { |
| 56 | + const result = coerceFsPath(["path", "to", "file"]) |
| 57 | + expect(result).toBe(path.join("path", "to", "file")) |
| 58 | + }) |
| 59 | + |
| 60 | + test("returns empty string for empty array", () => { |
| 61 | + expect(coerceFsPath([])).toBe("") |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + describe("rejects invalid inputs", () => { |
| 66 | + test("throws for number", () => { |
| 67 | + expect(() => coerceFsPath(123)).toThrow(TypeError) |
| 68 | + expect(() => coerceFsPath(123)).toThrow("expected string or path-like object, got number") |
| 69 | + }) |
| 70 | + |
| 71 | + test("throws for boolean", () => { |
| 72 | + expect(() => coerceFsPath(true)).toThrow(TypeError) |
| 73 | + expect(() => coerceFsPath(true)).toThrow("expected string or path-like object, got boolean") |
| 74 | + }) |
| 75 | + |
| 76 | + test("throws for function", () => { |
| 77 | + expect(() => coerceFsPath(() => {})).toThrow(TypeError) |
| 78 | + expect(() => coerceFsPath(() => {})).toThrow("expected string or path-like object, got function") |
| 79 | + }) |
| 80 | + |
| 81 | + test("throws for object without recognized path property", () => { |
| 82 | + expect(() => coerceFsPath({ foo: "bar" })).toThrow(TypeError) |
| 83 | + expect(() => coerceFsPath({ foo: "bar" })).toThrow("no recognized path property") |
| 84 | + }) |
| 85 | + |
| 86 | + test("throws for object with non-string path property", () => { |
| 87 | + expect(() => coerceFsPath({ path: 123 })).toThrow(TypeError) |
| 88 | + expect(() => coerceFsPath({ path: 123 })).toThrow("object.path is not a string") |
| 89 | + }) |
| 90 | + |
| 91 | + test("throws for array with non-string element", () => { |
| 92 | + expect(() => coerceFsPath(["a", 123, "b"])).toThrow(TypeError) |
| 93 | + expect(() => coerceFsPath(["a", 123, "b"])).toThrow("array element at index 1 is not a string") |
| 94 | + }) |
| 95 | + |
| 96 | + test("throws for object with nested object path", () => { |
| 97 | + expect(() => coerceFsPath({ path: { nested: "value" } })).toThrow(TypeError) |
| 98 | + expect(() => coerceFsPath({ path: { nested: "value" } })).toThrow("object.path is not a string") |
| 99 | + }) |
| 100 | + }) |
| 101 | + |
| 102 | + describe("includes context in error messages", () => { |
| 103 | + test("includes context for primitive type error", () => { |
| 104 | + expect(() => coerceFsPath(123, "session route")).toThrow("in session route") |
| 105 | + }) |
| 106 | + |
| 107 | + test("includes context for object property error", () => { |
| 108 | + expect(() => coerceFsPath({ path: 123 }, "permission check")).toThrow("in permission check") |
| 109 | + }) |
| 110 | + |
| 111 | + test("includes context for unrecognized object error", () => { |
| 112 | + expect(() => coerceFsPath({ unknown: "value" }, "list tool")).toThrow("in list tool") |
| 113 | + }) |
| 114 | + |
| 115 | + test("includes context for array element error", () => { |
| 116 | + expect(() => coerceFsPath(["a", null], "glob tool")).toThrow("in glob tool") |
| 117 | + }) |
| 118 | + }) |
| 119 | + |
| 120 | + describe("edge cases", () => { |
| 121 | + test("handles empty path property", () => { |
| 122 | + expect(coerceFsPath({ path: "" })).toBe("") |
| 123 | + }) |
| 124 | + |
| 125 | + test("handles whitespace-only path", () => { |
| 126 | + expect(coerceFsPath(" ")).toBe(" ") |
| 127 | + }) |
| 128 | + |
| 129 | + test("handles path with special characters", () => { |
| 130 | + expect(coerceFsPath("/path/with spaces/and-dashes")).toBe("/path/with spaces/and-dashes") |
| 131 | + }) |
| 132 | + |
| 133 | + test("handles object toString that returns [object Object]", () => { |
| 134 | + // This should NOT fall through to naive String() conversion |
| 135 | + const badObject = { toString: () => "[object Object]" } |
| 136 | + expect(() => coerceFsPath(badObject)).toThrow("no recognized path property") |
| 137 | + }) |
| 138 | + }) |
| 139 | +}) |
0 commit comments