Skip to content

Commit

Permalink
Add tests for limitFetcher and processThreshold.
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthiasZepper committed Oct 12, 2023
1 parent f82dcd7 commit cc36195
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 8 deletions.
84 changes: 84 additions & 0 deletions __tests__/limitFetcher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Unit tests for src/processThreshold.ts
*/

import * as core from '@actions/core'
import nock from 'nock'; // Import the nock library for mocking HTTP requests
import { fetchRateLimit } from '../src/limitFetcher';
import { expect } from '@jest/globals'

// Mock the setFailed function from '@actions/core'
jest.mock('@actions/core', () => ({
setFailed: jest.fn(),
}));

describe('limitFetcher.ts', () => {
beforeAll(() => {
// Set up a mock for the GitHub API
nock('https://api.github.com')
.get('/rate_limit')
.reply(200, {
data: {
resources: {
core: {
limit: 5000,
remaining: 4300,
reset: 1696896000,
used: 700,
},
search: {
limit: 30,
remaining: 18,
reset: 1696896400,
used: 12,
},
},
},
});
});

afterAll(() => {
// Clean up the nock mocks
nock.cleanAll();
});

it('should fetch and return the core rate limit', async () => {
const token = 'some_fake_github_token';
const resource = 'core';

const result = await fetchRateLimit(token, resource);

expect(core.setFailed).not.toHaveBeenCalled();
expect(result.limit).toBe(5000);
expect(result.remaining).toBe(4300);
expect(result.reset).toBe(1696896000);
});

it('should fetch and return the search rate limit', async () => {
const token = 'some_fake_github_token';
const resource = 'search';

const result = await fetchRateLimit(token, resource);

expect(core.setFailed).not.toHaveBeenCalled();
expect(result.limit).toBe(30);
expect(result.remaining).toBe(18);
expect(result.reset).toBe(1696896400);
});

it('should set a failed message on API error', async () => {
// Mock the GitHub API to return an error (e.g., 404 Not Found)
nock('https://api.github.com')
.get('/rate_limit')
.reply(404);

const token = 'some_fake_github_token';
const resource = 'core';

await fetchRateLimit(token, resource);

expect(core.setFailed).toHaveBeenCalledWith('Github API rateLimit could not be retrieved.');
});

});

73 changes: 73 additions & 0 deletions __tests__/processThreshold.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Unit tests for src/processThreshold.ts
*/

import * as core from '@actions/core'
import { processThreshold } from '../src/processThreshold';
import { expect } from '@jest/globals'

// Mock the setFailed function from '@actions/core'
jest.mock('@actions/core', () => ({
setFailed: jest.fn(),
}));


describe('processThreshold.ts', () => {
it('should process an absolute threshold correctly', () => {
const thresholdAsString = '26';
const result = processThreshold(thresholdAsString);

expect(core.setFailed).not.toHaveBeenCalled();
expect(result.thresholdAsAbsolute).toBe(26);
expect(result.thresholdAsFraction).toBeUndefined();
});

it('should process a fractional threshold correctly', () => {
const thresholdAsString = '0.3';
const result = processThreshold(thresholdAsString);

expect(core.setFailed).not.toHaveBeenCalled();
expect(result.thresholdAsAbsolute).toBeUndefined();
expect(result.thresholdAsFraction).toBe(0.3);
});

it('should process a percent threshold correctly', () => {
const thresholdAsString = '60%';
const result = processThreshold(thresholdAsString);

expect(core.setFailed).not.toHaveBeenCalled();
expect(result.thresholdAsAbsolute).toBeUndefined();
expect(result.thresholdAsFraction).toBe(0.6);
});

it('should set an error for an invalid threshold (negative number)', () => {
const thresholdAsString = '-0.1';
const result = processThreshold(thresholdAsString);

expect(core.setFailed).toHaveBeenCalledWith(
'The threshold must be a positive number, but -0.1 was provided.'
);
expect(result.thresholdAsAbsolute).toBeUndefined();
expect(result.thresholdAsFraction).toBeUndefined();
});

it('should handle invalid input (not a number)', () => {
const thresholdAsString = 'invalid';
const result = processThreshold(thresholdAsString);

expect(core.setFailed).toHaveBeenCalledWith(
'The threshold must be a positive number, but invalid was provided.'
);
expect(result.thresholdAsAbsolute).toBeUndefined();
expect(result.thresholdAsFraction).toBeUndefined();
});

it('should ignore a blank between number and percent sign', () => {
const thresholdAsString = '50 %';
const result = processThreshold(thresholdAsString);

expect(core.setFailed).not.toHaveBeenCalled();
expect(result.thresholdAsAbsolute).toBeUndefined();
expect(result.thresholdAsFraction).toBe(50);
});
})
12 changes: 6 additions & 6 deletions __tests__/validateResource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,32 @@ jest.mock('@actions/core', () => ({
}));

describe('validateResource.ts', () => {
it('Accepts core as valid input', async () => {
it('accepts core as valid input', async () => {
validateResource('core');
expect(core.setFailed).not.toHaveBeenCalled();
})

it('Accepts graphql as valid input', async () => {
it('accepts graphql as valid input', async () => {
validateResource('graphql');
expect(core.setFailed).not.toHaveBeenCalled();
})

it('Accepts search as valid input', async () => {
it('accepts search as valid input', async () => {
validateResource('search');
expect(core.setFailed).not.toHaveBeenCalled();
})

it('Accepts integration_manifest as valid input', async () => {
it('accepts integration_manifest as valid input', async () => {
validateResource('integration_manifest');
expect(core.setFailed).not.toHaveBeenCalled();
})

it('Accepts code_scanning_upload as valid input', async () => {
it('accepts code_scanning_upload as valid input', async () => {
validateResource('code_scanning_upload');
expect(core.setFailed).not.toHaveBeenCalled();
})

it('Fails the step for an invalid resource', () => {
it('fails the step for an invalid resource', () => {
validateResource('invalid_resource');
expect(core.setFailed).toHaveBeenCalledWith(
'The resource must be either core, graphql, search, integration_manifest, or code_scanning_upload.'
Expand Down
35 changes: 33 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"jest": "^29.7.0",
"js-yaml": "^4.1.0",
"make-coverage-badge": "^1.2.0",
"nock": "^13.3.4",
"prettier": "^3.0.3",
"prettier-eslint": "^15.0.1",
"ts-jest": "^29.1.1",
Expand Down

0 comments on commit cc36195

Please sign in to comment.