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

Merged
merged 74 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
d1fc6c4
tests added
vishvamsinh28 Sep 28, 2024
9b22bc1
fwqfwq
vishvamsinh28 Sep 28, 2024
3629568
fefeq
vishvamsinh28 Sep 28, 2024
cabfbae
errors added
vishvamsinh28 Sep 28, 2024
340c1f7
fixture update
vishvamsinh28 Sep 28, 2024
5d4771b
fwqf
vishvamsinh28 Sep 30, 2024
7673203
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Sep 30, 2024
e6a3505
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Oct 4, 2024
403bb4e
wfwqf
vishvamsinh28 Oct 4, 2024
9d74152
fwgff
vishvamsinh28 Oct 4, 2024
3e20593
updated test for errors
vishvamsinh28 Oct 4, 2024
5000a47
updated test for errors
vishvamsinh28 Oct 4, 2024
8efbc84
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Oct 7, 2024
56b0fdd
renamed a variabel
vishvamsinh28 Oct 7, 2024
859676e
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Oct 13, 2024
f51feb3
test updated
vishvamsinh28 Oct 13, 2024
f540e5b
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Oct 14, 2024
ae3e19b
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Oct 15, 2024
3e73220
path update
vishvamsinh28 Oct 16, 2024
f9906ae
Merge branch 'toolsObjectTest' of https://github.com/vishvamsinh28/we…
vishvamsinh28 Oct 16, 2024
d31a06f
commit to rerun workflow
vishvamsinh28 Oct 16, 2024
03f9dc1
commit to rerun workflow
vishvamsinh28 Oct 16, 2024
c866aa0
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Oct 18, 2024
bfc9e6e
fefewg
vishvamsinh28 Oct 18, 2024
9c1ef9e
supress logs
vishvamsinh28 Oct 18, 2024
a1af4f7
newsroom test update
vishvamsinh28 Oct 18, 2024
9d38d41
build-tools test update
vishvamsinh28 Oct 18, 2024
08a8970
uupdate newsroom function
vishvamsinh28 Oct 18, 2024
cb90254
update build-tools function
vishvamsinh28 Oct 18, 2024
d16f9a1
commit to re run the workflow
vishvamsinh28 Oct 18, 2024
1164703
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Oct 19, 2024
4540530
added retry mechanicasm for enoent errors
vishvamsinh28 Oct 19, 2024
cf6a37d
Merge branch 'toolsObjectTest' of https://github.com/vishvamsinh28/we…
vishvamsinh28 Oct 19, 2024
0a83f54
fegwg
vishvamsinh28 Oct 19, 2024
740d4d6
fwqfqf
vishvamsinh28 Oct 19, 2024
8f2162d
feeqg
vishvamsinh28 Oct 19, 2024
c2ee924
use fs-extra
vishvamsinh28 Oct 19, 2024
267023e
commit to re run the workflow
vishvamsinh28 Oct 19, 2024
e4be92f
fqefeqf
vishvamsinh28 Oct 19, 2024
f39e57c
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Oct 20, 2024
dcb955b
test updated
vishvamsinh28 Oct 21, 2024
b8c0558
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Oct 27, 2024
72f1e63
fef
vishvamsinh28 Oct 27, 2024
fed2691
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Oct 27, 2024
7554976
add empty line at end
vishvamsinh28 Oct 27, 2024
044a049
Merge branch 'toolsObjectTest' of https://github.com/vishvamsinh28/we…
vishvamsinh28 Oct 27, 2024
0270bf1
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Nov 1, 2024
771c5db
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Nov 4, 2024
2be6d42
fef
vishvamsinh28 Nov 5, 2024
d24c884
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Nov 8, 2024
ab9dff9
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Nov 10, 2024
159f150
loop update
vishvamsinh28 Nov 10, 2024
c71e125
added helper function
vishvamsinh28 Nov 10, 2024
2ff9704
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Nov 13, 2024
99dcd7c
helper function
vishvamsinh28 Nov 13, 2024
820ed4d
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Nov 15, 2024
0c05652
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Nov 22, 2024
d6c29e8
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Nov 23, 2024
c6ffd22
script updated
vishvamsinh28 Nov 23, 2024
e65f608
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Nov 28, 2024
43e23d5
Merge branch 'toolsObjectTest' of https://github.com/vishvamsinh28/we…
vishvamsinh28 Nov 28, 2024
d223549
tests updated
vishvamsinh28 Nov 28, 2024
7381dbe
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Nov 29, 2024
bff29b8
test updated
vishvamsinh28 Nov 29, 2024
4123ed4
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Nov 29, 2024
7f7ce0d
Optimize loop
vishvamsinh28 Nov 29, 2024
721c049
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Nov 30, 2024
dd54d8c
suggested changes applied
vishvamsinh28 Nov 30, 2024
fabdd79
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Dec 2, 2024
840d13a
test updated
vishvamsinh28 Dec 2, 2024
2b56cb3
applied coderabbit suggestions
vishvamsinh28 Dec 2, 2024
2550e0a
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Dec 4, 2024
7054e56
Merge branch 'master' into toolsObjectTest
vishvamsinh28 Dec 5, 2024
9427070
Merge branch 'master' into toolsObjectTest
asyncapi-bot Dec 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 58 additions & 55 deletions scripts/tools/tools-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ addFormats(ajv, ["uri"])
const validate = ajv.compile(schema)
const { convertToJson } = require('../utils');


