From f2144a89d2814ca3fd20ab61fc2426213cb52cdf Mon Sep 17 00:00:00 2001 From: Alexander Thoukydides Date: Tue, 17 Sep 2024 12:56:11 +0000 Subject: [PATCH] Minimal plugin startup test --- .github/workflows/test.yml | 33 ++++++++++++++++++++++++++++ bin/test-startup.ts | 45 ++++++++++++++++++++++++++++++++++++++ package.json | 3 ++- 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/test.yml create mode 100644 bin/test-startup.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..94dc5b2 --- /dev/null +++ b/.github/workflows/test.yml @@ -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 \ No newline at end of file diff --git a/bin/test-startup.ts b/bin/test-startup.ts new file mode 100644 index 0000000..ae810b1 --- /dev/null +++ b/bin/test-startup.ts @@ -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 => { + // 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}`); + } +})(); \ No newline at end of file diff --git a/package.json b/package.json index 288fab2..a98c2a6 100644 --- a/package.json +++ b/package.json @@ -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"