Skip to content

Maintenance: Refactor duplicate test handler functions in batch package #4248

@dreamorosi

Description

@dreamorosi

Summary

The packages/batch/tests/helpers/handlers.ts file contains multiple duplicate function implementations that violate the DRY (Don't Repeat Yourself) principle. SonarQube has identified 3 instances where functions have identical implementations:

  • Line 17: Function identical to the one on line 8 (sqsRecordHandler vs asyncSqsRecordHandler pattern)
  • Line 37: Function identical to the one on line 26 (kinesisRecordHandler vs asyncKinesisRecordHandler pattern)
  • Line 57: Function identical to the one on line 46 (dynamodbRecordHandler vs asyncDynamodbRecordHandler pattern)

Each pair of functions follows the same pattern where a synchronous handler and its asynchronous counterpart have nearly identical logic, differing only in their return type (string/object vs Promise<string>/Promise<object>).

Why is this needed?

  1. Code Quality: Eliminates code duplication and improves maintainability
  2. SonarQube Compliance: Resolves 3 MAJOR severity issues (typescript:S4144) that are currently flagged
  3. Consistency: Establishes a pattern for creating test handlers that can be reused across the batch processing tests
  4. Maintenance Burden: Reduces the risk of inconsistencies when updating test logic, as changes would only need to be made in one place

Currently, any changes to the test logic require updating multiple functions, increasing the chance of introducing bugs or inconsistencies.

Which area does this relate to?

Batch Processing, Tests

Solution

Important

The following changes are included as reference to help you understand the refactoring. Before implementing, please make sure to check the codebase and ensure that they make sense and they are exhaustive.

Refactor the duplicate functions using a base handler with async wrapper approach:

const baseSqsHandler = (record: SQSRecord): string => {
  const body = record.body;
  if (body.includes('fail')) {
    throw Error('Failed to process record.');
  }
  return body;
};

const sqsRecordHandler = baseSqsHandler;
const asyncSqsRecordHandler = async (record: SQSRecord): Promise<string> => 
  Promise.resolve(baseSqsHandler(record));

const baseKinesisHandler = (record: KinesisStreamRecord): string => {
  const body = record.kinesis.data;
  if (body.includes('fail')) {
    throw Error('Failed to process record.');
  }
  return body;
};

const kinesisRecordHandler = baseKinesisHandler;
const asyncKinesisRecordHandler = async (record: KinesisStreamRecord): Promise<string> => 
  Promise.resolve(baseKinesisHandler(record));

const baseDynamodbHandler = (record: DynamoDBRecord): object => {
  const body = record.dynamodb?.NewImage?.Message || { S: 'fail' };
  if (body.S?.includes('fail')) {
    throw Error('Failed to process record.');
  }
  return body;
};

const dynamodbRecordHandler = baseDynamodbHandler;
const asyncDynamodbRecordHandler = async (record: DynamoDBRecord): Promise<object> => 
  Promise.resolve(baseDynamodbHandler(record));

This approach:

  • Maintains clear, readable function signatures
  • Minimizes changes to existing test code
  • Clearly separates sync and async behavior
  • Is easy to understand and maintain

Implementation Details

  1. Files to modify:

    • packages/batch/tests/helpers/handlers.ts
  2. Testing:

    • Ensure all existing tests continue to pass
    • Verify both sync and async handlers behave identically
    • Run SonarQube analysis to confirm issues are resolved
  3. Validation:

    • Run npm run test:unit -w packages/batch to ensure no regressions
    • Run npm run lint -w packages/batch to verify code style compliance

Additional Context

This issue was identified as part of a SonarQube code quality review. The batch package currently has only 3 remaining open SonarQube issues, all of which are these duplicate function implementations. Resolving this will bring the batch package to zero open code quality issues.

SonarQube Issue Keys:

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

Labels

batchThis item relates to the Batch Processing UtilityconfirmedThe scope is clear, ready for implementationgood-first-issueSomething that is suitable for those who want to start contributinghelp-wantedWe would really appreciate some support from community for this oneinternalPRs that introduce changes in governance, tech debt and chores (linting setup, baseline, etc.)

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions