|
1 | 1 | import { describe, it, expect, vi, beforeEach } from "vitest"; |
2 | | -import { createFile } from "../src/commands/files.js"; |
| 2 | +import { createFile, listFiles, pathExists } 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"; |
6 | 6 | import * as commons from "../src/commons.js"; |
7 | 7 | import path from "path"; |
| 8 | +import * as utils from "../src/utils.js"; |
8 | 9 |
|
9 | 10 | // Mock console to prevent actual logging |
10 | 11 | vi.spyOn(console, "log").mockImplementation(() => {}); |
@@ -32,13 +33,15 @@ vi.mock("conf", () => { |
32 | 33 | vi.mock("../src/modules/PuterModule.js"); |
33 | 34 | vi.mock("../src/commands/auth.js"); |
34 | 35 | vi.mock("../src/commons.js"); |
| 36 | +vi.mock("../src/utils.js"); |
35 | 37 |
|
36 | 38 | const mockPuter = { |
37 | 39 | fs: { |
38 | 40 | stat: vi.fn(), |
39 | 41 | space: vi.fn(), |
40 | 42 | mkdir: vi.fn(), |
41 | 43 | upload: vi.fn(), |
| 44 | + readdir: vi.fn(), |
42 | 45 | }, |
43 | 46 | }; |
44 | 47 |
|
@@ -149,3 +152,136 @@ describe("createFile", () => { |
149 | 152 | ); |
150 | 153 | }); |
151 | 154 | }); |
| 155 | + |
| 156 | +describe("listFiles", () => { |
| 157 | + beforeEach(() => { |
| 158 | + vi.clearAllMocks(); |
| 159 | + vi.spyOn(PuterModule, "getPuter").mockReturnValue(mockPuter); |
| 160 | + vi.spyOn(auth, "getCurrentUserName").mockReturnValue("testuser"); |
| 161 | + vi.spyOn(auth, "getCurrentDirectory").mockReturnValue("/testuser/files"); |
| 162 | + vi.spyOn(commons, "resolvePath").mockImplementation((current, newPath) => |
| 163 | + path.join(current, newPath) |
| 164 | + ); |
| 165 | + vi.spyOn(utils, "formatSize").mockImplementation((size) => `${size}B`); |
| 166 | + vi.spyOn(utils, "formatDateTime").mockImplementation(() => "2024-01-01 12:00"); |
| 167 | + }); |
| 168 | + |
| 169 | + it("should list files successfully with mocked items", async () => { |
| 170 | + const mockFiles = [ |
| 171 | + { name: "file1.txt", is_dir: false, writable: true, size: 1024, modified: 1704067200, uid: "abc-123-def-456" }, |
| 172 | + { name: "folder1", is_dir: true, writable: true, size: 0, modified: 1704067200, uid: "ghi-789-jkl-012" }, |
| 173 | + ]; |
| 174 | + |
| 175 | + mockPuter.fs.stat.mockResolvedValue({ id: "dir-id" }); |
| 176 | + mockPuter.fs.readdir.mockResolvedValue(mockFiles); |
| 177 | + |
| 178 | + await listFiles(["/testuser/files"]); |
| 179 | + |
| 180 | + expect(console.log).toHaveBeenCalledWith( |
| 181 | + expect.stringContaining("Listing files in") |
| 182 | + ); |
| 183 | + expect(console.log).toHaveBeenCalledWith( |
| 184 | + expect.stringContaining("file1.txt") |
| 185 | + ); |
| 186 | + expect(console.log).toHaveBeenCalledWith( |
| 187 | + expect.stringContaining("folder1") |
| 188 | + ); |
| 189 | + expect(console.log).toHaveBeenCalledWith( |
| 190 | + expect.stringContaining("There are 2 object(s).") |
| 191 | + ); |
| 192 | + }); |
| 193 | + |
| 194 | + it("should handle empty directory", async () => { |
| 195 | + mockPuter.fs.stat.mockResolvedValue({ id: "dir-id" }); |
| 196 | + mockPuter.fs.readdir.mockResolvedValue([]); |
| 197 | + |
| 198 | + await listFiles(["/testuser/files"]); |
| 199 | + |
| 200 | + expect(console.log).toHaveBeenCalledWith( |
| 201 | + chalk.red("No files or directories found.") |
| 202 | + ); |
| 203 | + }); |
| 204 | + |
| 205 | + it("should handle error when listing files fails", async () => { |
| 206 | + mockPuter.fs.stat.mockResolvedValue({ id: "dir-id" }); |
| 207 | + mockPuter.fs.readdir.mockRejectedValue(new Error("Network error")); |
| 208 | + |
| 209 | + await listFiles(["/testuser/files"]); |
| 210 | + |
| 211 | + expect(console.log).toHaveBeenCalledWith( |
| 212 | + chalk.red("Failed to list files.") |
| 213 | + ); |
| 214 | + expect(console.error).toHaveBeenCalledWith( |
| 215 | + chalk.red("Error: Network error") |
| 216 | + ); |
| 217 | + }); |
| 218 | + |
| 219 | + it("should resolve relative path using getCurrentDirectory", async () => { |
| 220 | + const mockFiles = [ |
| 221 | + { name: "test.txt", is_dir: false, writable: true, size: 512, modified: 1704067200, uid: "xyz-999-abc-111" }, |
| 222 | + ]; |
| 223 | + |
| 224 | + mockPuter.fs.stat.mockResolvedValue({ id: "dir-id" }); |
| 225 | + mockPuter.fs.readdir.mockResolvedValue(mockFiles); |
| 226 | + |
| 227 | + await listFiles(["subdir"]); |
| 228 | + |
| 229 | + expect(commons.resolvePath).toHaveBeenCalledWith("/testuser/files", "subdir"); |
| 230 | + expect(mockPuter.fs.readdir).toHaveBeenCalledWith("/testuser/files/subdir"); |
| 231 | + }); |
| 232 | + |
| 233 | + it("should use absolute path directly without resolving", async () => { |
| 234 | + const mockFiles = [ |
| 235 | + { name: "test.txt", is_dir: false, writable: true, size: 512, modified: 1704067200, uid: "xyz-999-abc-111" }, |
| 236 | + ]; |
| 237 | + |
| 238 | + mockPuter.fs.stat.mockResolvedValue({ id: "dir-id" }); |
| 239 | + mockPuter.fs.readdir.mockResolvedValue(mockFiles); |
| 240 | + |
| 241 | + await listFiles(["/absolute/path"]); |
| 242 | + |
| 243 | + expect(commons.resolvePath).not.toHaveBeenCalled(); |
| 244 | + expect(mockPuter.fs.readdir).toHaveBeenCalledWith("/absolute/path"); |
| 245 | + }); |
| 246 | + |
| 247 | + it("should show message when path does not exist", async () => { |
| 248 | + mockPuter.fs.stat.mockRejectedValue({ code: "subject_does_not_exist" }); |
| 249 | + |
| 250 | + await listFiles(["/nonexistent/path"]); |
| 251 | + |
| 252 | + expect(console.log).toHaveBeenCalledWith( |
| 253 | + expect.stringContaining("doesn't exists!") |
| 254 | + ); |
| 255 | + expect(mockPuter.fs.readdir).not.toHaveBeenCalled(); |
| 256 | + }); |
| 257 | + |
| 258 | + it("should list files when path exists", async () => { |
| 259 | + const mockFiles = [ |
| 260 | + { name: "exists.txt", is_dir: false, writable: false, size: 256, modified: 1704067200, uid: "aaa-bbb-ccc-ddd" }, |
| 261 | + ]; |
| 262 | + |
| 263 | + mockPuter.fs.stat.mockResolvedValue({ id: "existing-dir" }); |
| 264 | + mockPuter.fs.readdir.mockResolvedValue(mockFiles); |
| 265 | + |
| 266 | + await listFiles(["/existing/path"]); |
| 267 | + |
| 268 | + expect(mockPuter.fs.stat).toHaveBeenCalledWith("/existing/path"); |
| 269 | + expect(mockPuter.fs.readdir).toHaveBeenCalledWith("/existing/path"); |
| 270 | + expect(console.log).toHaveBeenCalledWith( |
| 271 | + expect.stringContaining("exists.txt") |
| 272 | + ); |
| 273 | + }); |
| 274 | + |
| 275 | + it("should use current directory when no arguments provided", async () => { |
| 276 | + const mockFiles = [ |
| 277 | + { name: "default.txt", is_dir: false, writable: true, size: 100, modified: 1704067200, uid: "def-ault-uid-000" }, |
| 278 | + ]; |
| 279 | + |
| 280 | + mockPuter.fs.stat.mockResolvedValue({ id: "dir-id" }); |
| 281 | + mockPuter.fs.readdir.mockResolvedValue(mockFiles); |
| 282 | + |
| 283 | + await listFiles([]); |
| 284 | + |
| 285 | + expect(commons.resolvePath).toHaveBeenCalledWith("/testuser/files", "."); |
| 286 | + }); |
| 287 | +}); |
0 commit comments