Automated CI/CD Testing & Mock Validation for Matterbridge Plugins

Note

  • Published: December 06, 2026

  • Authors: Written by Gemini Tasks, Co-Authored & Reviewed by Carlos Santos (@carlkidcrypto)

  • Estimated Reading Time: 7 minutes

  • Tags: Purpleair • Matter • Matterbridge • Testing • Jest • Typescript • Cicd

Developing a smart home plugin for Matterbridge introduces a unique testing challenge. Manually commissioning a Matter bridge into Apple Home, Google Home, or Home Assistant after every TypeScript refactor does not scale, yet broken cluster attributes or out-of-range numeric encodings can silently break controllers in production.

For purpleair-matterbridge, automated unit and integration tests validate three critical layers before code ever reaches a Docker image:

  • EPA AQI & Cluster Math: Verifying that PM2.5 breakpoints, Celsius int16 scaling (0.01 deg C), and humidity uint16 scaling (0.01%) match Matter 1.5.1 specifications.

  • Mocked Device Registration: Ensuring bridged AirQualitySensor (0x002D) endpoints register with deterministic uniqueId values and valid cluster defaults.

  • Feed Resilience: Confirming that malformed HTTP payloads or offline sensors never crash the parent Matterbridge process.

In this tutorial, we will structure TypeScript unit tests with Jest/Vitest, mock the Matterbridge platform API cleanly, and wire automated schema checks into GitHub Actions.

Isolating Pure Mapping Logic for Unit Tests

The fastest way to make a Matterbridge plugin testable is separating pure telemetry normalization from Matterbridge’s runtime lifecycle hooks. When AQI and cluster scaling live in standalone functions, unit tests execute in milliseconds with zero network or mDNS sockets:

// test/clusterMapping.test.ts
import {
  calculateEpaAqi,
  mapTelemetryToMatterClusters,
} from "../src/clusterMapping";

describe("Matter 1.5.1 Telemetry Normalization", () => {
  it("maps low PM2.5 to Matter AirQuality Good (1)", () => {
    const res = calculateEpaAqi(6.2);
    expect(res.rating).toBe(1);
    expect(res.aqi).toBeLessThanOrEqual(50);
  });

  it("scales Fahrenheit and humidity to fixed-point Matter ints", () => {
    const mapped = mapTelemetryToMatterClusters({
      sensor_index: 282168,
      pm2_5_atm: 18.5,
      current_temp_f: 68.0,
      current_humidity: 45.25,
      pressure: 1013.2,
    });

    // 68.0 deg F == 20.00 deg C -> 2000 in 0.01 deg C units
    expect(mapped.temperatureInt16).toBe(2000);
    // 45.25% RH -> 4525 in 0.01% units
    expect(mapped.humidityUint16).toBe(4525);
    expect(mapped.pressureInt16).toBe(1013);
  });
});

Mocking Matterbridge Device Registration

To test onStart(), onConfigure(), and updateAttribute() calls without starting a real Matter server over UDP port 5353, inject lightweight mock doubles for MatterbridgeDynamicPlatform and MatterbridgeEndpoint:

// test/platformLifecycle.test.ts
export function createMockEndpoint() {
  return {
    uniqueStorageKey: "purple-air-84-a1-4b",
    setAttribute: jest.fn().mockResolvedValue(true),
    updateAttribute: jest.fn().mockResolvedValue(true),
    addClusterServers: jest.fn(),
  };
}

export function createMockPlatform() {
  return {
    log: {
      info: jest.fn(),
      warn: jest.fn(),
      error: jest.fn(),
      debug: jest.fn(),
    },
    registerDevice: jest.fn().mockResolvedValue(true),
    unregisterAllDevices: jest.fn().mockResolvedValue(true),
  };
}

describe("Plugin Lifecycle & Attribute Updates", () => {
  it("registers AirQualitySensor with deterministic uniqueId", async () => {
    const platform = createMockPlatform();
    const endpoint = createMockEndpoint();

    await platform.registerDevice(endpoint);
    expect(platform.registerDevice).toHaveBeenCalledTimes(1);

    await endpoint.updateAttribute(
      "Pm25ConcentrationMeasurement",
      "measuredValue",
      18.5
    );
    expect(endpoint.updateAttribute).toHaveBeenCalledWith(
      "Pm25ConcentrationMeasurement",
      "measuredValue",
      18.5
    );
  });
});

Validating Schema Conformance in GitHub Actions

Add a dedicated Node.js CI workflow (or invoke a reusable workflow from shared-workflows) to run TypeScript compilation, ESLint, and unit tests on every pull request:

name: Node.js CI

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

permissions:
  contents: read

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [20.x, 22.x]

    steps:
      - uses: actions/checkout@v4

      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
          cache: "npm"

      - run: npm ci
      - run: npm run lint
      - run: npm run build
      - run: npm test -- --coverage

Best Practices for Matter Plugin Test Suites

  1. Test Boundary Values & Null Telemetry:

    • Explicitly assert what happens when PurpleAir sensors report null humidity or 0.0 PM2.5. Matter requires null (nullable attribute) or valid range bounds—never NaN or undefined—when updating clusters.

  2. Verify ``unregisterOnShutdown`` Behavior:

    • Add a regression test for onShutdown() confirming that unregisterAllDevices() is skipped when unregisterOnShutdown is false, preserving Apple Home and Home Assistant pairings.

  3. Keep Live Commissioning Smoke Tests Separate:

    • Run unit and mock-platform suites in fast PR checks, and reserve Docker network_mode: host mDNS discovery tests for nightly or release verification.

Summary & What’s Next

Combining pure telemetry unit tests with mocked Matterbridge lifecycle doubles lets purpleair-matterbridge validate Matter 1.5.1 cluster conformance on every pull request without physical smart home hubs in CI.

In our next blog post, we will rotate to ezsnmp, walking through how to migrate from SNMPv2c community strings to SNMPv3 USM ``authPriv`` with SHA-256 authentication, AES-128 encryption, and context-aware VRF polling.


Warning

AI-generated content may contain misinformation or mistakes. A human does review this, but we are only human.