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

Improve Test Coverage for packages/web3/src/utils #468

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Benjtalkshow
Copy link

Closes #457

Improve Test Coverage for packages/web3/src/utils

Overview

This pull request significantly enhances the test coverage for the utils.ts file and other utility functions in the packages/web3/src/utils directory. We've added new test cases, improved existing ones, and created new test files to ensure comprehensive coverage of our utility functions.

Main Changes

utils.test.ts

  1. Expanded test coverage for existing functions
  2. Added new test cases for previously untested functions
  3. Improved error case testing

bs58.test.ts

  1. Enhanced existing tests for isBase58 function:
    • Added more test cases for valid and invalid Base58 strings
    • Included tests for empty strings and whitespace-only strings
  2. Added new tests for base58ToBytes function:
    • Test for successful conversion
    • Tests for error cases (invalid input and empty string)
  3. Added tests for bs58 object:
    • Tests for encoding and decoding
    • Test for handling empty input

djb2.test.ts

  1. Expanded test cases for the djb2 function:
    • Added tests for various input types (empty string, single characters, short strings, long strings)
    • Included tests for special characters and non-ASCII inputs
  2. Improved assertion messages for better clarity in test results

subscription.test.ts (New File)

  1. Created comprehensive test suite for the Subscription class:
    • Tests for initialization with correct properties
    • Tests for subscribe and unsubscribe functionality
    • Tests for polling behavior and cancellation
    • Tests for error handling in callbacks
  2. Implemented mock class TestSubscription to test abstract Subscription class
  3. Added tests for edge cases and multiple subscribe/unsubscribe scenarios

Additional Notes

  • The changes in utils.ts resolve type errors related to the SignatureInput type
  • Test coverage has been significantly improved across all utility functions
  • New test file subscription.test.ts provides thorough coverage for the Subscription class
  • All tests are passing after the modifications

@Benjtalkshow
Copy link
Author

@polarker please review

@h0ngcha0
Copy link
Member

h0ngcha0 commented Nov 27, 2024

Thanks for the contribution.

Please make sure all the updated test suite pass, right now all of them have failing tests. You can run them by

pnpm install
pnpm build
pnpm test

@Benjtalkshow
Copy link
Author

Thanks for the contribution.

Please make sure all the updated test suite pass, right now all of them have failing tests. You can run them by

pnpm install
pnpm build
pnpm test

Alright ser

@Benjtalkshow
Copy link
Author

@h0ngcha0 Will update my PR Soon

@Benjtalkshow
Copy link
Author

Benjtalkshow commented Nov 30, 2024

@h0ngcha0 Will update my PR Soon

  • Fix bs58.test.ts
  • Fix djb2.test.ts
  • Fixing... subscription.test.ts
  • Fixing... utils.test.ts

@Benjtalkshow
Copy link
Author

@h0ngcha0 Please review.

@@ -22,4 +22,4 @@ export default function djb2(bytes: Uint8Array): number {
hash = (hash << 5) + hash + (bytes[`${i}`] & 0xff)
}
return hash
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change of this file is unnecessary.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@@ -34,7 +34,7 @@
"author": "Alephium dev <[email protected]>",
"config": {
"alephium_version": "3.9.0",
"explorer_backend_version": "2.2.6"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change relevant in this PR? Usually the version is updated when we want to update the explorer backend API file, which doesn't seem to be the case here.

@@ -98,4 +98,4 @@
"node": ">=14.0.0",
"npm": ">=7.0.0"
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert the end-of-file change to all updated files, maybe it is a editor config issue.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was a mistake. will revert it

@@ -36,11 +36,13 @@ export function isBase58(s: string): boolean {
}

export function base58ToBytes(s: string): Uint8Array {
if (s === '') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we we should allow empty string here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

});
});


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove extra lines.

it('should convert valid base58 strings to bytes', () => {
const result = base58ToBytes('32UWxgjUHwH7P1J61tb12')
expect(result).toBeInstanceOf(Uint8Array)
expect(result.length).toBeGreaterThan(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can check bs58.encode(result) would give us 32UWxgjUHwH7P1J61tb12 instead.

Copy link

@pragmaxim pragmaxim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve Test Coverage for packages/web3/src/utils
3 participants