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 tools object #3302

Closed
wants to merge 34 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 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
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
32 changes: 27 additions & 5 deletions scripts/build-newsroom-videos.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
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',
Expand All @@ -19,7 +25,6 @@
}

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');
Expand All @@ -35,17 +40,34 @@
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) {
throw new Error(`Failed to build newsroom videos: ${err.message}`);
}
}

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));

Check warning on line 60 in scripts/build-newsroom-videos.js

View check run for this annotation

Codecov / codecov/patch

scripts/build-newsroom-videos.js#L58-L60

Added lines #L58 - L60 were not covered by tests
} else {
throw err;

Check warning on line 62 in scripts/build-newsroom-videos.js

View check run for this annotation

Codecov / codecov/patch

scripts/build-newsroom-videos.js#L62

Added line #L62 was not covered by tests
}
}
}
}

/* istanbul ignore next */
if (require.main === module) {
buildNewsroomVideos(resolve(__dirname, '../config', 'newsroom_videos.json'))
buildNewsroomVideos(resolve(__dirname, '../config', 'newsroom_videos.json'));
}

module.exports = { buildNewsroomVideos };
30 changes: 25 additions & 5 deletions scripts/build-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,44 @@
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);

fs.writeFileSync(
automatedToolsPath,
JSON.stringify(automatedTools, null, ' ')
);
if (!fs.existsSync(automatedDir)) {
fs.mkdirSync(automatedDir, { recursive: true });
}

await retryWriteFile(automatedToolsPath, JSON.stringify(automatedTools, null, ' '));

await combineTools(automatedTools, require(manualToolsPath), toolsPath, tagsPath);
} catch (err) {
throw new Error(`An error occurred while building tools: ${err.message}`);
}
};

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));

Check warning on line 35 in scripts/build-tools.js

View check run for this annotation

Codecov / codecov/patch

scripts/build-tools.js#L33-L35

Added lines #L33 - L35 were not covered by tests
} else {
throw err;

Check warning on line 37 in scripts/build-tools.js

View check run for this annotation

Codecov / codecov/patch

scripts/build-tools.js#L37

Added line #L37 was not covered by tests
}
}
}
}

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

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

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

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

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

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

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

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

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

module.exports = {convertTools, createToolObject}
module.exports = { convertTools, createToolObject }
16 changes: 12 additions & 4 deletions tests/build-newsroom-videos.test.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -11,15 +11,19 @@ describe('buildNewsroomVideos', () => {
const testFilePath = resolve(testDir, 'newsroom_videos.json');

beforeAll(() => {
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(() => {
if (!existsSync(testDir)) {
mkdirSync(testDir, { recursive: true });
}
fetch.mockClear();
});

Expand All @@ -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');
Expand All @@ -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);
Expand Down Expand Up @@ -97,5 +106,4 @@ describe('buildNewsroomVideos', () => {
expect(err.message).toMatch(/ENOENT|EACCES/);
}
});

});
25 changes: 22 additions & 3 deletions tests/build-tools.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => ({
Expand Down Expand Up @@ -30,22 +31,41 @@ describe('buildTools', () => {
const automatedToolsPath = resolve(testDir, 'tools-automated.json');
const manualToolsPath = resolve(testDir, 'tools-manual.json');

beforeAll(() => {
if (!fs.existsSync(testDir)) {
fs.mkdirSync(testDir, { recursive: true });
}
if (!fs.existsSync(manualToolsPath)) {
fs.writeFileSync(manualToolsPath, JSON.stringify(manualTools));
}

let consoleErrorMock;

beforeAll(() => {
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();
});

beforeEach(() => {
jest.clearAllMocks();

if (!fs.existsSync(manualToolsPath)) {
fs.writeFileSync(manualToolsPath, JSON.stringify(manualTools));
}
});

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'));
Expand All @@ -62,7 +82,6 @@ describe('buildTools', () => {
expect(combinedToolsContent["Category2"].description).toEqual(mockConvertedData["Category2"].description);

expect(tagsContent).toEqual(tagsData);

});

it('should handle getData error', async () => {
Expand Down
Loading
Loading