UNPKG

matterbridge-roborock-vacuum-plugin

Version:
166 lines 6.88 kB
import { describe, expect, it, vi } from 'vitest'; import { RoborockMatterbridgePlatform } from '../module.js'; import { asPartial, createMockLocalStorage, createMockLogger, createMockMatterbridge } from './helpers/testUtils.js'; describe('RoborockMatterbridgePlatform - startup branches', () => { it('onStart returns early when username is undefined', async () => { const logger = createMockLogger(); const config = asPartial({ name: 'Test', authentication: { username: '', password: 'pass', region: 'US', forceAuthentication: false, authenticationMethod: 'Password', }, pluginConfiguration: { whiteList: [], sanitizeSensitiveLogs: false, enableServerMode: false, enableMultipleMap: false, unregisterOnShutdown: false, refreshInterval: 60, debug: false, }, }); const platform = new RoborockMatterbridgePlatform(createMockMatterbridge(), logger, config); // ready and persist stubs platform.ready = Promise.resolve(); platform.persist = createMockLocalStorage({ init: vi.fn(async () => undefined), getItem: vi.fn(async () => undefined), setItem: vi.fn(async () => undefined), }); await platform.onStart(); expect(logger.log).toHaveBeenCalled(); }); it('onStart logs error when startDeviceDiscovery returns false', async () => { const logger = createMockLogger(); const config = asPartial({ name: 'Test', authentication: { username: 'u@example.com', password: 'pass', region: 'US', forceAuthentication: false, authenticationMethod: 'Password', }, pluginConfiguration: { whiteList: [], sanitizeSensitiveLogs: false, enableServerMode: false, enableMultipleMap: false, unregisterOnShutdown: false, refreshInterval: 60, debug: false, }, }); const platform = new RoborockMatterbridgePlatform(createMockMatterbridge(), logger, config); platform.ready = Promise.resolve(); platform.persist = createMockLocalStorage({ init: vi.fn(async () => undefined), getItem: vi.fn(async () => undefined), setItem: vi.fn(async () => undefined), }); // force discoverDevices to return false vi.spyOn(platform.discovery, 'discoverDevices').mockResolvedValue(false); await platform.onStart(); expect(logger.log).toHaveBeenCalledWith('error', 'Device discovery failed to start.'); }); it('onConfigure returns early if not started', async () => { const logger = createMockLogger(); const authentication = { username: 'abc', region: 'US', password: 'pass', authenticationMethod: 'Password', forceAuthentication: false, }; const pluginConfiguration = { whiteList: [], sanitizeSensitiveLogs: false, enableServerMode: false, enableMultipleMap: false, unregisterOnShutdown: false, refreshInterval: 60, debug: false, }; const advancedFeature = { enableAdvancedFeature: false, settings: { clearStorageOnStartup: false, showRoutinesAsRoom: false, includeDockStationStatus: false, includeVacuumErrorStatus: false, forceRunAtDefault: false, useVacationModeToSendVacuumToDock: false, enableCleanModeMapping: false, cleanModeSettings: { vacuuming: { fanMode: 'Silent', mopRouteMode: 'Standard' }, mopping: { waterFlowMode: 'Low', mopRouteMode: 'Standard', distanceOff: 500 }, vacmop: { fanMode: 'Silent', waterFlowMode: 'Low', mopRouteMode: 'Standard', distanceOff: 500 }, }, overrideMatterConfiguration: false, matterOverrideSettings: { matterVendorName: 'xxx', matterVendorId: 123, matterProductName: 'yy', matterProductId: 456, }, enableEmailNotification: false, emailNotificationSettings: {}, }, }; const config = { name: 'Test', authentication, pluginConfiguration, advancedFeature, type: 'DynamicPlatform', version: '1.0.0', debug: false, unregisterOnShutdown: false, }; const platform = new RoborockMatterbridgePlatform(createMockMatterbridge(), logger, config); // isStartPluginCompleted defaults to false - ensure no exception await platform.onConfigure(); expect(logger.log).toHaveBeenCalled(); }); // Helper to simulate the private `configureDevice` behavior in tests without casting to private members function simulateConfigureDevice(p, vacuum) { if (!p.roborockService) { p.log.error?.('Initializing: RoborockService is undefined'); return Promise.resolve(false); } return Promise.resolve(true); } it('configureDevice returns false when roborockService undefined', async () => { const logger = createMockLogger(); const cfg = asPartial({ name: 'Test', username: 'u@example.com', authentication: { username: 'u@example.com', password: 'pass', region: 'US', forceAuthentication: false, authenticationMethod: 'Password', }, pluginConfiguration: { whiteList: [], sanitizeSensitiveLogs: false, enableServerMode: false, enableMultipleMap: false, unregisterOnShutdown: false, refreshInterval: 60, debug: false, }, }); const platform = new RoborockMatterbridgePlatform(createMockMatterbridge(), logger, cfg); const vacuum = { name: 'Vac', serialNumber: 's1' }; const result = await simulateConfigureDevice(platform, vacuum); expect(result).toBe(false); expect(logger.log).toHaveBeenCalledWith('error', 'Initializing: RoborockService is undefined'); }); }); //# sourceMappingURL=module.startup.test.js.map