// // Copyright 2023 DXOS.org // import { beforeEach, describe, expect, test } from 'vitest'; import { Event, Trigger } from '@dxos/async'; import { Config } from '@dxos/config'; import { type SystemService, SystemStatus, type QueryStatusResponse } from '@dxos/protocols/proto/dxos/client/services'; import { SystemServiceImpl } from './system-service'; describe('SystemService', () => { let systemService: SystemService; let config: Config; let statusUpdate: Event; let currentStatus: SystemStatus; let updateStatus: Trigger; let reset: Trigger; const changeStatus = (status: SystemStatus) => { currentStatus = status; statusUpdate.emit(); }; beforeEach(() => { config = new Config({ runtime: { client: { log: { filter: 'system-service:debug' } } } }); statusUpdate = new Event(); currentStatus = SystemStatus.ACTIVE; updateStatus = new Trigger(); reset = new Trigger(); systemService = new SystemServiceImpl({ config: () => config, statusUpdate, getCurrentStatus: () => currentStatus, getDiagnostics: async () => ({}), onUpdateStatus: (status) => { updateStatus.wake(status); }, onReset: () => { reset.wake(true); }, }); }); test('getConfig returns config', async () => { expect(await systemService.getConfig()).to.deep.equal(config.values); }); test('updateStatus triggers callback', async () => { await systemService.updateStatus({ status: SystemStatus.INACTIVE }); const result = await updateStatus.wait(); expect(result).to.equal(SystemStatus.INACTIVE); }); test('queryStatus returns initial status', async () => { const status = new Trigger(); systemService.queryStatus({}).subscribe((response) => { status.wake(response); }); expect(await status.wait()).to.deep.equal({ status: SystemStatus.ACTIVE }); }); test('queryStatus streams status changes', async () => { const statuses: SystemStatus[] = []; systemService.queryStatus({}).subscribe(({ status }) => { statuses.push(status); }); changeStatus(SystemStatus.INACTIVE); changeStatus(SystemStatus.ACTIVE); expect(statuses).to.deep.equal([SystemStatus.ACTIVE, SystemStatus.INACTIVE, SystemStatus.ACTIVE]); }); test('reset triggers callback', async () => { await systemService.reset(); const result = await reset.wait(); expect(result).to.be.true; }); });