Skip to content

Commit

Permalink
Added tests for transactions.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Dennis Garcia committed Nov 4, 2024
1 parent b2ef70d commit 3570c3a
Show file tree
Hide file tree
Showing 4 changed files with 590 additions and 515 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 0.6.0 (TBD)

* Added Transaction Integration Tests for Web Client (#569).
* Moved note update logic outside of the `Store` (#559)
* Added delegated proving for web client + improved note models (#566).
* Allow to set expiration delta for `TransactionRequest` (#553).
Expand Down
148 changes: 148 additions & 0 deletions crates/web-client/test/transactions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { expect } from "chai";
import { testingPage } from "./mocha.global.setup.mjs";
import {
consumeTransaction,
mintTransaction,
setupWalletAndFaucet,
} from "./webClientTestUtils";

// GET_TRANSACTIONS TESTS
// =======================================================================================================

interface GetAllTransactionsResult {
transactionIds: string[];
uncomittedTransactionIds: string[];
}

const getAllTransactions = async (): Promise<GetAllTransactionsResult> => {
return await testingPage.evaluate(async () => {
const client = window.client;

let transactions = await client.get_transactions(
window.TransactionFilter.all()
);
let uncomittedTransactions = await client.get_transactions(
window.TransactionFilter.uncomitted()
);
let transactionIds = transactions.map((transaction) =>
transaction.id().to_hex()
);
let uncomittedTransactionIds = uncomittedTransactions.map((transaction) =>
transaction.id().to_hex()
);

return {
transactionIds: transactionIds,
uncomittedTransactionIds: uncomittedTransactionIds,
};
});
};

describe("get_transactions tests", () => {
it("get_transactions retrieves all transactions successfully", async () => {
const { accountId, faucetId } = await setupWalletAndFaucet();
const { transactionId: mintTransactionId, createdNoteId } =
await mintTransaction(accountId, faucetId);
const { transactionId: consumeTransactionId } = await consumeTransaction(
accountId,
faucetId,
createdNoteId
);

const result = await getAllTransactions();

expect(result.transactionIds).to.include(mintTransactionId);
expect(result.transactionIds).to.include(consumeTransactionId);
expect(result.uncomittedTransactionIds.length).to.equal(0);
});

it("get_transactions retrieves uncommitted transactions successfully", async () => {
const { accountId, faucetId } = await setupWalletAndFaucet();
const { transactionId: mintTransactionId } = await mintTransaction(
accountId,
faucetId,
false
);

const result = await getAllTransactions();

expect(result.transactionIds).to.include(mintTransactionId);
expect(result.uncomittedTransactionIds).to.include(mintTransactionId);
expect(result.transactionIds.length).to.equal(
result.uncomittedTransactionIds.length
);
});

it("get_transactions retrieves no transactions successfully", async () => {
const result = await getAllTransactions();

expect(result.transactionIds.length).to.equal(0);
expect(result.uncomittedTransactionIds.length).to.equal(0);
});
});

// COMPILE_TX_SCRIPT TESTS
// =======================================================================================================

interface CompileTxScriptResult {
scriptHash: string;
}

export const compileTxScript = async (
script: string
): Promise<CompileTxScriptResult> => {
return await testingPage.evaluate(async (_script) => {
const client = window.client;

let walletAccount = await client.new_wallet(
window.AccountStorageMode.private(),
true
);

let account_auth = await client.get_account_auth(walletAccount.id());
let public_key = account_auth.get_rpo_falcon_512_public_key_as_word();
let secret_key = account_auth.get_rpo_falcon_512_secret_key_as_felts();
let transcription_script_input_pair_array =
new window.TransactionScriptInputPairArray([
new window.TransactionScriptInputPair(public_key, secret_key),
]);

const compiledScript = await client.compile_tx_script(
_script,
transcription_script_input_pair_array
);

return {
scriptHash: compiledScript.hash().to_hex(),
};
}, script);
};

describe("compile_tx_script tests", () => {
it("compile_tx_script compiles script successfully", async () => {
const script = `
use.miden::contracts::auth::basic->auth_tx
use.miden::kernels::tx::prologue
use.miden::kernels::tx::memory
begin
push.0 push.0
# => [0, 0]
assert_eq
call.auth_tx::auth_tx_rpo_falcon512
end
`;
const result = await compileTxScript(script);

expect(result.scriptHash).to.not.be.empty;
});

it("compile_tx_script does not compile script successfully", async () => {
const script = "fakeScript";

await expect(compileTxScript(script)).to.be.rejectedWith(
`Failed to compile transaction script: Transaction script error: AssemblyError("invalid syntax")`
);
});
});
14 changes: 9 additions & 5 deletions crates/web-client/test/webClientTestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ interface MintTransactionResult {

export const mintTransaction = async (
targetAccountId: string,
faucetAccountId: string
faucetAccountId: string,
sync: boolean = true
): Promise<MintTransactionResult> => {
return await testingPage.evaluate(
async (_targetAccountId, _faucetAccountId) => {
async (_targetAccountId, _faucetAccountId, _sync) => {
const client = window.client;

await new Promise((r) => setTimeout(r, 20000));
Expand All @@ -30,8 +31,10 @@ export const mintTransaction = async (
BigInt(1000)
);

await new Promise((r) => setTimeout(r, 20000)); // TODO: Replace this with loop of sync -> check uncommitted transactions -> sleep
await client.sync_state();
if (_sync) {
await new Promise((r) => setTimeout(r, 20000)); // TODO: Replace this with loop of sync -> check uncommitted transactions -> sleep
await client.sync_state();
}

return {
transactionId: new_mint_transaction_result
Expand All @@ -50,7 +53,8 @@ export const mintTransaction = async (
};
},
targetAccountId,
faucetAccountId
faucetAccountId,
sync
);
};

Expand Down
Loading

0 comments on commit 3570c3a

Please sign in to comment.