11import { describe , it , expect , vi , beforeEach } from "vitest" ;
2- import { createFile , listFiles , pathExists , makeDirectory , renameFileOrDirectory , getInfo } from "../src/commands/files.js" ;
2+ import { createFile , listFiles , pathExists , makeDirectory , renameFileOrDirectory , getInfo , showCwd , getDiskUsage } from "../src/commands/files.js" ;
33import chalk from "chalk" ;
44import * as PuterModule from "../src/modules/PuterModule.js" ;
55import * as auth from "../src/commands/auth.js" ;
@@ -23,10 +23,11 @@ vi.mock("chalk", () => ({
2323 } ,
2424} ) ) ;
2525vi . mock ( "node-fetch" ) ;
26+ const mockConfigStore = { } ;
2627vi . mock ( "conf" , ( ) => {
2728 const Conf = vi . fn ( ( ) => ( {
28- get : vi . fn ( ) ,
29- set : vi . fn ( ) ,
29+ get : vi . fn ( ( key ) => mockConfigStore [ key ] ) ,
30+ set : vi . fn ( ( key , value ) => { mockConfigStore [ key ] = value ; } ) ,
3031 clear : vi . fn ( ) ,
3132 } ) ) ;
3233 return { default : Conf } ;
@@ -531,3 +532,109 @@ describe("getInfo", () => {
531532 expect ( mockPuter . fs . stat ) . toHaveBeenCalledWith ( "/testuser/files/." ) ;
532533 } ) ;
533534} ) ;
535+
536+ describe ( "showCwd" , ( ) => {
537+ beforeEach ( ( ) => {
538+ vi . clearAllMocks ( ) ;
539+ } ) ;
540+
541+ it ( "should display current working directory from config" , async ( ) => {
542+ mockConfigStore . cwd = "/testuser/documents" ;
543+
544+ await showCwd ( ) ;
545+
546+ expect ( console . log ) . toHaveBeenCalledWith (
547+ chalk . green ( "/testuser/documents" )
548+ ) ;
549+ } ) ;
550+ } ) ;
551+
552+ describe ( "getDiskUsage" , ( ) => {
553+ beforeEach ( ( ) => {
554+ vi . clearAllMocks ( ) ;
555+ vi . spyOn ( PuterModule , "getPuter" ) . mockReturnValue ( mockPuter ) ;
556+ vi . spyOn ( commons , "showDiskSpaceUsage" ) . mockImplementation ( ( ) => { } ) ;
557+ } ) ;
558+
559+ it ( "should fetch and display disk usage successfully" , async ( ) => {
560+ const mockSpaceData = { used : 500 , capacity : 1000 } ;
561+ mockPuter . fs . space . mockResolvedValue ( mockSpaceData ) ;
562+
563+ await getDiskUsage ( ) ;
564+
565+ expect ( mockPuter . fs . space ) . toHaveBeenCalled ( ) ;
566+ expect ( commons . showDiskSpaceUsage ) . toHaveBeenCalledWith ( mockSpaceData ) ;
567+ } ) ;
568+
569+ it ( "should handle unable to fetch disk usage" , async ( ) => {
570+ mockPuter . fs . space . mockResolvedValue ( null ) ;
571+
572+ await getDiskUsage ( ) ;
573+
574+ expect ( mockPuter . fs . space ) . toHaveBeenCalled ( ) ;
575+ expect ( commons . showDiskSpaceUsage ) . not . toHaveBeenCalled ( ) ;
576+ expect ( console . error ) . toHaveBeenCalledWith (
577+ chalk . red ( "Unable to fetch disk usage information." )
578+ ) ;
579+ } ) ;
580+
581+ it ( "should handle error when fetching disk usage" , async ( ) => {
582+ mockPuter . fs . space . mockRejectedValue ( new Error ( "Network failure" ) ) ;
583+
584+ await getDiskUsage ( ) ;
585+
586+ expect ( mockPuter . fs . space ) . toHaveBeenCalled ( ) ;
587+ expect ( commons . showDiskSpaceUsage ) . not . toHaveBeenCalled ( ) ;
588+ expect ( console . error ) . toHaveBeenCalledWith (
589+ chalk . red ( "Failed to fetch disk usage information.\nError: Network failure" )
590+ ) ;
591+ } ) ;
592+ } ) ;
593+
594+ describe ( "pathExists" , ( ) => {
595+ beforeEach ( ( ) => {
596+ vi . clearAllMocks ( ) ;
597+ vi . spyOn ( PuterModule , "getPuter" ) . mockReturnValue ( mockPuter ) ;
598+ } ) ;
599+
600+ it ( "should return false when no path provided" , async ( ) => {
601+ const result = await pathExists ( "" ) ;
602+
603+ expect ( result ) . toBe ( false ) ;
604+ expect ( console . log ) . toHaveBeenCalledWith (
605+ chalk . red ( "No path provided." )
606+ ) ;
607+ expect ( mockPuter . fs . stat ) . not . toHaveBeenCalled ( ) ;
608+ } ) ;
609+
610+ it ( "should return true when path exists" , async ( ) => {
611+ mockPuter . fs . stat . mockResolvedValue ( { id : "file-id" } ) ;
612+
613+ const result = await pathExists ( "/testuser/files/existing.txt" ) ;
614+
615+ expect ( result ) . toBe ( true ) ;
616+ expect ( mockPuter . fs . stat ) . toHaveBeenCalledWith ( "/testuser/files/existing.txt" ) ;
617+ } ) ;
618+
619+ it ( "should return false when subject does not exist" , async ( ) => {
620+ mockPuter . fs . stat . mockRejectedValue ( { code : "subject_does_not_exist" } ) ;
621+
622+ const result = await pathExists ( "/testuser/files/nonexistent.txt" ) ;
623+
624+ expect ( result ) . toBe ( false ) ;
625+ expect ( mockPuter . fs . stat ) . toHaveBeenCalledWith ( "/testuser/files/nonexistent.txt" ) ;
626+ } ) ;
627+
628+ it ( "should return false and log error on other errors" , async ( ) => {
629+ const otherError = new Error ( "Unknown error" ) ;
630+ mockPuter . fs . stat . mockRejectedValue ( otherError ) ;
631+
632+ const result = await pathExists ( "/testuser/files/error.txt" ) ;
633+
634+ expect ( result ) . toBe ( false ) ;
635+ expect ( console . error ) . toHaveBeenCalledWith (
636+ chalk . red ( "Failed to check if file exists." )
637+ ) ;
638+ expect ( console . error ) . toHaveBeenCalledWith ( "ERROR" , otherError ) ;
639+ } ) ;
640+ } ) ;
0 commit comments