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 tests for tool-object script #3265

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
106 changes: 55 additions & 51 deletions scripts/tools/tools-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const fuse = new Fuse(categoryList, options)
// isAsyncAPIrepo boolean variable to define whether the tool repository is under
// AsyncAPI organization or not, to create a JSON tool object as required in the frontend
// side to show ToolCard.
const createToolObject = async (toolFile, repositoryUrl='', repoDescription='', isAsyncAPIrepo='') => {
const createToolObject = async (toolFile, repositoryUrl = '', repoDescription = '', isAsyncAPIrepo = '') => {
let resultantObject = {
title: toolFile.title,
description: toolFile?.description ? toolFile.description : repoDescription,
Expand All @@ -47,67 +47,71 @@ const createToolObject = async (toolFile, repositoryUrl='', repoDescription='',
// and creating a JSON tool object in which all the tools are listed in defined
// categories order, which is then updated in `automated-tools.json` file.
async function convertTools(data) {
let finalToolsObject = {};
const dataArray = data.items;
try {
let finalToolsObject = {};
const dataArray = data.items;

// initialising finalToolsObject with all categories inside it with proper elements in each category
for (var index in categoryList) {
finalToolsObject[categoryList[index].name] = {
description: categoryList[index].description,
toolsList: []
};
}
// initialising finalToolsObject with all categories inside it with proper elements in each category
for (var index in categoryList) {
finalToolsObject[categoryList[index].name] = {
description: categoryList[index].description,
toolsList: []
};
}

for (let tool of dataArray) {
try {
if (tool.name.startsWith('.asyncapi-tool')) {
// extracting the reference id of the repository which will be used to extract the path of the .asyncapi-tool file in the Tools repository
// ex: for a url = "https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=61855e7365a881e98c2fe667a658a0005753d873"
// the text (id) present after '=' gives us a reference id for the repo
let reference_id = tool.url.split("=")[1];
let download_url = `https://raw.githubusercontent.com/${tool.repository.full_name}/${reference_id}/${tool.path}`;
for (let tool of dataArray) {
try {
if (tool.name.startsWith('.asyncapi-tool')) {
// extracting the reference id of the repository which will be used to extract the path of the .asyncapi-tool file in the Tools repository
// ex: for a url = "https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=61855e7365a881e98c2fe667a658a0005753d873"
// the text (id) present after '=' gives us a reference id for the repo
let reference_id = tool.url.split("=")[1];
let download_url = `https://raw.githubusercontent.com/${tool.repository.full_name}/${reference_id}/${tool.path}`;

const { data: toolFileContent } = await axios.get(download_url);
const { data: toolFileContent } = await axios.get(download_url);

//some stuff can be YAML
const jsonToolFileContent = await convertToJson(toolFileContent)
//some stuff can be YAML
const jsonToolFileContent = await convertToJson(toolFileContent)

//validating against JSON Schema for tools file
const isValid = await validate(jsonToolFileContent)
//validating against JSON Schema for tools file
const isValid = await validate(jsonToolFileContent)

if (isValid) {
let repositoryUrl = tool.repository.html_url;
let repoDescription = tool.repository.description;
let isAsyncAPIrepo = tool.repository.owner.login === "asyncapi";
let toolObject = await createToolObject(jsonToolFileContent, repositoryUrl, repoDescription, isAsyncAPIrepo);
if (isValid) {
let repositoryUrl = tool.repository.html_url;
let repoDescription = tool.repository.description;
let isAsyncAPIrepo = tool.repository.owner.login === "asyncapi";
let toolObject = await createToolObject(jsonToolFileContent, repositoryUrl, repoDescription, isAsyncAPIrepo);

// Tool Object is appended to each category array according to Fuse search for categories inside Tool Object
jsonToolFileContent.filters.categories.forEach(async (category) => {
const categorySearch = await fuse.search(category);
// Tool Object is appended to each category array according to Fuse search for categories inside Tool Object
jsonToolFileContent.filters.categories.forEach(async (category) => {
const categorySearch = await fuse.search(category);

if (categorySearch.length) {
let searchedCategoryName = categorySearch[0].item.name
if (!finalToolsObject[searchedCategoryName].toolsList.find((element => element === toolObject)))
finalToolsObject[searchedCategoryName].toolsList.push(toolObject);
} else {
// if Tool object has a category, not defined in our categorylist, then this provides a `other` category to the tool.
if (!finalToolsObject['Others'].toolsList.find((element => element === toolObject)))
finalToolsObject['Others'].toolsList.push(toolObject);
}
});
} else {
console.error('Script is not failing, it is just dropping errors for further investigation');
console.error('Invalid .asyncapi-tool file.');
console.error(`Located in: ${tool.html_url}`);
console.error('Validation errors:', JSON.stringify(validate.errors, null, 2));
if (categorySearch.length) {
let searchedCategoryName = categorySearch[0].item.name
if (!finalToolsObject[searchedCategoryName].toolsList.find((element => element === toolObject)))
finalToolsObject[searchedCategoryName].toolsList.push(toolObject);
} else {
// if Tool object has a category, not defined in our categorylist, then this provides a `other` category to the tool.
if (!finalToolsObject['Others'].toolsList.find((element => element === toolObject)))
finalToolsObject['Others'].toolsList.push(toolObject);
}
});
} else {
console.error('Script is not failing, it is just dropping errors for further investigation');
console.error('Invalid .asyncapi-tool file.');
console.error(`Located in: ${tool.html_url}`);
console.error('Validation errors:', JSON.stringify(validate.errors, null, 2));
}
}
} catch (err) {
console.error(err)
throw err;
}
} catch (err) {
console.error(err)
throw err;
}
return finalToolsObject;
} catch (err) {
throw new Error(`Error processing tool: ${err.message}`)
}
return finalToolsObject;
}

module.exports = {convertTools, createToolObject}
module.exports = { convertTools, createToolObject }
1 change: 1 addition & 0 deletions tests/build-tools.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@ describe('buildTools', () => {
expect(err.message).toMatch(/ENOENT|EACCES/);
}
});

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 file write error test case

The new test case for handling file write errors is a valuable addition. However, consider the following improvements to make it more robust and comprehensive:

  1. Add cleanup steps to remove any files that might have been created during the test.
  2. Make the error message check more specific to ensure it's catching the intended error.
  3. Verify that no files were written to the valid paths.

Here's a suggested improvement:

it('should handle file write errors', async () => {
  axios.get.mockResolvedValue({ data: mockExtractData });

  const invalidPath = '/invalid_dir/tools.json';
  const validPaths = [manualToolsPath, toolsPath, tagsPath];

  try {
    await buildTools(invalidPath, ...validPaths);
    fail('Expected an error to be thrown');
  } catch (err) {
    expect(err.code).toMatch(/ENOENT|EACCES/);
    
    // Verify no files were written to valid paths
    validPaths.forEach(path => {
      expect(fs.existsSync(path)).toBe(false);
    });
  }

  // Cleanup any files that might have been created
  [invalidPath, ...validPaths].forEach(path => {
    if (fs.existsSync(path)) {
      fs.unlinkSync(path);
    }
  });
});

This improved version:

  1. Uses err.code instead of err.message for more specific error checking.
  2. Verifies that no files were written to the valid paths.
  3. Includes cleanup steps to remove any files that might have been created.
  4. Uses fail() to ensure an error is thrown.

});
Loading
Loading