diff --git a/Cargo.toml b/Cargo.toml index 217b730..ece18b2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,3 @@ debug = false panic = "abort" # Opt into extra safety checks on arithmetic operations https://stackoverflow.com/a/64136471/249801 overflow-checks = true - -[workspace] -members = [] diff --git a/ava.config.cjs b/ava.config.cjs index a2c69b9..c488d9d 100644 --- a/ava.config.cjs +++ b/ava.config.cjs @@ -1,13 +1,9 @@ -require('util').inspect.defaultOptions.depth = 5; // Increase AVA's printing depth +require("util").inspect.defaultOptions.depth = 5; // Increase AVA's printing depth module.exports = { - timeout: '300000', - files: ['**/*.ava.ts'], + timeout: "300000", + files: ["**/*.ava.ts"], failWithoutAssertions: false, - extensions: [ - 'ts' - ], - require: [ - 'ts-node/register', - ], -}; \ No newline at end of file + extensions: ["ts"], + require: ["ts-node/register"], +}; diff --git a/integration-tests/js/test.js b/integration-tests/js/test.js deleted file mode 100644 index 5510f72..0000000 --- a/integration-tests/js/test.js +++ /dev/null @@ -1,126 +0,0 @@ -const nearAPI = require("near-api-js"); -const BN = require("bn.js"); -const fs = require("fs").promises; -const assert = require("assert").strict; - -function getConfig(env) { - switch (env) { - case "sandbox": - case "local": - return { - networkId: "sandbox", - nodeUrl: "http://localhost:3030", - masterAccount: "test.near", - contractAccount: "status-message.test.near", - keyPath: "/tmp/near-sandbox/validator_key.json", - }; - } -} - -const contractMethods = { - viewMethods: ["get_status"], - changeMethods: ["set_status"], -}; -let config; -let masterAccount; -let masterKey; -let pubKey; -let keyStore; -let near; - -async function initNear() { - config = getConfig(process.env.NEAR_ENV || "sandbox"); - const keyFile = require(config.keyPath); - masterKey = nearAPI.utils.KeyPair.fromString( - keyFile.secret_key || keyFile.private_key - ); - pubKey = masterKey.getPublicKey(); - keyStore = new nearAPI.keyStores.InMemoryKeyStore(); - keyStore.setKey(config.networkId, config.masterAccount, masterKey); - near = await nearAPI.connect({ - deps: { - keyStore, - }, - networkId: config.networkId, - nodeUrl: config.nodeUrl, - }); - masterAccount = new nearAPI.Account(near.connection, config.masterAccount); - console.log("Finish init NEAR"); -} - -async function createContractUser( - accountPrefix, - contractAccountId, - contractMethods -) { - let accountId = accountPrefix + "." + config.masterAccount; - await masterAccount.createAccount( - accountId, - pubKey, - new BN(10).pow(new BN(25)) - ); - keyStore.setKey(config.networkId, accountId, masterKey); - const account = new nearAPI.Account(near.connection, accountId); - const accountUseContract = new nearAPI.Contract( - account, - contractAccountId, - contractMethods - ); - return accountUseContract; -} - -async function initTest() { - const contract = await fs.readFile("./res/status_message.wasm"); - const _contractAccount = await masterAccount.createAndDeployContract( - config.contractAccount, - pubKey, - contract, - new BN(10).pow(new BN(25)) - ); - - const aliceUseContract = await createContractUser( - "alice", - config.contractAccount, - contractMethods - ); - - const bobUseContract = await createContractUser( - "bob", - config.contractAccount, - contractMethods - ); - console.log("Finish deploy contracts and create test accounts"); - return { aliceUseContract, bobUseContract }; -} - -async function test() { - // 1. Creates testing accounts and deploys a contract - await initNear(); - const { aliceUseContract, bobUseContract } = await initTest(); - - // 2. Performs a `set_status` transaction signed by Alice and then calls `get_status` to confirm `set_status` worked - await aliceUseContract.set_status({ args: { message: "hello" } }); - let alice_message = await aliceUseContract.get_status({ - account_id: "alice.test.near", - }); - assert.equal(alice_message, "hello"); - - // 3. Gets Bob's status and which should be `null` as Bob has not yet set status - let bob_message = await bobUseContract.get_status({ - account_id: "bob.test.near", - }); - assert.equal(bob_message, null); - - // 4. Performs a `set_status` transaction signed by Bob and then calls `get_status` to show Bob's changed status and should not affect Alice's status - await bobUseContract.set_status({ args: { message: "world" } }); - bob_message = await bobUseContract.get_status({ - account_id: "bob.test.near", - }); - assert.equal(bob_message, "world"); - alice_message = await aliceUseContract.get_status({ - account_id: "alice.test.near", - }); - assert.equal(alice_message, "hello"); -} - -test(); diff --git a/integration-tests/rs/Cargo.toml b/integration-tests/rs/Cargo.toml new file mode 100644 index 0000000..94d0c21 --- /dev/null +++ b/integration-tests/rs/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "rust-status-message-integration-tests" +version = "1.0.0" +publish = false +edition = "2018" + +[dev-dependencies] +anyhow = "1.0" +borsh = "0.9" +maplit = "1.0" +near-units = "0.2.0" +# arbitrary_precision enabled for u128 types that workspaces requires for Balance types +serde_json = { version = "1.0", features = ["arbitrary_precision"] } +tokio = { version = "1.18.1", features = ["full"] } +tracing = "0.1" +tracing-subscriber = { version = "0.3.11", features = ["env-filter"] } +workspaces = "0.2.1" +pkg-config = "0.3.1" + +[[example]] +name = "set" +path = "src/set.rs" diff --git a/integration-tests/rs/src/set.rs b/integration-tests/rs/src/set.rs new file mode 100644 index 0000000..b778dec --- /dev/null +++ b/integration-tests/rs/src/set.rs @@ -0,0 +1,39 @@ +use serde_json::json; +use near_units::parse_near; +use workspaces::prelude::*; + +const WASM_FILEPATH: &str = "../../res/status_message.wasm"; + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + let worker = workspaces::sandbox().await?; + let wasm = std::fs::read(WASM_FILEPATH)?; + let contract = worker.dev_deploy(&wasm).await?; + + // create accounts + let owner = worker.root_account(); + let alice = owner + .create_subaccount(&worker, "alice") + .initial_balance(parse_near!("30 N")) + .transact() + .await? + .into_result()?; + + // begin test + alice + .call(&worker, contract.id(), "set_status") + .args_json(json!({ "message": "hello" }))? + .transact() + .await?; + + let alice_status: String = owner + .call(&worker, contract.id(), "get_status") + .args_json(json!({ "account_id": alice.id() }))? + .transact() + .await? + .json()?; + + assert_eq!(alice_status, "hello"); + println!("Passed ✅"); + Ok(()) +} \ No newline at end of file diff --git a/integration-tests/js/main.ava.ts b/integration-tests/ts/main.ava.ts similarity index 100% rename from integration-tests/js/main.ava.ts rename to integration-tests/ts/main.ava.ts diff --git a/package.json b/package.json index 246b65e..5d31cb1 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "test": "npm run test:unit && npm run test:integration", "test:unit": "cargo test -- --nocapture", "test:integration": "npm run build && npm run postbuild && npm run test:integration:rs && npm run test:integration:ts", - "test:integration:rs": "cd integration-tests/rs && cargo run --example ", + "test:integration:rs": "cd integration-tests/rs && cargo run --example set", "test:integration:ts": "ava --verbose" }, "repository": {