|
| 1 | +# Testing the RFXCOM Homebridge Plugin |
| 2 | + |
| 3 | +This guide explains how to test the homebridge-rfxcom-accessories plugin with and without physical hardware. |
| 4 | + |
| 5 | +## Summary |
| 6 | + |
| 7 | +✅ **Plugin Status**: Successfully updated for Node.js 22+ compatibility |
| 8 | +✅ **Dependencies**: All updated to latest versions with security fixes |
| 9 | +✅ **Build**: Compiles successfully with TypeScript 5.6.0 |
| 10 | +✅ **Testing**: Jest-based test suite with mock capabilities |
| 11 | + |
| 12 | +## Running Tests |
| 13 | + |
| 14 | +### Basic Test Commands |
| 15 | + |
| 16 | +```bash |
| 17 | +# Run all tests (unit tests) |
| 18 | +npm test |
| 19 | + |
| 20 | +# Run tests in watch mode (for development) |
| 21 | +npm run test:watch |
| 22 | + |
| 23 | +# Run tests with coverage report |
| 24 | +npm run test:coverage |
| 25 | + |
| 26 | +# Run mock hardware testing |
| 27 | +npm run test:mock |
| 28 | +``` |
| 29 | + |
| 30 | +### Test Structure |
| 31 | + |
| 32 | +The test suite includes: |
| 33 | + |
| 34 | +- **Unit Tests** (`test/basic.test.ts`): 18 tests covering module loading, configuration validation, and build verification |
| 35 | +- **Mock Testing** (`test-with-mock.js`): Interactive testing with simulated RFXCOM hardware |
| 36 | +- **Test Setup** (`test/setup.ts`): Global mocks and utilities for Jest |
| 37 | + |
| 38 | +## Mock Testing for Development |
| 39 | + |
| 40 | +### Quick Mock Test |
| 41 | + |
| 42 | +The easiest way to test without hardware: |
| 43 | + |
| 44 | +```bash |
| 45 | +npm run test:mock |
| 46 | +``` |
| 47 | + |
| 48 | +This will: |
| 49 | + |
| 50 | +- Create a mock RFXCOM device |
| 51 | +- Test RFY blind commands (up/down/stop) |
| 52 | +- Test Lighting1 switch commands (on/off) |
| 53 | +- Start sensor event simulation |
| 54 | +- Show real-time sensor events (temperature, humidity) |
| 55 | + |
| 56 | +### Mock RFXCOM Library |
| 57 | + |
| 58 | +The plugin includes comprehensive mock capabilities in `mock-rfxcom.js`: |
| 59 | + |
| 60 | +- **MockRfxCom**: Simulates the main RFXCOM device |
| 61 | +- **MockRfy**: RFY blind/shade controller simulation |
| 62 | +- **MockLighting1**: Lighting1 switch controller simulation |
| 63 | +- **Sensor Events**: Automatic temperature/humidity sensor events |
| 64 | + |
| 65 | +### Using Mock in Your Code |
| 66 | + |
| 67 | +```javascript |
| 68 | +// Mocking is now handled automatically by Jest |
| 69 | +// No need to set environment variables or manually load mock files |
| 70 | + |
| 71 | +// In your Jest tests, simply import and use the modules normally: |
| 72 | +import { RFYAccessory } from '../src/accessories/rfyAccessory'; |
| 73 | + |
| 74 | +// Jest will automatically use the mocked rfxcom module |
| 75 | +const accessory = new RFYAccessory(mockPlatform, mockPlatformAccessory); |
| 76 | +``` |
| 77 | + |
| 78 | +## Integration with Homebridge |
| 79 | + |
| 80 | +### Test Configuration |
| 81 | + |
| 82 | +```json |
| 83 | +{ |
| 84 | + "platforms": [ |
| 85 | + { |
| 86 | + "name": "RFXCOM Test", |
| 87 | + "platform": "RFXCOMAccessories", |
| 88 | + "tty": "/dev/ttyUSB0", |
| 89 | + "debug": true, |
| 90 | + "devices": { |
| 91 | + "rfy": [ |
| 92 | + { |
| 93 | + "name": "Living Room Blind", |
| 94 | + "deviceId": "0x123456/1", |
| 95 | + "reversed": false, |
| 96 | + "openDurationSeconds": 30, |
| 97 | + "closeDurationSeconds": 30, |
| 98 | + "forceCloseAtStartup": false |
| 99 | + } |
| 100 | + ], |
| 101 | + "switch": [ |
| 102 | + { |
| 103 | + "name": "Living Room Switch", |
| 104 | + "type": "Lighting1", |
| 105 | + "subtype": "ARC", |
| 106 | + "id": "0xABCD/1", |
| 107 | + "forceOffAtStartup": false |
| 108 | + } |
| 109 | + ], |
| 110 | + "weatherSensors": [ |
| 111 | + { |
| 112 | + "name": "Temperature Sensor", |
| 113 | + "type": "temperature1", |
| 114 | + "id": "0x1234" |
| 115 | + } |
| 116 | + ] |
| 117 | + } |
| 118 | + } |
| 119 | + ] |
| 120 | +} |
| 121 | +``` |
| 122 | + |
| 123 | +### Development Workflow |
| 124 | + |
| 125 | +1. **Build the plugin**: `npm run build` |
| 126 | +2. **Run unit tests**: `npm test` |
| 127 | +3. **Test with mock**: `npm run test:mock` |
| 128 | +4. **Test coverage**: `npm run test:coverage` |
| 129 | +5. **Link locally**: `npm link` (for local Homebridge testing) |
| 130 | + |
| 131 | +## Current Test Status |
| 132 | + |
| 133 | +- **18 Unit Tests**: All passing ✅ |
| 134 | +- **Type Safety**: Full TypeScript support ✅ |
| 135 | +- **Mock Hardware**: Complete simulation ✅ |
| 136 | +- **Coverage Reports**: Available in `/coverage` directory ✅ |
| 137 | + |
| 138 | +## Mock Features in Detail |
| 139 | + |
| 140 | +### Device Types Supported |
| 141 | + |
| 142 | +- **RFY Blinds/Shades**: Up, down, stop commands |
| 143 | +- **Lighting1 Switches**: On/off commands |
| 144 | +- **Temperature Sensors**: Automatic events every 30 seconds |
| 145 | +- **Humidity Sensors**: Automatic events every 45 seconds |
| 146 | +- **Combined Sensors**: Temperature+humidity events every 60 seconds |
| 147 | + |
| 148 | +### Event Simulation |
| 149 | + |
| 150 | +The mock automatically generates: |
| 151 | + |
| 152 | +- Temperature readings (20-30°C) |
| 153 | +- Humidity readings (40-80%) |
| 154 | +- Battery levels (1-10 scale) |
| 155 | +- Command acknowledgments |
| 156 | +- Connection status events |
| 157 | + |
| 158 | +### Debug Output |
| 159 | + |
| 160 | +All mock operations are logged with emojis: |
| 161 | + |
| 162 | +- 🎭 Mock RFXCOM operations |
| 163 | +- 🎮 RFY commands |
| 164 | +- 💡 Lighting commands |
| 165 | +- 🌡️ Temperature events |
| 166 | +- 💧 Humidity events |
| 167 | + |
| 168 | +## Troubleshooting |
| 169 | + |
| 170 | +### Common Issues |
| 171 | + |
| 172 | +1. **Unit test failures**: Check `npm test` output for specific errors |
| 173 | +2. **Build errors**: Run `npm run build` to check TypeScript compilation |
| 174 | +3. **Coverage issues**: Use `npm run test:coverage` for detailed reports |
| 175 | +4. **Timer leaks**: Tests now properly clean up timers using Jest fake timers |
| 176 | + |
| 177 | +### Getting Help |
| 178 | + |
| 179 | +- Review test output for detailed error messages |
| 180 | +- Check the coverage report to identify untested areas |
| 181 | +- Verify all dependencies are installed with `npm install` |
| 182 | +- Use `npm run test:mock` for interactive testing |
| 183 | + |
| 184 | +## Files Structure |
| 185 | + |
| 186 | +```text |
| 187 | +test/ |
| 188 | +├── basic.test.ts # Unit tests (18 tests) |
| 189 | +├── setup.ts # Jest configuration and mocks |
| 190 | +test-with-mock.js # Interactive mock testing |
| 191 | +mock-rfxcom.js # Mock RFXCOM library |
| 192 | +jest.config.js # Jest configuration |
| 193 | +``` |
| 194 | + |
| 195 | +## Contributing |
| 196 | + |
| 197 | +When adding new features: |
| 198 | + |
| 199 | +1. Write unit tests for new functionality |
| 200 | +2. Test with mock hardware using `npm run test:mock` |
| 201 | +3. Ensure all existing tests pass: `npm test` |
| 202 | +4. Maintain TypeScript type safety |
| 203 | +5. Update documentation as needed |
| 204 | + |
| 205 | +The plugin is ready for both development testing and production use with real RFXCOM hardware. |
0 commit comments