Skip to content

Commit

Permalink
chore: add makeKind tests
Browse files Browse the repository at this point in the history
  • Loading branch information
katelynsills committed Mar 5, 2021
1 parent 3ee3eff commit 342eaec
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 3 deletions.
13 changes: 11 additions & 2 deletions packages/zoe/src/contractFacet/contractFacet.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// @ts-check

/* globals makeKind, makeWeakStore */

// This is the Zoe contract facet. Each time we make a new instance of a
// contract we will start by creating a new vat and running this code in it. In
// order to install this code in a vat, Zoe needs to import a bundle containing
Expand All @@ -9,7 +11,7 @@

import { assert, details as X, q, makeAssert } from '@agoric/assert';
import { E } from '@agoric/eventual-send';
import { makeStore, makeWeakStore } from '@agoric/store';
import { makeStore } from '@agoric/store';
import { Far, Data } from '@agoric/marshal';

import { makeAmountMath, MathKind } from '@agoric/ertp';
Expand All @@ -32,6 +34,8 @@ import '../../exported';
import '../internal-types';

export function buildRootObject(powers, _params, testJigSetter = undefined) {
console.log('makeKind in zcf', makeKind());
console.log('makeWeakStore in zcf', makeWeakStore());
/** @type {ExecuteContract} */
const executeContract = async (
bundle,
Expand All @@ -40,6 +44,8 @@ export function buildRootObject(powers, _params, testJigSetter = undefined) {
zoeInstanceAdmin,
instanceRecord,
) => {
console.log('makeKind in executeContract', makeKind());
console.log('makeWeakStore in executeContract', makeWeakStore());
/** @type {IssuerTable} */
const issuerTable = makeIssuerTable();
const getAmountMath = brand => issuerTable.getByBrand(brand).amountMath;
Expand Down Expand Up @@ -459,7 +465,10 @@ export function buildRootObject(powers, _params, testJigSetter = undefined) {
});

// First, evaluate the contract code bundle.
const contractCode = evalContractBundle(bundle);
const contractCode = evalContractBundle(bundle, {
makeKind,
makeWeakStore,
});
// Don't trigger Node.js's UnhandledPromiseRejectionWarning.
// This does not suppress any error messages.
contractCode.catch(() => {});
Expand Down
4 changes: 3 additions & 1 deletion packages/zoe/src/zoeService/zoe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* globals makeKind makeWeakStore */
// @ts-check
import { makeWeakStore } from '@agoric/store';
import { assert, details as X } from '@agoric/assert';
import { E } from '@agoric/eventual-send';
import { makePromiseKit } from '@agoric/promise-kit';
Expand Down Expand Up @@ -31,6 +31,8 @@ import { makeHandle } from '../makeHandle';
* @returns {ZoeService} The created Zoe service.
*/
function makeZoe(vatAdminSvc, zcfBundleName = undefined) {
console.log('makeKind in zoe', makeKind());
console.log('makeWeakStore in zoe', makeWeakStore());
const invitationKit = makeIssuerKit('Zoe Invitation', MathKind.SET);

// Zoe state shared among functions
Expand Down
11 changes: 11 additions & 0 deletions packages/zoe/test/minimalMakeKindContract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const start = zcf => {
console.log('makeKind in contract', makeKind());
console.log('makeWeakStore in contract', makeWeakStore());

makeKind();
makeWeakStore();

return harden({});
};
harden(start);
export { start };
20 changes: 20 additions & 0 deletions packages/zoe/test/swingsetTests/makeKind/bootstrap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { E } from '@agoric/eventual-send';
import { Far } from '@agoric/marshal';

export function buildRootObject(vatPowers, vatParameters) {
const { contractBundles: cb } = vatParameters;
return Far('root', {
async bootstrap(vats, devices) {
const vatAdminSvc = await E(vats.vatAdmin).createVatAdminService(
devices.vatAdmin,
);
const zoe = await E(vats.zoe).buildZoe(vatAdminSvc);
const installations = {
minimalMakeKind: await E(zoe).install(cb.minimalMakeKindContract),
};

const aliceP = E(vats.alice).build(zoe, installations);
await E(aliceP).minimalMakeKindTest();
},
});
}
72 changes: 72 additions & 0 deletions packages/zoe/test/swingsetTests/makeKind/test-makeKind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import '@agoric/install-metering-and-ses';
// eslint-disable-next-line import/no-extraneous-dependencies
import test from 'ava';
// eslint-disable-next-line import/no-extraneous-dependencies
import { buildVatController, buildKernelBundles } from '@agoric/swingset-vat';
import bundleSource from '@agoric/bundle-source';

const CONTRACT_FILES = ['minimalMakeKindContract'];

test.before(async t => {
const start = Date.now();
const kernelBundles = await buildKernelBundles();
const step2 = Date.now();
const contractBundles = {};
await Promise.all(
CONTRACT_FILES.map(async settings => {
let bundleName;
let contractPath;
if (typeof settings === 'string') {
bundleName = settings;
contractPath = settings;
} else {
({ bundleName, contractPath } = settings);
}
const source = `${__dirname}/../../${contractPath}`;
const bundle = await bundleSource(source);
contractBundles[bundleName] = bundle;
}),
);
const step3 = Date.now();

const vats = {};
await Promise.all(
['alice', 'zoe'].map(async name => {
const source = `${__dirname}/vat-${name}.js`;
const bundle = await bundleSource(source);
vats[name] = { bundle };
}),
);
const bootstrapSource = `${__dirname}/bootstrap.js`;
vats.bootstrap = {
bundle: await bundleSource(bootstrapSource),
parameters: { contractBundles }, // argv will be added to this
};
const config = { bootstrap: 'bootstrap', vats };

const step4 = Date.now();
const ktime = `${(step2 - start) / 1000}s kernel`;
const ctime = `${(step3 - step2) / 1000}s contracts`;
const vtime = `${(step4 - step3) / 1000}s vats`;
const ttime = `${(step4 - start) / 1000}s total`;
console.log(`bundling: ${ktime}, ${ctime}, ${vtime}, ${ttime}`);

t.context.data = { kernelBundles, config };
});

async function main(t, argv) {
const { kernelBundles, config } = t.context.data;
const controller = await buildVatController(config, argv, { kernelBundles });
await controller.run();
return controller.dump();
}

const expected = [
'{"adminFacet":{},"creatorFacet":{},"instance":{},"publicFacet":{}}',
];

test.serial('makeKind swingset', async t => {
const dump = await main(t);
t.deepEqual(dump.log, expected);
});
17 changes: 17 additions & 0 deletions packages/zoe/test/swingsetTests/makeKind/vat-alice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { E } from '@agoric/eventual-send';
import { Far } from '@agoric/marshal';

const build = async (log, zoe, installations) => {
return Far('build', {
minimalMakeKindTest: async () => {
const result = await E(zoe).startInstance(installations.minimalMakeKind);
log(result);
},
});
};

export function buildRootObject(vatPowers) {
return Far('root', {
build: (...args) => build(vatPowers.testLog, ...args),
});
}
10 changes: 10 additions & 0 deletions packages/zoe/test/swingsetTests/makeKind/vat-zoe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Far } from '@agoric/marshal';

// noinspection ES6PreferShortImport
import { makeZoe } from '../../../src/zoeService/zoe';

export function buildRootObject(_vatPowers) {
return Far('root', {
buildZoe: vatAdminSvc => makeZoe(vatAdminSvc),
});
}
24 changes: 24 additions & 0 deletions packages/zoe/test/unitTests/test-makeKind.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* globals makeKind makeWeakStore */

// eslint-disable-next-line import/no-extraneous-dependencies
import '@agoric/zoe/tools/prepare-test-env';
// eslint-disable-next-line import/no-extraneous-dependencies
import test from 'ava';
import bundleSource from '@agoric/bundle-source';

import { E } from '@agoric/eventual-send';
import { makeZoe } from '../../src/zoeService/zoe';
import fakeVatAdmin from '../../src/contractFacet/fakeVatAdmin';

const root = `${__dirname}/../minimalMakeKindContract`;

test('makeKind non-swingset', async t => {
const bundle = await bundleSource(root);
const zoe = makeZoe(fakeVatAdmin);
const installation = await E(zoe).install(bundle);
console.log('makeKind in test file', makeKind());
console.log('makeWeakStore in test file', makeWeakStore());
t.notThrows(() => makeKind());
t.notThrows(() => makeWeakStore());
await t.notThrowsAsync(() => zoe.startInstance(installation));
});

0 comments on commit 342eaec

Please sign in to comment.