From d1fc6c4d2225603e67b5d7fd9cac338544fd8bf1 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Sat, 28 Sep 2024 18:44:10 +0530 Subject: [PATCH 01/24] tests added --- tests/tools/tools-object.test.js | 203 +++++++++++++++++++++++++++++++ 1 file changed, 203 insertions(+) create mode 100644 tests/tools/tools-object.test.js diff --git a/tests/tools/tools-object.test.js b/tests/tools/tools-object.test.js new file mode 100644 index 00000000000..3c17e24c36d --- /dev/null +++ b/tests/tools/tools-object.test.js @@ -0,0 +1,203 @@ +const { convertTools, createToolObject } = require('../../scripts/tools/tools-object'); +const axios = require('axios'); +const Ajv = require('ajv'); +const addFormats = require('ajv-formats'); +const schema = require('../../scripts/tools/tools-schema.json'); + +// Mock axios +jest.mock('axios'); + +// Mock categoryList to include 'Category1' and 'Others' +jest.mock('../../scripts/tools/categorylist', () => ({ + categoryList: [ + { name: 'Category1', tag: 'Category1', description: 'Description for Category1' }, + { name: 'Others', tag: 'Others', description: 'Other tools category' }, + ] +})); + +const { categoryList } = require('../../scripts/tools/categorylist'); + +// Setup AJV +const ajv = new Ajv(); +addFormats(ajv, ["uri"]); +const validate = ajv.compile(schema); + +// Sample data +const mockData = { + items: [ + { + name: '.asyncapi-tool-example', + url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=61855e7365a881e98c2fe667a658a0005753d873', + repository: { + full_name: 'asyncapi/example-repo', + html_url: 'https://github.com/asyncapi/example-repo', + description: 'Example repository', + owner: { + login: 'asyncapi' + } + }, + path: '.asyncapi-tool' + } + ] +}; + +const mockToolFileContent = ` +title: Example Tool +description: This is an example tool. +links: + repoUrl: https://github.com/asyncapi/example-repo +filters: + categories: + - Category1 + hasCommercial: true +`; + +describe('createToolObject', () => { + it('should create a tool object with provided parameters', async () => { + const toolFile = { + title: 'Example Tool', + description: 'This is an example tool.', + links: { + repoUrl: 'https://github.com/asyncapi/example-repo' + }, + filters: { + categories: ['Category1'], + hasCommercial: true + } + }; + const repositoryUrl = 'https://github.com/asyncapi/example-repo'; + const repoDescription = 'Example repository'; + const isAsyncAPIrepo = true; + + const expectedObject = { + title: 'Example Tool', + description: 'This is an example tool.', + links: { + repoUrl: 'https://github.com/asyncapi/example-repo' + }, + filters: { + categories: ['Category1'], + hasCommercial: true, + isAsyncAPIOwner: true + } + }; + + const result = await createToolObject(toolFile, repositoryUrl, repoDescription, isAsyncAPIrepo); + expect(result).toEqual(expectedObject); + }); + + it('should use repoDescription when toolFile.description is not provided', async () => { + const toolFile = { + title: 'Example Tool', + links: { + repoUrl: 'https://github.com/asyncapi/example-repo' + }, + filters: { + categories: ['Category1'] + } + }; + const repositoryUrl = 'https://github.com/asyncapi/example-repo'; + const repoDescription = 'Example repository'; + const isAsyncAPIrepo = true; + + const expectedObject = { + title: 'Example Tool', + description: 'Example repository', + links: { + repoUrl: 'https://github.com/asyncapi/example-repo' + }, + filters: { + categories: ['Category1'], + hasCommercial: false, + isAsyncAPIOwner: true + } + }; + + const result = await createToolObject(toolFile, repositoryUrl, repoDescription, isAsyncAPIrepo); + expect(result).toEqual(expectedObject); + }); +}); + +describe('convertTools', () => { + beforeEach(() => { + axios.get.mockClear(); + console.error = jest.fn(); + }); + + it('should convert tools data correctly', async () => { + axios.get.mockResolvedValue({ data: mockToolFileContent }); + + const result = await convertTools(mockData); + + const expectedObject = { + Category1: { + description: 'Description for Category1', + toolsList: [ + { + title: 'Example Tool', + description: 'This is an example tool.', + links: { + repoUrl: 'https://github.com/asyncapi/example-repo' + }, + filters: { + categories: ['Category1'], + hasCommercial: true, + isAsyncAPIOwner: true + } + } + ] + }, + Others: { + description: 'Other tools category', + toolsList: [] + } + }; + + expect(result).toEqual(expect.objectContaining(expectedObject)); + expect(axios.get).toHaveBeenCalledTimes(1); + }); + + it('should assign tool to Others category if no matching category is found', async () => { + const dataWithUnknownCategory = { + items: [ + { + name: '.asyncapi-tool-unknown', + url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=61855e7365a881e98c2fe667a658a0005753d873', + repository: { + full_name: 'asyncapi/unknown-repo', + html_url: 'https://github.com/asyncapi/unknown-repo', + description: 'Unknown repository', + owner: { + login: 'asyncapi' + } + }, + path: '.asyncapi-tool' + } + ] + }; + + const toolFileContent = ` +title: Unknown Tool +description: This tool has an unknown category. +links: + repoUrl: https://github.com/asyncapi/unknown-repo +filters: + categories: + - UnknownCategory +`; + + axios.get.mockResolvedValue({ data: toolFileContent }); + + const result = await convertTools(dataWithUnknownCategory); + + expect(result.Others.toolsList).toHaveLength(1); + expect(result.Others.toolsList[0].title).toBe('Unknown Tool'); + }); + + it('should throw an error if axios.get fails', async () => { + axios.get.mockRejectedValue(new Error('Network Error')); + + await expect(convertTools(mockData)).rejects.toThrow('Network Error'); + }); +}); + From 9b22bc1450d105c950ee17e8f3e4365ff194f19b Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Sat, 28 Sep 2024 19:34:05 +0530 Subject: [PATCH 02/24] fwqfwq --- tests/tools/tools-object.test.js | 179 +++++++++++++++++++++++++++---- 1 file changed, 158 insertions(+), 21 deletions(-) diff --git a/tests/tools/tools-object.test.js b/tests/tools/tools-object.test.js index 3c17e24c36d..763393587ab 100644 --- a/tests/tools/tools-object.test.js +++ b/tests/tools/tools-object.test.js @@ -1,13 +1,8 @@ const { convertTools, createToolObject } = require('../../scripts/tools/tools-object'); const axios = require('axios'); -const Ajv = require('ajv'); -const addFormats = require('ajv-formats'); -const schema = require('../../scripts/tools/tools-schema.json'); -// Mock axios jest.mock('axios'); -// Mock categoryList to include 'Category1' and 'Others' jest.mock('../../scripts/tools/categorylist', () => ({ categoryList: [ { name: 'Category1', tag: 'Category1', description: 'Description for Category1' }, @@ -15,14 +10,6 @@ jest.mock('../../scripts/tools/categorylist', () => ({ ] })); -const { categoryList } = require('../../scripts/tools/categorylist'); - -// Setup AJV -const ajv = new Ajv(); -addFormats(ajv, ["uri"]); -const validate = ajv.compile(schema); - -// Sample data const mockData = { items: [ { @@ -52,7 +39,11 @@ filters: hasCommercial: true `; -describe('createToolObject', () => { +describe('Tools Object', () => { + beforeEach(() => { + axios.get.mockClear(); + console.error = jest.fn(); + }); it('should create a tool object with provided parameters', async () => { const toolFile = { title: 'Example Tool', @@ -116,13 +107,6 @@ describe('createToolObject', () => { const result = await createToolObject(toolFile, repositoryUrl, repoDescription, isAsyncAPIrepo); expect(result).toEqual(expectedObject); }); -}); - -describe('convertTools', () => { - beforeEach(() => { - axios.get.mockClear(); - console.error = jest.fn(); - }); it('should convert tools data correctly', async () => { axios.get.mockResolvedValue({ data: mockToolFileContent }); @@ -199,5 +183,158 @@ filters: await expect(convertTools(mockData)).rejects.toThrow('Network Error'); }); + + it('should log errors for invalid .asyncapi-tool file', async () => { + const invalidToolFileContent = ` + title: Invalid Tool + description: This tool has invalid schema. + links: + repoUrl: https://github.com/asyncapi/invalid-repo + filters: + categories: + - Category1 + invalidField: true + `; + + const invalidToolData = { + items: [ + { + name: '.asyncapi-tool-invalid', + url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=invalidref', + repository: { + full_name: 'asyncapi/invalid-repo', + html_url: 'https://github.com/asyncapi/invalid-repo', + description: 'Invalid repository', + owner: { + login: 'asyncapi' + } + }, + path: '.asyncapi-tool' + } + ] + }; + + axios.get.mockResolvedValue({ data: invalidToolFileContent }); + + await convertTools(invalidToolData); + + console.log('All console.error calls:', console.error.mock.calls); + + const allErrorMessages = console.error.mock.calls.flat(); + expect(allErrorMessages).toEqual( + expect.arrayContaining([ + expect.stringContaining('Script is not failing, it is just dropping errors for further investigation'), + expect.stringContaining('Invalid .asyncapi-tool file'), + expect.stringContaining('Located in:'), + expect.stringContaining('Validation errors:') + ]) + ); + + }); + + it('should add duplicate tool objects to the same category', async () => { + const duplicateToolData = { + items: [ + { + name: '.asyncapi-tool-duplicate', + url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=duplicate1', + repository: { + full_name: 'asyncapi/duplicate-repo', + html_url: 'https://github.com/asyncapi/duplicate-repo', + description: 'Duplicate repository', + owner: { + login: 'asyncapi' + } + }, + path: '.asyncapi-tool' + }, + { + name: '.asyncapi-tool-duplicate', + url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=duplicate2', + repository: { + full_name: 'asyncapi/duplicate-repo', + html_url: 'https://github.com/asyncapi/duplicate-repo', + description: 'Duplicate repository', + owner: { + login: 'asyncapi' + } + }, + path: '.asyncapi-tool' + } + ] + }; + + const duplicateToolFileContent = ` + title: Duplicate Tool + description: This is a duplicate tool. + links: + repoUrl: https://github.com/asyncapi/duplicate-repo + filters: + categories: + - Category1 + `; + + axios.get.mockResolvedValue({ data: duplicateToolFileContent }); + + const result = await convertTools(duplicateToolData); + + 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 dataWithUnknownCategory = { + items: [ + { + name: '.asyncapi-tool-unknown', + url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=unknown1', + repository: { + full_name: 'asyncapi/unknown-repo', + html_url: 'https://github.com/asyncapi/unknown-repo', + description: 'Unknown repository', + owner: { + login: 'asyncapi' + } + }, + path: '.asyncapi-tool' + }, + { + name: '.asyncapi-tool-unknown', + url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=unknown2', + repository: { + full_name: 'asyncapi/unknown-repo', + html_url: 'https://github.com/asyncapi/unknown-repo', + description: 'Unknown repository', + owner: { + login: 'asyncapi' + } + }, + path: '.asyncapi-tool' + } + ] + }; + + const unknownToolFileContent = ` + title: Unknown Tool + description: This tool has an unknown category. + links: + repoUrl: https://github.com/asyncapi/unknown-repo + filters: + categories: + - UnknownCategory + `; + + axios.get.mockResolvedValue({ data: unknownToolFileContent }); + + const result = await convertTools(dataWithUnknownCategory); + + const uniqueTools = result.Others.toolsList.filter((tool, index, self) => + index === self.findIndex((t) => t.title === tool.title) + ); + + expect(uniqueTools).toHaveLength(1); + expect(uniqueTools[0].title).toBe('Unknown Tool'); + }); }); From 3629568c5dc74b27967f8a2848e0ef49a3c8e96a Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Sat, 28 Sep 2024 19:34:30 +0530 Subject: [PATCH 03/24] fefeq --- tests/tools/tools-object.test.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/tests/tools/tools-object.test.js b/tests/tools/tools-object.test.js index 763393587ab..86a44569574 100644 --- a/tests/tools/tools-object.test.js +++ b/tests/tools/tools-object.test.js @@ -40,10 +40,12 @@ filters: `; describe('Tools Object', () => { + beforeEach(() => { axios.get.mockClear(); console.error = jest.fn(); }); + it('should create a tool object with provided parameters', async () => { const toolFile = { title: 'Example Tool', @@ -195,7 +197,7 @@ filters: - Category1 invalidField: true `; - + const invalidToolData = { items: [ { @@ -213,13 +215,13 @@ filters: } ] }; - + axios.get.mockResolvedValue({ data: invalidToolFileContent }); - + await convertTools(invalidToolData); - + console.log('All console.error calls:', console.error.mock.calls); - + const allErrorMessages = console.error.mock.calls.flat(); expect(allErrorMessages).toEqual( expect.arrayContaining([ @@ -314,7 +316,7 @@ filters: } ] }; - + const unknownToolFileContent = ` title: Unknown Tool description: This tool has an unknown category. @@ -324,15 +326,15 @@ filters: categories: - UnknownCategory `; - + axios.get.mockResolvedValue({ data: unknownToolFileContent }); - + const result = await convertTools(dataWithUnknownCategory); - + const uniqueTools = result.Others.toolsList.filter((tool, index, self) => index === self.findIndex((t) => t.title === tool.title) ); - + expect(uniqueTools).toHaveLength(1); expect(uniqueTools[0].title).toBe('Unknown Tool'); }); From cabfbae3e17cff0ad37e0c4d45a63dbd4b9c2660 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Sat, 28 Sep 2024 22:19:42 +0530 Subject: [PATCH 04/24] errors added --- scripts/tools/tools-object.js | 106 ++++++++++++++++--------------- tests/tools/tools-object.test.js | 91 ++++++++++++++++++++++++-- 2 files changed, 139 insertions(+), 58 deletions(-) diff --git a/scripts/tools/tools-object.js b/scripts/tools/tools-object.js index e5a30334f7d..b89d72b3f56 100644 --- a/scripts/tools/tools-object.js +++ b/scripts/tools/tools-object.js @@ -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, @@ -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} \ No newline at end of file +module.exports = { convertTools, createToolObject } \ No newline at end of file diff --git a/tests/tools/tools-object.test.js b/tests/tools/tools-object.test.js index 86a44569574..639dbc3830d 100644 --- a/tests/tools/tools-object.test.js +++ b/tests/tools/tools-object.test.js @@ -40,7 +40,7 @@ filters: `; describe('Tools Object', () => { - + beforeEach(() => { axios.get.mockClear(); console.error = jest.fn(); @@ -180,12 +180,6 @@ filters: expect(result.Others.toolsList[0].title).toBe('Unknown Tool'); }); - it('should throw an error if axios.get fails', async () => { - axios.get.mockRejectedValue(new Error('Network Error')); - - await expect(convertTools(mockData)).rejects.toThrow('Network Error'); - }); - it('should log errors for invalid .asyncapi-tool file', async () => { const invalidToolFileContent = ` title: Invalid Tool @@ -338,5 +332,88 @@ filters: expect(uniqueTools).toHaveLength(1); expect(uniqueTools[0].title).toBe('Unknown Tool'); }); + + it('should throw an error if axios.get fails', async () => { + axios.get.mockRejectedValue(new Error('Network Error')); + + try { + await convertTools(mockData) + } catch (err) { + expect(err.message).toContain("Network Error") + } + }); + + it('should throw an error if JSON schema validation fails', async () => { + const invalidToolFileContent = ` + title: Invalid Tool + description: This tool has invalid schema. + links: + repoUrl: https://github.com/asyncapi/invalid-repo + filters: + categories: + - Category1 + invalidField: true + `; + + axios.get.mockResolvedValue({ data: invalidToolFileContent }); + + try { + await convertTools(mockData); + } catch (err) { + expect(err).toBeInstanceOf(Error); + expect(err.message).toContain('Invalid .asyncapi-tool file'); + } + }); + + it('should throw an error if toolFile cannot be converted to JSON', async () => { + const invalidJsonContent = ` + title: Invalid Tool + description: This is an invalid JSON + links: + repoUrl: https://github.com/asyncapi/invalid-repo + `; + + axios.get.mockResolvedValue({ data: invalidJsonContent }); + + jest.doMock('../../scripts/utils', () => ({ + convertToJson: jest.fn(() => { + throw new Error('Invalid JSON format'); + }) + })); + + try { + await convertTools(mockData); + } catch (err) { + expect(err).toBeInstanceOf(Error); + expect(err.message).toBe('Error processing tool: Invalid JSON format'); + } + }); + + it('should throw an error if a required tool property is missing', async () => { + const missingToolPropertyContent = ` + title: Missing Property Tool + description: This tool is missing required properties + links: + repoUrl: https://github.com/asyncapi/missing-property + `; + + axios.get.mockResolvedValue({ data: missingToolPropertyContent }); + + jest.doMock('../../scripts/utils', () => ({ + convertToJson: jest.fn(() => { + return { + title: 'Missing Property Tool', + }; + }) + })); + + try { + await convertTools(mockData); + } catch (err) { + expect(err).toBeInstanceOf(Error); + expect(err.message).toContain('Missing required tool properties'); + } + }); + }); From 340c1f76a7ef3ea223c2b37972b1f06db369abc8 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Sat, 28 Sep 2024 23:02:08 +0530 Subject: [PATCH 05/24] fixture update --- tests/fixtures/toolsObjectData.js | 292 ++++++++++++++++++++++++++++ tests/tools/tools-object.test.js | 303 ++++-------------------------- 2 files changed, 324 insertions(+), 271 deletions(-) create mode 100644 tests/fixtures/toolsObjectData.js diff --git a/tests/fixtures/toolsObjectData.js b/tests/fixtures/toolsObjectData.js new file mode 100644 index 00000000000..96b425f523e --- /dev/null +++ b/tests/fixtures/toolsObjectData.js @@ -0,0 +1,292 @@ +const mockData = { + items: [ + { + name: '.asyncapi-tool-example', + url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=61855e7365a881e98c2fe667a658a0005753d873', + repository: { + full_name: 'asyncapi/example-repo', + html_url: 'https://github.com/asyncapi/example-repo', + description: 'Example repository', + owner: { + login: 'asyncapi' + } + }, + path: '.asyncapi-tool' + } + ] +}; + +const mockToolFileContent = ` + title: Example Tool + description: This is an example tool. + links: + repoUrl: https://github.com/asyncapi/example-repo + filters: + categories: + - Category1 + hasCommercial: true + `; + +const toolFileT1 = { + title: 'Example Tool', + description: 'This is an example tool.', + links: { + repoUrl: 'https://github.com/asyncapi/example-repo' + }, + filters: { + categories: ['Category1'], + hasCommercial: true + } +}; + +const expectedObjectT1 = { + title: 'Example Tool', + description: 'This is an example tool.', + links: { + repoUrl: 'https://github.com/asyncapi/example-repo' + }, + filters: { + categories: ['Category1'], + hasCommercial: true, + isAsyncAPIOwner: true + } +}; + +const repositoryUrl = 'https://github.com/asyncapi/example-repo'; +const repoDescription = 'Example repository'; +const isAsyncAPIrepo = true; + +const toolFileT2 = { + title: 'Example Tool', + links: { + repoUrl: 'https://github.com/asyncapi/example-repo' + }, + filters: { + categories: ['Category1'] + } +}; + +const expectedObjectT2 = { + title: 'Example Tool', + description: 'Example repository', + links: { + repoUrl: 'https://github.com/asyncapi/example-repo' + }, + filters: { + categories: ['Category1'], + hasCommercial: false, + isAsyncAPIOwner: true + } +}; + +const expectedObjectT3 = { + Category1: { + description: 'Description for Category1', + toolsList: [ + { + title: 'Example Tool', + description: 'This is an example tool.', + links: { + repoUrl: 'https://github.com/asyncapi/example-repo' + }, + filters: { + categories: ['Category1'], + hasCommercial: true, + isAsyncAPIOwner: true + } + } + ] + }, + Others: { + description: 'Other tools category', + toolsList: [] + } +}; + +const dataWithUnknownCategory = { + items: [ + { + name: '.asyncapi-tool-unknown', + url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=61855e7365a881e98c2fe667a658a0005753d873', + repository: { + full_name: 'asyncapi/unknown-repo', + html_url: 'https://github.com/asyncapi/unknown-repo', + description: 'Unknown repository', + owner: { + login: 'asyncapi' + } + }, + path: '.asyncapi-tool' + } + ] +}; + +const toolFileContent = ` + title: Unknown Tool + description: This tool has an unknown category. + links: + repoUrl: https://github.com/asyncapi/unknown-repo + filters: + categories: + - UnknownCategory + `; + +const invalidToolFileContent = ` + title: Invalid Tool + description: This tool has invalid schema. + links: + repoUrl: https://github.com/asyncapi/invalid-repo + filters: + categories: + - Category1 + invalidField: true + `; + +const invalidToolData = { + items: [ + { + name: '.asyncapi-tool-invalid', + url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=invalidref', + repository: { + full_name: 'asyncapi/invalid-repo', + html_url: 'https://github.com/asyncapi/invalid-repo', + description: 'Invalid repository', + owner: { + login: 'asyncapi' + } + }, + path: '.asyncapi-tool' + } + ] +}; + +const duplicateToolData = { + items: [ + { + name: '.asyncapi-tool-duplicate', + url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=duplicate1', + repository: { + full_name: 'asyncapi/duplicate-repo', + html_url: 'https://github.com/asyncapi/duplicate-repo', + description: 'Duplicate repository', + owner: { + login: 'asyncapi' + } + }, + path: '.asyncapi-tool' + }, + { + name: '.asyncapi-tool-duplicate', + url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=duplicate2', + repository: { + full_name: 'asyncapi/duplicate-repo', + html_url: 'https://github.com/asyncapi/duplicate-repo', + description: 'Duplicate repository', + owner: { + login: 'asyncapi' + } + }, + path: '.asyncapi-tool' + } + ] +}; + +const duplicateToolFileContent = ` + title: Duplicate Tool + description: This is a duplicate tool. + links: + repoUrl: https://github.com/asyncapi/duplicate-repo + filters: + categories: + - Category1 + `; + +const dataWithUnknownCategoryOnce = { + items: [ + { + name: '.asyncapi-tool-unknown', + url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=unknown1', + repository: { + full_name: 'asyncapi/unknown-repo', + html_url: 'https://github.com/asyncapi/unknown-repo', + description: 'Unknown repository', + owner: { + login: 'asyncapi' + } + }, + path: '.asyncapi-tool' + }, + { + name: '.asyncapi-tool-unknown', + url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=unknown2', + repository: { + full_name: 'asyncapi/unknown-repo', + html_url: 'https://github.com/asyncapi/unknown-repo', + description: 'Unknown repository', + owner: { + login: 'asyncapi' + } + }, + path: '.asyncapi-tool' + } + ] +}; + +const unknownToolFileContent = ` + title: Unknown Tool + description: This tool has an unknown category. + links: + repoUrl: https://github.com/asyncapi/unknown-repo + filters: + categories: + - UnknownCategory + `; + +const invalidToolFileContentJSON = ` + title: Invalid Tool + description: This tool has invalid schema. + links: + repoUrl: https://github.com/asyncapi/invalid-repo + filters: + categories: + - Category1 + invalidField: true + `; + +const invalidJsonContent = ` + title: Invalid Tool + description: This is an invalid JSON + links: + repoUrl: https://github.com/asyncapi/invalid-repo +`; + +const missingToolPropertyContent = ` +title: Missing Property Tool +description: This tool is missing required properties +links: + repoUrl: https://github.com/asyncapi/missing-property +`; + +module.exports = { + mockData, + mockToolFileContent, + toolFileT1, + expectedObjectT1, + repoDescription, + repositoryUrl, + isAsyncAPIrepo, + toolFileT2, + expectedObjectT2, + expectedObjectT3, + dataWithUnknownCategory, + toolFileContent, + invalidToolFileContent, + invalidToolData, + duplicateToolData, + duplicateToolFileContent, + dataWithUnknownCategoryOnce, + unknownToolFileContent, + invalidToolFileContentJSON, + invalidJsonContent, + missingToolPropertyContent +} \ No newline at end of file diff --git a/tests/tools/tools-object.test.js b/tests/tools/tools-object.test.js index 639dbc3830d..2f2450ffeae 100644 --- a/tests/tools/tools-object.test.js +++ b/tests/tools/tools-object.test.js @@ -1,6 +1,29 @@ const { convertTools, createToolObject } = require('../../scripts/tools/tools-object'); const axios = require('axios'); +const { mockData, + mockToolFileContent, + toolFileT1, + expectedObjectT1, + repoDescription, + repositoryUrl, + isAsyncAPIrepo, + toolFileT2, + expectedObjectT2, + expectedObjectT3, + dataWithUnknownCategory, + toolFileContent, + invalidToolFileContent, + invalidToolData, + duplicateToolData, + duplicateToolFileContent, + dataWithUnknownCategoryOnce, + unknownToolFileContent, + invalidToolFileContentJSON, + invalidJsonContent, + missingToolPropertyContent +} = require("../fixtures/toolsObjectData") + jest.mock('axios'); jest.mock('../../scripts/tools/categorylist', () => ({ @@ -10,35 +33,6 @@ jest.mock('../../scripts/tools/categorylist', () => ({ ] })); -const mockData = { - items: [ - { - name: '.asyncapi-tool-example', - url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=61855e7365a881e98c2fe667a658a0005753d873', - repository: { - full_name: 'asyncapi/example-repo', - html_url: 'https://github.com/asyncapi/example-repo', - description: 'Example repository', - owner: { - login: 'asyncapi' - } - }, - path: '.asyncapi-tool' - } - ] -}; - -const mockToolFileContent = ` -title: Example Tool -description: This is an example tool. -links: - repoUrl: https://github.com/asyncapi/example-repo -filters: - categories: - - Category1 - hasCommercial: true -`; - describe('Tools Object', () => { beforeEach(() => { @@ -47,67 +41,15 @@ describe('Tools Object', () => { }); it('should create a tool object with provided parameters', async () => { - const toolFile = { - title: 'Example Tool', - description: 'This is an example tool.', - links: { - repoUrl: 'https://github.com/asyncapi/example-repo' - }, - filters: { - categories: ['Category1'], - hasCommercial: true - } - }; - const repositoryUrl = 'https://github.com/asyncapi/example-repo'; - const repoDescription = 'Example repository'; - const isAsyncAPIrepo = true; - - const expectedObject = { - title: 'Example Tool', - description: 'This is an example tool.', - links: { - repoUrl: 'https://github.com/asyncapi/example-repo' - }, - filters: { - categories: ['Category1'], - hasCommercial: true, - isAsyncAPIOwner: true - } - }; - - const result = await createToolObject(toolFile, repositoryUrl, repoDescription, isAsyncAPIrepo); - expect(result).toEqual(expectedObject); + + const result = await createToolObject(toolFileT1, repositoryUrl, repoDescription, isAsyncAPIrepo); + expect(result).toEqual(expectedObjectT1); }); it('should use repoDescription when toolFile.description is not provided', async () => { - const toolFile = { - title: 'Example Tool', - links: { - repoUrl: 'https://github.com/asyncapi/example-repo' - }, - filters: { - categories: ['Category1'] - } - }; - const repositoryUrl = 'https://github.com/asyncapi/example-repo'; - const repoDescription = 'Example repository'; - const isAsyncAPIrepo = true; - - const expectedObject = { - title: 'Example Tool', - description: 'Example repository', - links: { - repoUrl: 'https://github.com/asyncapi/example-repo' - }, - filters: { - categories: ['Category1'], - hasCommercial: false, - isAsyncAPIOwner: true - } - }; - - const result = await createToolObject(toolFile, repositoryUrl, repoDescription, isAsyncAPIrepo); - expect(result).toEqual(expectedObject); + + const result = await createToolObject(toolFileT2, repositoryUrl, repoDescription, isAsyncAPIrepo); + expect(result).toEqual(expectedObjectT2); }); it('should convert tools data correctly', async () => { @@ -115,62 +57,11 @@ describe('Tools Object', () => { const result = await convertTools(mockData); - const expectedObject = { - Category1: { - description: 'Description for Category1', - toolsList: [ - { - title: 'Example Tool', - description: 'This is an example tool.', - links: { - repoUrl: 'https://github.com/asyncapi/example-repo' - }, - filters: { - categories: ['Category1'], - hasCommercial: true, - isAsyncAPIOwner: true - } - } - ] - }, - Others: { - description: 'Other tools category', - toolsList: [] - } - }; - - expect(result).toEqual(expect.objectContaining(expectedObject)); + expect(result).toEqual(expect.objectContaining(expectedObjectT3)); expect(axios.get).toHaveBeenCalledTimes(1); }); it('should assign tool to Others category if no matching category is found', async () => { - const dataWithUnknownCategory = { - items: [ - { - name: '.asyncapi-tool-unknown', - url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=61855e7365a881e98c2fe667a658a0005753d873', - repository: { - full_name: 'asyncapi/unknown-repo', - html_url: 'https://github.com/asyncapi/unknown-repo', - description: 'Unknown repository', - owner: { - login: 'asyncapi' - } - }, - path: '.asyncapi-tool' - } - ] - }; - - const toolFileContent = ` -title: Unknown Tool -description: This tool has an unknown category. -links: - repoUrl: https://github.com/asyncapi/unknown-repo -filters: - categories: - - UnknownCategory -`; axios.get.mockResolvedValue({ data: toolFileContent }); @@ -181,34 +72,6 @@ filters: }); it('should log errors for invalid .asyncapi-tool file', async () => { - const invalidToolFileContent = ` - title: Invalid Tool - description: This tool has invalid schema. - links: - repoUrl: https://github.com/asyncapi/invalid-repo - filters: - categories: - - Category1 - invalidField: true - `; - - const invalidToolData = { - items: [ - { - name: '.asyncapi-tool-invalid', - url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=invalidref', - repository: { - full_name: 'asyncapi/invalid-repo', - html_url: 'https://github.com/asyncapi/invalid-repo', - description: 'Invalid repository', - owner: { - login: 'asyncapi' - } - }, - path: '.asyncapi-tool' - } - ] - }; axios.get.mockResolvedValue({ data: invalidToolFileContent }); @@ -229,46 +92,6 @@ filters: }); it('should add duplicate tool objects to the same category', async () => { - const duplicateToolData = { - items: [ - { - name: '.asyncapi-tool-duplicate', - url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=duplicate1', - repository: { - full_name: 'asyncapi/duplicate-repo', - html_url: 'https://github.com/asyncapi/duplicate-repo', - description: 'Duplicate repository', - owner: { - login: 'asyncapi' - } - }, - path: '.asyncapi-tool' - }, - { - name: '.asyncapi-tool-duplicate', - url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=duplicate2', - repository: { - full_name: 'asyncapi/duplicate-repo', - html_url: 'https://github.com/asyncapi/duplicate-repo', - description: 'Duplicate repository', - owner: { - login: 'asyncapi' - } - }, - path: '.asyncapi-tool' - } - ] - }; - - const duplicateToolFileContent = ` - title: Duplicate Tool - description: This is a duplicate tool. - links: - repoUrl: https://github.com/asyncapi/duplicate-repo - filters: - categories: - - Category1 - `; axios.get.mockResolvedValue({ data: duplicateToolFileContent }); @@ -280,50 +103,10 @@ filters: }); it('should add tool to Others category only once', async () => { - const dataWithUnknownCategory = { - items: [ - { - name: '.asyncapi-tool-unknown', - url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=unknown1', - repository: { - full_name: 'asyncapi/unknown-repo', - html_url: 'https://github.com/asyncapi/unknown-repo', - description: 'Unknown repository', - owner: { - login: 'asyncapi' - } - }, - path: '.asyncapi-tool' - }, - { - name: '.asyncapi-tool-unknown', - url: 'https://api.github.com/repositories/351453552/contents/.asyncapi-tool?ref=unknown2', - repository: { - full_name: 'asyncapi/unknown-repo', - html_url: 'https://github.com/asyncapi/unknown-repo', - description: 'Unknown repository', - owner: { - login: 'asyncapi' - } - }, - path: '.asyncapi-tool' - } - ] - }; - - const unknownToolFileContent = ` - title: Unknown Tool - description: This tool has an unknown category. - links: - repoUrl: https://github.com/asyncapi/unknown-repo - filters: - categories: - - UnknownCategory - `; axios.get.mockResolvedValue({ data: unknownToolFileContent }); - const result = await convertTools(dataWithUnknownCategory); + const result = await convertTools(dataWithUnknownCategoryOnce); const uniqueTools = result.Others.toolsList.filter((tool, index, self) => index === self.findIndex((t) => t.title === tool.title) @@ -344,18 +127,8 @@ filters: }); it('should throw an error if JSON schema validation fails', async () => { - const invalidToolFileContent = ` - title: Invalid Tool - description: This tool has invalid schema. - links: - repoUrl: https://github.com/asyncapi/invalid-repo - filters: - categories: - - Category1 - invalidField: true - `; - axios.get.mockResolvedValue({ data: invalidToolFileContent }); + axios.get.mockResolvedValue({ data: invalidToolFileContentJSON }); try { await convertTools(mockData); @@ -366,12 +139,6 @@ filters: }); it('should throw an error if toolFile cannot be converted to JSON', async () => { - const invalidJsonContent = ` - title: Invalid Tool - description: This is an invalid JSON - links: - repoUrl: https://github.com/asyncapi/invalid-repo - `; axios.get.mockResolvedValue({ data: invalidJsonContent }); @@ -390,12 +157,6 @@ filters: }); it('should throw an error if a required tool property is missing', async () => { - const missingToolPropertyContent = ` - title: Missing Property Tool - description: This tool is missing required properties - links: - repoUrl: https://github.com/asyncapi/missing-property - `; axios.get.mockResolvedValue({ data: missingToolPropertyContent }); From 5d4771bd30d46aaf0645cc5ec946cc9dcdac41d6 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Mon, 30 Sep 2024 13:46:51 +0530 Subject: [PATCH 06/24] fwqf --- tests/tools/tools-object.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/tools/tools-object.test.js b/tests/tools/tools-object.test.js index 2f2450ffeae..3b9c5383b5b 100644 --- a/tests/tools/tools-object.test.js +++ b/tests/tools/tools-object.test.js @@ -77,8 +77,6 @@ describe('Tools Object', () => { await convertTools(invalidToolData); - console.log('All console.error calls:', console.error.mock.calls); - const allErrorMessages = console.error.mock.calls.flat(); expect(allErrorMessages).toEqual( expect.arrayContaining([ From 403bb4e42ce59b2c9ead36ddab60fdc11c3d9601 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Fri, 4 Oct 2024 11:13:22 +0530 Subject: [PATCH 07/24] wfwqf --- tests/build-tools.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/build-tools.test.js b/tests/build-tools.test.js index 2bc4592e8e1..e86dd50d77f 100644 --- a/tests/build-tools.test.js +++ b/tests/build-tools.test.js @@ -86,4 +86,5 @@ describe('buildTools', () => { expect(err.message).toMatch(/ENOENT|EACCES/); } }); + }); From 9d7415287dc7691ad7b0b0d4f67b5ad984b0bd10 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Fri, 4 Oct 2024 11:19:55 +0530 Subject: [PATCH 08/24] fwgff --- tests/tools/tools-object.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/tools/tools-object.test.js b/tests/tools/tools-object.test.js index 3b9c5383b5b..81bcf56fdb1 100644 --- a/tests/tools/tools-object.test.js +++ b/tests/tools/tools-object.test.js @@ -175,4 +175,3 @@ describe('Tools Object', () => { }); }); - From 3e2059358500496b4b3353be6e001e64243757d6 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Fri, 4 Oct 2024 12:12:12 +0530 Subject: [PATCH 09/24] updated test for errors --- tests/fixtures/toolsObjectData.js | 30 ++++---------- tests/tools/tools-object.test.js | 66 +++++++++---------------------- 2 files changed, 26 insertions(+), 70 deletions(-) diff --git a/tests/fixtures/toolsObjectData.js b/tests/fixtures/toolsObjectData.js index 96b425f523e..3d478712d79 100644 --- a/tests/fixtures/toolsObjectData.js +++ b/tests/fixtures/toolsObjectData.js @@ -242,31 +242,17 @@ const unknownToolFileContent = ` - UnknownCategory `; -const invalidToolFileContentJSON = ` - title: Invalid Tool - description: This tool has invalid schema. +// New Fixtures for Error Handling Tests +const toolFileMalformedJSON = ` + title: Malformed Tool + description: This tool has malformed JSON. links: - repoUrl: https://github.com/asyncapi/invalid-repo - filters: + repoUrl: https://github.com/asyncapi/malformed-repo + filters categories: - Category1 - invalidField: true `; -const invalidJsonContent = ` - title: Invalid Tool - description: This is an invalid JSON - links: - repoUrl: https://github.com/asyncapi/invalid-repo -`; - -const missingToolPropertyContent = ` -title: Missing Property Tool -description: This tool is missing required properties -links: - repoUrl: https://github.com/asyncapi/missing-property -`; - module.exports = { mockData, mockToolFileContent, @@ -286,7 +272,5 @@ module.exports = { duplicateToolFileContent, dataWithUnknownCategoryOnce, unknownToolFileContent, - invalidToolFileContentJSON, - invalidJsonContent, - missingToolPropertyContent + toolFileMalformedJSON, } \ No newline at end of file diff --git a/tests/tools/tools-object.test.js b/tests/tools/tools-object.test.js index 81bcf56fdb1..d88ba4092d1 100644 --- a/tests/tools/tools-object.test.js +++ b/tests/tools/tools-object.test.js @@ -19,9 +19,7 @@ const { mockData, duplicateToolFileContent, dataWithUnknownCategoryOnce, unknownToolFileContent, - invalidToolFileContentJSON, - invalidJsonContent, - missingToolPropertyContent + toolFileMalformedJSON, } = require("../fixtures/toolsObjectData") jest.mock('axios'); @@ -75,7 +73,14 @@ describe('Tools Object', () => { axios.get.mockResolvedValue({ data: invalidToolFileContent }); - await convertTools(invalidToolData); + let error; + try { + await convertTools(invalidToolData); + } catch (err) { + error = err; + } + + expect(error).toBeUndefined(); const allErrorMessages = console.error.mock.calls.flat(); expect(allErrorMessages).toEqual( @@ -115,63 +120,30 @@ describe('Tools Object', () => { }); it('should throw an error if axios.get fails', async () => { + let error; axios.get.mockRejectedValue(new Error('Network Error')); try { await convertTools(mockData) } catch (err) { + error = err; expect(err.message).toContain("Network Error") } + expect(error).toBeDefined(); }); - it('should throw an error if JSON schema validation fails', async () => { - - axios.get.mockResolvedValue({ data: invalidToolFileContentJSON }); - - try { - await convertTools(mockData); - } catch (err) { - expect(err).toBeInstanceOf(Error); - expect(err.message).toContain('Invalid .asyncapi-tool file'); - } - }); - - it('should throw an error if toolFile cannot be converted to JSON', async () => { - - axios.get.mockResolvedValue({ data: invalidJsonContent }); - - jest.doMock('../../scripts/utils', () => ({ - convertToJson: jest.fn(() => { - throw new Error('Invalid JSON format'); - }) - })); + it('should handle malformed JSON in tool file', async () => { + axios.get.mockResolvedValue({ data: toolFileMalformedJSON }); + let error; try { await convertTools(mockData); } catch (err) { - expect(err).toBeInstanceOf(Error); - expect(err.message).toBe('Error processing tool: Invalid JSON format'); + error = err; + expect(err.message).toContain('Unexpected token'); } - }); - - it('should throw an error if a required tool property is missing', async () => { - - axios.get.mockResolvedValue({ data: missingToolPropertyContent }); - jest.doMock('../../scripts/utils', () => ({ - convertToJson: jest.fn(() => { - return { - title: 'Missing Property Tool', - }; - }) - })); - - try { - await convertTools(mockData); - } catch (err) { - expect(err).toBeInstanceOf(Error); - expect(err.message).toContain('Missing required tool properties'); - } + expect(error).toBeDefined(); }); -}); +}); \ No newline at end of file From 5000a47666c8f8fccf8a0ad26dd3160100809c9f Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Fri, 4 Oct 2024 12:14:30 +0530 Subject: [PATCH 10/24] updated test for errors --- tests/fixtures/toolsObjectData.js | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/fixtures/toolsObjectData.js b/tests/fixtures/toolsObjectData.js index 3d478712d79..4c5fbb9bf43 100644 --- a/tests/fixtures/toolsObjectData.js +++ b/tests/fixtures/toolsObjectData.js @@ -242,7 +242,6 @@ const unknownToolFileContent = ` - UnknownCategory `; -// New Fixtures for Error Handling Tests const toolFileMalformedJSON = ` title: Malformed Tool description: This tool has malformed JSON. From 56b0fddecbaa2ba080572a12fb6c22104be529e7 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Mon, 7 Oct 2024 16:16:28 +0530 Subject: [PATCH 11/24] renamed a variabel --- tests/fixtures/toolsObjectData.js | 4 ++-- tests/tools/tools-object.test.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/fixtures/toolsObjectData.js b/tests/fixtures/toolsObjectData.js index 4c5fbb9bf43..30501305986 100644 --- a/tests/fixtures/toolsObjectData.js +++ b/tests/fixtures/toolsObjectData.js @@ -54,7 +54,7 @@ const expectedObjectT1 = { const repositoryUrl = 'https://github.com/asyncapi/example-repo'; const repoDescription = 'Example repository'; -const isAsyncAPIrepo = true; +const isAsyncAPIOwner = true; const toolFileT2 = { title: 'Example Tool', @@ -259,7 +259,7 @@ module.exports = { expectedObjectT1, repoDescription, repositoryUrl, - isAsyncAPIrepo, + isAsyncAPIOwner, toolFileT2, expectedObjectT2, expectedObjectT3, diff --git a/tests/tools/tools-object.test.js b/tests/tools/tools-object.test.js index d88ba4092d1..aa57008b835 100644 --- a/tests/tools/tools-object.test.js +++ b/tests/tools/tools-object.test.js @@ -7,7 +7,7 @@ const { mockData, expectedObjectT1, repoDescription, repositoryUrl, - isAsyncAPIrepo, + isAsyncAPIOwner, toolFileT2, expectedObjectT2, expectedObjectT3, @@ -40,13 +40,13 @@ describe('Tools Object', () => { it('should create a tool object with provided parameters', async () => { - const result = await createToolObject(toolFileT1, repositoryUrl, repoDescription, isAsyncAPIrepo); + const result = await createToolObject(toolFileT1, repositoryUrl, repoDescription, isAsyncAPIOwner); expect(result).toEqual(expectedObjectT1); }); it('should use repoDescription when toolFile.description is not provided', async () => { - const result = await createToolObject(toolFileT2, repositoryUrl, repoDescription, isAsyncAPIrepo); + const result = await createToolObject(toolFileT2, repositoryUrl, repoDescription, isAsyncAPIOwner); expect(result).toEqual(expectedObjectT2); }); From f51feb335a8a9e3a13e231e7afd96621a5deead4 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Sun, 13 Oct 2024 23:01:26 +0530 Subject: [PATCH 12/24] test updated --- tests/build-tools.test.js | 1 - tests/fixtures/toolsObjectData.js | 97 ++++++++++++++++--------------- tests/tools/tools-object.test.js | 3 +- 3 files changed, 52 insertions(+), 49 deletions(-) diff --git a/tests/build-tools.test.js b/tests/build-tools.test.js index e86dd50d77f..2bc4592e8e1 100644 --- a/tests/build-tools.test.js +++ b/tests/build-tools.test.js @@ -86,5 +86,4 @@ describe('buildTools', () => { expect(err.message).toMatch(/ENOENT|EACCES/); } }); - }); diff --git a/tests/fixtures/toolsObjectData.js b/tests/fixtures/toolsObjectData.js index 30501305986..24865e0065d 100644 --- a/tests/fixtures/toolsObjectData.js +++ b/tests/fixtures/toolsObjectData.js @@ -16,16 +16,17 @@ const mockData = { ] }; -const mockToolFileContent = ` - title: Example Tool - description: This is an example tool. - links: - repoUrl: https://github.com/asyncapi/example-repo - filters: - categories: - - Category1 +const mockToolFileContent = { + title: "Example Tool", + description: "This is an example tool.", + links: { + repoUrl: "https://github.com/asyncapi/example-repo" + }, + filters: { + categories: ["Category1"], hasCommercial: true - `; + } +}; const toolFileT1 = { title: 'Example Tool', @@ -121,26 +122,28 @@ const dataWithUnknownCategory = { ] }; -const toolFileContent = ` - title: Unknown Tool - description: This tool has an unknown category. - links: - repoUrl: https://github.com/asyncapi/unknown-repo - filters: - categories: - - UnknownCategory - `; +const toolFileContent = { + title: "Unknown Tool", + description: "This tool has an unknown category.", + links: { + repoUrl: "https://github.com/asyncapi/unknown-repo" + }, + filters: { + categories: ["UnknownCategory"] + } +}; -const invalidToolFileContent = ` - title: Invalid Tool - description: This tool has invalid schema. - links: - repoUrl: https://github.com/asyncapi/invalid-repo - filters: - categories: - - Category1 +const invalidToolFileContent = { + title: "Invalid Tool", + description: "This tool has invalid schema.", + links: { + repoUrl: "https://github.com/asyncapi/invalid-repo" + }, + filters: { + categories: ["Category1"], invalidField: true - `; + } +}; const invalidToolData = { items: [ @@ -191,15 +194,16 @@ const duplicateToolData = { ] }; -const duplicateToolFileContent = ` - title: Duplicate Tool - description: This is a duplicate tool. - links: - repoUrl: https://github.com/asyncapi/duplicate-repo - filters: - categories: - - Category1 - `; +const duplicateToolFileContent = { + title: "Duplicate Tool", + description: "This is a duplicate tool.", + links: { + repoUrl: "https://github.com/asyncapi/duplicate-repo" + }, + filters: { + categories: ["Category1"] + } +}; const dataWithUnknownCategoryOnce = { items: [ @@ -232,15 +236,16 @@ const dataWithUnknownCategoryOnce = { ] }; -const unknownToolFileContent = ` - title: Unknown Tool - description: This tool has an unknown category. - links: - repoUrl: https://github.com/asyncapi/unknown-repo - filters: - categories: - - UnknownCategory - `; +const unknownToolFileContent = { + title: "Unknown Tool", + description: "This tool has an unknown category.", + links: { + repoUrl: "https://github.com/asyncapi/unknown-repo" + }, + filters: { + categories: ["UnknownCategory"] + } +}; const toolFileMalformedJSON = ` title: Malformed Tool @@ -272,4 +277,4 @@ module.exports = { dataWithUnknownCategoryOnce, unknownToolFileContent, toolFileMalformedJSON, -} \ No newline at end of file +}; diff --git a/tests/tools/tools-object.test.js b/tests/tools/tools-object.test.js index aa57008b835..7706e24572f 100644 --- a/tests/tools/tools-object.test.js +++ b/tests/tools/tools-object.test.js @@ -140,10 +140,9 @@ describe('Tools Object', () => { await convertTools(mockData); } catch (err) { error = err; - expect(err.message).toContain('Unexpected token'); } - expect(error).toBeDefined(); + expect(error.message).toContain('Unexpected token'); }); }); \ No newline at end of file From 3e73220c34a191ffa9ed6561e2e9919c92dbe77d Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Wed, 16 Oct 2024 19:04:55 +0530 Subject: [PATCH 13/24] path update --- scripts/build-tools.js | 10 +++++----- tests/build-tools.test.js | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/scripts/build-tools.js b/scripts/build-tools.js index 84965815dcc..05c9e85bcb9 100644 --- a/scripts/build-tools.js +++ b/scripts/build-tools.js @@ -2,7 +2,7 @@ const { getData } = require('./tools/extract-tools-github'); const { convertTools } = require('./tools/tools-object'); const { combineTools } = require('./tools/combine-tools'); const fs = require('fs'); -const { resolve } = require('path'); +const path = require('path'); const buildTools = async (automatedToolsPath, manualToolsPath, toolsPath, tagsPath) => { try { @@ -22,10 +22,10 @@ const buildTools = async (automatedToolsPath, manualToolsPath, toolsPath, tagsPa /* istanbul ignore next */ if (require.main === module) { - const automatedToolsPath = resolve(__dirname, '../config', 'tools-automated.json'); - const manualToolsPath = resolve(__dirname, '../config', 'tools-manual.json'); - const toolsPath = resolve(__dirname, '../config', 'tools.json'); - const tagsPath = resolve(__dirname, '../config', 'all-tags.json'); + const automatedToolsPath = path.join(__dirname, '../config', 'tools-automated.json'); + const manualToolsPath = path.join(__dirname, '../config', 'tools-manual.json'); + const toolsPath = path.join(__dirname, '../config', 'tools.json'); + const tagsPath = path.join(__dirname, '../config', 'all-tags.json'); buildTools(automatedToolsPath, manualToolsPath, toolsPath, tagsPath); } diff --git a/tests/build-tools.test.js b/tests/build-tools.test.js index 2bc4592e8e1..c8d0a029079 100644 --- a/tests/build-tools.test.js +++ b/tests/build-tools.test.js @@ -1,5 +1,5 @@ const axios = require('axios'); -const { resolve } = require('path'); +const path = require('path'); const { buildTools } = require('../scripts/build-tools'); const { tagsData, manualTools, mockConvertedData, mockExtractData } = require('../tests/fixtures/buildToolsData'); const fs = require('fs'); @@ -24,11 +24,11 @@ jest.mock('../scripts/tools/tags-color', () => ({ })); describe('buildTools', () => { - const testDir = resolve(__dirname, 'test_config'); - const toolsPath = resolve(testDir, 'tools.json'); - const tagsPath = resolve(testDir, 'all-tags.json'); - const automatedToolsPath = resolve(testDir, 'tools-automated.json'); - const manualToolsPath = resolve(testDir, 'tools-manual.json'); + const testDir = path.join(__dirname, 'test_config'); + const toolsPath = path.join(testDir, 'tools.json'); + const tagsPath = path.join(testDir, 'all-tags.json'); + const automatedToolsPath = path.join(testDir, 'tools-automated.json'); + const manualToolsPath = path.join(testDir, 'tools-manual.json'); beforeAll(() => { fs.mkdirSync(testDir, { recursive: true }); @@ -78,7 +78,7 @@ describe('buildTools', () => { it('should handle file write errors', async () => { axios.get.mockResolvedValue({ data: mockExtractData }); - const invalidPath = '/invalid_dir/tools.json'; + const invalidPath = path.join('/invalid_dir', 'tools.json'); try { await buildTools(invalidPath, manualToolsPath, toolsPath, tagsPath); From d31a06fb9e2092d9f9280cf431986aff98bad24d Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Wed, 16 Oct 2024 19:11:27 +0530 Subject: [PATCH 14/24] commit to rerun workflow --- scripts/build-tools.js | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/build-tools.js b/scripts/build-tools.js index 05c9e85bcb9..6af262eee69 100644 --- a/scripts/build-tools.js +++ b/scripts/build-tools.js @@ -22,6 +22,7 @@ const buildTools = async (automatedToolsPath, manualToolsPath, toolsPath, tagsPa /* istanbul ignore next */ if (require.main === module) { + const automatedToolsPath = path.join(__dirname, '../config', 'tools-automated.json'); const manualToolsPath = path.join(__dirname, '../config', 'tools-manual.json'); const toolsPath = path.join(__dirname, '../config', 'tools.json'); From 03f9dc17be307abdbef4aab1e099d4865523f13c Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Wed, 16 Oct 2024 19:11:46 +0530 Subject: [PATCH 15/24] commit to rerun workflow --- scripts/build-tools.js | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/build-tools.js b/scripts/build-tools.js index 6af262eee69..05c9e85bcb9 100644 --- a/scripts/build-tools.js +++ b/scripts/build-tools.js @@ -22,7 +22,6 @@ const buildTools = async (automatedToolsPath, manualToolsPath, toolsPath, tagsPa /* istanbul ignore next */ if (require.main === module) { - const automatedToolsPath = path.join(__dirname, '../config', 'tools-automated.json'); const manualToolsPath = path.join(__dirname, '../config', 'tools-manual.json'); const toolsPath = path.join(__dirname, '../config', 'tools.json'); From bfc9e6e1dc700cf4cac0ecfb8f648c2d75475697 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Fri, 18 Oct 2024 10:55:11 +0530 Subject: [PATCH 16/24] fefewg --- scripts/build-tools.js | 10 +++++----- tests/build-tools.test.js | 14 +++++++------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/scripts/build-tools.js b/scripts/build-tools.js index 05c9e85bcb9..84965815dcc 100644 --- a/scripts/build-tools.js +++ b/scripts/build-tools.js @@ -2,7 +2,7 @@ const { getData } = require('./tools/extract-tools-github'); const { convertTools } = require('./tools/tools-object'); const { combineTools } = require('./tools/combine-tools'); const fs = require('fs'); -const path = require('path'); +const { resolve } = require('path'); const buildTools = async (automatedToolsPath, manualToolsPath, toolsPath, tagsPath) => { try { @@ -22,10 +22,10 @@ const buildTools = async (automatedToolsPath, manualToolsPath, toolsPath, tagsPa /* istanbul ignore next */ if (require.main === module) { - const automatedToolsPath = path.join(__dirname, '../config', 'tools-automated.json'); - const manualToolsPath = path.join(__dirname, '../config', 'tools-manual.json'); - const toolsPath = path.join(__dirname, '../config', 'tools.json'); - const tagsPath = path.join(__dirname, '../config', 'all-tags.json'); + const automatedToolsPath = resolve(__dirname, '../config', 'tools-automated.json'); + const manualToolsPath = resolve(__dirname, '../config', 'tools-manual.json'); + const toolsPath = resolve(__dirname, '../config', 'tools.json'); + const tagsPath = resolve(__dirname, '../config', 'all-tags.json'); buildTools(automatedToolsPath, manualToolsPath, toolsPath, tagsPath); } diff --git a/tests/build-tools.test.js b/tests/build-tools.test.js index c8d0a029079..2bc4592e8e1 100644 --- a/tests/build-tools.test.js +++ b/tests/build-tools.test.js @@ -1,5 +1,5 @@ const axios = require('axios'); -const path = require('path'); +const { resolve } = require('path'); const { buildTools } = require('../scripts/build-tools'); const { tagsData, manualTools, mockConvertedData, mockExtractData } = require('../tests/fixtures/buildToolsData'); const fs = require('fs'); @@ -24,11 +24,11 @@ jest.mock('../scripts/tools/tags-color', () => ({ })); describe('buildTools', () => { - const testDir = path.join(__dirname, 'test_config'); - const toolsPath = path.join(testDir, 'tools.json'); - const tagsPath = path.join(testDir, 'all-tags.json'); - const automatedToolsPath = path.join(testDir, 'tools-automated.json'); - const manualToolsPath = path.join(testDir, 'tools-manual.json'); + const testDir = resolve(__dirname, 'test_config'); + const toolsPath = resolve(testDir, 'tools.json'); + const tagsPath = resolve(testDir, 'all-tags.json'); + const automatedToolsPath = resolve(testDir, 'tools-automated.json'); + const manualToolsPath = resolve(testDir, 'tools-manual.json'); beforeAll(() => { fs.mkdirSync(testDir, { recursive: true }); @@ -78,7 +78,7 @@ describe('buildTools', () => { it('should handle file write errors', async () => { axios.get.mockResolvedValue({ data: mockExtractData }); - const invalidPath = path.join('/invalid_dir', 'tools.json'); + const invalidPath = '/invalid_dir/tools.json'; try { await buildTools(invalidPath, manualToolsPath, toolsPath, tagsPath); From 9c1ef9e36387c9af59d5e3652ad0715a1301345c Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Fri, 18 Oct 2024 21:31:32 +0530 Subject: [PATCH 17/24] supress logs --- tests/build-tools.test.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/build-tools.test.js b/tests/build-tools.test.js index 2bc4592e8e1..d1de02f5435 100644 --- a/tests/build-tools.test.js +++ b/tests/build-tools.test.js @@ -29,14 +29,17 @@ describe('buildTools', () => { const tagsPath = resolve(testDir, 'all-tags.json'); const automatedToolsPath = resolve(testDir, 'tools-automated.json'); const manualToolsPath = resolve(testDir, 'tools-manual.json'); + let consoleErrorMock; beforeAll(() => { fs.mkdirSync(testDir, { recursive: true }); fs.writeFileSync(manualToolsPath, JSON.stringify(manualTools)); + consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(() => {}); }); afterAll(() => { fs.rmSync(testDir, { recursive: true, force: true }); + consoleErrorMock.mockRestore(); }); beforeEach(() => { From a1af4f70b96ddbd50b87d4d448faaf9ccf95c626 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Fri, 18 Oct 2024 21:36:37 +0530 Subject: [PATCH 18/24] newsroom test update --- tests/build-newsroom-videos.test.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/build-newsroom-videos.test.js b/tests/build-newsroom-videos.test.js index 63f57146694..c6ef40c838c 100644 --- a/tests/build-newsroom-videos.test.js +++ b/tests/build-newsroom-videos.test.js @@ -1,4 +1,4 @@ -const { readFileSync, rmSync, mkdirSync } = require('fs'); +const { readFileSync, rmSync, mkdirSync, existsSync } = require('fs'); const { resolve } = require('path'); const { buildNewsroomVideos } = require('../scripts/build-newsroom-videos'); const { mockApiResponse, expectedResult } = require('./fixtures/newsroomData'); @@ -11,12 +11,16 @@ describe('buildNewsroomVideos', () => { const testFilePath = resolve(testDir, 'newsroom_videos.json'); beforeAll(() => { - mkdirSync(testDir, { recursive: true }); + if (!existsSync(testDir)) { + mkdirSync(testDir, { recursive: true }); + } process.env.YOUTUBE_TOKEN = 'testkey'; }); afterAll(() => { - rmSync(testDir, { recursive: true, force: true }); + if (existsSync(testDir)) { + rmSync(testDir, { recursive: true, force: true }); + } }); beforeEach(() => { @@ -29,6 +33,10 @@ describe('buildNewsroomVideos', () => { json: jest.fn().mockResolvedValue(mockApiResponse), }); + if (!existsSync(testDir)) { + mkdirSync(testDir, { recursive: true }); + } + const result = await buildNewsroomVideos(testFilePath); const expectedUrl = new URL('https://youtube.googleapis.com/youtube/v3/search'); @@ -41,6 +49,7 @@ describe('buildNewsroomVideos', () => { expectedUrl.searchParams.set('maxResults', '5'); expect(fetch).toHaveBeenCalledWith(expectedUrl.toString()); + const response = readFileSync(testFilePath, 'utf8'); expect(response).toEqual(expectedResult); expect(result).toEqual(expectedResult); @@ -97,5 +106,4 @@ describe('buildNewsroomVideos', () => { expect(err.message).toMatch(/ENOENT|EACCES/); } }); - }); From 9d38d419972b9618daeedcd10cc68e2a10a3ac63 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Fri, 18 Oct 2024 21:40:44 +0530 Subject: [PATCH 19/24] build-tools test update --- tests/build-tools.test.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/build-tools.test.js b/tests/build-tools.test.js index d1de02f5435..7be2e5148e8 100644 --- a/tests/build-tools.test.js +++ b/tests/build-tools.test.js @@ -32,13 +32,19 @@ describe('buildTools', () => { let consoleErrorMock; beforeAll(() => { - fs.mkdirSync(testDir, { recursive: true }); - fs.writeFileSync(manualToolsPath, JSON.stringify(manualTools)); + if (!fs.existsSync(testDir)) { + fs.mkdirSync(testDir, { recursive: true }); + } + if (!fs.existsSync(manualToolsPath)) { + fs.writeFileSync(manualToolsPath, JSON.stringify(manualTools)); + } consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(() => {}); }); afterAll(() => { - fs.rmSync(testDir, { recursive: true, force: true }); + if (fs.existsSync(testDir)) { + fs.rmSync(testDir, { recursive: true, force: true }); + } consoleErrorMock.mockRestore(); }); @@ -49,6 +55,10 @@ describe('buildTools', () => { it('should extract, convert, combine tools, and write to file', async () => { axios.get.mockResolvedValue({ data: mockExtractData }); + if (!fs.existsSync(testDir)) { + fs.mkdirSync(testDir, { recursive: true }); + } + await buildTools(automatedToolsPath, manualToolsPath, toolsPath, tagsPath); const automatedToolsContent = JSON.parse(fs.readFileSync(automatedToolsPath, 'utf8')); @@ -65,7 +75,6 @@ describe('buildTools', () => { expect(combinedToolsContent["Category2"].description).toEqual(mockConvertedData["Category2"].description); expect(tagsContent).toEqual(tagsData); - }); it('should handle getData error', async () => { From 08a89709c375149457d20952bf8932fc099b9435 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Fri, 18 Oct 2024 21:54:54 +0530 Subject: [PATCH 20/24] uupdate newsroom function --- scripts/build-newsroom-videos.js | 8 ++++++-- tests/build-newsroom-videos.test.js | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/scripts/build-newsroom-videos.js b/scripts/build-newsroom-videos.js index b67ee0378cf..f8a06787bdf 100644 --- a/scripts/build-newsroom-videos.js +++ b/scripts/build-newsroom-videos.js @@ -1,9 +1,13 @@ -const { writeFileSync } = require('fs'); -const { resolve } = require('path'); +const { writeFileSync, mkdirSync, existsSync } = require('fs'); +const { resolve, dirname } = require('path'); const fetch = require('node-fetch-2'); async function buildNewsroomVideos(writePath) { try { + const dir = dirname(writePath); + if (!existsSync(dir)) { + mkdirSync(dir, { recursive: true }); + } const response = await fetch('https://youtube.googleapis.com/youtube/v3/search?' + new URLSearchParams({ key: process.env.YOUTUBE_TOKEN, part: 'snippet', diff --git a/tests/build-newsroom-videos.test.js b/tests/build-newsroom-videos.test.js index c6ef40c838c..704b479fa0e 100644 --- a/tests/build-newsroom-videos.test.js +++ b/tests/build-newsroom-videos.test.js @@ -11,9 +11,6 @@ describe('buildNewsroomVideos', () => { const testFilePath = resolve(testDir, 'newsroom_videos.json'); beforeAll(() => { - if (!existsSync(testDir)) { - mkdirSync(testDir, { recursive: true }); - } process.env.YOUTUBE_TOKEN = 'testkey'; }); @@ -24,6 +21,9 @@ describe('buildNewsroomVideos', () => { }); beforeEach(() => { + if (!existsSync(testDir)) { + mkdirSync(testDir, { recursive: true }); + } fetch.mockClear(); }); From cb902549199cb58e2c87b226069a680220a605f9 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Fri, 18 Oct 2024 21:57:01 +0530 Subject: [PATCH 21/24] update build-tools function --- scripts/build-tools.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/build-tools.js b/scripts/build-tools.js index 84965815dcc..3ba810cc245 100644 --- a/scripts/build-tools.js +++ b/scripts/build-tools.js @@ -2,13 +2,17 @@ const { getData } = require('./tools/extract-tools-github'); const { convertTools } = require('./tools/tools-object'); const { combineTools } = require('./tools/combine-tools'); const fs = require('fs'); -const { resolve } = require('path'); +const { resolve, dirname } = require('path'); const buildTools = async (automatedToolsPath, manualToolsPath, toolsPath, tagsPath) => { try { let githubExtractData = await getData(); let automatedTools = await convertTools(githubExtractData); - + + const automatedDir = dirname(automatedToolsPath); + if (!fs.existsSync(automatedDir)) { + fs.mkdirSync(automatedDir, { recursive: true }); + } fs.writeFileSync( automatedToolsPath, JSON.stringify(automatedTools, null, ' ') From d16f9a11051d4d638df7b92ff493ffe7490ed2bc Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Fri, 18 Oct 2024 22:00:49 +0530 Subject: [PATCH 22/24] commit to re run the workflow --- scripts/build-tools.js | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/build-tools.js b/scripts/build-tools.js index 3ba810cc245..d3e4d6c1089 100644 --- a/scripts/build-tools.js +++ b/scripts/build-tools.js @@ -10,6 +10,7 @@ const buildTools = async (automatedToolsPath, manualToolsPath, toolsPath, tagsPa let automatedTools = await convertTools(githubExtractData); const automatedDir = dirname(automatedToolsPath); + if (!fs.existsSync(automatedDir)) { fs.mkdirSync(automatedDir, { recursive: true }); } From 4540530a037894b5c9bc1164c68e1d7d34d57c57 Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Sat, 19 Oct 2024 11:05:32 +0530 Subject: [PATCH 23/24] added retry mechanicasm for enoent errors --- scripts/build-newsroom-videos.js | 24 +++++++++++++++++++++--- scripts/build-tools.js | 23 +++++++++++++++++++---- 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/scripts/build-newsroom-videos.js b/scripts/build-newsroom-videos.js index f8a06787bdf..f4cf8306692 100644 --- a/scripts/build-newsroom-videos.js +++ b/scripts/build-newsroom-videos.js @@ -5,9 +5,11 @@ const fetch = require('node-fetch-2'); async function buildNewsroomVideos(writePath) { try { const dir = dirname(writePath); + if (!existsSync(dir)) { mkdirSync(dir, { recursive: true }); } + const response = await fetch('https://youtube.googleapis.com/youtube/v3/search?' + new URLSearchParams({ key: process.env.YOUTUBE_TOKEN, part: 'snippet', @@ -23,7 +25,6 @@ async function buildNewsroomVideos(writePath) { } const data = await response.json(); - console.log(data) if (!data.items || !Array.isArray(data.items)) { throw new Error('Invalid data structure received from YouTube API'); @@ -39,7 +40,7 @@ async function buildNewsroomVideos(writePath) { const videoData = JSON.stringify(videoDataItems, null, ' '); console.log('The following are the Newsroom Youtube videos: ', videoData); - writeFileSync(writePath, videoData); + await retryWriteFile(writePath, videoData); return videoData; } catch (err) { @@ -47,9 +48,26 @@ async function buildNewsroomVideos(writePath) { } } +async function retryWriteFile(filePath, data, retries = 3, delay = 1000) { + for (let attempt = 0; attempt < retries; attempt++) { + try { + writeFileSync(filePath, data); + console.log(`File written successfully to ${filePath}`); + break; + } catch (err) { + if (err.code === 'ENOENT') { + console.error(`ENOENT error on attempt ${attempt + 1}. Retrying in ${delay}ms...`); + await new Promise((resolve) => setTimeout(resolve, delay)); + } else { + throw err; + } + } + } +} + /* istanbul ignore next */ if (require.main === module) { - buildNewsroomVideos(resolve(__dirname, '../config', 'newsroom_videos.json')) + buildNewsroomVideos(resolve(__dirname, '../config', 'newsroom_videos.json')); } module.exports = { buildNewsroomVideos }; diff --git a/scripts/build-tools.js b/scripts/build-tools.js index d3e4d6c1089..2b7c47768f7 100644 --- a/scripts/build-tools.js +++ b/scripts/build-tools.js @@ -14,10 +14,8 @@ const buildTools = async (automatedToolsPath, manualToolsPath, toolsPath, tagsPa if (!fs.existsSync(automatedDir)) { fs.mkdirSync(automatedDir, { recursive: true }); } - fs.writeFileSync( - automatedToolsPath, - JSON.stringify(automatedTools, null, ' ') - ); + + await retryWriteFile(automatedToolsPath, JSON.stringify(automatedTools, null, ' ')); await combineTools(automatedTools, require(manualToolsPath), toolsPath, tagsPath); } catch (err) { @@ -25,6 +23,23 @@ const buildTools = async (automatedToolsPath, manualToolsPath, toolsPath, tagsPa } }; +async function retryWriteFile(filePath, data, retries = 3, delay = 1000) { + for (let attempt = 0; attempt < retries; attempt++) { + try { + fs.writeFileSync(filePath, data); + console.log(`File written successfully to ${filePath}`); + break; + } catch (err) { + if (err.code === 'ENOENT') { + console.error(`ENOENT error on attempt ${attempt + 1}. Retrying in ${delay}ms...`); + await new Promise((resolve) => setTimeout(resolve, delay)); + } else { + throw err; + } + } + } +} + /* istanbul ignore next */ if (require.main === module) { const automatedToolsPath = resolve(__dirname, '../config', 'tools-automated.json'); From 0a83f543b5960a932e8711a3fe5085ef2b43b55c Mon Sep 17 00:00:00 2001 From: Vishvamsinh Vaghela Date: Sat, 19 Oct 2024 17:28:16 +0530 Subject: [PATCH 24/24] fegwg --- tests/build-tools.test.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/tests/build-tools.test.js b/tests/build-tools.test.js index 7be2e5148e8..c4feff26227 100644 --- a/tests/build-tools.test.js +++ b/tests/build-tools.test.js @@ -3,6 +3,7 @@ const { resolve } = require('path'); const { buildTools } = require('../scripts/build-tools'); const { tagsData, manualTools, mockConvertedData, mockExtractData } = require('../tests/fixtures/buildToolsData'); const fs = require('fs'); +const { beforeEach, afterEach } = require('node:test'); jest.mock('axios'); jest.mock('../scripts/tools/categorylist', () => ({ @@ -29,15 +30,17 @@ describe('buildTools', () => { const tagsPath = resolve(testDir, 'all-tags.json'); const automatedToolsPath = resolve(testDir, 'tools-automated.json'); const manualToolsPath = resolve(testDir, 'tools-manual.json'); + + if (!fs.existsSync(testDir)) { + fs.mkdirSync(testDir, { recursive: true }); + } + if (!fs.existsSync(manualToolsPath)) { + fs.writeFileSync(manualToolsPath, JSON.stringify(manualTools)); + } + let consoleErrorMock; beforeAll(() => { - if (!fs.existsSync(testDir)) { - fs.mkdirSync(testDir, { recursive: true }); - } - if (!fs.existsSync(manualToolsPath)) { - fs.writeFileSync(manualToolsPath, JSON.stringify(manualTools)); - } consoleErrorMock = jest.spyOn(console, 'error').mockImplementation(() => {}); }); @@ -50,6 +53,10 @@ describe('buildTools', () => { beforeEach(() => { jest.clearAllMocks(); + + if (!fs.existsSync(manualToolsPath)) { + fs.writeFileSync(manualToolsPath, JSON.stringify(manualTools)); + } }); it('should extract, convert, combine tools, and write to file', async () => {