From 233386e31b98190fa13d00f4644da2e44d76bb46 Mon Sep 17 00:00:00 2001 From: "fazeel332@gmail.com" Date: Mon, 22 Sep 2025 02:12:25 +0500 Subject: [PATCH 1/2] fix: StorageInstance.exists returns boolean and LocalStorage handles undefined folder Signed-off-by: fazeel332@gmail.com --- .../connectors/LocalStorage.class.ts | 2 +- .../tests/unit/001-Core/LocalStorage.test.ts | 36 +++++++++++++++++++ .../sdk/src/Storage/StorageInstance.class.ts | 5 ++- 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 packages/core/tests/unit/001-Core/LocalStorage.test.ts diff --git a/packages/core/src/subsystems/IO/Storage.service/connectors/LocalStorage.class.ts b/packages/core/src/subsystems/IO/Storage.service/connectors/LocalStorage.class.ts index 3016e658f..bc2a02ff7 100644 --- a/packages/core/src/subsystems/IO/Storage.service/connectors/LocalStorage.class.ts +++ b/packages/core/src/subsystems/IO/Storage.service/connectors/LocalStorage.class.ts @@ -49,7 +49,7 @@ export class LocalStorage extends StorageConnector { private findStorageFolder(folder) { let _storageFolder = folder; - if (fs.existsSync(_storageFolder)) { + if (_storageFolder && fs.existsSync(_storageFolder)) { return _storageFolder; } diff --git a/packages/core/tests/unit/001-Core/LocalStorage.test.ts b/packages/core/tests/unit/001-Core/LocalStorage.test.ts new file mode 100644 index 000000000..99536e2f0 --- /dev/null +++ b/packages/core/tests/unit/001-Core/LocalStorage.test.ts @@ -0,0 +1,36 @@ +import { LocalStorage } from '@sre/IO/Storage.service/connectors/LocalStorage.class'; +import { describe, expect, it } from 'vitest'; + +describe('LocalStorage Tests', () => { + it('should initialize with undefined folder without crashing', () => { + // Test with undefined settings + expect(() => { + new LocalStorage(); + }).not.toThrow(); + + // Test with empty settings object + expect(() => { + new LocalStorage({}); + }).not.toThrow(); + + // Test with explicitly undefined folder + expect(() => { + new LocalStorage({ folder: undefined }); + }).not.toThrow(); + }); + + it('should initialize with valid folder path', () => { + const tempDir = require('os').tmpdir(); + + expect(() => { + new LocalStorage({ folder: tempDir }); + }).not.toThrow(); + }); + + it('should handle non-existent folder gracefully', () => { + // This should not crash but may log warnings as designed + expect(() => { + new LocalStorage({ folder: '/non/existent/path/that/should/not/exist' }); + }).not.toThrow(); + }); +}); diff --git a/packages/sdk/src/Storage/StorageInstance.class.ts b/packages/sdk/src/Storage/StorageInstance.class.ts index 5f630466e..7e7f8b682 100644 --- a/packages/sdk/src/Storage/StorageInstance.class.ts +++ b/packages/sdk/src/Storage/StorageInstance.class.ts @@ -119,11 +119,10 @@ export class StorageInstance extends SDKObject { * @param resourceName - The name or smythfs:// uri of the resource to check * @returns true if the resource exists, false otherwise */ - async exists(resourceName: string) { + async exists(resourceName: string): Promise { const uri = resourceName.startsWith('smythfs://') ? resourceName : await this.getResourceUri(resourceName); try { - await this.fs.exists(uri, this._candidate); - return uri; + return await this.fs.exists(uri, this._candidate); } catch (error) { console.error(error); throw error; From f45df8dd68e96af93741c62de59ca054fe72c6b1 Mon Sep 17 00:00:00 2001 From: "fazeel332@gmail.com" Date: Mon, 22 Sep 2025 04:14:12 +0500 Subject: [PATCH 2/2] test(core): align LocalStorage tests with dev; add undefined-folder guard Signed-off-by: fazeel332@gmail.com --- .../tests/unit/001-Core/LocalStorage.test.ts | 36 ------------------- .../unit/005-Storage/LocalStorage.test.ts | 5 +++ 2 files changed, 5 insertions(+), 36 deletions(-) delete mode 100644 packages/core/tests/unit/001-Core/LocalStorage.test.ts diff --git a/packages/core/tests/unit/001-Core/LocalStorage.test.ts b/packages/core/tests/unit/001-Core/LocalStorage.test.ts deleted file mode 100644 index 99536e2f0..000000000 --- a/packages/core/tests/unit/001-Core/LocalStorage.test.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { LocalStorage } from '@sre/IO/Storage.service/connectors/LocalStorage.class'; -import { describe, expect, it } from 'vitest'; - -describe('LocalStorage Tests', () => { - it('should initialize with undefined folder without crashing', () => { - // Test with undefined settings - expect(() => { - new LocalStorage(); - }).not.toThrow(); - - // Test with empty settings object - expect(() => { - new LocalStorage({}); - }).not.toThrow(); - - // Test with explicitly undefined folder - expect(() => { - new LocalStorage({ folder: undefined }); - }).not.toThrow(); - }); - - it('should initialize with valid folder path', () => { - const tempDir = require('os').tmpdir(); - - expect(() => { - new LocalStorage({ folder: tempDir }); - }).not.toThrow(); - }); - - it('should handle non-existent folder gracefully', () => { - // This should not crash but may log warnings as designed - expect(() => { - new LocalStorage({ folder: '/non/existent/path/that/should/not/exist' }); - }).not.toThrow(); - }); -}); diff --git a/packages/core/tests/unit/005-Storage/LocalStorage.test.ts b/packages/core/tests/unit/005-Storage/LocalStorage.test.ts index 02197e952..523a2f553 100644 --- a/packages/core/tests/unit/005-Storage/LocalStorage.test.ts +++ b/packages/core/tests/unit/005-Storage/LocalStorage.test.ts @@ -47,6 +47,11 @@ const testOriginalMetadata = { }; describe('Local Storage Tests', () => { + it('Initializes with undefined folder without crashing (guard)', () => { + expect(() => new LocalStorage()).not.toThrow(); + expect(() => new LocalStorage({} as any)).not.toThrow(); + expect(() => new LocalStorage({ folder: undefined } as any)).not.toThrow(); + }); it('Get Default Storage instance', async () => { const localStorage: StorageConnector = ConnectorService.getStorageConnector();