Skip to content

Commit

Permalink
test(pox-4): Add GetStxAccountCommand
Browse files Browse the repository at this point in the history
  • Loading branch information
moodmosaic committed Mar 18, 2024
1 parent 7b31963 commit b86104d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
13 changes: 13 additions & 0 deletions 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(
Expand Down Expand Up @@ -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
Expand Down
@@ -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<Stub>): 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`;
}
}

0 comments on commit b86104d

Please sign in to comment.