Skip to content

Add build validation unit tests for CLI packages #364

@groupsky

Description

@groupsky

Problem

CLI packages (emulator, cli, device-profiler, mqtt-bridge) have no unit tests that validate their build outputs. This includes:

  • Build output directory structure
  • Executable permissions on bin files
  • Shebang presence in bin files
  • package.json bin field correctness

Context

  • TypeScript compilation could fail silently
  • tsconfig changes could break output structure
  • Build scripts could be modified incorrectly
  • No early detection of build issues

Proposed Solution

Add build validation tests to each CLI package:

packages/emulator/src/build.test.ts:

import fs from 'fs'
import path from 'path'

describe('Build output validation', () => {
  const binPaths = [
    'dist/esm/bin/ya-modbus-emulator.js',
    'dist/cjs/bin/ya-modbus-emulator.js'
  ]

  test.each(binPaths)('bin file exists: %s', (binPath) => {
    expect(fs.existsSync(binPath)).toBe(true)
  })

  test.each(binPaths)('bin file has executable permissions: %s', (binPath) => {
    const stats = fs.statSync(binPath)
    const isExecutable = (stats.mode & 0o111) !== 0
    expect(isExecutable).toBe(true)
  })

  test.each(binPaths)('bin file starts with shebang: %s', (binPath) => {
    const content = fs.readFileSync(binPath, 'utf8')
    expect(content.startsWith('#!/usr/bin/env node')).toBe(true)
  })

  test('package.json bin field points to built file', () => {
    const pkg = JSON.parse(fs.readFileSync('package.json', 'utf8'))
    const binPath = pkg.bin['ya-modbus-emulator']
    expect(fs.existsSync(binPath)).toBe(true)
  })
})

Apply similar tests to:

  • packages/cli
  • packages/device-profiler
  • packages/mqtt-bridge

Benefits

  • Early detection of build configuration issues
  • Validates critical build requirements automatically
  • Documents expected build output structure
  • Prevents regressions in tsconfig or build scripts

Effort

~2-3 hours (4 packages × ~30-45 min each)

Implementation Notes

  • Tests should run after npm run build in CI
  • Use test.each for DRY test code (see docs/TESTING.md)
  • Tests validate behavior, not implementation details

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority:mediumMedium priorityreliabilitySystem reliability and robustnessscope:cliIssues related to the CLI packagescope:device-profilerIssues related to the device-profiler packagescope:emulatorIssues related to the emulator packagescope:mqtt-bridgeIssues related to the MQTT bridge packagetype:testingTesting improvements or additionsvalidationInput validation and configuration checks

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions