|
| 1 | +import { BadRequestException } from '@nestjs/common'; |
| 2 | +import { Test, TestingModule } from '@nestjs/testing'; |
| 3 | + |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +import { emcmd } from '@app/core/utils/clients/emcmd.js'; |
| 7 | +import * as PhpLoaderModule from '@app/core/utils/plugins/php-loader.js'; |
| 8 | +import { getters, store } from '@app/store/index.js'; |
| 9 | +import { |
| 10 | + MANUAL_TIME_REGEX, |
| 11 | + UpdateSystemTimeInput, |
| 12 | +} from '@app/unraid-api/graph/resolvers/system-time/system-time.model.js'; |
| 13 | +import { SystemTimeService } from '@app/unraid-api/graph/resolvers/system-time/system-time.service.js'; |
| 14 | + |
| 15 | +vi.mock('@app/core/utils/clients/emcmd.js', () => ({ |
| 16 | + emcmd: vi.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +const phpLoaderSpy = vi.spyOn(PhpLoaderModule, 'phpLoader'); |
| 20 | + |
| 21 | +describe('SystemTimeService', () => { |
| 22 | + let service: SystemTimeService; |
| 23 | + const emhttpSpy = vi.spyOn(getters, 'emhttp'); |
| 24 | + const pathsSpy = vi.spyOn(getters, 'paths'); |
| 25 | + const dispatchSpy = vi.spyOn(store, 'dispatch'); |
| 26 | + |
| 27 | + beforeEach(async () => { |
| 28 | + vi.clearAllMocks(); |
| 29 | + |
| 30 | + const module: TestingModule = await Test.createTestingModule({ |
| 31 | + providers: [SystemTimeService], |
| 32 | + }).compile(); |
| 33 | + |
| 34 | + service = module.get<SystemTimeService>(SystemTimeService); |
| 35 | + |
| 36 | + emhttpSpy.mockReturnValue({ |
| 37 | + var: { |
| 38 | + timeZone: 'UTC', |
| 39 | + useNtp: true, |
| 40 | + ntpServer1: 'time1.google.com', |
| 41 | + ntpServer2: 'time2.google.com', |
| 42 | + ntpServer3: '', |
| 43 | + ntpServer4: '', |
| 44 | + }, |
| 45 | + } as any); |
| 46 | + |
| 47 | + pathsSpy.mockReturnValue({ |
| 48 | + webGuiBase: '/usr/local/emhttp/webGui', |
| 49 | + } as any); |
| 50 | + |
| 51 | + dispatchSpy.mockResolvedValue({} as any); |
| 52 | + vi.mocked(emcmd).mockResolvedValue({ ok: true } as any); |
| 53 | + phpLoaderSpy.mockResolvedValue(''); |
| 54 | + }); |
| 55 | + |
| 56 | + afterEach(() => { |
| 57 | + emhttpSpy.mockReset(); |
| 58 | + pathsSpy.mockReset(); |
| 59 | + dispatchSpy.mockReset(); |
| 60 | + phpLoaderSpy.mockReset(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('returns system time from store state', async () => { |
| 64 | + const result = await service.getSystemTime(); |
| 65 | + expect(result.timeZone).toBe('UTC'); |
| 66 | + expect(result.useNtp).toBe(true); |
| 67 | + expect(result.ntpServers).toEqual(['time1.google.com', 'time2.google.com', '', '']); |
| 68 | + expect(typeof result.currentTime).toBe('string'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('updates time settings, disables NTP, and triggers timezone reset', async () => { |
| 72 | + const oldState = { |
| 73 | + var: { |
| 74 | + timeZone: 'UTC', |
| 75 | + useNtp: true, |
| 76 | + ntpServer1: 'pool.ntp.org', |
| 77 | + ntpServer2: '', |
| 78 | + ntpServer3: '', |
| 79 | + ntpServer4: '', |
| 80 | + }, |
| 81 | + } as any; |
| 82 | + const newState = { |
| 83 | + var: { |
| 84 | + timeZone: 'America/Los_Angeles', |
| 85 | + useNtp: false, |
| 86 | + ntpServer1: 'time.google.com', |
| 87 | + ntpServer2: '', |
| 88 | + ntpServer3: '', |
| 89 | + ntpServer4: '', |
| 90 | + }, |
| 91 | + } as any; |
| 92 | + |
| 93 | + emhttpSpy.mockImplementationOnce(() => oldState).mockReturnValue(newState); |
| 94 | + |
| 95 | + const input: UpdateSystemTimeInput = { |
| 96 | + timeZone: 'America/Los_Angeles', |
| 97 | + useNtp: false, |
| 98 | + manualDateTime: '2025-01-22 10:00:00', |
| 99 | + ntpServers: ['time.google.com'], |
| 100 | + }; |
| 101 | + |
| 102 | + const result = await service.updateSystemTime(input); |
| 103 | + |
| 104 | + expect(emcmd).toHaveBeenCalledTimes(1); |
| 105 | + const [commands, options] = vi.mocked(emcmd).mock.calls[0]; |
| 106 | + expect(options).toEqual({ waitForToken: true }); |
| 107 | + expect(commands).toEqual({ |
| 108 | + setDateTime: 'apply', |
| 109 | + timeZone: 'America/Los_Angeles', |
| 110 | + USE_NTP: 'no', |
| 111 | + NTP_SERVER1: 'time.google.com', |
| 112 | + NTP_SERVER2: '', |
| 113 | + NTP_SERVER3: '', |
| 114 | + NTP_SERVER4: '', |
| 115 | + newDateTime: '2025-01-22 10:00:00', |
| 116 | + }); |
| 117 | + |
| 118 | + expect(phpLoaderSpy).toHaveBeenCalledWith({ |
| 119 | + file: '/usr/local/emhttp/webGui/include/ResetTZ.php', |
| 120 | + method: 'GET', |
| 121 | + }); |
| 122 | + expect(dispatchSpy).toHaveBeenCalledTimes(1); |
| 123 | + expect(typeof dispatchSpy.mock.calls[0][0]).toBe('function'); |
| 124 | + |
| 125 | + expect(result.timeZone).toBe('America/Los_Angeles'); |
| 126 | + expect(result.useNtp).toBe(false); |
| 127 | + expect(result.ntpServers).toEqual(['time.google.com', '', '', '']); |
| 128 | + }); |
| 129 | + |
| 130 | + it('throws when provided timezone is invalid', async () => { |
| 131 | + await expect(service.updateSystemTime({ timeZone: 'Not/AZone' })).rejects.toBeInstanceOf( |
| 132 | + BadRequestException |
| 133 | + ); |
| 134 | + expect(emcmd).not.toHaveBeenCalled(); |
| 135 | + }); |
| 136 | + |
| 137 | + it('throws when disabling NTP without manualDateTime', async () => { |
| 138 | + await expect(service.updateSystemTime({ useNtp: false })).rejects.toBeInstanceOf( |
| 139 | + BadRequestException |
| 140 | + ); |
| 141 | + expect(emcmd).not.toHaveBeenCalled(); |
| 142 | + }); |
| 143 | + |
| 144 | + it('retains manual mode and generates timestamp when not supplied', async () => { |
| 145 | + const manualState = { |
| 146 | + var: { |
| 147 | + timeZone: 'UTC', |
| 148 | + useNtp: false, |
| 149 | + ntpServer1: '', |
| 150 | + ntpServer2: '', |
| 151 | + ntpServer3: '', |
| 152 | + ntpServer4: '', |
| 153 | + }, |
| 154 | + } as any; |
| 155 | + |
| 156 | + const manualStateAfter = { |
| 157 | + var: { |
| 158 | + ...manualState.var, |
| 159 | + ntpServer1: 'time.cloudflare.com', |
| 160 | + }, |
| 161 | + } as any; |
| 162 | + |
| 163 | + emhttpSpy.mockImplementationOnce(() => manualState).mockReturnValue(manualStateAfter); |
| 164 | + |
| 165 | + const result = await service.updateSystemTime({ ntpServers: ['time.cloudflare.com'] }); |
| 166 | + |
| 167 | + const [commands] = vi.mocked(emcmd).mock.calls[0]; |
| 168 | + expect(commands.USE_NTP).toBe('no'); |
| 169 | + expect(commands.NTP_SERVER1).toBe('time.cloudflare.com'); |
| 170 | + expect(commands.newDateTime).toMatch(MANUAL_TIME_REGEX); |
| 171 | + expect(phpLoaderSpy).not.toHaveBeenCalled(); |
| 172 | + expect(result.ntpServers).toEqual(['time.cloudflare.com', '', '', '']); |
| 173 | + }); |
| 174 | +}); |
0 commit comments