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

Add unit tests in Clarity #4100

Closed
wants to merge 12 commits into from
Closed

Add unit tests in Clarity #4100

wants to merge 12 commits into from

Conversation

friedger
Copy link
Collaborator

Description

This PR adds the ability to write clarinet-sdk tests in Clarity.

As an example a simple unit tests and a simple flow tests was added. These tests do not test anything meaningful, but show how these tests work.

Applicable issues

Additional info (benefits, drawbacks, caveats)

Checklist

  • Test coverage for new or modified code paths
  • Changelog is updated
  • Required documentation changes (e.g., docs/rpc/openapi.yaml and rpc-endpoints.md for v2 endpoints, event-dispatcher.md for new events)
  • New clarity functions have corresponding PR in clarity-benchmarking repo
  • New integration test(s) added to bitcoin-tests.yml

Copy link

codecov bot commented Nov 30, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (8cffc38) 85.08% compared to head (fedc750) 82.06%.

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #4100      +/-   ##
==========================================
- Coverage   85.08%   82.06%   -3.03%     
==========================================
  Files         429      429              
  Lines      302009   302009              
==========================================
- Hits       256976   247832    -9144     
- Misses      45033    54177    +9144     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@friedger friedger marked this pull request as ready for review December 1, 2023 20:19
Copy link
Member

@moodmosaic moodmosaic left a comment

Choose a reason for hiding this comment

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

LGTM

(define-constant ERR_NAMESPACE_NOT_FOUND 1005)

;; @name: test delegation to wallet_2, stacking and revoking
(define-public (test-name-registration)
Copy link
Member

Choose a reason for hiding this comment

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

In a future PR I think it's doable (and a bit challenging too) to enable tests with parameters, to run those tests also as fuzz tests:

(define-public (test-name-registration (hashed-salted-fqn (buff 20))
                                       (stx-to-burn uint)
                                       (namespace (buff 20))
                                       (name (buff 48))
                                       (salt (buff 20))
                                       (zonefile-hash (buff 20)))

The tricky part might be the extra work around regexes, though the heavily lifting part around fast-check shouldn't be an issue.

Copy link
Member

Choose a reason for hiding this comment

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

I think we can specify parameter testing / fuzzing with annotations. The regexes originally came from a concept implementation to enable us to write Clarity tests in Clarity. After that, they were good enough so we just left it. If we want to make it more robust we will need to switch to a LISP interpreter, should not be too hard.

Copy link
Member

Choose a reason for hiding this comment

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

If we can sort this part out, I can integrate this with fast-check and we'll have it generate/fuzz everything for us. What's very nice about this is the shrinking part; if there's a bug/edge-case detected, fast-check will give us the minimum counterexample and a seed to reproduce the failure (if the bug/edge-case is detected when running on CI).

Copy link
Member

@MarvinJanssen MarvinJanssen left a comment

Choose a reason for hiding this comment

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

Naturally I much prefer writing tests in Clarity. The question is do the other code owners like (or at least are they OK with) bringing this new testing paradigm into the repository. Who decides?

(define-constant ERR_NAMESPACE_NOT_FOUND 1005)

;; @name: test delegation to wallet_2, stacking and revoking
(define-public (test-name-registration)
Copy link
Member

Choose a reason for hiding this comment

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

I think we can specify parameter testing / fuzzing with annotations. The regexes originally came from a concept implementation to enable us to write Clarity tests in Clarity. After that, they were good enough so we just left it. If we want to make it more robust we will need to switch to a LISP interpreter, should not be too hard.

@@ -0,0 +1,138 @@
import { ParsedTransactionResult, tx } from "@hirosystems/clarinet-sdk";
Copy link
Member

Choose a reason for hiding this comment

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

Hey, do you think you can add some method-level comments for each of these functions in this file and the clar.test.ts file? I'm having a hard time following how these functions fit together. Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

This parser evolved over time during our work on sBTC Mini. We just discussed in the Clarity WG to clean it up and perhaps introduce a more robust parser. The current files have served our needs and were recently edited to support clarinet-sdk.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Comments have been added


export function getContractName(contractId: string) {
return contractId.split(".")[1];
}
Copy link
Member

Choose a reason for hiding this comment

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

This parser looks somewhat involved. Is there a way to add a (simple) set of unit tests to verify that all the test parsers work as intended? Thanks!

Copy link
Member

Choose a reason for hiding this comment

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

@friedger @jcnelson happy to do this with fast-check? This way we’ll test and have the ability to also fuzz the interpreter at the same time.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Code improvements have been pushed

Copy link
Member

Choose a reason for hiding this comment

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

FYI, some prop/fuzz tests I added can be found in 3e9b3af and 3420904.

friedger and others added 7 commits December 5, 2023 16:15
…sed tests

- Implemented property-based testing for string, uint, and tuple conversions using fast-check.
- Defined custom arbitraries for generating 128-bit unsigned (uint128) and signed (int128) integers.
- Enhanced test robustness and coverage by automatically testing a wide range of input values.
- Included fast-check's shrinking feature to identify minimal failure cases, aiding in effective debugging.
Copy link
Collaborator

@setzeus setzeus left a comment

Choose a reason for hiding this comment

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

Pulled & followed the logic due to familiarity w/ mini. My only feedback here is to make an issue for documenting how these tests are built & how a contributor can write additional tests.

LGTM.

@wileyj
Copy link
Contributor

wileyj commented Dec 18, 2023

@MarvinJanssen @friedger I like the approach here, and what you're trying to do.
however, i think it might be better to think about these changes as something that would be easier to maintain as a separate repo entirely (possibly even a monorepo with a few other testing repos that i know of).

my concern is that if we need to make changes, the bar to getting them merged to master is much higher in this repo vs a separate repo.

merging to next, and updating next is less of an issue i'll agree. i'm thinking longer-term though, when eventually next is merged to master and we may find issues/improvement that should be made. having this in a separate repo would make those changes much easier to make.

@friedger
Copy link
Collaborator Author

@wileyj I agree.

As there is currently no other repo, this seems to be the only solution to get a good discussion.

Merging a version update of the tool into main should be as hard as updating the source code 😉

@wileyj
Copy link
Contributor

wileyj commented Dec 19, 2023

@wileyj I agree.

As there is currently no other repo, this seems to be the only solution to get a good discussion.

Merging a version update of the tool into main should be as hard as updating the source code 😉

that's what concerns me actually - unless i'm misunderstanding the reasoning behind this PR, i see a lot of benefit in moving most (or all) of the tooling outside of the blockchain repo (we can still maintain a higher bar for merging PR's, but maybe not quite as high as the blockchain repo requires).
the tests themselves, i have no opinion on - i'm mostly looking at the tooling code/dirs in this PR.
we can discuss wednesday, but the goal for me is to make updating the tooling as easy as it needs to be.

@obycode
Copy link
Contributor

obycode commented Dec 19, 2023

Being discussed tomorrow.

@friedger
Copy link
Collaborator Author

using clarunit now: #4203 (comment)

@friedger friedger closed this Jan 10, 2024
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.

[clarity] Setup Standard Clarinet Project For PoX-4 Flow Tests
7 participants