// Config options set for the Fuse object
const options = {
includeScore: true,
Expand All @@ -25,8 +24,8 @@ 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='') => {
let resultantObject = {
const createToolObject = async (toolFile, repositoryUrl = '', repoDescription = '', isAsyncAPIrepo = '') => {
const resultantObject = {
title: toolFile.title,
description: toolFile?.description ? toolFile.description : repoDescription,
links: {
Expand All @@ -47,67 +46,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;

// 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: []
};
}
try {
let finalToolsObject = {};
akshatnema marked this conversation as resolved.
Show resolved Hide resolved
const dataArray = data.items;
akshatnema marked this conversation as resolved.
Show resolved Hide resolved

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}`;
// initialising finalToolsObject with all categories inside it with proper elements in each category
finalToolsObject = Object.fromEntries(
categoryList.map((category) => [
category.name,
{
description: category.description,
toolsList: []
}
])
);

const { data: toolFileContent } = await axios.get(download_url);
await Promise.all(dataArray.map(async (tool) => {
try {
if (tool.name.startsWith('.asyncapi-tool')) {
const referenceId = tool.url.split('=')[1];
const downloadUrl = `https://raw.githubusercontent.com/${tool.repository.full_name}/${referenceId}/${tool.path}`;
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved

//some stuff can be YAML
const jsonToolFileContent = await convertToJson(toolFileContent)
const { data: toolFileContent } = await axios.get(downloadUrl);

//validating against JSON Schema for tools file
const isValid = await validate(jsonToolFileContent)
//some stuff can be YAML
const jsonToolFileContent = await convertToJson(toolFileContent)

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);
//validating against JSON Schema for tools file
const isValid = await validate(jsonToolFileContent)

// 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 (isValid) {
const repositoryUrl = tool.repository.html_url;
const repoDescription = tool.repository.description;
const isAsyncAPIrepo = tool.repository.owner.login === 'asyncapi';
const toolObject = await createToolObject(
jsonToolFileContent,
repositoryUrl,
repoDescription,
isAsyncAPIrepo
);

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));
// Tool Object is appended to each category array according to Fuse search for categories inside Tool Object
await Promise.all(jsonToolFileContent.filters.categories.map(async (category) => {
const categorySearch = await fuse.search(category);
const targetCategory = categorySearch.length ? categorySearch[0].item.name : 'Others';
const { toolsList } = finalToolsObject[targetCategory];
if (!toolsList.includes(toolObject)) {
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));
}
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved
}
} 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 }
79 changes: 79 additions & 0 deletions tests/helper/toolsObjectData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const createToolRepositoryData = ({
name = '.asyncapi-tool',
refId = '61855e7365a881e98c2fe667a658a0005753d873',
owner = 'asyncapi',
repoName = 'example-repo',
description = 'Example repository',
path = '.asyncapi-tool'
} = {}) => ({
name,
url: `https://api.github.com/repositories/351453552/contents/${path}?ref=${refId}`,
repository: {
full_name: `${owner}/${repoName}`,
html_url: `https://github.com/${owner}/${repoName}`,
description,
owner: { login: owner }
},
path
});
coderabbitai[bot] marked this conversation as resolved.
Show resolved Hide resolved

