Skip to content

test: add utility tests and fix advanced search handling#2615

Open
samuelcney wants to merge 4 commits into
evolution-foundation:mainfrom
samuelcney:feat/improve-coverage
Open

test: add utility tests and fix advanced search handling#2615
samuelcney wants to merge 4 commits into
evolution-foundation:mainfrom
samuelcney:feat/improve-coverage

Conversation

@samuelcney

@samuelcney samuelcney commented Jun 28, 2026

Copy link
Copy Markdown

Description

This PR adds an initial automated test setup and unit test coverage for selected utility functions in the Evolution API project.

The work was developed as part of an academic Software Testing assignment, using the project as a real-world open-source codebase to apply testing techniques in practice.

Changes

  • Added Jest configuration with TypeScript support.

  • Added unit tests for utility functions related to:

    • JID creation and phone number formatting;
    • advanced text search operators;
    • chatbot trigger lookup;
    • message content extraction;
    • WhatsApp Web version fetching;
    • error responses and base exceptions.
  • Added mocks to isolate external dependencies such as:

    • database repositories;
    • configuration services;
    • HTTP requests;
    • WhatsApp-related libraries.

Bug fix

While writing structural tests for the advanced text search logic, an issue was found in the handling of empty or malformed search operators.

This PR adds safer handling for these cases, preventing invalid query parts from affecting the expected search behavior.

Notes

The goal of this contribution is to provide an initial testing foundation for critical utility logic, making it easier to validate future changes and expand test coverage over time.

Summary by Sourcery

Introduce Jest-based testing infrastructure and add initial unit tests for critical utility and exception handling logic, while tightening advanced search operator input validation.

Bug Fixes:

  • Prevent empty or malformed advanced search operators from producing invalid query filters by ignoring blank tokens and short-circuiting on empty queries.

Build:

  • Update npm test script and devDependencies to use Jest with ts-jest for TypeScript test execution.

Tests:

  • Add Jest configuration and TypeScript support to run unit tests across the codebase.
  • Add unit tests for utility functions including JID creation, phone formatting, advanced text search, bot trigger lookup, message extraction, WhatsApp Web version fetching, and error responses/exceptions.

@sourcery-ai

sourcery-ai Bot commented Jun 28, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

This PR introduces Jest-based automated testing for key utility and exception logic, adds targeted unit tests with supporting mocks, and hardens the advanced text search utility to safely ignore empty or malformed operators.

File-Level Changes

Change Details Files
Switch test runner from custom tsx script to Jest with TypeScript support and coverage reporting.
  • Updated npm test script to run Jest instead of tsx-based test harness
  • Added Jest, ts-jest, @jest/globals, @types/jest, and supporting TypeScript tooling as dev dependencies
  • Ensured runtime dependency list remains consistent while reordering fetch-socks entry
package.json
package-lock.json
Add centralized Jest configuration tailored to the project’s TypeScript setup and path aliases.
  • Configured ts-jest preset and node testEnvironment
  • Enabled coverage collection for src TypeScript files while excluding entrypoints and declaration files
  • Mapped tsconfig-style path aliases to source directories via moduleNameMapper
  • Defined transform settings and relaxed TypeScript strictness for tests
  • Specified test match patterns and ignored paths
jest.config.ts
Harden advanced text search utility to ignore empty queries and whitespace-only filters.
  • Return false when the query string is empty or only whitespace
  • Skip processing of empty filter tokens produced by extra spaces in the query before splitting into operators
src/utils/advancedOperatorsSearch.ts
Introduce unit tests for utility functions and base HTTP exception classes to validate core behaviors and error handling.
  • Added tests for advanced search operator handling, including malformed and empty operators
  • Covered JID creation and phone number formatting utilities
  • Validated chatbot trigger lookup logic, message content extraction, and WhatsApp Web version fetch behavior
  • Added tests for error response helper and HTTP exception classes (400, 401, 403, 404, 500)
  • Included mocks to isolate external dependencies like repositories, config services, HTTP requests, and WhatsApp-related libraries where needed
test/utils/advancedOperatorsSearch.test.ts
test/utils/createJid.test.ts
test/utils/errorResponse.test.ts
test/utils/fetchLatestWaWebVersion.test.ts
test/utils/findBotByTrigger.test.ts
test/utils/getConversationMessage.test.ts
test/exceptions/400.exception.test.ts
test/exceptions/401.exception.test.ts
test/exceptions/403.exception.test.ts
test/exceptions/404.exception.test.ts
test/exceptions/500.exception.test.ts

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot 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.

