filesystem.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import { describe, test, expect } from "bun:test"
  2. import path from "path"
  3. import fs from "fs/promises"
  4. import { Filesystem } from "../../src/util/filesystem"
  5. import { tmpdir } from "../fixture/fixture"
  6. describe("filesystem", () => {
  7. describe("exists()", () => {
  8. test("returns true for existing file", async () => {
  9. await using tmp = await tmpdir()
  10. const filepath = path.join(tmp.path, "test.txt")
  11. await fs.writeFile(filepath, "content", "utf-8")
  12. expect(await Filesystem.exists(filepath)).toBe(true)
  13. })
  14. test("returns false for non-existent file", async () => {
  15. await using tmp = await tmpdir()
  16. const filepath = path.join(tmp.path, "does-not-exist.txt")
  17. expect(await Filesystem.exists(filepath)).toBe(false)
  18. })
  19. test("returns true for existing directory", async () => {
  20. await using tmp = await tmpdir()
  21. const dirpath = path.join(tmp.path, "subdir")
  22. await fs.mkdir(dirpath)
  23. expect(await Filesystem.exists(dirpath)).toBe(true)
  24. })
  25. })
  26. describe("isDir()", () => {
  27. test("returns true for directory", async () => {
  28. await using tmp = await tmpdir()
  29. const dirpath = path.join(tmp.path, "testdir")
  30. await fs.mkdir(dirpath)
  31. expect(await Filesystem.isDir(dirpath)).toBe(true)
  32. })
  33. test("returns false for file", async () => {
  34. await using tmp = await tmpdir()
  35. const filepath = path.join(tmp.path, "test.txt")
  36. await fs.writeFile(filepath, "content", "utf-8")
  37. expect(await Filesystem.isDir(filepath)).toBe(false)
  38. })
  39. test("returns false for non-existent path", async () => {
  40. await using tmp = await tmpdir()
  41. const filepath = path.join(tmp.path, "does-not-exist")
  42. expect(await Filesystem.isDir(filepath)).toBe(false)
  43. })
  44. })
  45. describe("size()", () => {
  46. test("returns file size", async () => {
  47. await using tmp = await tmpdir()
  48. const filepath = path.join(tmp.path, "test.txt")
  49. const content = "Hello, World!"
  50. await fs.writeFile(filepath, content, "utf-8")
  51. expect(await Filesystem.size(filepath)).toBe(content.length)
  52. })
  53. test("returns 0 for non-existent file", async () => {
  54. await using tmp = await tmpdir()
  55. const filepath = path.join(tmp.path, "does-not-exist.txt")
  56. expect(await Filesystem.size(filepath)).toBe(0)
  57. })
  58. test("returns directory size", async () => {
  59. await using tmp = await tmpdir()
  60. const dirpath = path.join(tmp.path, "testdir")
  61. await fs.mkdir(dirpath)
  62. // Directories have size on some systems
  63. const size = await Filesystem.size(dirpath)
  64. expect(typeof size).toBe("number")
  65. })
  66. })
  67. describe("readText()", () => {
  68. test("reads file content", async () => {
  69. await using tmp = await tmpdir()
  70. const filepath = path.join(tmp.path, "test.txt")
  71. const content = "Hello, World!"
  72. await fs.writeFile(filepath, content, "utf-8")
  73. expect(await Filesystem.readText(filepath)).toBe(content)
  74. })
  75. test("throws for non-existent file", async () => {
  76. await using tmp = await tmpdir()
  77. const filepath = path.join(tmp.path, "does-not-exist.txt")
  78. await expect(Filesystem.readText(filepath)).rejects.toThrow()
  79. })
  80. test("reads UTF-8 content correctly", async () => {
  81. await using tmp = await tmpdir()
  82. const filepath = path.join(tmp.path, "unicode.txt")
  83. const content = "Hello 世界 🌍"
  84. await fs.writeFile(filepath, content, "utf-8")
  85. expect(await Filesystem.readText(filepath)).toBe(content)
  86. })
  87. })
  88. describe("readJson()", () => {
  89. test("reads and parses JSON", async () => {
  90. await using tmp = await tmpdir()
  91. const filepath = path.join(tmp.path, "test.json")
  92. const data = { key: "value", nested: { array: [1, 2, 3] } }
  93. await fs.writeFile(filepath, JSON.stringify(data), "utf-8")
  94. const result: typeof data = await Filesystem.readJson(filepath)
  95. expect(result).toEqual(data)
  96. })
  97. test("throws for invalid JSON", async () => {
  98. await using tmp = await tmpdir()
  99. const filepath = path.join(tmp.path, "invalid.json")
  100. await fs.writeFile(filepath, "{ invalid json", "utf-8")
  101. await expect(Filesystem.readJson(filepath)).rejects.toThrow()
  102. })
  103. test("throws for non-existent file", async () => {
  104. await using tmp = await tmpdir()
  105. const filepath = path.join(tmp.path, "does-not-exist.json")
  106. await expect(Filesystem.readJson(filepath)).rejects.toThrow()
  107. })
  108. test("returns typed data", async () => {
  109. await using tmp = await tmpdir()
  110. const filepath = path.join(tmp.path, "typed.json")
  111. interface Config {
  112. name: string
  113. version: number
  114. }
  115. const data: Config = { name: "test", version: 1 }
  116. await fs.writeFile(filepath, JSON.stringify(data), "utf-8")
  117. const result = await Filesystem.readJson<Config>(filepath)
  118. expect(result.name).toBe("test")
  119. expect(result.version).toBe(1)
  120. })
  121. })
  122. describe("readBytes()", () => {
  123. test("reads file as buffer", async () => {
  124. await using tmp = await tmpdir()
  125. const filepath = path.join(tmp.path, "test.txt")
  126. const content = "Hello, World!"
  127. await fs.writeFile(filepath, content, "utf-8")
  128. const buffer = await Filesystem.readBytes(filepath)
  129. expect(buffer).toBeInstanceOf(Buffer)
  130. expect(buffer.toString("utf-8")).toBe(content)
  131. })
  132. test("throws for non-existent file", async () => {
  133. await using tmp = await tmpdir()
  134. const filepath = path.join(tmp.path, "does-not-exist.bin")
  135. await expect(Filesystem.readBytes(filepath)).rejects.toThrow()
  136. })
  137. })
  138. describe("write()", () => {
  139. test("writes text content", async () => {
  140. await using tmp = await tmpdir()
  141. const filepath = path.join(tmp.path, "test.txt")
  142. const content = "Hello, World!"
  143. await Filesystem.write(filepath, content)
  144. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  145. })
  146. test("writes buffer content", async () => {
  147. await using tmp = await tmpdir()
  148. const filepath = path.join(tmp.path, "test.bin")
  149. const content = Buffer.from([0x00, 0x01, 0x02, 0x03])
  150. await Filesystem.write(filepath, content)
  151. const read = await fs.readFile(filepath)
  152. expect(read).toEqual(content)
  153. })
  154. test("writes with permissions", async () => {
  155. await using tmp = await tmpdir()
  156. const filepath = path.join(tmp.path, "protected.txt")
  157. const content = "secret"
  158. await Filesystem.write(filepath, content, 0o600)
  159. const stats = await fs.stat(filepath)
  160. // Check permissions on Unix
  161. if (process.platform !== "win32") {
  162. expect(stats.mode & 0o777).toBe(0o600)
  163. }
  164. })
  165. test("creates parent directories", async () => {
  166. await using tmp = await tmpdir()
  167. const filepath = path.join(tmp.path, "nested", "deep", "file.txt")
  168. const content = "nested content"
  169. await Filesystem.write(filepath, content)
  170. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  171. })
  172. })
  173. describe("writeJson()", () => {
  174. test("writes JSON data", async () => {
  175. await using tmp = await tmpdir()
  176. const filepath = path.join(tmp.path, "data.json")
  177. const data = { key: "value", number: 42 }
  178. await Filesystem.writeJson(filepath, data)
  179. const content = await fs.readFile(filepath, "utf-8")
  180. expect(JSON.parse(content)).toEqual(data)
  181. })
  182. test("writes formatted JSON", async () => {
  183. await using tmp = await tmpdir()
  184. const filepath = path.join(tmp.path, "pretty.json")
  185. const data = { key: "value" }
  186. await Filesystem.writeJson(filepath, data)
  187. const content = await fs.readFile(filepath, "utf-8")
  188. expect(content).toContain("\n")
  189. expect(content).toContain(" ")
  190. })
  191. test("writes with permissions", async () => {
  192. await using tmp = await tmpdir()
  193. const filepath = path.join(tmp.path, "config.json")
  194. const data = { secret: "data" }
  195. await Filesystem.writeJson(filepath, data, 0o600)
  196. const stats = await fs.stat(filepath)
  197. if (process.platform !== "win32") {
  198. expect(stats.mode & 0o777).toBe(0o600)
  199. }
  200. })
  201. })
  202. describe("mimeType()", () => {
  203. test("returns correct MIME type for JSON", () => {
  204. expect(Filesystem.mimeType("test.json")).toContain("application/json")
  205. })
  206. test("returns correct MIME type for JavaScript", () => {
  207. expect(Filesystem.mimeType("test.js")).toContain("javascript")
  208. })
  209. test("returns MIME type for TypeScript (or video/mp2t due to extension conflict)", () => {
  210. const mime = Filesystem.mimeType("test.ts")
  211. // .ts is ambiguous: TypeScript vs MPEG-2 TS video
  212. expect(mime === "video/mp2t" || mime === "application/typescript" || mime === "text/typescript").toBe(true)
  213. })
  214. test("returns correct MIME type for images", () => {
  215. expect(Filesystem.mimeType("test.png")).toContain("image/png")
  216. expect(Filesystem.mimeType("test.jpg")).toContain("image/jpeg")
  217. })
  218. test("returns default for unknown extension", () => {
  219. expect(Filesystem.mimeType("test.unknown")).toBe("application/octet-stream")
  220. })
  221. test("handles files without extension", () => {
  222. expect(Filesystem.mimeType("Makefile")).toBe("application/octet-stream")
  223. })
  224. })
  225. })