|
1 | 1 | import { describe, it, expect, vi, beforeEach } from "vitest"; |
2 | | -import { createFile, listFiles, pathExists } from "../src/commands/files.js"; |
| 2 | +import { createFile, listFiles, pathExists, makeDirectory } from "../src/commands/files.js"; |
3 | 3 | import chalk from "chalk"; |
4 | 4 | import * as PuterModule from "../src/modules/PuterModule.js"; |
5 | 5 | import * as auth from "../src/commands/auth.js"; |
@@ -285,3 +285,80 @@ describe("listFiles", () => { |
285 | 285 | expect(commons.resolvePath).toHaveBeenCalledWith("/testuser/files", "."); |
286 | 286 | }); |
287 | 287 | }); |
| 288 | + |
| 289 | +describe("makeDirectory", () => { |
| 290 | + beforeEach(() => { |
| 291 | + vi.clearAllMocks(); |
| 292 | + vi.spyOn(PuterModule, "getPuter").mockReturnValue(mockPuter); |
| 293 | + vi.spyOn(auth, "getCurrentDirectory").mockReturnValue("/testuser/files"); |
| 294 | + }); |
| 295 | + |
| 296 | + it("should show usage when no arguments provided", async () => { |
| 297 | + await makeDirectory([]); |
| 298 | + |
| 299 | + expect(console.log).toHaveBeenCalledWith( |
| 300 | + chalk.red("Usage: mkdir <directory_name>") |
| 301 | + ); |
| 302 | + expect(mockPuter.fs.mkdir).not.toHaveBeenCalled(); |
| 303 | + }); |
| 304 | + |
| 305 | + it("should create directory successfully", async () => { |
| 306 | + mockPuter.fs.mkdir.mockResolvedValue({ |
| 307 | + id: "new-dir-id", |
| 308 | + path: "/testuser/files/newdir", |
| 309 | + uid: "dir-uid-123", |
| 310 | + }); |
| 311 | + |
| 312 | + await makeDirectory(["newdir"]); |
| 313 | + |
| 314 | + expect(mockPuter.fs.mkdir).toHaveBeenCalledWith("/testuser/files/newdir", { |
| 315 | + overwrite: false, |
| 316 | + dedupeName: true, |
| 317 | + createMissingParents: false, |
| 318 | + }); |
| 319 | + expect(console.log).toHaveBeenCalledWith( |
| 320 | + expect.stringContaining('Directory "newdir" created successfully!') |
| 321 | + ); |
| 322 | + expect(console.log).toHaveBeenCalledWith( |
| 323 | + expect.stringContaining("Path: /testuser/files/newdir") |
| 324 | + ); |
| 325 | + expect(console.log).toHaveBeenCalledWith( |
| 326 | + expect.stringContaining("UID: dir-uid-123") |
| 327 | + ); |
| 328 | + }); |
| 329 | + |
| 330 | + it("should handle case when mkdir returns invalid response", async () => { |
| 331 | + mockPuter.fs.mkdir.mockResolvedValue({}); |
| 332 | + |
| 333 | + await makeDirectory(["newdir"]); |
| 334 | + |
| 335 | + expect(mockPuter.fs.mkdir).toHaveBeenCalled(); |
| 336 | + expect(console.log).toHaveBeenCalledWith( |
| 337 | + chalk.red("Failed to create directory. Please check your input.") |
| 338 | + ); |
| 339 | + }); |
| 340 | + |
| 341 | + it("should handle case when mkdir returns null", async () => { |
| 342 | + mockPuter.fs.mkdir.mockResolvedValue(null); |
| 343 | + |
| 344 | + await makeDirectory(["newdir"]); |
| 345 | + |
| 346 | + expect(mockPuter.fs.mkdir).toHaveBeenCalled(); |
| 347 | + expect(console.log).toHaveBeenCalledWith( |
| 348 | + chalk.red("Failed to create directory. Please check your input.") |
| 349 | + ); |
| 350 | + }); |
| 351 | + |
| 352 | + it("should handle error when mkdir throws", async () => { |
| 353 | + mockPuter.fs.mkdir.mockRejectedValue(new Error("Permission denied")); |
| 354 | + |
| 355 | + await makeDirectory(["newdir"]); |
| 356 | + |
| 357 | + expect(console.log).toHaveBeenCalledWith( |
| 358 | + chalk.red("Failed to create directory.") |
| 359 | + ); |
| 360 | + expect(console.error).toHaveBeenCalledWith( |
| 361 | + chalk.red("Error: Permission denied") |
| 362 | + ); |
| 363 | + }); |
| 364 | +}); |
0 commit comments