Skip to content

test: increase critical utils coverage from 50% to 97% (exceeds 80% v1.0 target)#597

Merged
pethers merged 4 commits into
mainfrom
copilot/increase-test-coverage-80
Nov 14, 2025
Merged

test: increase critical utils coverage from 50% to 97% (exceeds 80% v1.0 target)#597
pethers merged 4 commits into
mainfrom
copilot/increase-test-coverage-80

Conversation

Copilot AI commented Nov 14, 2025

Copy link
Copy Markdown
Contributor

Pull Request Description

Three critical utility modules (riskUtils, securityLevelUtils, levelValuesUtils) had 41-59% coverage, below the 80% v1.0 release requirement. Added 138 comprehensive test cases achieving 97% average coverage.

Coverage improvements:

  • riskUtils.ts: 51% → 96% (+45 points)
  • securityLevelUtils.ts: 59% → 99% (+40 points)
  • levelValuesUtils.ts: 41% → 97% (+56 points)

Key additions:

  • Complete security level permutation testing using it.each()
  • Edge case coverage (null, undefined, invalid inputs, boundary values)
  • Business impact calculations with all CIA component combinations
  • Legacy function mapping validation
  • Calculation strategy verification (min, max, avg, weighted)

Example test pattern:

it.each([
  ["None" as SecurityLevel],
  ["Low" as SecurityLevel],
  ["Moderate" as SecurityLevel],
  ["High" as SecurityLevel],
  ["Very High" as SecurityLevel],
])("should handle %s security level", (level) => {
  const result = getDefaultBusinessImpact("integrity", level);
  expect(result.summary).toContain(level);
});

Type of Change

  • 🔄 Code Quality & Refactoring

Component(s) Modified

  • Constants / Data Model

CIA Impact Area

  • Confidentiality
  • Integrity
  • Availability

Security Level Impact

  • Basic
  • Moderate
  • High
  • Very High

Test Coverage Impact

  • I've added tests to improve coverage

Testing Performed

  • Unit tests added/updated (138 new tests)
  • Manual testing completed (all 138 tests passing)

Related Issues

Closes #[issue_number]

Checklist

  • My PR title follows the conventional commit format (e.g., feat: add new feature)
  • Code follows project coding standards
  • Tests are passing
  • Documentation has been updated (if applicable)
  • Security compliance is maintained or improved
  • Changes have been reviewed for performance impact
  • Breaking changes are documented (if any)

Additional Notes

Test execution time: <3 seconds. No production code modified - test-only changes.

Original prompt

This section details on the original issue you should resolve

<issue_title>✅ Increase Test Coverage to 80%+ for Critical Utility Modules</issue_title>
<issue_description>## 🎯 Objective

Increase test coverage from current 75% average to 80%+ for critical utility modules to meet v1.0 release requirements and improve code quality.

📋 Background

The repository custom instructions specify a target of 80%+ test coverage for v1.0 release. Current test coverage analysis shows:

Low Coverage Areas:

  • src/utils/riskUtils.ts: 51.07% coverage (572 lines)
  • src/utils/securityLevelUtils.ts: 59.22% coverage (474 lines)
  • src/utils/levelValuesUtils.ts: 41.02% coverage (133 lines)
  • src/utils/typeGuards.ts: 71.73% coverage (912 lines)
  • src/utils/index.ts: 48.57% coverage (222 lines)

Current State:

  • Overall test coverage: ~75%
  • 97 test files exist
  • Critical utility functions lack comprehensive test coverage
  • Risk of regression bugs in core business logic

Why This Matters:

  • These utilities contain core business logic for security calculations
  • Low coverage increases risk of bugs in production
  • Testing helps document expected behavior
  • Supports safe refactoring for v1.0 release

✅ Acceptance Criteria

  • Increase src/utils/riskUtils.ts coverage from 51% to 80%+
    • Test getDefaultBusinessImpact() function
    • Test legacy function mappings
    • Test error handling paths
    • Test edge cases for security level inputs
  • Increase src/utils/securityLevelUtils.ts coverage from 59% to 80%+
    • Test getSecurityLevelValue() with all security levels
    • Test compareSecurityLevels() edge cases
    • Test validation functions
    • Test error paths and boundary conditions
  • Increase src/utils/levelValuesUtils.ts coverage from 41% to 80%+
    • Test value calculation functions
    • Test all security level permutations
    • Test error handling and invalid inputs
  • All new tests pass in CI
  • No regression in existing test coverage
  • Test execution time remains under 30 seconds

🛠️ Implementation Guidance

Files to Modify

  • src/utils/riskUtils.test.ts - Expand test coverage
  • src/utils/securityLevelUtils.test.ts - Add missing test cases
  • src/utils/levelValuesUtils.test.ts - Add comprehensive tests

Approach

  1. Analyze coverage gaps:
# Generate coverage report
npm run coverage

# Review HTML coverage report
open docs/coverage/index.html

# Identify untested lines in each file
  1. For riskUtils.ts (Target: 51% → 80%+):
