|
| 1 | +import { describe, test, expect } from "bun:test"; |
| 2 | +import { PlatformPaths } from "./paths"; |
| 3 | +import * as os from "os"; |
| 4 | +import * as path from "path"; |
| 5 | + |
| 6 | +describe("PlatformPaths", () => { |
| 7 | + describe("basename", () => { |
| 8 | + test("extracts basename from path using current platform", () => { |
| 9 | + expect(PlatformPaths.basename("/home/user/project")).toBe("project"); |
| 10 | + expect(PlatformPaths.basename("/home/user/project/file.txt")).toBe("file.txt"); |
| 11 | + }); |
| 12 | + |
| 13 | + test("handles edge cases", () => { |
| 14 | + expect(PlatformPaths.basename("")).toBe(""); |
| 15 | + expect(PlatformPaths.basename("project")).toBe("project"); |
| 16 | + }); |
| 17 | + }); |
| 18 | + |
| 19 | + describe("parse", () => { |
| 20 | + test("parses absolute path on current platform", () => { |
| 21 | + const testPath = path.join("/", "home", "user", "projects", "cmux"); |
| 22 | + const result = PlatformPaths.parse(testPath); |
| 23 | + expect(result.segments).toContain("home"); |
| 24 | + expect(result.segments).toContain("user"); |
| 25 | + expect(result.segments).toContain("projects"); |
| 26 | + expect(result.basename).toBe("cmux"); |
| 27 | + }); |
| 28 | + |
| 29 | + test("parses relative path", () => { |
| 30 | + const result = PlatformPaths.parse("src/utils/paths.ts"); |
| 31 | + expect(result.root).toBe(""); |
| 32 | + expect(result.basename).toBe("paths.ts"); |
| 33 | + }); |
| 34 | + |
| 35 | + test("handles edge cases", () => { |
| 36 | + expect(PlatformPaths.parse("")).toEqual({ root: "", segments: [], basename: "" }); |
| 37 | + expect(PlatformPaths.parse("file.txt").basename).toBe("file.txt"); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe("abbreviate", () => { |
| 42 | + test("abbreviates path", () => { |
| 43 | + const testPath = path.join("/", "home", "user", "Projects", "coder", "cmux"); |
| 44 | + const result = PlatformPaths.abbreviate(testPath); |
| 45 | + |
| 46 | + // Should end with the full basename |
| 47 | + expect(result.endsWith("cmux")).toBe(true); |
| 48 | + |
| 49 | + // Should be shorter than original (segments abbreviated) |
| 50 | + expect(result.length).toBeLessThan(testPath.length); |
| 51 | + }); |
| 52 | + |
| 53 | + test("handles short paths", () => { |
| 54 | + const testPath = path.join("/", "home"); |
| 55 | + const result = PlatformPaths.abbreviate(testPath); |
| 56 | + // Short paths should not be abbreviated much |
| 57 | + expect(result).toContain("home"); |
| 58 | + }); |
| 59 | + |
| 60 | + test("handles empty input", () => { |
| 61 | + expect(PlatformPaths.abbreviate("")).toBe(""); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe("splitAbbreviated", () => { |
| 66 | + test("splits abbreviated path", () => { |
| 67 | + const testPath = path.join("/", "h", "u", "P", "c", "cmux"); |
| 68 | + const result = PlatformPaths.splitAbbreviated(testPath); |
| 69 | + expect(result.basename).toBe("cmux"); |
| 70 | + expect(result.dirPath.endsWith(path.sep)).toBe(true); |
| 71 | + }); |
| 72 | + |
| 73 | + test("handles path without directory", () => { |
| 74 | + const result = PlatformPaths.splitAbbreviated("file.txt"); |
| 75 | + expect(result.dirPath).toBe(""); |
| 76 | + expect(result.basename).toBe("file.txt"); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + describe("formatHome", () => { |
| 81 | + test("replaces home directory with tilde on Unix", () => { |
| 82 | + const home = os.homedir(); |
| 83 | + const testPath = path.join(home, "projects", "cmux"); |
| 84 | + const result = PlatformPaths.formatHome(testPath); |
| 85 | + |
| 86 | + // On Unix-like systems, should use tilde |
| 87 | + if (process.platform !== "win32") { |
| 88 | + expect(result).toBe("~/projects/cmux"); |
| 89 | + } else { |
| 90 | + // On Windows, should keep full path |
| 91 | + expect(result).toContain(home); |
| 92 | + } |
| 93 | + }); |
| 94 | + |
| 95 | + test("leaves non-home paths unchanged", () => { |
| 96 | + const result = PlatformPaths.formatHome("/tmp/test"); |
| 97 | + expect(result).toBe("/tmp/test"); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe("expandHome", () => { |
| 102 | + test("expands tilde to home directory", () => { |
| 103 | + const home = os.homedir(); |
| 104 | + expect(PlatformPaths.expandHome("~")).toBe(home); |
| 105 | + }); |
| 106 | + |
| 107 | + test("expands tilde with path", () => { |
| 108 | + const home = os.homedir(); |
| 109 | + const sep = path.sep; |
| 110 | + const result = PlatformPaths.expandHome(`~${sep}projects${sep}cmux`); |
| 111 | + expect(result).toBe(path.join(home, "projects", "cmux")); |
| 112 | + }); |
| 113 | + |
| 114 | + test("leaves absolute paths unchanged", () => { |
| 115 | + const testPath = path.join("/", "home", "user", "project"); |
| 116 | + expect(PlatformPaths.expandHome(testPath)).toBe(testPath); |
| 117 | + }); |
| 118 | + |
| 119 | + test("handles empty input", () => { |
| 120 | + expect(PlatformPaths.expandHome("")).toBe(""); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe("getProjectName", () => { |
| 125 | + test("extracts project name from path", () => { |
| 126 | + const testPath = path.join("/", "home", "user", "projects", "cmux"); |
| 127 | + expect(PlatformPaths.getProjectName(testPath)).toBe("cmux"); |
| 128 | + }); |
| 129 | + |
| 130 | + test("handles relative paths", () => { |
| 131 | + expect(PlatformPaths.getProjectName("projects/cmux")).toBe("cmux"); |
| 132 | + }); |
| 133 | + |
| 134 | + test("returns 'unknown' for empty path", () => { |
| 135 | + expect(PlatformPaths.getProjectName("")).toBe("unknown"); |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + describe("separator", () => { |
| 140 | + test("returns correct separator for platform", () => { |
| 141 | + const sep = PlatformPaths.separator; |
| 142 | + // Should match the current platform's separator |
| 143 | + expect(sep).toBe(path.sep); |
| 144 | + }); |
| 145 | + }); |
| 146 | +}); |
0 commit comments