const createToolFileContent = ({
title = 'Example Tool',
description = 'This is an example tool.',
repoUrl = null,
categories = ['Category1'],
hasCommercial = false,
additionalLinks = {},
additionalFilters = {}
} = {}) => ({
title,
description,
links: {
repoUrl: repoUrl || `https://github.com/asyncapi/${encodeURIComponent(title.toLowerCase().replace(/\s+/g, '-'))}`,
...additionalLinks
},
filters: { categories, hasCommercial, ...additionalFilters }
});

const createExpectedToolObject = ({
title = 'Example Tool',
description = 'This is an example tool.',
repoUrl = null,
categories = ['Category1'],
hasCommercial = false,
isAsyncAPIOwner = true,
additionalLinks = {},
additionalFilters = {}
} = {}) =>
createToolFileContent({
title,
description,
repoUrl,
categories,
hasCommercial,
additionalLinks,
additionalFilters: { isAsyncAPIOwner, ...additionalFilters }
});

const createMockData = (tools = []) => ({
items: tools.map((tool) =>
typeof tool === 'string'
? createToolRepositoryData({ name: `.asyncapi-tool-${tool}`, repoName: tool })
: createToolRepositoryData(tool)
)
});

const createMalformedYAML = ({
title = 'Malformed Tool',
description = 'This tool has malformed YAML.',
repoUrl = 'https://github.com/asyncapi/malformed-repo' } = {}) => `
title: ${title}
description: ${description}
links:
repoUrl: ${repoUrl}
filters:
categories:
- Category1
`;

module.exports = { createToolFileContent, createExpectedToolObject, createMockData, createMalformedYAML };
161 changes: 161 additions & 0 deletions tests/tools/tools-object.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
const axios = require('axios');
const { convertTools, createToolObject } = require('../../scripts/tools/tools-object');
const {
createToolFileContent,
createExpectedToolObject,
createMockData,
createMalformedYAML
} = require('../helper/toolsObjectData');

jest.mock('axios');
jest.mock('../../scripts/tools/categorylist', () => ({
categoryList: [
{ name: 'Category1', tag: 'Category1', description: 'Description for Category1' },
{ name: 'Others', tag: 'Others', description: 'Other tools category' },
]
}));

