11import { describe , it , expect , vi , beforeEach } from 'vitest' ;
2- import { createStorageClient , StorageClientImpl , StorageClientInterface } from '../../src/clients/storage' ;
2+ import { createStorageClient , StorageClientInstanceImpl , StorageClientInterface } from '../../src/clients/storage' ;
33import * as Storage from '@web3-storage/w3up-client' ;
44import { StoreMemory } from '@web3-storage/w3up-client/stores/memory' ;
55import { defaultGatewayUrl } from '../../src/utils' ;
@@ -62,27 +62,31 @@ vi.mock('../../src/environments', () => ({
6262 } )
6363} ) ) ;
6464
65+ // Mock fetch for getContent tests
66+ global . fetch = vi . fn ( ) ;
67+
6568// Clear mocks once before all tests
6669beforeEach ( ( ) => {
6770 vi . clearAllMocks ( ) ;
6871} ) ;
6972
7073describe ( 'StorageClientImpl' , ( ) => {
71- let storageClientImpl : StorageClientImpl ;
74+ let storageClientImpl : StorageClientInstanceImpl ;
7275 let mockRuntime : any ;
7376
7477 beforeEach ( ( ) => {
7578 mockRuntime = {
7679 getParameter : vi . fn ( ) ,
7780 } ;
7881 console . log = vi . fn ( ) ; // Mock console.log to avoid cluttering test output
79- storageClientImpl = new StorageClientImpl ( mockRuntime ) ;
82+ storageClientImpl = new StorageClientInstanceImpl ( mockRuntime ) ;
83+ ( global . fetch as any ) . mockReset ( ) ;
8084 } ) ;
8185
8286 it ( 'should initialize correctly' , async ( ) => {
8387 await storageClientImpl . start ( ) ;
8488
85- expect ( storageClientImpl . getStorageClient ( ) ) . not . toBeNull ( ) ;
89+ expect ( storageClientImpl . getStorage ( ) ) . not . toBeNull ( ) ;
8690 expect ( elizaLogger . success ) . toHaveBeenCalledWith ( '✅ Storage client successfully started' ) ;
8791 } ) ;
8892
@@ -106,12 +110,12 @@ describe('StorageClientImpl', () => {
106110 } ) ;
107111
108112 it ( 'should throw error if client is not initialized when getting storage client' , ( ) => {
109- expect ( ( ) => storageClientImpl . getStorageClient ( ) ) . toThrow ( 'Storage client not initialized' ) ;
113+ expect ( ( ) => storageClientImpl . getStorage ( ) ) . toThrow ( 'Storage client not initialized' ) ;
110114 } ) ;
111115
112116 it ( 'should return the storage client when initialized' , async ( ) => {
113117 await storageClientImpl . start ( ) ;
114- expect ( storageClientImpl . getStorageClient ( ) ) . not . toBeNull ( ) ;
118+ expect ( storageClientImpl . getStorage ( ) ) . not . toBeNull ( ) ;
115119 } ) ;
116120
117121 it ( 'should throw error if client is not initialized when getting config' , ( ) => {
@@ -138,13 +142,48 @@ describe('StorageClientImpl', () => {
138142
139143 it ( 'should properly clean up resources when stopped' , async ( ) => {
140144 await storageClientImpl . start ( ) ;
141- expect ( storageClientImpl . getStorageClient ( ) ) . not . toBeNull ( ) ;
145+ expect ( storageClientImpl . getStorage ( ) ) . not . toBeNull ( ) ;
142146
143147 await storageClientImpl . stop ( ) ;
144148
145- expect ( ( ) => storageClientImpl . getStorageClient ( ) ) . toThrow ( 'Storage client not initialized' ) ;
149+ expect ( ( ) => storageClientImpl . getStorage ( ) ) . toThrow ( 'Storage client not initialized' ) ;
146150 expect ( ( ) => storageClientImpl . getConfig ( ) ) . toThrow ( 'Storage client not initialized' ) ;
147151 } ) ;
152+
153+ describe ( 'getContent' , ( ) => {
154+ it ( 'should fetch content from the configured gateway URL' , async ( ) => {
155+ await storageClientImpl . start ( ) ;
156+ const testCid = 'bafytest123' ;
157+ const mockResponse = new Response ( 'mock content' ) ;
158+ ( global . fetch as any ) . mockResolvedValueOnce ( mockResponse ) ;
159+
160+ const result = await storageClientImpl . getContent ( testCid ) ;
161+
162+ expect ( global . fetch ) . toHaveBeenCalledWith ( 'https://mock-gateway.link/ipfs/bafytest123' ) ;
163+ expect ( result ) . toBe ( mockResponse ) ;
164+ } ) ;
165+
166+ it ( 'should fetch content from the default gateway URL when not configured' , async ( ) => {
167+ storageClientImpl . config = null ; // No config set
168+ const testCid = 'bafytest123' ;
169+ const mockResponse = new Response ( 'mock content' ) ;
170+ ( global . fetch as any ) . mockResolvedValueOnce ( mockResponse ) ;
171+
172+ const result = await storageClientImpl . getContent ( testCid ) ;
173+
174+ expect ( global . fetch ) . toHaveBeenCalledWith ( `${ defaultGatewayUrl } /ipfs/bafytest123` ) ;
175+ expect ( result ) . toBe ( mockResponse ) ;
176+ } ) ;
177+
178+ it ( 'should propagate fetch errors' , async ( ) => {
179+ await storageClientImpl . start ( ) ;
180+ const testCid = 'bafytest123' ;
181+ const mockError = new Error ( 'Network error' ) ;
182+ ( global . fetch as any ) . mockRejectedValueOnce ( mockError ) ;
183+
184+ await expect ( storageClientImpl . getContent ( testCid ) ) . rejects . toThrow ( 'Network error' ) ;
185+ } ) ;
186+ } ) ;
148187} ) ;
149188
150189describe ( 'StorageClientInterface' , ( ) => {
0 commit comments