Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add test for combine tools script #3136

Open
wants to merge 57 commits into
base: master
Choose a base branch
from

Conversation

vishvamsinh28
Copy link
Contributor

@vishvamsinh28 vishvamsinh28 commented Aug 9, 2024

This PR adds test for combine tools script.

Summary by CodeRabbit

  • New Features

    • Introduced new tools data structures for testing purposes, enhancing the testing framework.
    • Added new entries in JSON files for automated and manual tools, including "Tool A" and "Tool B".
    • Enhanced directory handling in various scripts to ensure directories exist before writing files.
    • Updated file writing methods to be asynchronous for improved performance.
  • Bug Fixes

    • Improved error handling in the combineTools function to catch exceptions and log validation errors.
  • Tests

    • Implemented a comprehensive suite of unit tests for the combineTools function to ensure reliability and correctness.
    • Updated tests to maintain consistency in path construction and enhance robustness in setup and teardown processes.

Copy link

netlify bot commented Aug 9, 2024

Deploy Preview for asyncapi-website ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 464b1dc
🔍 Latest deploy log https://app.netlify.com/sites/asyncapi-website/deploys/671da2a48084fe0008cf126c
😎 Deploy Preview https://deploy-preview-3136--asyncapi-website.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@asyncapi-bot
Copy link
Contributor

asyncapi-bot commented Aug 9, 2024

⚡️ Lighthouse report for the changes in this PR:

Category Score
🔴 Performance 47
🟢 Accessibility 98
🟢 Best practices 92
🟢 SEO 100
🔴 PWA 33

Lighthouse ran on https://deploy-preview-3136--asyncapi-website.netlify.app/

@@ -106,7 +105,7 @@ const getFinalTool = async (toolObject) => {

// Combine the automated tools and manual tools list into single JSON object file, and
// lists down all the language and technology tags in one JSON file.
const combineTools = async (automatedTools, manualTools) => {
const combineTools = async (automatedTools, manualTools, toolsPath, tagsPath) => {
Copy link
Member

Choose a reason for hiding this comment

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

what does tagspath signify??

JSON.stringify({ languages: languageList, technologies: technologyList }),
)
fs.writeFileSync(toolsPath,JSON.stringify(finalTools));
fs.writeFileSync(tagsPath,JSON.stringify({ languages: languageList, technologies: technologyList }),)
}

module.exports = { combineTools }
Copy link
Member

Choose a reason for hiding this comment

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

fix this linter error

const tagsData = readJSON(tagsPath);
expect(tagsData.languages).toContainEqual(expect.objectContaining({ name: 'NewLanguage' }));
});
});
Copy link
Member

Choose a reason for hiding this comment

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

fix this one also

@anshgoyalevil anshgoyalevil added the gsoc This label should be used for issues or discussions related to ideas for Google Summer of Code label Aug 18, 2024
@@ -14,7 +14,7 @@ const buildTools = async () => {
resolve(__dirname, '../config', 'tools-automated.json'),
JSON.stringify(automatedTools, null, ' ')
);
await combineTools(automatedTools, manualTools);
await combineTools(automatedTools, manualTools, resolve(__dirname, '../../config', 'tools.json'), resolve(__dirname, '../../config', 'all-tags.json'));
Copy link
Member

Choose a reason for hiding this comment

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

Can we do it in a more better way, instead of passing filePath here? Try to refactor tools scripts instead of manually passing filePaths for tags file.

});

it('should combine tools and create correct JSON files', async () => {
await combineTools(automatedTools, manualTools, toolsPath, tagsPath);
Copy link
Member

Choose a reason for hiding this comment

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

try to add this function call in a try/catch block here to monitor if we are receiving any error or not.

});