describe('Tools Object', () => {
beforeEach(() => {
axios.get.mockClear();
console.error = jest.fn();
});

const mockToolData = (toolContent, toolNames = ['valid-tool']) => {
const mockData = createMockData(toolNames.map((name) => ({ name: `.asyncapi-tool-${name}`, repoName: name })));
axios.get.mockResolvedValue({ data: toolContent });
return mockData;
};

it('should create a tool object with provided parameters', async () => {
const toolFile = createToolFileContent({
title: 'Test Tool',
description: 'Test Description',
hasCommercial: true,
additionalLinks: { docsUrl: 'https://docs.example.com' }
});

const expected = createExpectedToolObject({
title: 'Test Tool',
description: 'Test Description',
hasCommercial: true,
additionalLinks: { docsUrl: 'https://docs.example.com' }
});

const result = await createToolObject(
toolFile,
expected.links.repoUrl,
'Repository Description',
true
);

expect(result).toEqual(expected);
});

it('should convert tools data correctly', async () => {
const toolContent = createToolFileContent({ title: 'Valid Tool', categories: ['Category1'] });
const mockData = mockToolData(toolContent);

const result = await convertTools(mockData);

expect(result.Category1.toolsList).toHaveLength(1);
expect(result.Category1.toolsList[0].title).toBe('Valid Tool');
});

it('should assign tool to Others category if no matching category is found', async () => {
const toolContent = createToolFileContent({ title: 'Unknown Category Tool', categories: ['UnknownCategory'] });
const mockData = mockToolData(toolContent);

const result = await convertTools(mockData);

expect(result.Others.toolsList).toHaveLength(1);
expect(result.Others.toolsList[0].title).toBe('Unknown Category Tool');
});

it('should log errors for invalid .asyncapi-tool file', async () => {
const invalidContent = createToolFileContent({
title: 'Invalid Tool',
additionalFilters: { invalidField: true }
});
const mockData = mockToolData(invalidContent);

await convertTools(mockData);

expect(console.error).toHaveBeenCalledWith(expect.stringContaining('Script is not failing'));
expect(console.error).toHaveBeenCalledWith(expect.stringContaining('Invalid .asyncapi-tool file'));
});
Comment on lines +75 to +86
Copy link

@coderabbitai coderabbitai bot Dec 2, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

Update error assertions to match new error handling.

The error assertions need to be updated to match the new error handling implementation.

     await convertTools(mockData);

-    expect(console.error).toHaveBeenCalledWith(expect.stringContaining('Script is not failing'));
-    expect(console.error).toHaveBeenCalledWith(expect.stringContaining('Invalid .asyncapi-tool file'));
+    await expect(convertTools(mockData)).rejects.toThrow('Invalid .asyncapi-tool file');
+    expect(console.error).toHaveBeenCalledWith(
+      expect.stringContaining(JSON.stringify({
+        message: 'Invalid .asyncapi-tool file',
+        location: expect.any(String),
+        errors: expect.any(Array)
+      }))
+    );

Committable suggestion skipped: line range outside the PR's diff.

Copy link
Member

Choose a reason for hiding this comment

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

@vishvamsinh28 Can you please apply this suggestion?

Copy link

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Contributor Author

@vishvamsinh28 vishvamsinh28 Dec 2, 2024

Choose a reason for hiding this comment

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

@akshatnema This will fail the test because the script logs errors in multiple lines, whereas the suggestion expects a single log entry containing all error details as a JSON string. I have tested it locally.


it('should add duplicate tool objects to the same category', async () => {
const toolContent = createToolFileContent({
title: 'Duplicate Tool',
categories: ['Category1']
});

const mockData = createMockData([
{ name: '.asyncapi-tool-dup1', repoName: 'dup1' },
{ name: '.asyncapi-tool-dup2', repoName: 'dup2' }
]);

axios.get.mockResolvedValue({ data: toolContent });

const result = await convertTools(mockData);

expect(result.Category1.toolsList).toHaveLength(2);
expect(result.Category1.toolsList[0].title).toBe('Duplicate Tool');
expect(result.Category1.toolsList[1].title).toBe('Duplicate Tool');
});

it('should add tool to Others category only once', async () => {
const toolContent = createToolFileContent({
title: 'Duplicate Tool in Others',
categories: ['UnknownCategory']
});

const mockData = mockToolData(toolContent);

const result = await convertTools(mockData);

expect(result.Others.toolsList).toHaveLength(1);
expect(result.Others.toolsList[0].title).toBe('Duplicate Tool in Others');
});

it('should throw an error if axios.get fails', async () => {
const mockData = createMockData([{
name: '.asyncapi-tool-error',
repoName: 'error-tool'
}]);

axios.get.mockRejectedValue(new Error('Network Error'));

await expect(convertTools(mockData)).rejects.toThrow('Network Error');
});

it('should handle malformed JSON in tool file', async () => {
const malformedContent = createMalformedYAML();
await expect(convertTools(malformedContent)).rejects.toThrow();
});

it('should use repository description when tool description is missing', async () => {
const toolFile = createToolFileContent({
title: 'No Description Tool',
description: '',
});

const repositoryDescription = 'Fallback Repository Description';
const mockData = createMockData([{
name: '.asyncapi-tool-no-description',
repoName: 'no-description',
description: repositoryDescription
}]);

axios.get.mockResolvedValue({ data: toolFile });

const result = await convertTools(mockData);

const toolObject = result.Category1.toolsList[0];

expect(toolObject.description).toBe(repositoryDescription);
expect(toolObject.title).toBe('No Description Tool');
});

});
Loading