matterbridge-roborock-vacuum-plugin
Version:
Matterbridge Roborock Vacuum Plugin
99 lines • 4.89 kB
JavaScript
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { BehaviorDeviceGeneric, CommandNames, } from '../../../../behaviors/BehaviorDeviceGeneric.js';
import { registerCommonCommands } from '../../../../behaviors/roborock.vacuum/core/commonCommands.js';
import { asPartial, createMockLogger } from '../../../helpers/testUtils.js';
function createMockHandler() {
return new BehaviorDeviceGeneric(createMockLogger());
}
function createMockService() {
return asPartial({
setSelectedAreas: vi.fn(),
skipRoomCleaning: vi.fn().mockResolvedValue(undefined),
pauseClean: vi.fn().mockResolvedValue(undefined),
resumeClean: vi.fn().mockResolvedValue(undefined),
stopAndGoHome: vi.fn().mockResolvedValue(undefined),
playSoundToLocate: vi.fn().mockResolvedValue(undefined),
stopClean: vi.fn().mockResolvedValue(undefined),
});
}
describe('registerCommonCommands', () => {
const duid = 'test-duid';
const behaviorName = 'TestBehavior';
let handler;
let service;
let onActionTriggered;
let logger;
beforeEach(() => {
vi.clearAllMocks();
logger = createMockLogger();
handler = createMockHandler();
service = createMockService();
onActionTriggered = vi.fn();
registerCommonCommands(duid, handler, logger, service, behaviorName, onActionTriggered);
});
afterEach(() => {
vi.clearAllMocks();
});
describe('SKIP_AREA command', () => {
it('should call skipRoomCleaning with device duid', async () => {
await handler.executeCommand(CommandNames.SKIP_AREA, 5);
expect(service.skipRoomCleaning).toHaveBeenCalledWith(duid);
expect(logger.notice).toHaveBeenCalledWith(`${behaviorName}-skipArea: 5`);
});
});
describe('SELECT_AREAS command', () => {
it('should call setSelectedAreas with provided areas', async () => {
await handler.executeCommand(CommandNames.SELECT_AREAS, [1, 2, 3]);
expect(service.setSelectedAreas).toHaveBeenCalledWith(duid, [1, 2, 3]);
expect(logger.notice).toHaveBeenCalledWith(`${behaviorName}-selectAreas: 1,2,3`);
});
it('should call setSelectedAreas with empty array when undefined is passed', async () => {
await handler.executeCommand(CommandNames.SELECT_AREAS, undefined);
expect(service.setSelectedAreas).toHaveBeenCalledWith(duid, []);
});
});
describe('PAUSE command', () => {
it('should call pauseClean and not trigger onActionTriggered', async () => {
await handler.executeCommand(CommandNames.PAUSE);
expect(service.pauseClean).toHaveBeenCalledWith(duid);
expect(onActionTriggered).not.toHaveBeenCalled();
expect(logger.notice).toHaveBeenCalledWith(`${behaviorName}-Pause`);
});
});
describe('RESUME command', () => {
it('should call resumeClean and trigger onActionTriggered', async () => {
await handler.executeCommand(CommandNames.RESUME);
expect(service.resumeClean).toHaveBeenCalledWith(duid);
expect(onActionTriggered).toHaveBeenCalledOnce();
expect(logger.notice).toHaveBeenCalledWith(`${behaviorName}-Resume`);
});
});
describe('GO_HOME command', () => {
it('should call stopAndGoHome and trigger onActionTriggered', async () => {
await handler.executeCommand(CommandNames.GO_HOME);
expect(service.stopAndGoHome).toHaveBeenCalledWith(duid);
expect(onActionTriggered).toHaveBeenCalledOnce();
expect(logger.notice).toHaveBeenCalledWith(`${behaviorName}-GoHome`);
});
});
describe('IDENTIFY command', () => {
it('should call playSoundToLocate and not trigger onActionTriggered', async () => {
await handler.executeCommand(CommandNames.IDENTIFY, 0);
expect(service.playSoundToLocate).toHaveBeenCalledWith(duid);
expect(onActionTriggered).not.toHaveBeenCalled();
expect(logger.notice).toHaveBeenCalledWith(`${behaviorName}-identify`);
});
});
describe('STOP command', () => {
it('should call stopClean and not trigger onActionTriggered', async () => {
await handler.executeCommand(CommandNames.STOP);
expect(service.stopClean).toHaveBeenCalledWith(duid);
expect(onActionTriggered).not.toHaveBeenCalled();
expect(logger.notice).toHaveBeenCalledWith(`${behaviorName}-Stop`);
});
});
it('should throw when registering same command handler twice', () => {
expect(() => registerCommonCommands(duid, handler, logger, service, behaviorName, onActionTriggered)).toThrow(/Handler already registered/);
});
});
//# sourceMappingURL=commonCommands.test.js.map