Hey - I've found 1 issue, and left some high level feedback:

  • In package.json devDependencies, ts-jest and ts-node are listed twice; remove the duplicates to keep the configuration clean and avoid confusion.
  • In jest.config.ts, you’re overriding several TypeScript compiler options inside ts-jest; consider referencing the existing tsconfig instead of redefining options to avoid divergence between test and production builds.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In package.json devDependencies, ts-jest and ts-node are listed twice; remove the duplicates to keep the configuration clean and avoid confusion.
- In jest.config.ts, you’re overriding several TypeScript compiler options inside ts-jest; consider referencing the existing tsconfig instead of redefining options to avoid divergence between test and production builds.

## Individual Comments

### Comment 1
<location path="jest.config.ts" line_range="47-44" />
<code_context>
+  },
+
+  // ts-jest configuration
+  transform: {
+    '^.+\\.tsx?$': [
+      'ts-jest',
+      {
+        tsconfig: {
+          // Relax strict mode for tests
+          strict: false,
+          strictNullChecks: false,
+          esModuleInterop: true,
+          // Point rootDir to project root so test files are included
+          rootDir: './',
+        },
+      },
+    ],
</code_context>
<issue_to_address>
**suggestion:** Consider pointing ts-jest at the existing tsconfig instead of an inline override.

The custom tsconfig block here (e.g., strict: false, rootDir: './') can drift from tsconfig.json and cause tests to compile differently from the app. Prefer using ts-jest’s `tsconfig` option to point at the main tsconfig (or a dedicated test tsconfig) so the configuration stays consistent and easier to maintain.

Suggested implementation:

```typescript
    '^@utils/(.*)$': '<rootDir>/src/utils/$1',
    '^@validate/(.*)$': '<rootDir>/src/validate/$1',
  },

  // ts-jest configuration
  transform: {
    '^.+\\.tsx?$': [
      'ts-jest',
      {
        // Use the main TypeScript configuration to keep tests aligned
        // with the application compilation settings.
        tsconfig: '<rootDir>/tsconfig.json',
      },
    ],
  },

  // Map TypeScript path aliases to actual paths (mirrors tsconfig.json paths)
  moduleNameMapper: {

```

- If you have a dedicated test TypeScript configuration (e.g., `tsconfig.test.json`), update the `tsconfig` path accordingly: `tsconfig: '<rootDir>/tsconfig.test.json'`.
- Make sure `ts-jest` is correctly set up in the rest of `jest.config.ts` (e.g., `preset: 'ts-jest'` if you are using the preset); adjust as needed to match existing project conventions.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread jest.config.ts
'^@libs/(.*)$': '<rootDir>/src/libs/$1',
'^@utils/(.*)$': '<rootDir>/src/utils/$1',
'^@validate/(.*)$': '<rootDir>/src/validate/$1',
},

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.

suggestion: Consider pointing ts-jest at the existing tsconfig instead of an inline override.

The custom tsconfig block here (e.g., strict: false, rootDir: './') can drift from tsconfig.json and cause tests to compile differently from the app. Prefer using ts-jest’s tsconfig option to point at the main tsconfig (or a dedicated test tsconfig) so the configuration stays consistent and easier to maintain.

Suggested implementation:

    '^@utils/(.*)$': '<rootDir>/src/utils/$1',
    '^@validate/(.*)$': '<rootDir>/src/validate/$1',
  },

  // ts-jest configuration
  transform: {
    '^.+\\.tsx?$': [
      'ts-jest',
      {
        // Use the main TypeScript configuration to keep tests aligned
        // with the application compilation settings.
        tsconfig: '<rootDir>/tsconfig.json',
      },
    ],
  },

  // Map TypeScript path aliases to actual paths (mirrors tsconfig.json paths)
  moduleNameMapper: {
  • If you have a dedicated test TypeScript configuration (e.g., tsconfig.test.json), update the tsconfig path accordingly: tsconfig: '<rootDir>/tsconfig.test.json'.
  • Make sure ts-jest is correctly set up in the rest of jest.config.ts (e.g., preset: 'ts-jest' if you are using the preset); adjust as needed to match existing project conventions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant