Skip to content

Add edge case tests for devices field in loadConfig() #349

Description

@groupsky

Problem

The test coverage for the devices field in loadConfig() only covers the happy path (non-empty array). Missing critical edge cases that the CLI depends on.

Discovered during aspected review of #347 fix.

Current Coverage

Test added in #347 (config.test.ts lines 121-141):

  • ✅ Non-empty devices array with valid device

Missing Edge Cases

1. Empty Devices Array

Why it matters: CLI explicitly checks for config.devices.length > 0 (cli.ts line 103)

Test needed:

it('should preserve empty devices array in config', async () => {
  const configJson = JSON.stringify({
    mqtt: { url: 'mqtt://localhost:1883' },
    devices: [],
  })

  mockedReadFile.mockResolvedValue(configJson)
  const config = await loadConfig('/path/to/config.json')

  expect(config.devices).toBeDefined()
  expect(config.devices).toEqual([])
})

2. Omitted Devices Field

Why it matters: Ensures backward compatibility for configs without device support

Test needed:

it('should not include devices field when omitted from config', async () => {
  const configJson = JSON.stringify({
    mqtt: { url: 'mqtt://localhost:1883' },
  })

  mockedReadFile.mockResolvedValue(configJson)
  const config = await loadConfig('/path/to/config.json')

  expect(config.devices).toBeUndefined()
  expect('devices' in config).toBe(false)
})

3. Invalid Device Configuration

Why it matters: Zod schema should validate and reject invalid device configs

Test needed:

it('should throw error for invalid device configuration', async () => {
  const configJson = JSON.stringify({
    mqtt: { url: 'mqtt://localhost:1883' },
    devices: [
      {
        deviceId: 'device+invalid',  // Contains MQTT special character
        driver: '@ya-modbus/driver-ex9em',
        connection: { type: 'rtu', port: '/dev/ttyUSB0', baudRate: 9600, slaveId: 1 },
      },
    ],
  })

  mockedReadFile.mockResolvedValue(configJson)

  await expect(loadConfig('/path/to/config.json')).rejects.toThrow('Invalid configuration')
})

Impact

Without these tests:

  • Edge cases could break without detection
  • CLI behavior depends on these cases (empty array vs undefined)
  • No regression protection for validation logic

Files Affected

  • packages/mqtt-bridge/src/utils/config.test.ts

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions