22'use strict'
33
44const expect = require ( 'chai' ) . expect
5+ const fs = require ( 'fs' )
6+ const os = require ( 'os' )
7+ const path = require ( 'path' )
58const repoVersion = require ( 'ipfs-repo' ) . repoVersion
9+ const hat = require ( 'hat' )
10+ const clean = require ( '../utils/clean' )
611
712const runOnAndOff = require ( '../utils/on-and-off' )
813
@@ -18,4 +23,34 @@ describe('repo', () => runOnAndOff((thing) => {
1823 expect ( out ) . to . eql ( `${ repoVersion } \n` )
1924 } )
2025 } )
26+
27+ // Note: There are more comprehensive GC tests in interface-js-ipfs-core
28+ it ( 'should run garbage collection' , async ( ) => {
29+ // Create and add a file to IPFS
30+ const filePath = path . join ( os . tmpdir ( ) , hat ( ) )
31+ const content = String ( Math . random ( ) )
32+ fs . writeFileSync ( filePath , content )
33+ const cid = ( await ipfs ( `add -Q ${ filePath } ` ) ) . trim ( )
34+
35+ // File hash should be in refs local
36+ const localRefs = await ipfs ( 'refs local' )
37+ expect ( localRefs . split ( '\n' ) ) . includes ( cid )
38+
39+ // Run GC, file should not have been removed because it's pinned
40+ const gcOut = await ipfs ( 'repo gc' )
41+ expect ( gcOut . split ( '\n' ) ) . not . includes ( 'Removed ' + cid )
42+
43+ // Unpin file
44+ await ipfs ( 'pin rm ' + cid )
45+
46+ // Run GC, file should now be removed
47+ const gcOutAfterUnpin = await ipfs ( 'repo gc' )
48+ expect ( gcOutAfterUnpin . split ( '\n' ) ) . to . includes ( 'Removed ' + cid )
49+
50+ const localRefsAfterGc = await ipfs ( 'refs local' )
51+ expect ( localRefsAfterGc . split ( '\n' ) ) . not . includes ( cid )
52+
53+ // Clean up file
54+ await clean ( filePath )
55+ } )
2156} ) )
0 commit comments