file-path.test.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { describe, expect, test } from "bun:test"
  2. import { truncateFilePath } from "../../src/ui/file-path"
  3. describe("truncateFilePath", () => {
  4. const path = "packages/tui/src/ui/dialog-select.tsx"
  5. test("keeps the full path when it fits", () => {
  6. expect(truncateFilePath(path, 37)).toBe(path)
  7. })
  8. test("adds nearest parent segments from right to left", () => {
  9. expect(truncateFilePath(path, 26)).toBe("…/src/ui/dialog-select.tsx")
  10. expect(truncateFilePath(path, 22)).toBe("…/ui/dialog-select.tsx")
  11. expect(truncateFilePath(path, 19)).toBe("…/dialog-select.tsx")
  12. })
  13. test("preserves the working directory and branch suffix", () => {
  14. expect(truncateFilePath("~/code/experiments/category-theory:main", 30)).toBe("…/experi…/category-theory:main")
  15. })
  16. test("uses remaining width for part of a long parent segment", () => {
  17. const path = "/private/var/folders/run-17f048ec-dbb2-4b36-860c-98637bb51a8d/files"
  18. expect(truncateFilePath(path, 40)).toBe("/…/run-17f048ec-dbb2-4b36-860c-98…/files")
  19. })
  20. test("preserves the extension when the basename must shrink", () => {
  21. expect(truncateFilePath(path, 16)).toBe("…/dialog-se….tsx")
  22. expect(truncateFilePath("dialog-select.tsx", 12)).toBe("dialog-….tsx")
  23. })
  24. test("preserves the input separator", () => {
  25. expect(truncateFilePath("packages\\tui\\src\\ui\\dialog-select.tsx", 22)).toBe("…\\ui\\dialog-select.tsx")
  26. })
  27. test("does not treat a backslash in a POSIX filename as a separator", () => {
  28. expect(truncateFilePath("dir/file\\name.ts", 14)).toBe("…/file\\name.ts")
  29. })
  30. test("preserves absolute roots", () => {
  31. expect(truncateFilePath("/file.ts", 7)).toBe("/fi….ts")
  32. expect(truncateFilePath("C:\\file.ts", 9)).toBe("C:\\fi….ts")
  33. expect(truncateFilePath("/usr/local/bin/file.ts", 14)).toBe("/…/bin/file.ts")
  34. expect(truncateFilePath("C:\\Users\\kit\\src\\file.ts", 16)).toBe("C:\\…\\src\\file.ts")
  35. expect(truncateFilePath("C:/Users/kit/src/file.ts", 16)).toBe("C:/…/src/file.ts")
  36. expect(truncateFilePath("C:\\Users\\kit/src/file.ts", 16)).toBe("C:\\…\\src\\file.ts")
  37. expect(truncateFilePath("\\\\server\\share\\src\\file.ts", 25)).toBe("\\\\server\\share\\…\\file.ts")
  38. })
  39. test("measures terminal columns without splitting graphemes", () => {
  40. expect(truncateFilePath("packages/组件/对话框.tsx", 12)).toBe("…/对话框.tsx")
  41. expect(truncateFilePath("src/👩‍💻-notes.tsx", 12)).toContain("👩‍💻")
  42. expect(truncateFilePath("中a.txt", 6)).toBe("….txt")
  43. expect(truncateFilePath("file.中a", 3)).toBe("…a")
  44. })
  45. test("never exceeds the requested width", () => {
  46. for (let width = 0; width <= Bun.stringWidth(path); width++) {
  47. expect(Bun.stringWidth(truncateFilePath(path, width))).toBeLessThanOrEqual(width)
  48. }
  49. })
  50. })