-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
const assert = require('assert') | ||
const Projects = require('../../src/internal/agora/Projects') | ||
const hre = require('hardhat') | ||
const output = require('ethernaut-common/src/ui/output') | ||
|
||
describe('projects task', function () { | ||
let originalGetProjects, | ||
originalGetRoundProjects, | ||
originalGetLatestRound, | ||
originalOutputResultBox, | ||
originalOutputErrorBox | ||
|
||
beforeEach(function () { | ||
// Mock Projects class methods | ||
originalGetProjects = Projects.prototype.getProjects | ||
originalGetRoundProjects = Projects.prototype.getRoundProjects | ||
originalGetLatestRound = Projects.prototype.getLatestRound | ||
|
||
Projects.prototype.getProjects = async function ({ | ||
limit: _limit, | ||
offset: _offset, | ||
}) { | ||
return [ | ||
{ | ||
name: 'Project 1', | ||
category: 'Category A', | ||
description: 'Description 1', | ||
}, | ||
{ | ||
name: 'Project 2', | ||
category: 'Category B', | ||
description: 'Description 2', | ||
}, | ||
] | ||
} | ||
|
||
Projects.prototype.getRoundProjects = async function ({ | ||
roundId, | ||
limit: _limit, | ||
offset: _offset, | ||
}) { | ||
return [ | ||
{ | ||
name: `Project for Round ${roundId}`, | ||
category: 'Category A', | ||
description: 'Description for round project', | ||
}, | ||
] | ||
} | ||
|
||
Projects.prototype.getLatestRound = async function () { | ||
return 5 | ||
} | ||
|
||
// Mock the output methods | ||
originalOutputResultBox = output.resultBox | ||
originalOutputErrorBox = output.errorBox | ||
|
||
output.resultBox = (content, title) => `${title}: ${content}` | ||
output.errorBox = (error) => `Error: ${error.message}` | ||
}) | ||
|
||
afterEach(function () { | ||
// Restore the original methods after each test | ||
Projects.prototype.getProjects = originalGetProjects | ||
Projects.prototype.getRoundProjects = originalGetRoundProjects | ||
Projects.prototype.getLatestRound = originalGetLatestRound | ||
|
||
output.resultBox = originalOutputResultBox | ||
output.errorBox = originalOutputErrorBox | ||
}) | ||
|
||
it('fetches a list of projects with limit and offset', async function () { | ||
const result = await hre.run( | ||
{ scope: 'optigov', task: 'projects' }, | ||
{ limit: 2, offset: 0, round: 'any' }, | ||
) | ||
assert.equal( | ||
result, | ||
'Projects: - Project 1: Category A: Description 1\n\n - Project 2: Category B: Description 2', | ||
) | ||
}) | ||
|
||
it('fetches projects for the latest round', async function () { | ||
const result = await hre.run( | ||
{ scope: 'optigov', task: 'projects' }, | ||
{ round: 'latest', limit: 2, offset: 0 }, | ||
) | ||
|
||
assert.equal( | ||
result, | ||
'Projects: - Project for Round 5: Category A: Description for round project', | ||
) | ||
}) | ||
|
||
it('fetches projects filtered by name and category', async function () { | ||
const result = await hre.run( | ||
{ scope: 'optigov', task: 'projects' }, | ||
{ | ||
round: 'any', | ||
limit: 2, | ||
offset: 0, | ||
name: 'Project 1', | ||
category: 'Category A', | ||
}, | ||
) | ||
|
||
assert.equal(result, 'Projects: - Project 1: Category A: Description 1') | ||
}) | ||
|
||
it('handles errors gracefully when fetching projects fails', async function () { | ||
Projects.prototype.getProjects = async function () { | ||
throw new Error('Failed to fetch projects') | ||
} | ||
|
||
const result = await hre.run( | ||
{ scope: 'optigov', task: 'projects' }, | ||
{ limit: 2, offset: 0, round: 'any' }, | ||
) | ||
|
||
assert.equal(result, 'Error: Failed to fetch projects') | ||
}) | ||
}) |