Skip to content

Commit

Permalink
fix: fix tests to work with AVA
Browse files Browse the repository at this point in the history
  • Loading branch information
katelynsills committed Aug 25, 2020
1 parent 5802914 commit 24afea7
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 73 deletions.
8 changes: 6 additions & 2 deletions packages/zoe/src/contractFacet/contractFacet.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,12 @@ export function buildRootObject() {

// First, evaluate the contract code bundle.
const contractCode = evalContractBundle(bundle);
// Don't trigger Node.js's UnhandledPromiseRejectionWarning
contractCode.catch(() => {});

// Next, execute the contract code, passing in zcf
/** @type {Promise<Invitation>} */
return E(contractCode)
/** @type {Promise<ExecuteContractResult>} */
const result = E(contractCode)
.start(zcf)
.then(({ creatorFacet, publicFacet, creatorInvitation }) => {
return harden({
Expand All @@ -368,6 +370,8 @@ export function buildRootObject() {
addSeatObj,
});
});
result.catch(() => {}); // Don't trigger Node.js's UnhandledPromiseRejectionWarning
return result;
};

return harden({ executeContract });
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/src/contractFacet/evalContractCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const evalContractBundle = (bundle, additionalEndowments = {}) => {

const installation = importBundle(bundle, {
endowments: fullEndowments,
});
}).catch(() => {}); // Don't trigger Node.js's UnhandledPromiseRejectionWarning
return installation;
};

Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/src/internal-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
* @typedef {Object} ZoeSeatAdmin
* @property {(allocation: Allocation) => void} replaceAllocation
* @property {() => void} exit
* @property {(reason: any) => never} kickOut called with the reason this seat
* @property {(reason: any) => void} kickOut called with the reason this seat
* is being kicked out, where reason is normally an instanceof Error.
* @property {() => Allocation} getCurrentAllocation
*/
Expand Down Expand Up @@ -173,7 +173,7 @@
* @param {Issuer} invitationIssuer
* @param {ZoeInstanceAdmin} zoeInstanceAdmin
* @param {InstanceRecord} instanceRecord
* @returns {ExecuteContractResult}
* @returns {Promise<ExecuteContractResult>}
*
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const meterExceededInSecondOfferLog = [
'counter: 2',
];

test('ZCF metering crash on invitation exercise', async t => {
test('ZCF metering crash on second invitation', async t => {
const dump = await main(['meterInSecondInvitation', [8, 0, 0]]);
t.deepEqual(dump.log, meterExceededInSecondOfferLog);
});
Expand Down Expand Up @@ -146,7 +146,7 @@ const thrownExceptionInMakeContractILog = [
'newCounter: 2',
];

test('ZCF metering crash in makeContract call', async t => {
test('throw in makeContract call', async t => {
const dump = await main(['throwInMakeContract', [3, 0, 0]]);
t.deepEqual(dump.log, thrownExceptionInMakeContractILog);
});
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ test('getInputPrice ok 6', t => {
testGetPrice(t, input, expectedOutput);
});

test('calculate value to mint - positive supply', t => {
test('calculate value to mint - positive supply 1', t => {
const res = calcLiqValueToMint({
liqTokenSupply: 20,
inputValue: 30,
Expand All @@ -103,11 +103,14 @@ test('calculate value to mint - mispelled key', t => {
inputValue: 30,
inputReserve: 5,
}),
{
message: /value required/,
},
`calcLiqValueToMint should throw if a key is misspelled`,
);
});

test('calculate value to mint - positive supply', t => {
test('calculate value to mint - positive supply 2', t => {
const res = calcLiqValueToMint({
liqTokenSupply: 5,
inputValue: 8,
Expand Down
97 changes: 56 additions & 41 deletions packages/zoe/test/unitTests/contracts/test-escrowToVote.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,27 @@ test('zoe - escrowToVote', async t => {
const result = await E(voter).vote('YES');

t.is(result, `Successfully voted 'YES'`, `voter1 votes YES`);
return { voter, seat };
};

seat.getPayout('Assets').then(async moolaPayment => {
t.deepEqual(
await moolaIssuer.getAmountOf(moolaPayment),
moola(3),
`voter1 gets everything she escrowed back`,
);

console.log('EXPECTED ERROR ->>>');
t.throws(
() => voter.vote('NO'),
{ message: /the voter seat has exited/ },
`voter1 voting fails once offer is withdrawn or amounts are reallocated`,
);
});
const voter1CollectsPayout = async ({ voter, seat }) => {
const moolaPayment = await seat.getPayout('Assets');
t.deepEqual(
await moolaIssuer.getAmountOf(moolaPayment),
moola(3),
`voter1 gets everything she escrowed back`,
);

console.log('EXPECTED ERROR ->>>');
t.throws(
() => voter.vote('NO'),
{ message: /the voter seat has exited/ },
`voter1 voting fails once offer is withdrawn or amounts are reallocated`,
);
};

await voter1Votes(voterInvitation1);
const voter1Result = await voter1Votes(voterInvitation1);
const voter1DoneP = voter1CollectsPayout(voter1Result);

// Voter 2 makes badly formed vote, then votes YES, then changes
// vote to NO. Vote will be weighted by 5 (5 moola escrowed).
Expand All @@ -106,24 +109,27 @@ test('zoe - escrowToVote', async t => {
// Votes can be recast at any time
const result2 = await E(voter).vote('NO');
t.is(result2, `Successfully voted 'NO'`, `voter 2 recast vote for NO`);
return { voter, seat };
};

seat.getPayout('Assets').then(async moolaPayment => {
t.deepEqual(
await moolaIssuer.getAmountOf(moolaPayment),
moola(5),
`voter2 gets everything she escrowed back`,
);

console.log('EXPECTED ERROR ->>>');
t.throws(
() => voter.vote('NO'),
{ message: /the voter seat has exited/ },
`voter2 voting fails once offer is withdrawn or amounts are reallocated`,
);
});
const voter2CollectsPayout = async ({ voter, seat }) => {
const moolaPayment = await seat.getPayout('Assets');
t.deepEqual(
await moolaIssuer.getAmountOf(moolaPayment),
moola(5),
`voter2 gets everything she escrowed back`,
);

console.log('EXPECTED ERROR ->>>');
t.throws(
() => voter.vote('NO'),
{ message: /the voter seat has exited/ },
`voter2 voting fails once offer is withdrawn or amounts are reallocated`,
);
};

await voter2Votes(voterInvitation2);
const voter2Result = await voter2Votes(voterInvitation2);
const voter2DoneP = voter2CollectsPayout(voter2Result);

// Voter 3 votes NO and then exits the seat, retrieving their
// assets, meaning that their vote should not be counted. They get
Expand All @@ -145,6 +151,10 @@ test('zoe - escrowToVote', async t => {
// not be counted.
seat.tryExit();

return { voter, seat };
};

const voter3CollectsPayout = async ({ voter, seat }) => {
const moolaPayment = await seat.getPayout('Assets');

t.deepEqual(
Expand All @@ -161,7 +171,8 @@ test('zoe - escrowToVote', async t => {
);
};

await voter3Votes(voterInvitation3);
const voter3Result = await voter3Votes(voterInvitation3);
const voter3DoneP = voter3CollectsPayout(voter3Result);

// Voter4 votes YES with a weight of 4
const voter4Votes = async invitation => {
Expand All @@ -178,22 +189,26 @@ test('zoe - escrowToVote', async t => {

t.is(result, `Successfully voted 'YES'`, `voter1 votes YES`);

seat.getPayout('Assets').then(async moolaPayment => {
t.deepEqual(
await moolaIssuer.getAmountOf(moolaPayment),
moola(4),
`voter4 gets everything she escrowed back`,
);
});
return seat;
};

const voter4CollectsPayout = async seat => {
const moolaPayment = seat.getPayout('Assets');
t.deepEqual(
await moolaIssuer.getAmountOf(moolaPayment),
moola(4),
`voter4 gets everything she escrowed back`,
);
};

await voter4Votes(voterInvitation4);
const voter4Seat = await voter4Votes(voterInvitation4);
const voter4DoneP = voter4CollectsPayout(voter4Seat);

// Secretary closes election and tallies the votes.
const electionResults = await E(secretary).closeElection();
t.deepEqual(electionResults, { YES: moola(7), NO: moola(5) });

// Once the election is closed, the voters get their escrowed funds
// back and can no longer vote. See the voter functions for the
// resolution of the payout promises for each voter.
// back and can no longer vote.
await Promise.all([voter1DoneP, voter2DoneP, voter3DoneP, voter4DoneP]);
});
56 changes: 33 additions & 23 deletions packages/zoe/test/unitTests/contracts/test-secondPriceAuction.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ test('zoe - secondPriceAuction w/ 3 bids', async t => {

const seat = await E(zoe).offer(sellInvitation, proposal, payments);

E(seat)
const makeBidInvitationObj = await E(seat).getOfferResult();
return { seat, makeBidInvitationObj };
},
collectPayout: async seat => {
await E(seat)
.getPayout('Asset')
.then(moolaPurse.deposit)
.then(amountDeposited =>
Expand All @@ -64,7 +68,7 @@ test('zoe - secondPriceAuction w/ 3 bids', async t => {
),
);

E(seat)
await E(seat)
.getPayout('Ask')
.then(simoleanPurse.deposit)
.then(amountDeposited =>
Expand All @@ -74,9 +78,6 @@ test('zoe - secondPriceAuction w/ 3 bids', async t => {
`Alice got the second price bid, Carol's bid, even though Bob won`,
),
);

const makeBidInvitationObj = await E(seat).getOfferResult();
return makeBidInvitationObj;
},
};
};
Expand Down Expand Up @@ -129,19 +130,17 @@ test('zoe - secondPriceAuction w/ 3 bids', async t => {
await E(seat).getOfferResult(),
'The offer has been accepted. Once the contract has been completed, please check your payout',
);

E(seat)
return seat;
},
collectPayout: async seat => {
await E(seat)
.getPayout('Asset')
.then(moolaPurse.deposit)
.then(amountDeposited =>
t.deepEqual(
amountDeposited,
proposal.want.Asset,
`Bob wins the auction`,
),
t.deepEqual(amountDeposited, moola(1), `Bob wins the auction`),
);

E(seat)
await E(seat)
.getPayout('Bid')
.then(simoleanPurse.deposit)
.then(amountDeposited =>
Expand Down Expand Up @@ -175,15 +174,17 @@ test('zoe - secondPriceAuction w/ 3 bids', async t => {
await E(seat).getOfferResult(),
'The offer has been accepted. Once the contract has been completed, please check your payout',
);

E(seat)
return seat;
},
collectPayout: async seat => {
await E(seat)
.getPayout('Asset')
.then(moolaPurse.deposit)
.then(amountDeposited =>
t.deepEqual(amountDeposited, moola(0), `didn't win the auction`),
);

E(seat)
await E(seat)
.getPayout('Bid')
.then(simoleanPurse.deposit)
.then(amountDeposited =>
Expand Down Expand Up @@ -214,16 +215,25 @@ test('zoe - secondPriceAuction w/ 3 bids', async t => {

const { creatorInvitation } = await alice.startInstance(installation);

const makeInvitationsObj = await alice.offer(creatorInvitation);
const { seat: aliceSeat, makeBidInvitationObj } = await alice.offer(
creatorInvitation,
);

const bidInvitation1 = E(makeInvitationsObj).makeBidInvitation();
const bidInvitation2 = E(makeInvitationsObj).makeBidInvitation();
const bidInvitation3 = E(makeInvitationsObj).makeBidInvitation();
const bidInvitation1 = E(makeBidInvitationObj).makeBidInvitation();
const bidInvitation2 = E(makeBidInvitationObj).makeBidInvitation();
const bidInvitation3 = E(makeBidInvitationObj).makeBidInvitation();

await bob.offer(bidInvitation1);
await carol.offer(bidInvitation2);
await dave.offer(bidInvitation3);
const bobSeat = await bob.offer(bidInvitation1);
const carolSeat = await carol.offer(bidInvitation2);
const daveSeat = await dave.offer(bidInvitation3);
timer.tick();

await Promise.all([
alice.collectPayout(aliceSeat),
bob.collectPayout(bobSeat),
carol.collectPayout(carolSeat),
dave.collectPayout(daveSeat),
]);
});

test('zoe - secondPriceAuction - alice tries to exit', async t => {
Expand Down

0 comments on commit 24afea7

Please sign in to comment.