Skip to content

Commit

Permalink
improved tests + cleaned up model constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-demox committed Oct 30, 2024
1 parent bf00bf1 commit 75376d1
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 4 deletions.
1 change: 0 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

## 0.6.0 (TBD)

* Added WASM consumable notes API + improved note models (#561).
* Allow to set expiration delta for `TransactionRequest` (#553).
* Added WASM consumable notes API + improved note models (#561).
* [BREAKING] Refactored `OutputNoteRecord` to use states and transitions for updates (#551).
Expand Down
6 changes: 4 additions & 2 deletions crates/web-client/src/models/consumable_note_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ pub struct NoteConsumability {

#[wasm_bindgen]
impl NoteConsumability {
#[wasm_bindgen(constructor)]
pub fn new(account_id: AccountId, consumable_after_block: Option<u32>) -> NoteConsumability {
pub(crate) fn new(
account_id: AccountId,
consumable_after_block: Option<u32>,
) -> NoteConsumability {
NoteConsumability { account_id, consumable_after_block }
}

Expand Down
37 changes: 37 additions & 0 deletions crates/web-client/test/notes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
consumeTransaction,
fetchAndCacheAccountAuth,
mintTransaction,
sendTransaction,
setupWalletAndFaucet,
syncState,
} from "./webClientTestUtils";

const getInputNote = async (noteId: string) => {
Expand Down Expand Up @@ -49,6 +51,7 @@ const getConsumableNotes = async (accountId?: string) => {
const client = window.client;
let records;
if (_accountId) {
console.log({ _accountId });
const accountId = window.AccountId.from_hex(_accountId);
records = await client.get_consumable_notes(accountId);
} else {
Expand Down Expand Up @@ -102,6 +105,7 @@ describe("get_consumable_notes", () => {
expect(record.consumability).to.have.lengthOf(1);
expect(record.consumability[0].accountId).to.equal(accountId1);
expect(record.noteId).to.equal(noteId1);
expect(record.consumability[0].consumableAfterBlock).to.be.undefined;
});
});
it("no filter by account", async () => {
Expand All @@ -117,6 +121,39 @@ describe("get_consumable_notes", () => {
accountId2,
]);
expect(result).to.have.lengthOf(2);
const consumableRecord1 = result.find((r) => r.noteId === noteId1);
const consumableRecord2 = result.find((r) => r.noteId === noteId2);

consumableRecord1!!.consumability.forEach((c) => {
expect(c.accountId).to.equal(accountId1);
});

consumableRecord2!!.consumability.forEach((c) => {
expect(c.accountId).to.equal(accountId2);
});
});
it.only("p2idr consume after block", async () => {
const { accountId: senderAccountId, faucetId } =
await setupWalletAndFaucet();
const { accountId: targetAccountId } = await setupWalletAndFaucet();
const recallHeight = 100;
await sendTransaction(
senderAccountId,
targetAccountId,
faucetId,
100,
recallHeight
);

const consumableRecipient = await getConsumableNotes(targetAccountId);
const consumableSender = await getConsumableNotes(senderAccountId);
expect(consumableSender).to.have.lengthOf(1);
expect(consumableSender[0].consumability[0].consumableAfterBlock).to.equal(
recallHeight
);
expect(consumableRecipient).to.have.lengthOf(1);
expect(consumableRecipient[0].consumability[0].consumableAfterBlock).to.be
.undefined;
});
});

Expand Down
71 changes: 70 additions & 1 deletion crates/web-client/test/webClientTestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,72 @@ export const mintTransaction = async (
);
};

export const sendTransaction = async (
senderAccountId: string,
targetAccountId: string,
faucetAccountId: string,
amount: number,
recallHeight?: number
) => {
return testingPage.evaluate(
async (
_senderAccountId,
_targetAccountId,
_faucetAccountId,
_amount,
_recallHeight
) => {
const client = window.client;

const senderAccountId = window.AccountId.from_hex(_senderAccountId);
const targetAccountId = window.AccountId.from_hex(_targetAccountId);
const faucetAccountId = window.AccountId.from_hex(_faucetAccountId);

await client.fetch_and_cache_account_auth_by_pub_key(
window.AccountId.from_hex(_faucetAccountId)
);
let mint_transaction_result = await client.new_mint_transaction(
senderAccountId,
window.AccountId.from_hex(_faucetAccountId),
window.NoteType.private(),
BigInt(1000)
);
let created_notes = mint_transaction_result.created_notes().notes();
let created_note_ids = created_notes.map((note) => note.id().to_string());
await new Promise((r) => setTimeout(r, 20000)); // TODO: Replace this with loop of sync -> check uncommitted transactions -> sleep
await client.sync_state();

await client.fetch_and_cache_account_auth_by_pub_key(senderAccountId);
await client.new_consume_transaction(senderAccountId, created_note_ids);
await new Promise((r) => setTimeout(r, 20000)); // TODO: Replace this with loop of sync -> check uncommitted transactions -> sleep
await client.sync_state();

await client.fetch_and_cache_account_auth_by_pub_key(senderAccountId);
let send_transaction_result = await client.new_send_transaction(
senderAccountId,
targetAccountId,
faucetAccountId,
window.NoteType.public(),
BigInt(_amount),
_recallHeight
);
let send_created_notes = send_transaction_result.created_notes().notes();
let send_created_note_ids = send_created_notes.map((note) =>
note.id().to_string()
);
await new Promise((r) => setTimeout(r, 20000)); // TODO: Replace this with loop of sync -> check uncommitted transactions -> sleep
await client.sync_state();

return send_created_note_ids;
},
senderAccountId,
targetAccountId,
faucetAccountId,
amount,
recallHeight
);
};

interface ConsumeTransactionResult {
transactionId: string;
nonce: string | undefined;
Expand Down Expand Up @@ -145,7 +211,10 @@ export const fetchAndCacheAccountAuth = async (accountId: string) => {
export const syncState = async () => {
return await testingPage.evaluate(async () => {
const client = window.client;
await client.sync_state();
const summary = await client.sync_state();
return {
blockNum: summary.block_num(),
};
});
};

Expand Down

0 comments on commit 75376d1

Please sign in to comment.