Skip to content

Commit 8823857

Browse files
Add test getInfo
1 parent 6b67825 commit 8823857

1 file changed

Lines changed: 79 additions & 1 deletion

File tree

tests/files.test.js

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect, vi, beforeEach } from "vitest";
2-
import { createFile, listFiles, pathExists, makeDirectory, renameFileOrDirectory } from "../src/commands/files.js";
2+
import { createFile, listFiles, pathExists, makeDirectory, renameFileOrDirectory, getInfo } from "../src/commands/files.js";
33
import chalk from "chalk";
44
import * as PuterModule from "../src/modules/PuterModule.js";
55
import * as auth from "../src/commands/auth.js";
@@ -19,6 +19,7 @@ vi.mock("chalk", () => ({
1919
dim: vi.fn((text) => text),
2020
yellow: vi.fn((text) => text),
2121
cyan: vi.fn((text) => text),
22+
white: vi.fn((text) => text),
2223
},
2324
}));
2425
vi.mock("node-fetch");
@@ -453,3 +454,80 @@ describe("renameFileOrDirectory", () => {
453454
expect(mockPuter.fs.rename).toHaveBeenCalledWith("abs-uid", "dest.txt");
454455
});
455456
});
457+
458+
describe("getInfo", () => {
459+
beforeEach(() => {
460+
vi.clearAllMocks();
461+
vi.spyOn(PuterModule, "getPuter").mockReturnValue(mockPuter);
462+
vi.spyOn(auth, "getCurrentDirectory").mockReturnValue("/testuser/files");
463+
vi.spyOn(utils, "formatSize").mockImplementation((size) => `${size}B`);
464+
});
465+
466+
it("should display file info successfully", async () => {
467+
mockPuter.fs.stat.mockResolvedValue({
468+
name: "test.txt",
469+
path: "/testuser/files/test.txt",
470+
is_dir: false,
471+
size: 1024,
472+
created: 1704067200,
473+
modified: 1704153600,
474+
writable: true,
475+
owner: { username: "testuser" },
476+
});
477+
478+
await getInfo(["test.txt"]);
479+
480+
expect(mockPuter.fs.stat).toHaveBeenCalledWith("/testuser/files/test.txt");
481+
expect(console.log).toHaveBeenCalledWith(
482+
expect.stringContaining("Getting stat info for")
483+
);
484+
expect(console.log).toHaveBeenCalledWith(
485+
expect.stringContaining("Name: ")
486+
);
487+
expect(console.log).toHaveBeenCalledWith(
488+
expect.stringContaining("Path: ")
489+
);
490+
expect(console.log).toHaveBeenCalledWith(
491+
expect.stringContaining("Type: ")
492+
);
493+
});
494+
495+
it("should handle case when stat returns null", async () => {
496+
mockPuter.fs.stat.mockResolvedValue(null);
497+
498+
await getInfo(["notfound.txt"]);
499+
500+
expect(mockPuter.fs.stat).toHaveBeenCalledWith("/testuser/files/notfound.txt");
501+
expect(console.error).toHaveBeenCalledWith(
502+
chalk.red("Unable to get stat info. Please check your credentials.")
503+
);
504+
});
505+
506+
it("should handle error when stat throws", async () => {
507+
mockPuter.fs.stat.mockRejectedValue(new Error("File not found"));
508+
509+
await getInfo(["error.txt"]);
510+
511+
expect(mockPuter.fs.stat).toHaveBeenCalledWith("/testuser/files/error.txt");
512+
expect(console.error).toHaveBeenCalledWith(
513+
chalk.red("Failed to get stat info.\nError: File not found")
514+
);
515+
});
516+
517+
it("should use current directory with default argument", async () => {
518+
mockPuter.fs.stat.mockResolvedValue({
519+
name: "files",
520+
path: "/testuser/files",
521+
is_dir: true,
522+
size: 0,
523+
created: 1704067200,
524+
modified: 1704153600,
525+
writable: true,
526+
owner: { username: "testuser" },
527+
});
528+
529+
await getInfo([]);
530+
531+
expect(mockPuter.fs.stat).toHaveBeenCalledWith("/testuser/files/.");
532+
});
533+
});

0 commit comments

Comments
 (0)