From a963f41ce9fed1d920fc52e714bffeb11a3f8dfb Mon Sep 17 00:00:00 2001 From: Eli Bosley Date: Mon, 29 Dec 2025 11:11:27 -0500 Subject: [PATCH] feat(system-time): implement default NTP server configuration - Added functionality to default to 'pool.ntp.org' when no NTP servers are configured in the SystemTimeService. - Updated the service logic to handle scenarios where NTP servers are not explicitly set, ensuring a fallback to the default server. - Introduced a new test case to verify the default NTP server behavior, enhancing test coverage for system time configuration. These changes improve the reliability of time synchronization settings in the system. --- .../system-time/system-time.service.spec.ts | 33 +++++++++++++++++++ .../system-time/system-time.service.ts | 29 ++++++++++++---- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/api/src/unraid-api/graph/resolvers/system-time/system-time.service.spec.ts b/api/src/unraid-api/graph/resolvers/system-time/system-time.service.spec.ts index 9d75aa6a4..c64b69065 100644 --- a/api/src/unraid-api/graph/resolvers/system-time/system-time.service.spec.ts +++ b/api/src/unraid-api/graph/resolvers/system-time/system-time.service.spec.ts @@ -94,6 +94,39 @@ describe('SystemTimeService', () => { }); }); + it('defaults to pool.ntp.org when no NTP servers are configured', async () => { + vi.mocked(configService.get).mockImplementation((key: string, defaultValue?: any) => { + if (key === 'store.emhttp.var') { + return { + timeZone: 'UTC', + useNtp: true, + ntpServer1: '', + ntpServer2: '', + ntpServer3: '', + ntpServer4: '', + }; + } + if (key === 'store.paths.webGuiBase') { + return '/usr/local/emhttp/webGui'; + } + return defaultValue; + }); + + await service.updateSystemTime({ timeZone: 'America/New_York' }); + + expect(emcmd).toHaveBeenCalledTimes(1); + const [commands] = vi.mocked(emcmd).mock.calls[0]; + expect(commands).toEqual({ + setDateTime: 'apply', + timeZone: 'America/New_York', + USE_NTP: 'yes', + NTP_SERVER1: 'pool.ntp.org', + NTP_SERVER2: '', + NTP_SERVER3: '', + NTP_SERVER4: '', + }); + }); + it('updates time settings, disables NTP, and triggers timezone reset', async () => { const oldState = { timeZone: 'UTC', diff --git a/api/src/unraid-api/graph/resolvers/system-time/system-time.service.ts b/api/src/unraid-api/graph/resolvers/system-time/system-time.service.ts index 6588720c6..8b91e68e7 100644 --- a/api/src/unraid-api/graph/resolvers/system-time/system-time.service.ts +++ b/api/src/unraid-api/graph/resolvers/system-time/system-time.service.ts @@ -13,6 +13,7 @@ import { } from '@app/unraid-api/graph/resolvers/system-time/system-time.model.js'; const MAX_NTP_SERVERS = 4; +const DEFAULT_NTP_SERVER = 'pool.ntp.org'; @Injectable() export class SystemTimeService { @@ -42,14 +43,28 @@ export class SystemTimeService { this.validateTimeZone(desiredTimeZone); const hasCurrentUseNtp = typeof current.useNtp !== 'undefined'; - const desiredUseNtp = input.useNtp ?? (hasCurrentUseNtp ? Boolean(current.useNtp) : undefined); const hasCurrentNtpServers = this.hasNtpServerState(current); - const desiredServers = - input.ntpServers !== undefined - ? this.normalizeNtpServers(input.ntpServers, current) - : hasCurrentNtpServers - ? this.normalizeNtpServers(undefined, current) - : null; + const currentServers = hasCurrentNtpServers + ? this.normalizeNtpServers(undefined, current) + : null; + const hasConfiguredServers = currentServers + ? currentServers.some((server) => server.length > 0) + : false; + const allowDefaultNtp = input.useNtp !== false; + + let desiredUseNtp = input.useNtp ?? (hasCurrentUseNtp ? Boolean(current.useNtp) : undefined); + let desiredServers: string[] | null = null; + + if (input.ntpServers !== undefined) { + desiredServers = this.normalizeNtpServers(input.ntpServers, current); + } else if (hasCurrentNtpServers) { + if (!hasConfiguredServers && allowDefaultNtp) { + desiredServers = this.normalizeNtpServers([DEFAULT_NTP_SERVER], current); + desiredUseNtp = true; + } else { + desiredServers = currentServers; + } + } const commands: Record = { setDateTime: 'apply',