Skip to content

Commit d74eded

Browse files
committed
fix: add missing resolvers to spec
1 parent cc64219 commit d74eded

File tree

1 file changed

+300
-4
lines changed

1 file changed

+300
-4
lines changed

api/src/unraid-api/graph/resolvers/info/info.resolver.spec.ts

Lines changed: 300 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ vi.mock('@app/core/pubsub.js', () => ({
2020
PUBSUB_CHANNEL: {
2121
INFO: 'info',
2222
},
23+
createSubscription: vi.fn().mockReturnValue('mock-subscription'),
2324
}));
2425

2526
vi.mock('dockerode', () => {
@@ -39,6 +40,28 @@ vi.mock('@app/store/index.js', () => ({
3940
},
4041
}));
4142

43+
vi.mock('systeminformation', () => ({
44+
baseboard: vi.fn().mockResolvedValue({
45+
manufacturer: 'ASUS',
46+
model: 'PRIME X570-P',
47+
version: 'Rev X.0x',
48+
serial: 'ABC123',
49+
assetTag: 'Default string',
50+
}),
51+
system: vi.fn().mockResolvedValue({
52+
manufacturer: 'ASUS',
53+
model: 'System Product Name',
54+
version: 'System Version',
55+
serial: 'System Serial Number',
56+
uuid: '550e8400-e29b-41d4-a716-446655440000',
57+
sku: 'SKU',
58+
}),
59+
}));
60+
61+
vi.mock('@app/core/utils/misc/get-machine-id.js', () => ({
62+
getMachineId: vi.fn().mockResolvedValue('test-machine-id-123'),
63+
}));
64+
4265
// Mock Cache Manager
4366
const mockCacheManager = {
4467
get: vi.fn(),
@@ -49,12 +72,122 @@ const mockCacheManager = {
4972
describe('InfoResolver', () => {
5073
let resolver: InfoResolver;
5174

75+
// Mock data for testing
76+
const mockAppsData = {
77+
id: 'info/apps',
78+
installed: 5,
79+
started: 3,
80+
};
81+
82+
const mockCpuData = {
83+
id: 'info/cpu',
84+
manufacturer: 'AMD',
85+
brand: 'AMD Ryzen 9 5900X',
86+
vendor: 'AMD',
87+
family: '19',
88+
model: '33',
89+
stepping: 0,
90+
revision: '',
91+
voltage: '1.4V',
92+
speed: 3.7,
93+
speedmin: 2.2,
94+
speedmax: 4.8,
95+
threads: 24,
96+
cores: 12,
97+
processors: 1,
98+
socket: 'AM4',
99+
cache: { l1d: 32768, l1i: 32768, l2: 524288, l3: 33554432 },
100+
flags: ['fpu', 'vme', 'de', 'pse'],
101+
};
102+
103+
const mockDevicesData = {
104+
id: 'info/devices',
105+
gpu: [],
106+
pci: [],
107+
usb: [],
108+
};
109+
110+
const mockDisplayData = {
111+
id: 'dynamix-config/display',
112+
theme: 'black',
113+
unit: 'C',
114+
scale: true,
115+
tabs: false,
116+
resize: true,
117+
wwn: false,
118+
total: true,
119+
usage: false,
120+
text: true,
121+
warning: 40,
122+
critical: 50,
123+
hot: 60,
124+
max: 80,
125+
locale: 'en_US',
126+
};
127+
128+
const mockMemoryData = {
129+
id: 'info/memory',
130+
max: 68719476736,
131+
total: 67108864000,
132+
free: 33554432000,
133+
used: 33554432000,
134+
active: 16777216000,
135+
available: 50331648000,
136+
buffcache: 8388608000,
137+
swaptotal: 4294967296,
138+
swapused: 0,
139+
swapfree: 4294967296,
140+
layout: [],
141+
};
142+
143+
const mockOsData = {
144+
id: 'info/os',
145+
platform: 'linux',
146+
distro: 'Unraid',
147+
release: '6.12.0',
148+
codename: '',
149+
kernel: '6.1.0-unraid',
150+
arch: 'x64',
151+
hostname: 'Tower',
152+
codepage: 'UTF-8',
153+
logofile: 'unraid',
154+
serial: '',
155+
build: '',
156+
uptime: '2024-01-01T00:00:00.000Z',
157+
};
158+
159+
const mockVersionsData = {
160+
id: 'info/versions',
161+
unraid: '6.12.0',
162+
kernel: '6.1.0',
163+
node: '20.10.0',
164+
npm: '10.2.3',
165+
docker: '24.0.7',
166+
};
167+
168+
// Mock InfoService
169+
const mockInfoService = {
170+
generateApps: vi.fn().mockResolvedValue(mockAppsData),
171+
generateCpu: vi.fn().mockResolvedValue(mockCpuData),
172+
generateDevices: vi.fn().mockResolvedValue(mockDevicesData),
173+
generateDisplay: vi.fn().mockResolvedValue(mockDisplayData),
174+
generateMemory: vi.fn().mockResolvedValue(mockMemoryData),
175+
generateOs: vi.fn().mockResolvedValue(mockOsData),
176+
generateVersions: vi.fn().mockResolvedValue(mockVersionsData),
177+
};
178+
52179
beforeEach(async () => {
53180
const module: TestingModule = await Test.createTestingModule({
54181
providers: [
55182
InfoResolver,
56-
InfoService,
57-
DockerService,
183+
{
184+
provide: InfoService,
185+
useValue: mockInfoService,
186+
},
187+
{
188+
provide: DockerService,
189+
useValue: {},
190+
},
58191
{
59192
provide: CACHE_MANAGER,
60193
useValue: mockCacheManager,
@@ -63,9 +196,172 @@ describe('InfoResolver', () => {
63196
}).compile();
64197

65198
resolver = module.get<InfoResolver>(InfoResolver);
199+
200+
// Reset mocks before each test
201+
vi.clearAllMocks();
202+
});
203+
204+
describe('info', () => {
205+
it('should return basic info object', async () => {
206+
const result = await resolver.info();
207+
208+
expect(result).toEqual({
209+
id: 'info',
210+
});
211+
});
212+
});
213+
214+
describe('time', () => {
215+
it('should return current date', async () => {
216+
const beforeCall = new Date();
217+
const result = await resolver.time();
218+
const afterCall = new Date();
219+
220+
expect(result).toBeInstanceOf(Date);
221+
expect(result.getTime()).toBeGreaterThanOrEqual(beforeCall.getTime());
222+
expect(result.getTime()).toBeLessThanOrEqual(afterCall.getTime());
223+
});
224+
});
225+
226+
describe('apps', () => {
227+
it('should return apps info from service', async () => {
228+
const result = await resolver.apps();
229+
230+
expect(mockInfoService.generateApps).toHaveBeenCalledOnce();
231+
expect(result).toEqual(mockAppsData);
232+
});
233+
});
234+
235+
describe('baseboard', () => {
236+
it('should return baseboard info with id', async () => {
237+
const result = await resolver.baseboard();
238+
239+
expect(result).toEqual({
240+
id: 'baseboard',
241+
manufacturer: 'ASUS',
242+
model: 'PRIME X570-P',
243+
version: 'Rev X.0x',
244+
serial: 'ABC123',
245+
assetTag: 'Default string',
246+
});
247+
});
248+
});
249+
250+
describe('cpu', () => {
251+
it('should return cpu info from service', async () => {
252+
const result = await resolver.cpu();
253+
254+
expect(mockInfoService.generateCpu).toHaveBeenCalledOnce();
255+
expect(result).toEqual(mockCpuData);
256+
});
257+
});
258+
259+
describe('devices', () => {
260+
it('should return devices info from service', async () => {
261+
const result = await resolver.devices();
262+
263+
expect(mockInfoService.generateDevices).toHaveBeenCalledOnce();
264+
expect(result).toEqual(mockDevicesData);
265+
});
266+
});
267+
268+
describe('display', () => {
269+
it('should return display info from service', async () => {
270+
const result = await resolver.display();
271+
272+
expect(mockInfoService.generateDisplay).toHaveBeenCalledOnce();
273+
expect(result).toEqual(mockDisplayData);
274+
});
275+
});
276+
277+
describe('machineId', () => {
278+
it('should return machine id', async () => {
279+
const result = await resolver.machineId();
280+
281+
expect(result).toBe('test-machine-id-123');
282+
});
283+
284+
it('should handle getMachineId errors gracefully', async () => {
285+
const { getMachineId } = await import('@app/core/utils/misc/get-machine-id.js');
286+
vi.mocked(getMachineId).mockRejectedValueOnce(new Error('Machine ID error'));
287+
288+
await expect(resolver.machineId()).rejects.toThrow('Machine ID error');
289+
});
290+
});
291+
292+
describe('memory', () => {
293+
it('should return memory info from service', async () => {
294+
const result = await resolver.memory();
295+
296+
expect(mockInfoService.generateMemory).toHaveBeenCalledOnce();
297+
expect(result).toEqual(mockMemoryData);
298+
});
66299
});
67300

68-
it('should be defined', () => {
69-
expect(resolver).toBeDefined();
301+
describe('os', () => {
302+
it('should return os info from service', async () => {
303+
const result = await resolver.os();
304+
305+
expect(mockInfoService.generateOs).toHaveBeenCalledOnce();
306+
expect(result).toEqual(mockOsData);
307+
});
308+
});
309+
310+
describe('system', () => {
311+
it('should return system info with id', async () => {
312+
const result = await resolver.system();
313+
314+
expect(result).toEqual({
315+
id: 'system',
316+
manufacturer: 'ASUS',
317+
model: 'System Product Name',
318+
version: 'System Version',
319+
serial: 'System Serial Number',
320+
uuid: '550e8400-e29b-41d4-a716-446655440000',
321+
sku: 'SKU',
322+
});
323+
});
324+
});
325+
326+
describe('versions', () => {
327+
it('should return versions info from service', async () => {
328+
const result = await resolver.versions();
329+
330+
expect(mockInfoService.generateVersions).toHaveBeenCalledOnce();
331+
expect(result).toEqual(mockVersionsData);
332+
});
333+
});
334+
335+
describe('infoSubscription', () => {
336+
it('should create and return subscription', async () => {
337+
const { createSubscription, PUBSUB_CHANNEL } = await import('@app/core/pubsub.js');
338+
339+
const result = await resolver.infoSubscription();
340+
341+
expect(createSubscription).toHaveBeenCalledWith(PUBSUB_CHANNEL.INFO);
342+
expect(result).toBe('mock-subscription');
343+
});
344+
});
345+
346+
describe('error handling', () => {
347+
it('should handle baseboard errors gracefully', async () => {
348+
const { baseboard } = await import('systeminformation');
349+
vi.mocked(baseboard).mockRejectedValueOnce(new Error('Baseboard error'));
350+
351+
await expect(resolver.baseboard()).rejects.toThrow('Baseboard error');
352+
});
353+
354+
it('should handle system errors gracefully', async () => {
355+
const { system } = await import('systeminformation');
356+
vi.mocked(system).mockRejectedValueOnce(new Error('System error'));
357+
358+
await expect(resolver.system()).rejects.toThrow('System error');
359+
});
360+
361+
it('should handle service errors gracefully', async () => {
362+
mockInfoService.generateApps.mockRejectedValueOnce(new Error('Service error'));
363+
364+
await expect(resolver.apps()).rejects.toThrow('Service error');
365+
});
70366
});
71367
});

0 commit comments

Comments
 (0)