11import { describe , it , expect , vi , beforeEach , afterEach } from 'vitest' ;
2+ import { getAuthToken } from '../src/commands/auth.js' ;
3+ import { formatSize } from '../src/utils.js' ;
4+ import { readFile } from 'fs/promises' ;
25
3- vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
4- vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
5-
6- const mockGetAuthToken = vi . hoisted ( ( ) => vi . fn ( ( ) => 'mock-token' ) ) ;
7- const mockFormatSize = vi . hoisted ( ( ) => vi . fn ( ( size ) => `${ size } bytes` ) ) ;
8- const mockReadFile = vi . hoisted ( ( ) => vi . fn ( ) ) ;
9-
10- vi . mock ( '../src/commands/auth.js' , ( ) => ( {
11- getAuthToken : mockGetAuthToken ,
12- } ) ) ;
13-
14- vi . mock ( '../src/utils.js' , ( ) => ( {
15- formatSize : mockFormatSize ,
16- } ) ) ;
17-
18- vi . mock ( 'fs/promises' , ( ) => ( {
19- readFile : mockReadFile ,
20- } ) ) ;
21-
6+ vi . mock ( '../src/commands/auth.js' ) ;
7+ vi . mock ( '../src/utils.js' ) ;
8+ vi . mock ( 'fs/promises' ) ;
229vi . mock ( 'dotenv' , ( ) => ( {
2310 default : {
2411 config : vi . fn ( ) ,
2512 } ,
2613} ) ) ;
2714
15+ vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
16+ vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
17+
2818let commons ;
2919
3020beforeEach ( async ( ) => {
3121 vi . resetModules ( ) ;
32- mockGetAuthToken . mockReturnValue ( 'mock-token' ) ;
33- commons = await import ( '../src/commons.js' ) ;
34- } ) ;
35-
36- afterEach ( ( ) => {
3722 vi . clearAllMocks ( ) ;
23+
24+ vi . mocked ( getAuthToken ) . mockReturnValue ( 'mock-token' ) ;
25+ vi . mocked ( formatSize ) . mockImplementation ( ( size ) => `${ size } bytes` ) ;
26+
27+ commons = await import ( '../src/commons.js' ) ;
3828} ) ;
3929
4030describe ( 'constants' , ( ) => {
@@ -167,9 +157,9 @@ describe('showDiskSpaceUsage', () => {
167157 commons . showDiskSpaceUsage ( data ) ;
168158
169159 expect ( consoleLogSpy ) . toHaveBeenCalled ( ) ;
170- expect ( mockFormatSize ) . toHaveBeenCalledWith ( '1000000000' ) ; // capacity
171- expect ( mockFormatSize ) . toHaveBeenCalledWith ( '500000000' ) ; // used
172- expect ( mockFormatSize ) . toHaveBeenCalledWith ( 500000000 ) ; // free space
160+ expect ( formatSize ) . toHaveBeenCalledWith ( '1000000000' ) ; // capacity
161+ expect ( formatSize ) . toHaveBeenCalledWith ( '500000000' ) ; // used
162+ expect ( formatSize ) . toHaveBeenCalledWith ( 500000000 ) ; // free space
173163 } ) ;
174164
175165 it ( 'should calculate usage percentage correctly' , ( ) => {
@@ -312,15 +302,15 @@ describe('getDefaultHomePage', () => {
312302
313303describe ( 'getVersionFromPackage' , ( ) => {
314304 it ( 'should return version from package.json' , async ( ) => {
315- mockReadFile . mockResolvedValueOnce ( JSON . stringify ( { version : '1.2.3' } ) ) ;
305+ vi . mocked ( readFile ) . mockResolvedValueOnce ( JSON . stringify ( { version : '1.2.3' } ) ) ;
316306
317307 const version = await commons . getVersionFromPackage ( ) ;
318308
319309 expect ( version ) . toBe ( '1.2.3' ) ;
320310 } ) ;
321311
322312 it ( 'should fallback to production package.json on dev error' , async ( ) => {
323- mockReadFile
313+ vi . mocked ( readFile )
324314 . mockRejectedValueOnce ( new Error ( 'File not found' ) )
325315 . mockResolvedValueOnce ( JSON . stringify ( { version : '2.0.0' } ) ) ;
326316
@@ -330,7 +320,7 @@ describe('getVersionFromPackage', () => {
330320 } ) ;
331321
332322 it ( 'should return null on error' , async ( ) => {
333- mockReadFile . mockRejectedValue ( new Error ( 'Read error' ) ) ;
323+ vi . mocked ( readFile ) . mockRejectedValue ( new Error ( 'Read error' ) ) ;
334324
335325 const version = await commons . getVersionFromPackage ( ) ;
336326
@@ -340,16 +330,12 @@ describe('getVersionFromPackage', () => {
340330
341331describe ( 'getLatestVersion' , ( ) => {
342332 beforeEach ( ( ) => {
343- vi . stubGlobal ( 'fetch' , vi . fn ( ) ) ;
344- } ) ;
345-
346- afterEach ( ( ) => {
347- vi . unstubAllGlobals ( ) ;
333+ global . fetch = vi . fn ( ) ;
348334 } ) ;
349335
350336 it ( 'should return up-to-date status when versions match' , async ( ) => {
351- mockReadFile . mockResolvedValueOnce ( JSON . stringify ( { version : '1.0.0' } ) ) ;
352- global . fetch . mockResolvedValueOnce ( {
337+ vi . mocked ( readFile ) . mockResolvedValueOnce ( JSON . stringify ( { version : '1.0.0' } ) ) ;
338+ vi . mocked ( global . fetch ) . mockResolvedValueOnce ( {
353339 ok : true ,
354340 json : ( ) => Promise . resolve ( { version : '1.0.0' } ) ,
355341 } ) ;
@@ -360,8 +346,8 @@ describe('getLatestVersion', () => {
360346 } ) ;
361347
362348 it ( 'should return latest version when different' , async ( ) => {
363- mockReadFile . mockResolvedValueOnce ( JSON . stringify ( { version : '1.0.0' } ) ) ;
364- global . fetch . mockResolvedValueOnce ( {
349+ vi . mocked ( readFile ) . mockResolvedValueOnce ( JSON . stringify ( { version : '1.0.0' } ) ) ;
350+ vi . mocked ( global . fetch ) . mockResolvedValueOnce ( {
365351 ok : true ,
366352 json : ( ) => Promise . resolve ( { version : '2.0.0' } ) ,
367353 } ) ;
@@ -372,17 +358,17 @@ describe('getLatestVersion', () => {
372358 } ) ;
373359
374360 it ( 'should return offline status when fetch fails' , async ( ) => {
375- mockReadFile . mockResolvedValueOnce ( JSON . stringify ( { version : '1.0.0' } ) ) ;
376- global . fetch . mockRejectedValueOnce ( new Error ( 'Network error' ) ) ;
361+ vi . mocked ( readFile ) . mockResolvedValueOnce ( JSON . stringify ( { version : '1.0.0' } ) ) ;
362+ vi . mocked ( global . fetch ) . mockRejectedValueOnce ( new Error ( 'Network error' ) ) ;
377363
378364 const result = await commons . getLatestVersion ( 'puter-cli' ) ;
379365
380366 expect ( result ) . toBe ( 'v1.0.0 (offline)' ) ;
381367 } ) ;
382368
383369 it ( 'should handle unknown current version' , async ( ) => {
384- mockReadFile . mockRejectedValue ( new Error ( 'Read error' ) ) ;
385- global . fetch . mockResolvedValueOnce ( {
370+ vi . mocked ( readFile ) . mockRejectedValue ( new Error ( 'Read error' ) ) ;
371+ vi . mocked ( global . fetch ) . mockResolvedValueOnce ( {
386372 ok : true ,
387373 json : ( ) => Promise . resolve ( { version : '2.0.0' } ) ,
388374 } ) ;
0 commit comments