import { CompositeError } from '@linzjs/s3fs' ; import { fsa } from '../index.js' ; export interface MockFs { /**
     * Storage for `FileOperator.readJson` and `writeJson`. If an Error is used as a value then
     * `readJson` will throw the error.
     */ jsStore : Record < string , any > ; /**
     * Storage for `FileOperator.read` and `write`. If an Error is used as a value then
     * `read` will throw the error.
     */ rawStore : Record < string , Buffer | Error > ; /** Use in `o.beforeEach(mockFs.setup)` */ setup ( ) : void ; /** Use in `o.afterEach(mockFs.teardown)` */ teardown ( ) : void ; } /**
 * Create an interface for stubbing `FileOperator.read,write,readJson,writeJson`
 */ export function mockFileOperator ( ) { const origWrite = fsa . write ; const origRead = fsa . read ; const origReadJson = fsa . readJson ; const origWriteJson = fsa . writeJson ; let jsStore = { } ; const mockReadJson = async ( path ) : Promise < any > => { const ans = jsStore [ path ] ?? new CompositeError ( 'Not Found' , 404 , new Error ( 'Mock' ) ) ; if ( ans instanceof Error ) throw ans ; return ans ; } ; const mockWriteJson = async ( path , json ) : Promise < void > => { jsStore [ path ] = json ; } ; let rawStore = { } ; const mockRead = async ( path ) : Promise < Buffer > => { const ans = rawStore [ path ] ?? new CompositeError ( 'Not Found' , 404 , new Error ( 'Mock' ) ) ; if ( ans instanceof Error ) throw ans ; return ans ; } ; const mockWrite = async ( path , buffer ) : Promise < void > => { rawStore [ path ] = buffer ; } ; return { get jsStore ( ) : Record < string , any > { return jsStore ; } , get rawStore ( ) : Record < string , Buffer | Error > { return rawStore ; } , setup ( ) { fsa . read = mockRead ; fsa . write = mockWrite ; fsa . readJson = mockReadJson ; fsa . writeJson = mockWriteJson ; jsStore = { } ; rawStore = { } ; } , teardown ( ) { fsa . read = origRead ; fsa . write = origWrite ; fsa . readJson = origReadJson ; fsa . writeJson = origWriteJson ; } , } ; }