Skip to content

🧪 Add branch coverage tests for constants files (currently 0% branch coverage) #634

Description

@pethers

🤖 Recommended Agent: @testing-agent

This issue is best handled by the Testing Agent - an expert in test coverage improvement and branch testing.

🎯 Objective

Add comprehensive branch coverage tests for constants files to ensure conditional logic is properly tested and achieve 70%+ branch coverage for v1.0 release.

📋 Background

Many constant files in src/constants/ have 0% branch coverage despite containing conditional logic, type guards, and data transformations. This creates risk of untested code paths and reduces overall branch coverage below the 70% ISMS requirement.

📊 Current State (Measured Metrics)

Constants Files with 0% or Low Branch Coverage:

  • src/constants/appConstants.ts: 90.24% statements / 0% branches
  • src/constants/businessConstants.ts: 53.33% statements / 0% branches
  • src/constants/costConstants.ts: 80% statements / 0% branches
  • src/constants/riskConstants.ts: 86.66% statements / 0% branches
  • src/constants/securityLevels.ts: 72.72% statements / 0% branches
  • src/constants/testIds.ts: 81.66% statements / 0% branches (64.51% functions)
  • src/constants/uiConstants.ts: 50% statements / 16.66% branches

Overall Impact:

  • Current branch coverage: 64.9%
  • Target branch coverage: 70%
  • Gap: 5.1% - constants contribute significantly to this gap

✅ Acceptance Criteria

  • All constants files have ≥70% branch coverage
  • Test files created for each constants file with conditional logic
  • All conditional branches tested (if/else, ternary operators, switch statements)
  • All exported helper functions tested with edge cases
  • Overall project branch coverage ≥70%
  • All tests follow Vitest + existing test patterns

🛠️ Implementation Guidance for @testing-agent

Test Files to Create:

  1. src/constants/appConstants.test.ts - Test app configuration logic
  2. src/constants/businessConstants.test.ts - Test business logic constants
  3. src/constants/costConstants.test.ts - Test cost calculation constants
  4. src/constants/riskConstants.test.ts - Test risk assessment logic
  5. src/constants/securityLevels.test.ts - Test security level mappings
  6. src/constants/uiConstants.test.ts - Test UI configuration branches
  7. src/constants/testIds.test.ts - Test ID generation functions

Agent-Specific Testing Strategy:

Use Vitest for all constant file tests
Follow Arrange-Act-Assert pattern
Test all conditional branches: if/else, ternary operators, switch statements
Test helper functions with edge cases (null, undefined, empty strings)
Test type guards and validation functions
Use descriptive test names that explain what is being tested

Example Test Pattern:

// src/constants/securityLevels.test.ts
import { describe, it, expect } from 'vitest';
import { 
  SECURITY_LEVELS, 
  getSecurityLevelName, 
  isValidSecurityLevel 
} from './securityLevels';

describe('securityLevels', () => {
  describe('SECURITY_LEVELS constant', () => {
    it('contains all required levels', () => {
      expect(SECURITY_LEVELS).toContain('None');
      expect(SECURITY_LEVELS).toContain('Low');
      expect(SECURITY_LEVELS).toContain('Moderate');
      expect(SECURITY_LEVELS).toContain('High');
      expect(SECURITY_LEVELS).toContain('Very High');
    });
  });

  describe('getSecurityLevelName', () => {
    it('returns correct name for each level', () => {
      expect(getSecurityLevelName('None')).toBe('No Security');
      expect(getSecurityLevelName('Low')).toBe('Basic Security');
      expect(getSecurityLevelName('High')).toBe('Advanced Security');
    });

    it('handles invalid levels', () => {
      expect(getSecurityLevelName('invalid')).toBe('Unknown');
    });

    it('handles edge cases', () => {
      expect(getSecurityLevelName(null as any)).toBe('Unknown');
      expect(getSecurityLevelName(undefined as any)).toBe('Unknown');
      expect(getSecurityLevelName('')).toBe('Unknown');
    });
  });

  describe('isValidSecurityLevel', () => {
    it('returns true for valid levels', () => {
      SECURITY_LEVELS.forEach(level => {
        expect(isValidSecurityLevel(level)).toBe(true);
      });
    });

    it('returns false for invalid levels', () => {
      expect(isValidSecurityLevel('invalid')).toBe(false);
      expect(isValidSecurityLevel('')).toBe(false);
      expect(isValidSecurityLevel(null as any)).toBe(false);
    });
  });
});

Testing Patterns by File:

appConstants.ts:

  • Test environment-specific configurations
  • Test feature flag logic
  • Test version and build metadata

businessConstants.ts:

  • Test business rules with different inputs
  • Test calculation thresholds and limits
  • Test data classification logic

costConstants.ts:

  • Test cost tier logic (small, medium, large)
  • Test percentage calculations
  • Test currency formatting functions

riskConstants.ts:

  • Test risk level classification
  • Test risk score calculations
  • Test risk categorization logic

testIds.ts:

  • Test ID generation for different component types
  • Test ID sanitization logic
  • Test edge cases (empty strings, special characters)

Verification Commands:

# Run tests for constants
npm test -- src/constants

# Check coverage for specific file
npm run coverage -- src/constants/securityLevels.ts

# Run all tests with coverage
npm run coverage

🔗 Related Resources

📊 Metadata

Agent: @testing-agent | Priority: Medium | Effort: S (3-4h)
Labels: type:test, domain:testing, priority:medium, size:small, v1.0

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions