From 97da1794902089c06b095f9fb4d4e91f60626c4a Mon Sep 17 00:00:00 2001 From: "Mark S. Miller" Date: Thu, 11 Aug 2022 20:55:23 -0700 Subject: [PATCH] fix: tests bigger than 1 --- packages/zoe/src/contractFacet/offerSafety.js | 21 ++++++----- .../zoe/src/contractSupport/zoeHelpers.js | 4 +-- packages/zoe/src/zoeService/zoeSeat.js | 4 +-- .../zoe/test/unitTests/test-offerSafety.js | 36 +++++++++++++------ packages/zoe/test/unitTests/zcf/test-zcf.js | 2 +- 5 files changed, 42 insertions(+), 25 deletions(-) diff --git a/packages/zoe/src/contractFacet/offerSafety.js b/packages/zoe/src/contractFacet/offerSafety.js index 4c9324408fa9..3c3dbead8a36 100644 --- a/packages/zoe/src/contractFacet/offerSafety.js +++ b/packages/zoe/src/contractFacet/offerSafety.js @@ -7,11 +7,11 @@ const { details: X } = assert; const { entries } = Object; /** - * Helper to perform satisfiesWant and satisfiesGive. How many times + * Helper to perform numWantsSatisfied and satisfiesGive. How many times * does the `allocation` satisfy the `giveOrWant`? * - * To prepare for multiples, satisfiesWant and satisfiesGive return 0 or 1. - * isOfferSafe will still be boolean. When we have Multiples, satisfiesWant and + * To prepare for multiples, numWantsSatisfied and satisfiesGive return 0 or 1. + * isOfferSafe will still be boolean. When we have Multiples, numWantsSatisfied and * satisfiesGive will tell how many times the offer was matched. * * @param {AmountKeywordRecord} giveOrWant @@ -19,7 +19,7 @@ const { entries } = Object; * @returns {number} If the giveOrWant is empty, then any allocation satisfies * it an `Infinity` number of times. */ -const satisfiesInternal = (giveOrWant = {}, allocation) => { +const numSatisfied = (giveOrWant = {}, allocation) => { let multiples = Infinity; for (const [keyword, requiredAmount] of entries(giveOrWant)) { if (allocation[keyword] === undefined) { @@ -64,11 +64,12 @@ const satisfiesInternal = (giveOrWant = {}, allocation) => { * @param {AmountKeywordRecord} allocation - a record with keywords * as keys and amounts as values. These amounts are the reallocation * to be given to a user. - * @returns {number} + * @returns {number} If the want is empty, then any allocation satisfies + * it an `Infinity` number of times. */ -export const satisfiesWant = (proposal, allocation) => - satisfiesInternal(proposal.want, allocation); -harden(satisfiesWant); +export const numWantsSatisfied = (proposal, allocation) => + numSatisfied(proposal.want, allocation); +harden(numWantsSatisfied); /** * For this allocation to count as a full refund, the allocated @@ -84,6 +85,8 @@ harden(satisfiesWant); * @param {AmountKeywordRecord} allocation - a record with keywords * as keys and amounts as values. These amounts are the reallocation * to be given to a user. + * @returns {number} If the give is empty, then any allocation satisfies + * it an `Infinity` number of times. */ // Commented out because not currently used // const satisfiesGive = (proposal, allocation) => @@ -109,7 +112,7 @@ harden(satisfiesWant); export const isOfferSafe = (proposal, allocation) => { const { give, want, multiples } = proposal; const howMany = - satisfiesInternal(give, allocation) + satisfiesInternal(want, allocation); + numSatisfied(give, allocation) + numSatisfied(want, allocation); return howMany >= multiples; }; harden(isOfferSafe); diff --git a/packages/zoe/src/contractSupport/zoeHelpers.js b/packages/zoe/src/contractSupport/zoeHelpers.js index fb8147ade923..5033232c18fc 100644 --- a/packages/zoe/src/contractSupport/zoeHelpers.js +++ b/packages/zoe/src/contractSupport/zoeHelpers.js @@ -5,7 +5,7 @@ import { keyEQ, fit } from '@agoric/store'; import { E } from '@endo/eventual-send'; import { makePromiseKit } from '@endo/promise-kit'; import { AssetKind } from '@agoric/ertp'; -import { satisfiesWant } from '../contractFacet/offerSafety.js'; +import { numWantsSatisfied } from '../contractFacet/offerSafety.js'; export const defaultAcceptanceMsg = `The offer has been accepted. Once the contract has been completed, please check your payout`; @@ -55,7 +55,7 @@ export const satisfies = (zcf, seat, update) => { const currentAllocation = seat.getCurrentAllocation(); const newAllocation = { ...currentAllocation, ...update }; const proposal = seat.getProposal(); - return satisfiesWant(proposal, newAllocation); + return numWantsSatisfied(proposal, newAllocation); }; /** @type {Swap} */ diff --git a/packages/zoe/src/zoeService/zoeSeat.js b/packages/zoe/src/zoeService/zoeSeat.js index 7380f3225d5f..aa9e12dd5f01 100644 --- a/packages/zoe/src/zoeService/zoeSeat.js +++ b/packages/zoe/src/zoeService/zoeSeat.js @@ -6,7 +6,7 @@ import { E } from '@endo/eventual-send'; import { Far } from '@endo/marshal'; import { handlePKitWarning } from '../handleWarning.js'; -import { satisfiesWant } from '../contractFacet/offerSafety.js'; +import { numWantsSatisfied as numWantsSatisfied } from '../contractFacet/offerSafety.js'; import '../types.js'; import '../internal-types.js'; @@ -99,7 +99,7 @@ export const makeZoeSeatAdminKit = ( numWantsSatisfied: async () => { return E.when(payoutPromiseKit.promise, () => - satisfiesWant(proposal, currentAllocation), + numWantsSatisfied(proposal, currentAllocation), ); }, }); diff --git a/packages/zoe/test/unitTests/test-offerSafety.js b/packages/zoe/test/unitTests/test-offerSafety.js index 5edaa0f78147..1449570d49a9 100644 --- a/packages/zoe/test/unitTests/test-offerSafety.js +++ b/packages/zoe/test/unitTests/test-offerSafety.js @@ -5,7 +5,7 @@ import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js'; import { isOfferSafe, - satisfiesWant, + numWantsSatisfied, } from '../../src/contractFacet/offerSafety.js'; import { setup } from './setupBasicMints.js'; @@ -38,7 +38,21 @@ test('isOfferSafe - more than want, more than give', t => { const amounts = harden({ A: moola(10n), B: simoleans(7n), C: bucks(8n) }); t.truthy(isOfferSafe(proposal, amounts)); - t.is(satisfiesWant(proposal, amounts), 1); + t.is(numWantsSatisfied(proposal, amounts), 1); +}); + +test('isOfferSafe - much more than want, much more than give', t => { + const { moola, simoleans, bucks } = setup(); + const proposal = harden({ + give: { A: moola(8n) }, + want: { B: simoleans(6n), C: bucks(7n) }, + multiples: 1n, + exit: { waived: null }, + }); + const amounts = harden({ A: moola(100n), B: simoleans(70n), C: bucks(80n) }); + + t.truthy(isOfferSafe(proposal, amounts)); + t.is(numWantsSatisfied(proposal, amounts), 11); }); // more than want, less than give -> true @@ -53,7 +67,7 @@ test('isOfferSafe - more than want, less than give', t => { const amounts = harden({ A: moola(1n), B: simoleans(7n), C: bucks(8n) }); t.truthy(isOfferSafe(proposal, amounts)); - t.is(satisfiesWant(proposal, amounts), 1); + t.is(numWantsSatisfied(proposal, amounts), 1); }); // more than want, equal to give -> true @@ -68,7 +82,7 @@ test('isOfferSafe - more than want, equal to give', t => { const amounts = harden({ A: moola(9n), B: simoleans(6n), C: bucks(7n) }); t.truthy(isOfferSafe(proposal, amounts)); - t.is(satisfiesWant(proposal, amounts), 1); + t.is(numWantsSatisfied(proposal, amounts), 1); }); // less than want, more than give -> true @@ -83,7 +97,7 @@ test('isOfferSafe - less than want, more than give', t => { const amounts = harden({ A: moola(7n), B: simoleans(9n), C: bucks(19n) }); t.truthy(isOfferSafe(proposal, amounts)); - t.is(satisfiesWant(proposal, amounts), 0); + t.is(numWantsSatisfied(proposal, amounts), 0); }); // less than want, less than give -> false @@ -98,7 +112,7 @@ test('isOfferSafe - less than want, less than give', t => { const amounts = harden({ A: moola(7n), B: simoleans(5n), C: bucks(6n) }); t.falsy(isOfferSafe(proposal, amounts)); - t.is(satisfiesWant(proposal, amounts), 0); + t.is(numWantsSatisfied(proposal, amounts), 0); }); // less than want, equal to give -> true @@ -113,7 +127,7 @@ test('isOfferSafe - less than want, equal to give', t => { const amounts = harden({ A: moola(1n), B: simoleans(5n), C: bucks(7n) }); t.truthy(isOfferSafe(proposal, amounts)); - t.is(satisfiesWant(proposal, amounts), 0); + t.is(numWantsSatisfied(proposal, amounts), 0); }); // equal to want, more than give -> true @@ -128,7 +142,7 @@ test('isOfferSafe - equal to want, more than give', t => { const amounts = harden({ A: moola(2n), B: simoleans(6n), C: bucks(8n) }); t.truthy(isOfferSafe(proposal, amounts)); - t.is(satisfiesWant(proposal, amounts), 1); + t.is(numWantsSatisfied(proposal, amounts), 1); }); // equal to want, less than give -> true @@ -143,7 +157,7 @@ test('isOfferSafe - equal to want, less than give', t => { const amounts = harden({ A: moola(0n), B: simoleans(6n), C: bucks(0n) }); t.truthy(isOfferSafe(proposal, amounts)); - t.is(satisfiesWant(proposal, amounts), 1); + t.is(numWantsSatisfied(proposal, amounts), 1); }); // equal to want, equal to give -> true @@ -158,7 +172,7 @@ test('isOfferSafe - equal to want, equal to give', t => { const amounts = harden({ A: moola(1n), B: simoleans(6n), C: bucks(7n) }); t.truthy(isOfferSafe(proposal, amounts)); - t.is(satisfiesWant(proposal, amounts), 1); + t.is(numWantsSatisfied(proposal, amounts), 1); }); test('isOfferSafe - empty proposal', t => { @@ -172,5 +186,5 @@ test('isOfferSafe - empty proposal', t => { const amounts = harden({ A: moola(1n), B: simoleans(6n), C: bucks(7n) }); t.truthy(isOfferSafe(proposal, amounts)); - t.is(satisfiesWant(proposal, amounts), 1); + t.is(numWantsSatisfied(proposal, amounts), Infinity); }); diff --git a/packages/zoe/test/unitTests/zcf/test-zcf.js b/packages/zoe/test/unitTests/zcf/test-zcf.js index af9f33df2a05..fd2e7e06a5cc 100644 --- a/packages/zoe/test/unitTests/zcf/test-zcf.js +++ b/packages/zoe/test/unitTests/zcf/test-zcf.js @@ -1445,7 +1445,7 @@ test('numWantsSatisfied: no', async t => { await zcfSeat.exit(); t.is(await E(userSeat).numWantsSatisfied(), 0); }); - + test('numWantsSatisfied: yes', async t => { const { zcf } = await setupZCFTest(); const doubloonMint = await zcf.makeZCFMint('Doubloons');