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.
This commit is contained in:
Eli Bosley
2025-12-29 11:11:27 -05:00
parent 9564532ba1
commit a963f41ce9
2 changed files with 55 additions and 7 deletions

View File

@@ -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',

View File

@@ -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<string, string> = {
setDateTime: 'apply',