From b86104dca5d4743da9e748260c46a99d395fe9ce Mon Sep 17 00:00:00 2001 From: Nikos Baxevanis Date: Mon, 18 Mar 2024 01:24:26 +0100 Subject: [PATCH] test(pox-4): Add GetStxAccountCommand --- .../tests/pox-4/pox_Commands.ts | 13 +++++ .../tests/pox-4/pox_GetStxAccountCommand.ts | 57 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 contrib/core-contract-tests/tests/pox-4/pox_GetStxAccountCommand.ts diff --git a/contrib/core-contract-tests/tests/pox-4/pox_Commands.ts b/contrib/core-contract-tests/tests/pox-4/pox_Commands.ts index aeb44ba375..c7222666f3 100644 --- a/contrib/core-contract-tests/tests/pox-4/pox_Commands.ts +++ b/contrib/core-contract-tests/tests/pox-4/pox_Commands.ts @@ -1,6 +1,7 @@ import fc from "fast-check"; import { Real, Stub, StxAddress, Wallet } from "./pox_CommandModel"; import { GetStackingMinimumCommand } from "./pox_GetStackingMinimumCommand"; +import { GetStxAccountCommand } from "./pox_GetStxAccountCommand"; import { StackStxCommand } from "./pox_StackStxCommand"; export function PoxCommands( @@ -40,6 +41,18 @@ export function PoxCommands( r.margin, ) ), + // GetStxAccountCommand + fc.record({ + wallet: fc.constantFrom(...wallets.values()), + }).map(( + r: { + wallet: Wallet; + }, + ) => + new GetStxAccountCommand( + r.wallet, + ) + ), ]; // More on size: https://github.com/dubzzz/fast-check/discussions/2978 diff --git a/contrib/core-contract-tests/tests/pox-4/pox_GetStxAccountCommand.ts b/contrib/core-contract-tests/tests/pox-4/pox_GetStxAccountCommand.ts new file mode 100644 index 0000000000..780f363417 --- /dev/null +++ b/contrib/core-contract-tests/tests/pox-4/pox_GetStxAccountCommand.ts @@ -0,0 +1,57 @@ +import { PoxCommand, Real, Stub, Wallet } from "./pox_CommandModel.ts"; +import { expect } from "vitest"; +import { Cl } from "@stacks/transactions"; + +/** + * Implements the `PoxCommand` interface to get the info returned from the + * `stx-account`. + */ +export class GetStxAccountCommand implements PoxCommand { + readonly wallet: Wallet; + + /** + * Constructs a new `GetStxAccountCommand`. + * + * @param wallet The wallet information, including the STX address used to + * query the `stx-account`. + */ + constructor(wallet: Wallet) { + this.wallet = wallet; + } + + check(_model: Readonly): boolean { + // Can always check the `stx-account` info. + return true; + } + + run(model: Stub, real: Real): void { + const actual = model.wallets.get(this.wallet.stxAddress)!; + expect(real.network.runSnippet(`(stx-account '${actual.stxAddress})`)) + .toBeTuple({ + "locked": Cl.uint(actual.amountLocked), + "unlocked": Cl.uint(actual.ustxBalance), + "unlock-height": Cl.uint(actual.unlockHeight), + }); + + // Log to console for debugging purposes. This is not necessary for the + // test to pass but it is useful for debugging and eyeballing the test. + console.info( + `✓ ${this.wallet.stxAddress.padStart(8, " ")} ${ + "stx-account".padStart(34, " ") + } ${"lock-amount".padStart(12, " ")} ${ + actual.amountLocked.toString().padStart(13, " ") + } ${"unlocked-amount".padStart(12, " ")} ${ + actual.ustxBalance.toString().padStart(15, " ") + } ${"unlocked-height".padStart(12, " ")} ${ + actual.unlockHeight.toString().padStart(7, " ") + }`, + ); + } + + toString() { + // fast-check will call toString() in case of errors, e.g. property failed. + // It will then make a minimal counterexample, a process called 'shrinking' + // https://github.com/dubzzz/fast-check/issues/2864#issuecomment-1098002642 + return `${this.wallet.stxAddress} stx-account`; + } +}