Skip to content

Commit

Permalink
chore: a test for interactionType check
Browse files Browse the repository at this point in the history
  • Loading branch information
ppedziwiatr committed Dec 27, 2023
1 parent 5015a3f commit 01d69d8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/__tests__/integration/basic/pst.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,20 @@ describe('Testing the Profit Sharing Token', () => {
expect(resultVM.target).toEqual('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M');
});

// note that this requires a check inside contract code!
it('should not eval interaction that calls view function', async () => {
const interaction = await pst.writeInteraction({
function: 'balance'
});

await mineBlock(warp);

const result = await pst.readState();

expect(result.cachedValue.validity[interaction.originalTxId]).toBeFalsy();
expect(result.cachedValue.errorMessages[interaction.originalTxId]).toContain('Trying to call "view" function from a "write" action');
});

it('should properly dispatch an event', async () => {
let handlerCalled = false;
const interactionResult = await pst.writeInteraction({
Expand Down
5 changes: 5 additions & 0 deletions src/__tests__/integration/data/token-pst.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export async function handle(state, action) {
const canEvolve = state.canEvolve;
const input = action.input;
const caller = action.caller;
const interactionType = action.interactionType;

if (input.function === 'transfer') {
const target = input.target;
Expand Down Expand Up @@ -66,6 +67,10 @@ export async function handle(state, action) {
}

if (input.function === 'balance') {
if (interactionType !== 'view') {
throw new ContractError('Trying to call "view" function from a "write" action.');
}

const target = input.target;
const ticker = state.ticker;

Expand Down

0 comments on commit 01d69d8

Please sign in to comment.