it('should log validation errors to console.error', async () => {
Copy link
Member

Choose a reason for hiding this comment

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

Errors shouldn't be validated via console messages. You should formally return a error using Promise.reject.

@@ -6,15 +6,19 @@ const manualTools = require('../config/tools-manual.json')
const fs = require('fs');
const { resolve } = require('path');

let toolsPath = resolve(__dirname, '../../config', 'tools.json')
Copy link
Member

Choose a reason for hiding this comment

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

is using let a good option here?

@vishvamsinh28 vishvamsinh28 marked this pull request as draft September 21, 2024 09:47
Copy link

codecov bot commented Oct 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 56.85%. Comparing base (c9ef36d) to head (464b1dc).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3136      +/-   ##
==========================================
+ Coverage   49.14%   56.85%   +7.70%     
==========================================
  Files          21       21              
  Lines         647      649       +2     
==========================================
+ Hits          318      369      +51     
+ Misses        329      280      -49     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@anshgoyalevil
Copy link
Member

@vishvamsinh28 You need to check what caused this workflow to fail

Additionally, can we upload base reports for each PR, or does that work as a one time process for a new repo. Kindly research on that and fix

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (3)
scripts/build-tools.js (1)

Line range hint 9-11: Consider using 'const' instead of 'let'

The variables githubExtractData and automatedTools are declared using let, but they are not reassigned after their initial assignment. To improve code clarity and prevent accidental reassignment, consider using const instead:

-    let githubExtractData = await getData();
-    let automatedTools = await convertTools(githubExtractData);
+    const githubExtractData = await getData();
+    const automatedTools = await convertTools(githubExtractData);

This change aligns with best practices for variable declaration in JavaScript and makes the code's intent clearer.

tests/build-tools.test.js (2)

Line range hint 1-89: Consider enhancing test coverage

While the existing tests cover the main scenarios, consider adding the following to improve coverage:

  1. Test with empty input files.
  2. Test with malformed JSON in input files.
  3. Test the behavior when the categoryList is empty or contains unexpected data.
  4. Verify the structure and content of the output files more thoroughly.

These additional tests would help ensure the robustness of the buildTools function across various edge cases.


3-5: Improve visibility of mock data

While using mock data from external files is a good practice for keeping tests clean, it can sometimes make it harder to understand the tests at a glance. Consider adding brief comments or examples of the mock data structure directly in this test file. This would make it easier for developers to understand the test scenarios without having to switch between multiple files.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 0f9052e and 5c4c192.

📒 Files selected for processing (2)
  • scripts/build-tools.js (2 hunks)
  • tests/build-tools.test.js (3 hunks)
🧰 Additional context used
🔇 Additional comments (4)
scripts/build-tools.js (1)

25-28: 🛠️ Refactor suggestion

Consider the implications of changing from path.resolve() to path.join()

The switch from path.resolve() to path.join() might alter how paths are constructed. While path.resolve() always returns an absolute path, path.join() simply concatenates path segments. This change could make the paths relative to the current working directory instead of absolute paths.

Please verify that this change doesn't negatively impact how these paths are used in other parts of the codebase. If absolute paths are required, consider using path.resolve(__dirname, ...) instead.

To verify the impact of this change, run the following script:

Additionally, as per a previous review comment, consider refactoring the tools scripts to avoid manually passing file paths. This could involve creating a configuration file or using environment variables to manage these paths centrally.

✅ Verification successful

Path resolution change verified; no impact detected

The change from path.resolve() to path.join() in scripts/build-tools.js has been verified. Searches across the codebase show that automatedToolsPath, manualToolsPath, toolsPath, and tagsPath are used consistently within their modules and tests, with no negative impacts detected.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for usage of these file paths in other parts of the codebase

# Test: Search for usage of these file paths
echo "Searching for usage of automatedToolsPath:"
rg "automatedToolsPath" --type js

echo "Searching for usage of manualToolsPath:"
rg "manualToolsPath" --type js

echo "Searching for usage of toolsPath:"
rg "toolsPath" --type js

echo "Searching for usage of tagsPath:"
rg "tagsPath" --type js

Length of output: 9115

tests/build-tools.test.js (3)

27-31: Improved path handling with path.join()

The change from path.resolve() to path.join() for constructing file paths is a good improvement. path.join() is more predictable and safer for cross-platform compatibility, as it doesn't resolve to an absolute path like resolve() might in some cases. This change aligns with best practices for path manipulation in Node.js.


81-81: Consistent path handling for invalid path

The update to use path.join() for constructing the invalidPath is consistent with the earlier changes. This maintains the test's intention of creating an invalid path while aligning with the improved path handling approach.


Line range hint 1-89: Overall assessment: Good improvements with room for enhancement

The changes to path handling using path.join() are positive and improve cross-platform compatibility. The overall test structure remains robust, covering main scenarios effectively. To further enhance this test suite, consider:

  1. Implementing the suggested additional test cases for better coverage.
  2. Improving the visibility of mock data structures within the test file.
  3. Adding more detailed assertions to verify the structure and content of output files.

These enhancements would contribute to a more comprehensive and maintainable test suite.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (1)
tests/tools/combine-tools.test.js (1)

30-32: Remove unused mock for 'ajv-formats'

The mock for 'ajv-formats' is defined but not utilized in the tests. Removing this unused mock will clean up the code.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 5c4c192 and db33c88.

📒 Files selected for processing (2)
  • tests/build-tools.test.js (1 hunks)
  • tests/tools/combine-tools.test.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/build-tools.test.js
🧰 Additional context used
🔇 Additional comments (18)
tests/tools/combine-tools.test.js (18)

1-22: Module imports and data setups are well-structured

The required modules and fixtures are correctly imported, providing a solid foundation for the tests.


34-43: Mocks for 'tags-color' are correctly set up

The color configurations for languages and technologies are well-defined, aiding in the consistency of test data.


45-50: 'categoryList' mock is properly configured

The mocked categories provide a clear structure for testing category-related functionalities.


52-52: Efficient JSON reading with readJSON helper

The readJSON function enhances code readability and reuse by encapsulating JSON file reading.


54-76: Test initialization and cleanup are properly handled

Using beforeAll and afterAll ensures that tests are isolated and the environment is clean, preventing side effects between tests.


78-88: Comprehensive test for combining tools and generating JSON files

The test correctly verifies that the combineTools function creates the expected files with the right properties.


90-96: Correct handling of tools with missing data

The test ensures that tools lacking language or technology information are still processed appropriately.


98-105: Tools are sorted alphabetically as expected

Verifying the alphabetical sorting of tools by title confirms that the sorting functionality works correctly.


107-120: Validation errors are effectively logged

The test successfully checks that validation errors are captured and logged, aiding in debugging and error tracking.


122-136: Proper handling of multiple and new languages

The test confirms that tools with multiple languages, including new ones, are processed, and tags are updated accordingly.


138-162: New languages and technologies are added correctly

The test ensures that unfamiliar languages and technologies are added to the tags, maintaining the integrity of the tag system.


164-180: Addition of new languages is handled properly

By testing with a new language, the code demonstrates flexibility in accommodating growth in the list of supported languages.


182-198: Valid tool objects are processed accurately

The test validates that tools with complete and correct data are handled as expected, reinforcing confidence in the normal operation of combineTools.


200-208: Handling tools without repoUrl gracefully

The absence of repoUrl doesn't cause issues, and the test confirms that the isAsyncAPIrepo property is managed correctly.


210-221: Refactor error handling using Jest's .rejects.toThrow()

The previous review comment suggesting refactoring this test to use .rejects.toThrow() instead of a try/catch block is still applicable.


223-233: Simplify error assertions with Jest's .rejects.toThrow()

The prior suggestion to use .rejects.toThrow() for cleaner error handling remains valid for this test case.


235-245: Use .rejects.toThrow() for clearer asynchronous error tests

The earlier recommendation to refactor this test using Jest's .rejects.toThrow() method is still pertinent.


247-258: Refactor to simplify error handling with Jest

The test can be streamlined by adopting .rejects.toThrow() as previously suggested.

Comment on lines +23 to +27
jest.mock('ajv', () => {
return jest.fn().mockImplementation(() => ({
compile: jest.fn().mockImplementation(() => (data) => data.title !== 'Invalid Tool'),
}));
});
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance the AJV mock for comprehensive validation testing

The current mock of the ajv validator simplifies validation to only check the title field. To improve test coverage and ensure robustness, consider using the actual ajv validator or a more detailed mock that simulates various validation scenarios.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (4)
scripts/build-newsroom-videos.js (2)

7-10: LGTM: Directory creation logic added.

The new code block effectively ensures that the target directory exists before attempting to write the file. This prevents potential errors and improves the robustness of the script.

Consider adding error handling for the mkdirSync call:

if (!existsSync(dir)) {
    try {
        mkdirSync(dir, { recursive: true });
    } catch (err) {
        throw new Error(`Failed to create directory: ${err.message}`);
    }
}

This will provide more specific error information if directory creation fails.


1-10: Overall assessment: Improved robustness without affecting core functionality.

The changes in this file focus on ensuring the target directory exists before writing the output file. This improvement adds robustness to the script without altering its core functionality of fetching and processing YouTube video data.

The error handling for the YouTube API interaction remains intact, maintaining the script's reliability for its primary purpose.

Consider adding a config file or environment variable for the output directory path. This would make the script more flexible and easier to configure in different environments.

tests/build-newsroom-videos.test.js (2)

18-20: LGTM: Improved error handling in cleanup.

The addition of the existence check before removing the test directory is a good practice. It prevents potential errors if the directory doesn't exist.

Consider using the fs.promises API for asynchronous file system operations, which can lead to more consistent and easier-to-maintain code:

const fs = require('fs').promises;

afterAll(async () => {
  try {
    await fs.rm(testDir, { recursive: true, force: true });
  } catch (error) {
    if (error.code !== 'ENOENT') {
      throw error;
    }
  }
});

This approach handles the directory removal asynchronously and catches any errors, only re-throwing if it's not a "directory not found" error.


24-26: LGTM: Improved directory handling in test setup.

The addition of the existence check before creating the test directory is a good practice. It prevents unnecessary directory creation attempts and potential errors.

Consider using the fs.promises API for asynchronous file system operations:

const fs = require('fs').promises;

beforeEach(async () => {
  await fs.mkdir(testDir, { recursive: true });
  fetch.mockClear();
});

This approach handles the directory creation asynchronously and the recursive: true option automatically creates parent directories if they don't exist, eliminating the need for an explicit existence check.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between db33c88 and 4a4dcb7.

📒 Files selected for processing (5)
  • scripts/build-newsroom-videos.js (1 hunks)
  • scripts/build-tools.js (1 hunks)
  • tests/build-newsroom-videos.test.js (4 hunks)
  • tests/build-tools.test.js (2 hunks)
  • tests/tools/combine-tools.test.js (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • scripts/build-tools.js
  • tests/build-tools.test.js
  • tests/tools/combine-tools.test.js
🧰 Additional context used
🔇 Additional comments (3)
scripts/build-newsroom-videos.js (1)

1-2: LGTM: Necessary imports added for new functionality.

The additional imports (mkdirSync, existsSync, and dirname) are correctly added and align with the new directory creation logic introduced in the file.

tests/build-newsroom-videos.test.js (2)

1-1: LGTM: Appropriate import added for directory existence check.

The addition of existsSync to the imports is necessary and aligns with the new directory existence checks implemented in the test suite. This change improves the robustness of the tests.


Line range hint 1-95: Overall assessment: Good improvements with minor suggestions.

The changes in this file generally improve the robustness of the test suite by adding necessary checks for directory existence. Here's a summary of the review:

  1. The addition of existsSync import is appropriate.
  2. The afterAll and beforeEach hooks now handle directory operations more safely.
  3. There's a redundant directory check in one of the test cases that should be addressed.

Consider the suggestions for using fs.promises API for more consistent asynchronous file operations. These changes will further enhance the quality and maintainability of the test suite.

Comment on lines 36 to 39
if (!existsSync(testDir)) {
mkdirSync(testDir, { recursive: true });
}

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Consider removing redundant directory check.

The directory existence check and creation logic here is redundant with the one in the beforeEach hook. Unless there's a specific reason for this additional check that's not immediately apparent, consider removing it to keep the test case focused on its primary purpose.

If you decide to keep this check, please add a comment explaining why it's necessary here in addition to the beforeEach hook. Otherwise, you can remove these lines:

- if (!existsSync(testDir)) {
-     mkdirSync(testDir, { recursive: true });
- }
-
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (!existsSync(testDir)) {
mkdirSync(testDir, { recursive: true });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gsoc This label should be used for issues or discussions related to ideas for Google Summer of Code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants