Skip to content

Commit

Permalink
Minimal plugin startup test
Browse files Browse the repository at this point in the history
  • Loading branch information
thoukydides authored Sep 17, 2024
1 parent 71f0912 commit f2144a8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
33 changes: 33 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Test

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

strategy:
matrix:
# the Node.js versions to build on
node-version: [18.x, 20.x, 22.x]
homebridge-version: ['^1.8.0', '^2.0.0-beta.0']

steps:
- uses: actions/checkout@v3

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm install

- name: Use homebridge ${{ matrix.homebridge-version }}
run: npm install homebridge@${{ matrix.homebridge-version }}

- name: Build the project
run: npm run build

- name: Tune the tests
run: npm run test
45 changes: 45 additions & 0 deletions bin/test-startup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Homebridge plugin for AEG RX 9 / Electrolux Pure i9 robot vacuum
// Copyright © 2024 Alexander Thoukydides

import { spawn } from 'child_process';
import assert from 'node:assert';

// Command to use to launch Homebridge
const SPAWN_COMMAND = 'homebridge';
const SPAWN_ARGS = '-D -I -P .. --strict-plugin-resolution'.split(' ');

// Log messages indicating success
const SUCCESS_OUTPUT_REGEX = /\[Homebridge AEG Robot Vacuum\] (Starting new authorisation|Using saved access token)/;

// Length of time to wait for the message
const TIMEOUT_MS = 15 * 1000; // (15 seconds)

// Run the test
void (async (): Promise<void> => {
// Attempt to launch Homebridge
const homebridge = spawn(SPAWN_COMMAND, SPAWN_ARGS, { stdio: 'pipe', timeout: TIMEOUT_MS });

// Log any error output
//homebridge.stderr.on('data', (data) => { console.error(`stderr: ${data.trim()}`); });

// Collect stdout and check for success message(s)
let output = '';
homebridge.stdout.setEncoding('utf8');
for await (const chunk of homebridge.stdout) {
assert(typeof chunk === 'string');
output += chunk.toString();
if (SUCCESS_OUTPUT_REGEX.test(output)) {
// Test completed successfully
homebridge.kill('SIGTERM');
console.log('Success');
return;
}
}

// Homebridge did not start successfully
switch (homebridge.exitCode) {
case null: throw new Error('Unexpected stdout termination');
case 0: throw new Error('Unexpected successful process termination');
default: throw new Error(`Homebridge exited with code ${homebridge.exitCode}`);
}
})();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
"checkers": "mkdir -p ./src/ti && ts-interface-builder -c -o ./src/ti ./src/*-types.ts && tsx bin/ts-interface-post.ts ./src/ti",
"build": "rimraf ./dist && npm run checkers && tsc",
"postversion": "git push && git push --tags",
"prepublishOnly": "npm run lint && npm run build"
"prepublishOnly": "npm run lint && npm run build",
"test": "tsx bin/test-startup.ts"
},
"optionalDependencies": {
"fsevents": "^2.3.3"
Expand Down

0 comments on commit f2144a8

Please sign in to comment.