-
-
Notifications
You must be signed in to change notification settings - Fork 993
feat: add tests for tool-object script #3265
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
Changes from all commits
d1fc6c4
9b22bc1
3629568
cabfbae
340c1f7
5d4771b
7673203
e6a3505
403bb4e
9d74152
3e20593
5000a47
8efbc84
56b0fdd
859676e
f51feb3
f540e5b
ae3e19b
3e73220
f9906ae
d31a06f
03f9dc1
c866aa0
bfc9e6e
9c1ef9e
a1af4f7
9d38d41
08a8970
cb90254
d16f9a1
1164703
4540530
cf6a37d
0a83f54
740d4d6
8f2162d
c2ee924
267023e
e4be92f
f39e57c
dcb955b
b8c0558
72f1e63
fed2691
7554976
044a049
0270bf1
771c5db
2be6d42
d24c884
ab9dff9
159f150
c71e125
2ff9704
99dcd7c
820ed4d
0c05652
d6c29e8
c6ffd22
e65f608
43e23d5
d223549
7381dbe
bff29b8
4123ed4
7f7ce0d
721c049
dd54d8c
fabdd79
840d13a
2b56cb3
2550e0a
7054e56
9427070
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 }; | ||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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)
+ }))
+ );
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @vishvamsinh28 Can you please apply this suggestion?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
| }); | ||
|
|
||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.