diff --git a/.github/workflows/move-tests.yml b/.github/workflows/move-tests.yml new file mode 100644 index 00000000..3ef31c84 --- /dev/null +++ b/.github/workflows/move-tests.yml @@ -0,0 +1,45 @@ +name: Run Move tests + +on: + push: + branches: + - main + paths: + - move/** + pull_request: + paths: + - move/** + +jobs: + run-tests: + name: Run Tests + runs-on: ubuntu-latest + + defaults: + run: + working-directory: move/walrus_site + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Download Sui + run: | + VERSION=$(curl -s https://api.github.com/repos/MystenLabs/sui/releases/latest | jq -r '.tag_name') + curl -L "https://github.com/MystenLabs/sui/releases/download/$VERSION/sui-$VERSION-ubuntu-x86_64.tgz" -o sui.tgz + sudo tar -xvzf sui.tgz -C /usr/local/bin + + # Run Tests with Coverage + - name: Run tests + run: sui-debug move test --coverage + + - name: Check Move Coverage + run: | + COVERAGE_OUTPUT=$(sui-debug move coverage summary) + echo "$COVERAGE_OUTPUT" + COVERAGE_PERCENT=$(echo "$COVERAGE_OUTPUT" | grep "% Move Coverage:" | sed -E 's/.*: ([0-9]+(\.[0-9]+)?).*/\1/') + if (( $(echo "$COVERAGE_PERCENT < 80" | bc -l) )); then + echo "Coverage is below 80%. Build failed." + exit 1 + fi + echo "Coverage is $COVERAGE_PERCENT%. Build passed." diff --git a/move/walrus_site/tests/site_tests.move b/move/walrus_site/tests/site_tests.move index cefa1922..cf1b4068 100644 --- a/move/walrus_site/tests/site_tests.move +++ b/move/walrus_site/tests/site_tests.move @@ -2,8 +2,10 @@ module walrus_site::site_tests { use walrus_site::site::{ ERangeStartGreaterThanRangeEnd, - EStartAndEndRangeAreNone + EStartAndEndRangeAreNone, + Site, Range }; + #[test] #[expected_failure(abort_code = EStartAndEndRangeAreNone)] fun test_new_range_no_bounds_defined() { @@ -48,4 +50,104 @@ module walrus_site::site_tests { option::some(1) ); } + + /// This test runs a typical process + /// checking many of the contract's functions. + #[test] + fun test_site_flow_with_resources_and_routes() { + use sui::test_scenario; + let owner = @0xCAFE; + let mut scenario = test_scenario::begin(owner); + // Create a site. + { + let site = walrus_site::site::new_site( + b"Example".to_string(), scenario.ctx() + ); + transfer::public_transfer(site, owner) + }; + + // Rename site and add a resource with headers to the site. + scenario.next_tx(owner); + { + let mut site = scenario.take_from_sender(); + // Update the site name. + walrus_site::site::update_name(&mut site, b"Fancy Example".to_string()); + // Create a resource. + let mut resource = walrus_site::site::new_resource( + b"index.html".to_string(), + 601749199, + 124794210, + option::none() + ); + // Add a header to the resource. + walrus_site::site::add_header( + &mut resource, + b"Content-Type".to_string(), + b"text/html; charset=utf-8".to_string(), + ); + // Add the resource to the site. + walrus_site::site::add_resource(&mut site, resource); + // Move the resource to a different path. + walrus_site::site::move_resource( + &mut site, + b"index.html".to_string(), + b"styles.css".to_string(), + ); + // Delete the resource. + walrus_site::site::remove_resource( + &mut site, b"styles.css".to_string() + ); + scenario.return_to_sender(site); + }; + + // Create a route, add it to the site. + scenario.next_tx(owner); + { + let mut site = scenario.take_from_sender(); + // Create the routes DF. + walrus_site::site::create_routes(&mut site); + + // Create a resource and add it to the site. + // This is needed for the insert_route to work, + // since objects from previous transactions are lost. + let resource = walrus_site::site::new_resource( + b"index.html".to_string(), + 601749199, + 124794210, + option::none() + ); + walrus_site::site::add_resource(&mut site, resource); + + // Add some rerouting pairs. + walrus_site::site::insert_route( + &mut site, + b"/path1".to_string(), + b"index.html".to_string() + ); + walrus_site::site::insert_route( + &mut site, + b"/path2".to_string(), + b"index.html".to_string() + ); + // Delete the last route. + walrus_site::site::remove_route( + &mut site, + &b"/path2".to_string(), + ); + + // Remove all routes. + walrus_site::site::remove_all_routes_if_exist(&mut site); + scenario.return_to_sender(site); + }; + + // Burn the site. + scenario.next_tx(owner); + { + let site = scenario.take_from_sender(); + walrus_site::site::burn(site); + }; + scenario.end(); + } + + }