// Add tests for:
describe('riskUtils', () => {
  describe('getDefaultBusinessImpact', () => {
    it('should return business impact for each security level', () => {
      // Test Basic, Moderate, High, Very High
    });
    
    it('should handle edge cases', () => {
      // Test undefined, null, invalid levels
    });
  });
  
  describe('legacy function mappings', () => {
    // Test all legacy export functions
  });
});
  1. For securityLevelUtils.ts (Target: 59% → 80%+):
describe('securityLevelUtils', () => {
  describe('getSecurityLevelValue', () => {
    it.each(Object.values(SecurityLevel))('should handle %s level', (level) => {
      const value = getSecurityLevelValue(level);
      expect(value).toBeGreaterThanOrEqual(0);
    });
  });
  
  describe('compareSecurityLevels', () => {
    // Test all comparison cases
  });
});
  1. For levelValuesUtils.ts (Target: 41% → 80%+):
describe('levelValuesUtils', () => {
  describe('calculateLevelValue', () => {
    // Test value calculations
  });
  
  describe('edge cases', () => {
    // Test boundary conditions
  });
});
  1. Testing Strategy:
    • Use it.each() for testing multiple security levels
    • Test both happy paths and error paths
    • Use descriptive test names
    • Group related tests with describe() blocks
    • Mock dependencies where appropriate

Edge Cases to Handle

  • Null/undefined security levels
  • Invalid security level strings
  • Boundary values (min/max)
  • Missing or incomplete data structures
  • Deprecated function compatibility

Example Test Pattern

import { describe, it, expect } from 'vitest';
import { getDefaultBusinessImpact } from './riskUtils';
import { SecurityLevel } from '../types/cia';

describe('riskUtils', () => {
  describe('getDefaultBusinessImpact', () => {
    it('should return valid business impact for Basic level', () => {
      const result = getDefaultBusinessImpact('availability', SecurityLevel.BASIC);
      expect(result).toBeDefined();
      expect(result.component).toBe('availability');
      expect(result.level).toBe(SecurityLevel.BASIC);
    });
    
    it.each([
      [SecurityLevel.BASIC],
      [SecurityLevel.MODERATE],
      [SecurityLevel.HIGH],
      [SecurityLevel.VERY_HIGH]
    ])('should handle %s level', (level) => {
      const result = getDefaultBusinessImpact('integrity', level);
      expe...

</details>


> **Custom agent used: testing-agent**
> Expert in testing for CIA Compliance Manager using Vitest and Cypress

- Fixes Hack23/cia-compliance-manager#582

<!-- START COPILOT CODING AGENT TIPS -->
---

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs.

Copilot AI and others added 3 commits November 14, 2025 17:05
…utils

Co-authored-by: pethers <1726836+pethers@users.noreply.github.com>
Co-authored-by: pethers <1726836+pethers@users.noreply.github.com>
Co-authored-by: pethers <1726836+pethers@users.noreply.github.com>
@github-actions github-actions Bot added documentation Improvements or additions to documentation infrastructure testing test-coverage-impact labels Nov 14, 2025
@github-actions

github-actions Bot commented Nov 14, 2025

Copy link
Copy Markdown
Contributor

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@pethers
pethers requested a review from Copilot November 14, 2025 17:27
Copilot AI changed the title [WIP] Increase test coverage to 80% for critical utility modules test: increase critical utils coverage from 50% to 97% (exceeds 80% v1.0 target) Nov 14, 2025
Copilot AI requested a review from pethers November 14, 2025 17:28
@pethers
pethers marked this pull request as ready for review November 14, 2025 17:29

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR successfully increases test coverage for three critical utility modules from an average of 50.44% to 97.38%, exceeding the 80% target by 17.38 points. The changes are test-only additions with no modifications to production code, adding 138 comprehensive test cases across risk, security level, and level value utilities.

Key Changes:

  • Added 64 test cases to riskUtils.test.ts achieving 95.68% coverage (up from 51.07%)
  • Added 50 test cases to securityLevelUtils.test.ts achieving 99.02% coverage (up from 59.22%)
  • Added 24 test cases to levelValuesUtils.test.ts achieving 97.43% coverage (up from 41.02%)

Reviewed Changes

Copilot reviewed 3 out of 198 changed files in this pull request and generated no comments.

File Description
src/utils/riskUtils.test.ts Comprehensive tests for risk calculation functions, business impact methods, risk level formatting/parsing, and legacy function mappings
src/utils/securityLevelUtils.test.ts Tests for security level percentages, CSS classes, badge variants, type guards, and status mappings with edge case handling
src/utils/levelValuesUtils.test.ts Tests for all calculation strategies (min/max/avg/weighted), security level comparisons, value normalization, and boundary conditions
Coverage HTML files Updated coverage reports showing improved metrics across all three utility modules

@pethers
pethers merged commit 9baf3a4 into main Nov 14, 2025
26 checks passed
@pethers
pethers deleted the copilot/increase-test-coverage-80 branch November 14, 2025 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants