A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.
test: enhance global settings tests for restic and rclone
towfiqibcd465959423
diff --git a/backend/__tests__/managers/BaseSystemManager.test.ts b/backend/__tests__/managers/BaseSystemManager.test.tsindex 8467e8c..4482dc5 100644--- a/backend/__tests__/managers/BaseSystemManager.test.ts+++ b/backend/__tests__/managers/BaseSystemManager.test.ts@@ -97,6 +97,14 @@ jest.mock('../../src/services/ConfigService', () => ({},}));+const mockSaveResticGlobalSettings = jest.fn();+const mockSaveRcloneGlobalSettings = jest.fn();+jest.mock('../../src/utils/globalSettings', () => ({+ __esModule: true,+ saveResticGlobalSettings: (...args: any[]) => mockSaveResticGlobalSettings(...args),+ saveRcloneGlobalSettings: (...args: any[]) => mockSaveRcloneGlobalSettings(...args),+}));+import os from 'os';import si from 'systeminformation';import { execSync } from 'child_process';@@ -410,54 +418,53 @@ describe('BaseSystemManager', () => {});describe('updateSettings', () => {- it('writes environment vars on linux', async () => {- jest.spyOn(os, 'platform').mockReturnValue('linux' as any);- readFileMock.mockResolvedValue('RCLONE_TEMP_DIR=/old\nSOME=keep\n');+ it('persists restic and rclone settings via globalSettings helpers', async () => {+ const resticSettings = {+ maxProcessor: 4,+ cacheDir: '/cache/restic',+ tmpDir: '/tmp',+ readConcurrency: 8,+ packSize: '4M',+ };+ const rcloneSettings = {+ tempDir: '/tmp/rclone',+ cacheDir: '/cache/rclone',+ bwlimit: '10M:100k',+ timeout: '5m',+ retries: 3,+ lowLevelRetries: 5,+ transfers: 4,+ checkers: 8,+ bufferSize: '16M',+ multiThreadStream: 4,+ configPass: 'secret',+ };const manager = new BaseSystemManager();const res = await manager.updateSettings({tempDir: '/tmp/pluton',- restic: {- maxProcessor: 4,- cacheDir: '/cache/restic',- tmpDir: '/tmp', // not used by code, but fine- readConcurrency: 8,- packSize: '4M',- },- rclone: {- tempDir: '/tmp/rclone',- cacheDir: '/cache/rclone',- bwlimit: '10M:100k',- timeout: '5m',- retries: 3,- lowLevelRetries: 5,- transfers: 4,- checkers: 8,- bufferSize: '16M',- multiThreadStream: 4,- // configPass included below in deletion test- configPass: 'secret',- },+ restic: resticSettings,+ rclone: rcloneSettings,} as any);expect(res.success).toBe(true);- expect(writeFileMock).toHaveBeenCalledTimes(1);- const content = (writeFileMock.mock.calls[0] as any[])[1] as string;- expect(content).toEqual(expect.stringContaining('GOMAXPROCS=4'));- expect(content).toEqual(expect.stringContaining('RESTIC_CACHE_DIR=/cache/restic'));- expect(content).toEqual(expect.stringContaining('RESTIC_READ_CONCURRENCY=8'));- expect(content).toEqual(expect.stringContaining('RESTIC_PACK_SIZE=4M'));- expect(content).toEqual(expect.stringContaining('RCLONE_TEMP_DIR=/tmp/rclone'));- expect(content).toEqual(expect.stringContaining('RCLONE_CACHE_DIR=/cache/rclone'));- expect(content).toEqual(expect.stringContaining('RCLONE_BWLIMIT=10M:100k'));- expect(content).toEqual(expect.stringContaining('RCLONE_TIMEOUT=5m'));- expect(content).toEqual(expect.stringContaining('RCLONE_RETRIES=3'));- expect(content).toEqual(expect.stringContaining('RCLONE_LOW_LEVEL_RETRIES=5'));- expect(content).toEqual(expect.stringContaining('RCLONE_TRANSFERS=4'));- expect(content).toEqual(expect.stringContaining('RCLONE_CHECKERS=8'));- expect(content).toEqual(expect.stringContaining('RCLONE_BUFFER_SIZE=16M'));- expect(content).toEqual(expect.stringContaining('RCLONE_MULTI_THREAD_STREAMS=4'));- expect(content).toEqual(expect.stringContaining('RCLONE_CONFIG_PASS=secret'));+ expect(res.result).toBe('Global rclone/restic settings updated successfully!');+ expect(mockSaveResticGlobalSettings).toHaveBeenCalledWith(resticSettings);+ expect(mockSaveRcloneGlobalSettings).toHaveBeenCalledWith(rcloneSettings);+ });++ it('returns failure when saving throws', async () => {+ mockSaveResticGlobalSettings.mockImplementation(() => {+ throw new Error('disk full');+ });++ const manager = new BaseSystemManager();+ const res = await manager.updateSettings({+ restic: { maxProcessor: 2 },+ } as any);++ expect(res.success).toBe(false);+ expect(res.result).toBe('disk full');});});diff --git a/backend/__tests__/utils/restic/restic.test.ts b/backend/__tests__/utils/restic/restic.test.tsindex 1e8fdd9..808b3d4 100644--- a/backend/__tests__/utils/restic/restic.test.ts+++ b/backend/__tests__/utils/restic/restic.test.ts@@ -4,7 +4,12 @@ import { EventEmitter } from 'events';jest.mock('child_process');jest.mock('../../../src/utils/binaryPathResolver');jest.mock('../../../src/utils/rclone/helpers');-jest.mock('../../../src/utils/AppPaths');+jest.mock('../../../src/utils/AppPaths', () => ({+ appPaths: {+ getConfigDir: jest.fn().mockReturnValue('/mock/config'),+ getTempDir: jest.fn().mockReturnValue('/mock/temp'),+ },+}));jest.mock('../../../src/utils/restic/helpers');jest.mock('../../../src/services/ConfigService', () => ({configService: {diff --git a/backend/__tests__/utils/versions.test.ts b/backend/__tests__/utils/versions.test.tsindex 7f1c93a..87b9e62 100644--- a/backend/__tests__/utils/versions.test.ts+++ b/backend/__tests__/utils/versions.test.ts@@ -1,4 +1,4 @@-import { execSync } from 'child_process';+import { execFileSync } from 'child_process';import { getResticVersion, getRcloneVersion } from '../../src/utils/versions';jest.mock('child_process');@@ -6,7 +6,7 @@ jest.mock('../../src/utils/binaryPathResolver', () => ({getBinaryPath: jest.fn((name: string) => name),}));-const mockedExecSync = execSync as jest.MockedFunction<typeof execSync>;+const mockedExecFileSync = execFileSync as jest.MockedFunction<typeof execFileSync>;describe('versions', () => {beforeEach(() => {@@ -16,16 +16,16 @@ describe('versions', () => {describe('getResticVersion', () => {it('should return version when restic is installed', () => {const mockOutput = JSON.stringify({ version: '0.16.0' });- mockedExecSync.mockReturnValue(Buffer.from(mockOutput));+ mockedExecFileSync.mockReturnValue(Buffer.from(mockOutput));const version = getResticVersion();expect(version).toBe('0.16.0');- expect(mockedExecSync).toHaveBeenCalledWith('restic version --json');+ expect(mockedExecFileSync).toHaveBeenCalledWith('restic', ['version', '--json']);});it('should return "not_installed" when restic command fails', () => {- mockedExecSync.mockImplementation(() => {+ mockedExecFileSync.mockImplementation(() => {throw new Error('Command not found');});@@ -35,7 +35,7 @@ describe('versions', () => {});it('should return "not_installed" when output is empty', () => {- mockedExecSync.mockReturnValue(Buffer.from(''));+ mockedExecFileSync.mockReturnValue(Buffer.from(''));const version = getResticVersion();@@ -44,7 +44,7 @@ describe('versions', () => {it('should handle whitespace in output', () => {const mockOutput = ` ${JSON.stringify({ version: '0.15.2' })} \n`;- mockedExecSync.mockReturnValue(Buffer.from(mockOutput));+ mockedExecFileSync.mockReturnValue(Buffer.from(mockOutput));const version = getResticVersion();@@ -52,7 +52,7 @@ describe('versions', () => {});it('should return "not_installed" when JSON parsing fails', () => {- mockedExecSync.mockReturnValue(Buffer.from('invalid json'));+ mockedExecFileSync.mockReturnValue(Buffer.from('invalid json'));const version = getResticVersion();@@ -61,7 +61,7 @@ describe('versions', () => {it('should handle different version formats', () => {const mockOutput = JSON.stringify({ version: '0.17.0-dev' });- mockedExecSync.mockReturnValue(Buffer.from(mockOutput));+ mockedExecFileSync.mockReturnValue(Buffer.from(mockOutput));const version = getResticVersion();@@ -72,16 +72,16 @@ describe('versions', () => {describe('getRcloneVersion', () => {it('should return version when rclone is installed', () => {const mockOutput = 'rclone v1.65.0\n- os/version: ...\n- go/version: ...';- mockedExecSync.mockReturnValue(Buffer.from(mockOutput));+ mockedExecFileSync.mockReturnValue(Buffer.from(mockOutput));const version = getRcloneVersion();expect(version).toBe('1.65.0');- expect(mockedExecSync).toHaveBeenCalledWith('rclone version');+ expect(mockedExecFileSync).toHaveBeenCalledWith('rclone', ['version']);});it('should return "not_installed" when rclone command fails', () => {- mockedExecSync.mockImplementation(() => {+ mockedExecFileSync.mockImplementation(() => {throw new Error('Command not found');});@@ -91,7 +91,7 @@ describe('versions', () => {});it('should return "not_installed" when version pattern not found', () => {- mockedExecSync.mockReturnValue(Buffer.from('some random output'));+ mockedExecFileSync.mockReturnValue(Buffer.from('some random output'));const version = getRcloneVersion();@@ -100,7 +100,7 @@ describe('versions', () => {it('should handle different rclone version formats', () => {const mockOutput = 'rclone v1.64.2\nSome other info';- mockedExecSync.mockReturnValue(Buffer.from(mockOutput));+ mockedExecFileSync.mockReturnValue(Buffer.from(mockOutput));const version = getRcloneVersion();@@ -109,7 +109,7 @@ describe('versions', () => {it('should handle whitespace in output', () => {const mockOutput = ' rclone v1.66.0 \n\n';- mockedExecSync.mockReturnValue(Buffer.from(mockOutput));+ mockedExecFileSync.mockReturnValue(Buffer.from(mockOutput));const version = getRcloneVersion();@@ -120,7 +120,7 @@ describe('versions', () => {const mockOutput = `rclone v1.65.1- os/arch: linux/amd64- go version: go1.21.4`;- mockedExecSync.mockReturnValue(Buffer.from(mockOutput));+ mockedExecFileSync.mockReturnValue(Buffer.from(mockOutput));const version = getRcloneVersion();