filesystem.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. describe("windowsPath()", () => {
  226. test("converts Git Bash paths", () => {
  227. if (process.platform === "win32") {
  228. expect(Filesystem.windowsPath("/c/Users/test")).toBe("C:/Users/test")
  229. expect(Filesystem.windowsPath("/d/dev/project")).toBe("D:/dev/project")
  230. } else {
  231. expect(Filesystem.windowsPath("/c/Users/test")).toBe("/c/Users/test")
  232. }
  233. })
  234. test("converts Cygwin paths", () => {
  235. if (process.platform === "win32") {
  236. expect(Filesystem.windowsPath("/cygdrive/c/Users/test")).toBe("C:/Users/test")
  237. expect(Filesystem.windowsPath("/cygdrive/x/dev/project")).toBe("X:/dev/project")
  238. } else {
  239. expect(Filesystem.windowsPath("/cygdrive/c/Users/test")).toBe("/cygdrive/c/Users/test")
  240. }
  241. })
  242. test("converts WSL paths", () => {
  243. if (process.platform === "win32") {
  244. expect(Filesystem.windowsPath("/mnt/c/Users/test")).toBe("C:/Users/test")
  245. expect(Filesystem.windowsPath("/mnt/z/dev/project")).toBe("Z:/dev/project")
  246. } else {
  247. expect(Filesystem.windowsPath("/mnt/c/Users/test")).toBe("/mnt/c/Users/test")
  248. }
  249. })
  250. test("ignores normal Windows paths", () => {
  251. expect(Filesystem.windowsPath("C:/Users/test")).toBe("C:/Users/test")
  252. expect(Filesystem.windowsPath("D:\\dev\\project")).toBe("D:\\dev\\project")
  253. })
  254. })
  255. describe("writeStream()", () => {
  256. test("writes from Web ReadableStream", async () => {
  257. await using tmp = await tmpdir()
  258. const filepath = path.join(tmp.path, "streamed.txt")
  259. const content = "Hello from stream!"
  260. const encoder = new TextEncoder()
  261. const stream = new ReadableStream({
  262. start(controller) {
  263. controller.enqueue(encoder.encode(content))
  264. controller.close()
  265. },
  266. })
  267. await Filesystem.writeStream(filepath, stream)
  268. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  269. })
  270. test("writes from Node.js Readable stream", async () => {
  271. await using tmp = await tmpdir()
  272. const filepath = path.join(tmp.path, "node-streamed.txt")
  273. const content = "Hello from Node stream!"
  274. const { Readable } = await import("stream")
  275. const stream = Readable.from([content])
  276. await Filesystem.writeStream(filepath, stream)
  277. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  278. })
  279. test("writes binary data from Web ReadableStream", async () => {
  280. await using tmp = await tmpdir()
  281. const filepath = path.join(tmp.path, "binary.dat")
  282. const binaryData = new Uint8Array([0x00, 0x01, 0x02, 0x03, 0xff])
  283. const stream = new ReadableStream({
  284. start(controller) {
  285. controller.enqueue(binaryData)
  286. controller.close()
  287. },
  288. })
  289. await Filesystem.writeStream(filepath, stream)
  290. const read = await fs.readFile(filepath)
  291. expect(Buffer.from(read)).toEqual(Buffer.from(binaryData))
  292. })
  293. test("writes large content in chunks", async () => {
  294. await using tmp = await tmpdir()
  295. const filepath = path.join(tmp.path, "large.txt")
  296. const chunks = ["chunk1", "chunk2", "chunk3", "chunk4", "chunk5"]
  297. const stream = new ReadableStream({
  298. start(controller) {
  299. for (const chunk of chunks) {
  300. controller.enqueue(new TextEncoder().encode(chunk))
  301. }
  302. controller.close()
  303. },
  304. })
  305. await Filesystem.writeStream(filepath, stream)
  306. expect(await fs.readFile(filepath, "utf-8")).toBe(chunks.join(""))
  307. })
  308. test("creates parent directories", async () => {
  309. await using tmp = await tmpdir()
  310. const filepath = path.join(tmp.path, "nested", "deep", "streamed.txt")
  311. const content = "nested stream content"
  312. const stream = new ReadableStream({
  313. start(controller) {
  314. controller.enqueue(new TextEncoder().encode(content))
  315. controller.close()
  316. },
  317. })
  318. await Filesystem.writeStream(filepath, stream)
  319. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  320. })
  321. test("writes with permissions", async () => {
  322. await using tmp = await tmpdir()
  323. const filepath = path.join(tmp.path, "protected-stream.txt")
  324. const content = "secret stream content"
  325. const stream = new ReadableStream({
  326. start(controller) {
  327. controller.enqueue(new TextEncoder().encode(content))
  328. controller.close()
  329. },
  330. })
  331. await Filesystem.writeStream(filepath, stream, 0o600)
  332. const stats = await fs.stat(filepath)
  333. if (process.platform !== "win32") {
  334. expect(stats.mode & 0o777).toBe(0o600)
  335. }
  336. })
  337. test("writes executable with permissions", async () => {
  338. await using tmp = await tmpdir()
  339. const filepath = path.join(tmp.path, "script.sh")
  340. const content = "#!/bin/bash\necho hello"
  341. const stream = new ReadableStream({
  342. start(controller) {
  343. controller.enqueue(new TextEncoder().encode(content))
  344. controller.close()
  345. },
  346. })
  347. await Filesystem.writeStream(filepath, stream, 0o755)
  348. const stats = await fs.stat(filepath)
  349. if (process.platform !== "win32") {
  350. expect(stats.mode & 0o777).toBe(0o755)
  351. }
  352. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  353. })
  354. })
  355. })