filesystem.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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("writeStream()", () => {
  226. test("writes from Web ReadableStream", async () => {
  227. await using tmp = await tmpdir()
  228. const filepath = path.join(tmp.path, "streamed.txt")
  229. const content = "Hello from stream!"
  230. const encoder = new TextEncoder()
  231. const stream = new ReadableStream({
  232. start(controller) {
  233. controller.enqueue(encoder.encode(content))
  234. controller.close()
  235. },
  236. })
  237. await Filesystem.writeStream(filepath, stream)
  238. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  239. })
  240. test("writes from Node.js Readable stream", async () => {
  241. await using tmp = await tmpdir()
  242. const filepath = path.join(tmp.path, "node-streamed.txt")
  243. const content = "Hello from Node stream!"
  244. const { Readable } = await import("stream")
  245. const stream = Readable.from([content])
  246. await Filesystem.writeStream(filepath, stream)
  247. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  248. })
  249. test("writes binary data from Web ReadableStream", async () => {
  250. await using tmp = await tmpdir()
  251. const filepath = path.join(tmp.path, "binary.dat")
  252. const binaryData = new Uint8Array([0x00, 0x01, 0x02, 0x03, 0xff])
  253. const stream = new ReadableStream({
  254. start(controller) {
  255. controller.enqueue(binaryData)
  256. controller.close()
  257. },
  258. })
  259. await Filesystem.writeStream(filepath, stream)
  260. const read = await fs.readFile(filepath)
  261. expect(Buffer.from(read)).toEqual(Buffer.from(binaryData))
  262. })
  263. test("writes large content in chunks", async () => {
  264. await using tmp = await tmpdir()
  265. const filepath = path.join(tmp.path, "large.txt")
  266. const chunks = ["chunk1", "chunk2", "chunk3", "chunk4", "chunk5"]
  267. const stream = new ReadableStream({
  268. start(controller) {
  269. for (const chunk of chunks) {
  270. controller.enqueue(new TextEncoder().encode(chunk))
  271. }
  272. controller.close()
  273. },
  274. })
  275. await Filesystem.writeStream(filepath, stream)
  276. expect(await fs.readFile(filepath, "utf-8")).toBe(chunks.join(""))
  277. })
  278. test("creates parent directories", async () => {
  279. await using tmp = await tmpdir()
  280. const filepath = path.join(tmp.path, "nested", "deep", "streamed.txt")
  281. const content = "nested stream content"
  282. const stream = new ReadableStream({
  283. start(controller) {
  284. controller.enqueue(new TextEncoder().encode(content))
  285. controller.close()
  286. },
  287. })
  288. await Filesystem.writeStream(filepath, stream)
  289. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  290. })
  291. test("writes with permissions", async () => {
  292. await using tmp = await tmpdir()
  293. const filepath = path.join(tmp.path, "protected-stream.txt")
  294. const content = "secret stream content"
  295. const stream = new ReadableStream({
  296. start(controller) {
  297. controller.enqueue(new TextEncoder().encode(content))
  298. controller.close()
  299. },
  300. })
  301. await Filesystem.writeStream(filepath, stream, 0o600)
  302. const stats = await fs.stat(filepath)
  303. if (process.platform !== "win32") {
  304. expect(stats.mode & 0o777).toBe(0o600)
  305. }
  306. })
  307. test("writes executable with permissions", async () => {
  308. await using tmp = await tmpdir()
  309. const filepath = path.join(tmp.path, "script.sh")
  310. const content = "#!/bin/bash\necho hello"
  311. const stream = new ReadableStream({
  312. start(controller) {
  313. controller.enqueue(new TextEncoder().encode(content))
  314. controller.close()
  315. },
  316. })
  317. await Filesystem.writeStream(filepath, stream, 0o755)
  318. const stats = await fs.stat(filepath)
  319. if (process.platform !== "win32") {
  320. expect(stats.mode & 0o777).toBe(0o755)
  321. }
  322. expect(await fs.readFile(filepath, "utf-8")).toBe(content)
  323. })
  324. })
  325. })