Skip to content

Commit 8589afb

Browse files
committed
refactor(disks): replace store dependency with ConfigService in DisksService
- Refactored `DisksService` to utilize `ConfigService` for accessing disk state data instead of the previously injected store. - Updated unit tests in `disks.service.spec.ts` to mock `ConfigService` and verify interactions, ensuring proper functionality with the new implementation. - Enhanced test coverage by checking various scenarios for disk retrieval using the configuration service.
1 parent e780726 commit 8589afb

2 files changed

Lines changed: 38 additions & 42 deletions

File tree

api/src/unraid-api/graph/resolvers/disks/disks.service.spec.ts

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ConfigService } from '@nestjs/config';
12
import { Test, TestingModule } from '@nestjs/testing';
23

34
import type { Systeminformation } from 'systeminformation';
@@ -6,7 +7,6 @@ import { blockDevices, diskLayout } from 'systeminformation';
67
// Vitest imports
78
import { beforeEach, describe, expect, it, vi } from 'vitest';
89

9-
import { store } from '@app/store/index.js';
1010
import {
1111
ArrayDisk,
1212
ArrayDiskStatus,
@@ -30,11 +30,6 @@ vi.mock('@app/utils.js', () => ({
3030
return { data, errors: [] };
3131
}),
3232
}));
33-
vi.mock('@app/store/index.js', () => ({
34-
store: {
35-
getState: vi.fn(),
36-
},
37-
}));
3833

3934
// Remove explicit type assertions for mocks
4035
const mockExeca = execa as any; // Using 'any' for simplicity with complex mock setups
@@ -44,6 +39,7 @@ const mockBatchProcess = batchProcess as any;
4439

4540
describe('DisksService', () => {
4641
let service: DisksService;
42+
let configService: ConfigService;
4743

4844
// Mock ArrayDisk data from state
4945
const mockArrayDisks: ArrayDisk[] = [
@@ -305,21 +301,28 @@ describe('DisksService', () => {
305301
// Reset mocks before each test using vi
306302
vi.clearAllMocks();
307303

308-
// Setup default store state
309-
vi.mocked(store.getState).mockReturnValue({
310-
paths: {
311-
states: 'dev/states',
312-
},
313-
emhttp: {
314-
disks: mockArrayDisks,
315-
},
316-
} as any);
304+
// Create mock ConfigService
305+
const mockConfigService = {
306+
get: vi.fn().mockImplementation((key: string, defaultValue?: any) => {
307+
if (key === 'store.emhttp.disks') {
308+
return mockArrayDisks;
309+
}
310+
return defaultValue;
311+
}),
312+
};
317313

318314
const module: TestingModule = await Test.createTestingModule({
319-
providers: [DisksService],
315+
providers: [
316+
DisksService,
317+
{
318+
provide: ConfigService,
319+
useValue: mockConfigService,
320+
},
321+
],
320322
}).compile();
321323

322324
service = module.get<DisksService>(DisksService);
325+
configService = module.get<ConfigService>(ConfigService);
323326

324327
// Setup default mock implementations
325328
mockDiskLayout.mockResolvedValue(mockDiskLayoutData);
@@ -347,7 +350,7 @@ describe('DisksService', () => {
347350

348351
expect(mockDiskLayout).toHaveBeenCalledTimes(1);
349352
expect(mockBlockDevices).toHaveBeenCalledTimes(1);
350-
expect(store.getState).toHaveBeenCalledTimes(1);
353+
expect(configService.get).toHaveBeenCalledWith('store.emhttp.disks', []);
351354
expect(mockBatchProcess).toHaveBeenCalledTimes(1);
352355

353356
expect(disks).toHaveLength(mockDiskLayoutData.length);
@@ -380,14 +383,12 @@ describe('DisksService', () => {
380383
});
381384

382385
it('should handle empty state gracefully', async () => {
383-
vi.mocked(store.getState).mockReturnValue({
384-
paths: {
385-
states: 'dev/states',
386-
},
387-
emhttp: {
388-
disks: [],
389-
},
390-
} as any);
386+
vi.mocked(configService.get).mockImplementation((key: string, defaultValue?: any) => {
387+
if (key === 'store.emhttp.disks') {
388+
return [];
389+
}
390+
return defaultValue;
391+
});
391392

392393
const disks = await service.getDisks();
393394

@@ -406,14 +407,12 @@ describe('DisksService', () => {
406407
id: ' S4ENNF0N123456 ', // spaces around ID
407408
};
408409

409-
vi.mocked(store.getState).mockReturnValue({
410-
paths: {
411-
states: 'dev/states',
412-
},
413-
emhttp: {
414-
disks: disksWithSpaces,
415-
},
416-
} as any);
410+
vi.mocked(configService.get).mockImplementation((key: string, defaultValue?: any) => {
411+
if (key === 'store.emhttp.disks') {
412+
return disksWithSpaces;
413+
}
414+
return defaultValue;
415+
});
417416

418417
const disks = await service.getDisks();
419418
const disk = disks.find((d) => d.id === 'S4ENNF0N123456');
@@ -447,14 +446,11 @@ describe('DisksService', () => {
447446
});
448447
});
449448

450-
it('should use state data instead of reloading config file', async () => {
451-
const getStateSpy = vi.spyOn(store, 'getState');
452-
449+
it('should use ConfigService to get state data', async () => {
453450
await service.getDisks();
454451

455-
expect(getStateSpy).toHaveBeenCalled();
456-
// Verify we're accessing the state exactly once
457-
expect(store.getState).toHaveBeenCalledTimes(1);
452+
// Verify we're accessing the state through ConfigService
453+
expect(configService.get).toHaveBeenCalledWith('store.emhttp.disks', []);
458454
});
459455

460456
it('should handle empty disk layout or block devices', async () => {

api/src/unraid-api/graph/resolvers/disks/disks.service.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Injectable, NotFoundException } from '@nestjs/common';
2+
import { ConfigService } from '@nestjs/config';
23

34
import type { Systeminformation } from 'systeminformation';
45
import { execa } from 'execa';
56
import { blockDevices, diskLayout } from 'systeminformation';
67

7-
import { store } from '@app/store/index.js';
88
import { ArrayDisk } from '@app/unraid-api/graph/resolvers/array/array.model.js';
99
import {
1010
Disk,
@@ -16,6 +16,7 @@ import { batchProcess } from '@app/utils.js';
1616

1717
@Injectable()
1818
export class DisksService {
19+
constructor(private readonly configService: ConfigService) {}
1920
public async getTemperature(device: string): Promise<number | null> {
2021
try {
2122
const { stdout } = await execa('smartctl', ['-A', device]);
@@ -139,8 +140,7 @@ export class DisksService {
139140
const partitions = await blockDevices().then((devices) =>
140141
devices.filter((device) => device.type === 'part')
141142
);
142-
const { emhttp } = store.getState();
143-
const arrayDisks = emhttp.disks;
143+
const arrayDisks = this.configService.get<ArrayDisk[]>('store.emhttp.disks', []);
144144
const { data } = await batchProcess(await diskLayout(), async (disk) =>
145145
this.parseDisk(disk, partitions, arrayDisks)
146146
);

0 commit comments

Comments
 (0)