From 3a48c76a6cfc54972a969f35992fadaec9edb1cb Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 14 Feb 2026 14:15:08 +0000 Subject: [PATCH] Add unit tests for loadTorchConfig with dependency injection Refactored `loadTorchConfig` to support dependency injection for `fs`, enabling true unit tests without disk I/O. Added comprehensive unit tests in `test/torch-config-unit.test.mjs`. - Refactored `loadTorchConfig` to accept optional `fileSystem` argument. - Created `test/torch-config-unit.test.mjs` with mocked fs tests. - Verified existing tests pass. Co-authored-by: PR0M3TH3AN <53631862+PR0M3TH3AN@users.noreply.github.com> --- src/torch-config.mjs | 6 +-- test/torch-config-unit.test.mjs | 71 +++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 test/torch-config-unit.test.mjs diff --git a/src/torch-config.mjs b/src/torch-config.mjs index 8ece5d2..82a1929 100644 --- a/src/torch-config.mjs +++ b/src/torch-config.mjs @@ -84,15 +84,15 @@ export function _resetTorchConfigCache() { cachedConfig = null; } -export function loadTorchConfig() { +export function loadTorchConfig(fileSystem = fs) { if (cachedConfig) return cachedConfig; const configPath = getTorchConfigPath(); let raw = {}; - if (fs.existsSync(configPath)) { + if (fileSystem.existsSync(configPath)) { try { - raw = JSON.parse(fs.readFileSync(configPath, 'utf8')); + raw = JSON.parse(fileSystem.readFileSync(configPath, 'utf8')); } catch (err) { throw new Error(`Failed to parse ${configPath}: ${err.message}`, { cause: err }); } diff --git a/test/torch-config-unit.test.mjs b/test/torch-config-unit.test.mjs new file mode 100644 index 0000000..239894c --- /dev/null +++ b/test/torch-config-unit.test.mjs @@ -0,0 +1,71 @@ +import { describe, it, beforeEach, mock } from 'node:test'; +import assert from 'node:assert'; +import { loadTorchConfig, _resetTorchConfigCache } from '../src/torch-config.mjs'; + +describe('loadTorchConfig (Unit Tests with Mocked FS)', () => { + beforeEach(() => { + _resetTorchConfigCache(); + }); + + it('loads valid config correctly', () => { + const mockFs = { + existsSync: mock.fn(() => true), + readFileSync: mock.fn(() => JSON.stringify({ + nostrLock: { namespace: 'unit-test-ns' } + })) + }; + + const config = loadTorchConfig(mockFs); + + assert.strictEqual(config.nostrLock.namespace, 'unit-test-ns'); + assert.strictEqual(mockFs.existsSync.mock.calls.length, 1); + assert.strictEqual(mockFs.readFileSync.mock.calls.length, 1); + }); + + it('returns default config when file is missing', () => { + const mockFs = { + existsSync: mock.fn(() => false), + readFileSync: mock.fn() + }; + + const config = loadTorchConfig(mockFs); + + assert.strictEqual(config.nostrLock.namespace, null); + assert.strictEqual(mockFs.existsSync.mock.calls.length, 1); + assert.strictEqual(mockFs.readFileSync.mock.calls.length, 0); + }); + + it('throws error on malformed JSON', () => { + const mockFs = { + existsSync: mock.fn(() => true), + readFileSync: mock.fn(() => '{ invalid json }') + }; + + assert.throws(() => { + loadTorchConfig(mockFs); + }, /Failed to parse/); + }); + + it('caches the config and ignores subsequent fs calls', () => { + const mockFs = { + existsSync: mock.fn(() => true), + readFileSync: mock.fn(() => JSON.stringify({ nostrLock: { namespace: 'cached' } })) + }; + + const config1 = loadTorchConfig(mockFs); + assert.strictEqual(config1.nostrLock.namespace, 'cached'); + + // Second call with a different "fs" (or same) should not trigger fs operations + const mockFs2 = { + existsSync: mock.fn(() => true), + readFileSync: mock.fn(() => JSON.stringify({ nostrLock: { namespace: 'changed' } })) + }; + + const config2 = loadTorchConfig(mockFs2); + + assert.strictEqual(config2.nostrLock.namespace, 'cached'); + assert.strictEqual(config1, config2); + assert.strictEqual(mockFs.existsSync.mock.calls.length, 1); + assert.strictEqual(mockFs2.existsSync.mock.calls.length, 0); + }); +});