UNPKG

near-protocol-rewards

Version:

A transparent, metric-based rewards system for NEAR projects

110 lines (109 loc) 5.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const errors_1 = require("../../../src/types/errors"); describe('ErrorCode', () => { it('should have all error codes defined', () => { expect(errors_1.ErrorCode.SDK_ERROR).toBe('SDK_ERROR'); expect(errors_1.ErrorCode.API_ERROR).toBe('API_ERROR'); expect(errors_1.ErrorCode.VALIDATION_ERROR).toBe('VALIDATION_ERROR'); expect(errors_1.ErrorCode.STORAGE_ERROR).toBe('STORAGE_ERROR'); expect(errors_1.ErrorCode.RATE_LIMIT_ERROR).toBe('RATE_LIMIT_ERROR'); expect(errors_1.ErrorCode.PROCESSING_ERROR).toBe('PROCESSING_ERROR'); expect(errors_1.ErrorCode.CALCULATION_ERROR).toBe('CALCULATION_ERROR'); expect(errors_1.ErrorCode.CONFIGURATION_ERROR).toBe('CONFIGURATION_ERROR'); expect(errors_1.ErrorCode.COLLECTION_ERROR).toBe('COLLECTION_ERROR'); expect(errors_1.ErrorCode.INVALID_CONFIG).toBe('INVALID_CONFIG'); expect(errors_1.ErrorCode.UNKNOWN_ERROR).toBe('UNKNOWN_ERROR'); expect(errors_1.ErrorCode.UNAUTHORIZED).toBe('UNAUTHORIZED'); expect(errors_1.ErrorCode.LOW_COMMIT_COUNT).toBe('LOW_COMMIT_COUNT'); expect(errors_1.ErrorCode.SUSPICIOUS_COMMIT_FREQUENCY).toBe('SUSPICIOUS_COMMIT_FREQUENCY'); expect(errors_1.ErrorCode.LOW_AUTHOR_DIVERSITY).toBe('LOW_AUTHOR_DIVERSITY'); expect(errors_1.ErrorCode.HIGH_VELOCITY).toBe('HIGH_VELOCITY'); expect(errors_1.ErrorCode.SINGLE_PR_AUTHOR).toBe('SINGLE_PR_AUTHOR'); expect(errors_1.ErrorCode.LOW_PR_MERGE_RATE).toBe('LOW_PR_MERGE_RATE'); expect(errors_1.ErrorCode.LOW_REVIEW_ENGAGEMENT).toBe('LOW_REVIEW_ENGAGEMENT'); expect(errors_1.ErrorCode.LOW_ISSUE_ENGAGEMENT).toBe('LOW_ISSUE_ENGAGEMENT'); expect(errors_1.ErrorCode.LOW_ISSUE_RESOLUTION_RATE).toBe('LOW_ISSUE_RESOLUTION_RATE'); expect(errors_1.ErrorCode.MISSING_TIMESTAMP).toBe('MISSING_TIMESTAMP'); expect(errors_1.ErrorCode.STALE_DATA).toBe('STALE_DATA'); }); }); describe('BaseError', () => { it('should create error with message and code', () => { const error = new errors_1.BaseError('Test error', errors_1.ErrorCode.SDK_ERROR); expect(error.message).toBe('Test error'); expect(error.code).toBe(errors_1.ErrorCode.SDK_ERROR); expect(error.details).toBeUndefined(); expect(error.context).toBeUndefined(); }); it('should create error with details', () => { const details = { foo: 'bar' }; const error = new errors_1.BaseError('Test error', errors_1.ErrorCode.SDK_ERROR, details); expect(error.message).toBe('Test error'); expect(error.code).toBe(errors_1.ErrorCode.SDK_ERROR); expect(error.details).toEqual(details); expect(error.context).toEqual(details); }); it('should be instance of Error', () => { const error = new errors_1.BaseError('Test error', errors_1.ErrorCode.SDK_ERROR); expect(error).toBeInstanceOf(Error); }); it('should create an instance of BaseError without details', () => { const message = 'Test error message'; const code = errors_1.ErrorCode.API_ERROR; const error = new errors_1.BaseError(message, code); expect(error).toBeInstanceOf(errors_1.BaseError); expect(error.message).toBe(message); expect(error.code).toBe(code); expect(error.context).toBeUndefined(); }); it('should have a stack trace', () => { const message = 'Test error message'; const code = errors_1.ErrorCode.VALIDATION_ERROR; const error = new errors_1.BaseError(message, code); expect(error.stack).toBeDefined(); }); }); describe('formatError', () => { it('should format Error instance', () => { const error = new Error('Test error'); error.stack = 'test stack'; const formatted = (0, errors_1.formatError)(error); expect(formatted).toEqual({ code: errors_1.ErrorCode.PROCESSING_ERROR, message: 'Test error', context: { stack: 'test stack' }, }); }); it('should format BaseError instance', () => { const error = new errors_1.BaseError('Test error', errors_1.ErrorCode.SDK_ERROR, { foo: 'bar' }); const formatted = (0, errors_1.formatError)(error); expect(formatted).toEqual({ code: errors_1.ErrorCode.PROCESSING_ERROR, message: 'Test error', context: { stack: error.stack }, }); }); it('should format non-Error values', () => { expect((0, errors_1.formatError)('string error')).toEqual({ code: errors_1.ErrorCode.PROCESSING_ERROR, message: 'string error', }); expect((0, errors_1.formatError)(123)).toEqual({ code: errors_1.ErrorCode.PROCESSING_ERROR, message: '123', }); expect((0, errors_1.formatError)(null)).toEqual({ code: errors_1.ErrorCode.PROCESSING_ERROR, message: 'null', }); expect((0, errors_1.formatError)(undefined)).toEqual({ code: errors_1.ErrorCode.PROCESSING_ERROR, message: 'undefined', }); expect((0, errors_1.formatError)({ custom: 'error' })).toEqual({ code: errors_1.ErrorCode.PROCESSING_ERROR, message: '[object Object]', }); }); });