Skip to content

Commit

Permalink
Merge pull request #1602 from Agoric/1568-cosmic-swingset
Browse files Browse the repository at this point in the history
chore: update 'cosmic-swingset' package to use AVA
  • Loading branch information
warner authored Aug 24, 2020
2 parents 167ff0f + 37b6a40 commit d4a19ce
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 299 deletions.
17 changes: 12 additions & 5 deletions packages/cosmic-swingset/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"build": "exit 0",
"build:gyp": "make compile-gyp",
"build:gyp-debug": "make compile-gyp GYP_DEBUG=--debug",
"test": "tap --no-coverage --jobs=1 --timeout 600 'test/**/test*.js'",
"test": "ava --serial",
"pretty-fix": "prettier --write '**/*.js'",
"pretty-check": "prettier --check '**/*.js'",
"lint-fix": "yarn lint --fix",
Expand Down Expand Up @@ -66,16 +66,23 @@
"ws": "^7.2.0"
},
"devDependencies": {
"ava": "3.11.1",
"napi-thread-safe-callback": "0.0.6",
"node-addon-api": "^1.7.1",
"tap": "^14.10.5",
"tape": "^4.11.0",
"tape-promise": "^4.0.0"
"node-addon-api": "^1.7.1"
},
"publishConfig": {
"access": "public"
},
"engines": {
"node": ">=11.0"
},
"ava": {
"files": [
"test/**/test-*.js"
],
"require": [
"esm"
],
"timeout": "10m"
}
}
195 changes: 83 additions & 112 deletions packages/cosmic-swingset/test/test-home.js
Original file line number Diff line number Diff line change
@@ -1,134 +1,105 @@
import '@agoric/install-ses';
import { test } from 'tape-promise/tape';
import test from 'ava';
import bundleSource from '@agoric/bundle-source';

import { makeFixture, E } from './captp-fixture';

// This runs before all the tests.
let home;
let teardown;
test('setup', async t => {
try {
const { homeP, kill } = makeFixture();
teardown = kill;
home = await homeP;
} catch (e) {
t.isNot(e, e, 'unexpected exception');
} finally {
t.end();
}
test.before('setup', async t => {
const { homeP, kill } = makeFixture();
teardown = kill;
home = await homeP;
t.truthy('ready');
});

// Now come the tests that use `home`...
// =========================================

test('home.registry', async t => {
try {
const { registry } = E.G(home);
const regVal = await E(registry).get('foolobr_19191');
t.equals(regVal, undefined, 'random registry name is undefined');

const target = 'something';
const myRegKey = await E(registry).register('myname', target);
t.equals(typeof myRegKey, 'string', 'registry key is string');

const registered = await E(registry).get(myRegKey);
t.equals(registered, target, 'registry registers target');
} catch (e) {
t.isNot(e, e, 'unexpected exception');
} finally {
t.end();
}
test.serial('home.registry', async t => {
const { registry } = E.G(home);
const regVal = await E(registry).get('foolobr_19191');
t.is(regVal, undefined, 'random registry name is undefined');

const target = 'something';
const myRegKey = await E(registry).register('myname', target);
t.is(typeof myRegKey, 'string', 'registry key is string');

const registered = await E(registry).get(myRegKey);
t.is(registered, target, 'registry registers target');
});

test('home.board', async t => {
try {
const { board } = E.G(home);
t.rejects(
() => E(board).getValue('0000000000'),
`getting a value for a fake id throws`,
);

const myValue = {};
const myId = await E(board).getId(myValue);
t.equals(typeof myId, 'string', `board key is string`);

const valueInBoard = await E(board).getValue(myId);
t.deepEquals(valueInBoard, myValue, `board contains myValue`);

const myId2 = await E(board).getId(myValue);
t.equals(myId2, myId, `board gives the same id for the same value`);
} catch (e) {
t.isNot(e, e, 'unexpected exception');
} finally {
t.end();
}
test.serial('home.board', async t => {
const { board } = E.G(home);
t.throwsAsync(
() => E(board).getValue('0000000000'),
{ message: /board does not have id/ },
`getting a value for a fake id throws`,
);

const myValue = {};
const myId = await E(board).getId(myValue);
t.is(typeof myId, 'string', `board key is string`);

const valueInBoard = await E(board).getValue(myId);
t.deepEqual(valueInBoard, myValue, `board contains myValue`);

const myId2 = await E(board).getId(myValue);
t.is(myId2, myId, `board gives the same id for the same value`);
});

test('home.wallet - receive zoe invite', async t => {
try {
const { wallet, zoe, board } = E.G(home);

// Setup contract in order to get an invite to use in tests
const contractRoot = require.resolve(
'@agoric/zoe/src/contracts/automaticRefund',
);
const bundle = await bundleSource(contractRoot);
const installationHandle = await E(zoe).install(bundle);
const { creatorInvitation: invite } = await E(zoe).startInstance(
installationHandle,
);

// Check that the wallet knows about the Zoe invite issuer and starts out
// with a default Zoe invite issuer purse.
const zoeInviteIssuer = await E(zoe).getInvitationIssuer();
const issuers = await E(wallet).getIssuers();
const issuersMap = new Map(issuers);
t.deepEquals(
issuersMap.get('zoe invite'),
zoeInviteIssuer,
`wallet knows about the Zoe invite issuer`,
);
const invitePurse = await E(wallet).getPurse('Default Zoe invite purse');
const zoeInviteBrand = await E(invitePurse).getAllegedBrand();
t.equals(
zoeInviteBrand,
await E(zoeInviteIssuer).getBrand(),
`invite purse is actually a zoe invite purse`,
);

// The code below is meant to be carried out in a Dapp backend.
// The dapp gets the depositBoardId for the default Zoe invite purse
// and sends the invite.
const inviteBrandBoardId = await E(board).getId(zoeInviteBrand);
const depositBoardId = await E(wallet).getDepositFacetId(
inviteBrandBoardId,
);
const depositFacet = await E(board).getValue(depositBoardId);
await E(depositFacet).receive(invite);

// The invite was successfully received in the user's wallet.
const invitePurseBalance = await E(invitePurse).getCurrentAmount();
t.equals(
invitePurseBalance.value[0].description,
'getRefund',
`invite successfully deposited`,
);
} catch (e) {
t.isNot(e, e, 'unexpected exception');
} finally {
t.end();
}
test.serial('home.wallet - receive zoe invite', async t => {
const { wallet, zoe, board } = E.G(home);

// Setup contract in order to get an invite to use in tests
const contractRoot = require.resolve(
'@agoric/zoe/src/contracts/automaticRefund',
);
const bundle = await bundleSource(contractRoot);
const installationHandle = await E(zoe).install(bundle);
const { creatorInvitation: invite } = await E(zoe).startInstance(
installationHandle,
);

// Check that the wallet knows about the Zoe invite issuer and starts out
// with a default Zoe invite issuer purse.
const zoeInviteIssuer = await E(zoe).getInvitationIssuer();
const issuers = await E(wallet).getIssuers();
const issuersMap = new Map(issuers);
t.deepEqual(
issuersMap.get('zoe invite'),
zoeInviteIssuer,
`wallet knows about the Zoe invite issuer`,
);
const invitePurse = await E(wallet).getPurse('Default Zoe invite purse');
const zoeInviteBrand = await E(invitePurse).getAllegedBrand();
t.is(
zoeInviteBrand,
await E(zoeInviteIssuer).getBrand(),
`invite purse is actually a zoe invite purse`,
);

// The code below is meant to be carried out in a Dapp backend.
// The dapp gets the depositBoardId for the default Zoe invite purse
// and sends the invite.
const inviteBrandBoardId = await E(board).getId(zoeInviteBrand);
const depositBoardId = await E(wallet).getDepositFacetId(inviteBrandBoardId);
const depositFacet = await E(board).getValue(depositBoardId);
await E(depositFacet).receive(invite);

// The invite was successfully received in the user's wallet.
const invitePurseBalance = await E(invitePurse).getCurrentAmount();
t.is(
invitePurseBalance.value[0].description,
'getRefund',
`invite successfully deposited`,
);
});

// =========================================
// This runs after all the tests.
test('teardown', async t => {
try {
await teardown();
} catch (e) {
t.isNot(e, e, 'unexpected exception');
} finally {
t.end();
}
test.after.always('teardown', async t => {
await teardown();
t.truthy('shutdown');
});
26 changes: 10 additions & 16 deletions packages/cosmic-swingset/test/test-make.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { test } from 'tape-promise/tape';
import test from 'ava';
import { spawn } from 'child_process';

test('make', async t => {
try {
await new Promise(resolve =>
spawn('make', {
cwd: `${__dirname}/..`,
stdio: ['ignore', 'ignore', 'inherit'],
}).addListener('exit', code => {
t.equal(code, 0, 'exits successfully');
resolve();
}),
);
} catch (e) {
t.isNot(e, e, 'unexpected exception');
} finally {
t.end();
}
await new Promise(resolve =>
spawn('make', {
cwd: `${__dirname}/..`,
stdio: ['ignore', 'ignore', 'inherit'],
}).addListener('exit', code => {
t.is(code, 0, 'exits successfully');
resolve();
}),
);
});
20 changes: 0 additions & 20 deletions packages/cosmic-swingset/test/test-scenario3-setup.js

This file was deleted.

32 changes: 13 additions & 19 deletions packages/cosmic-swingset/test/unitTests/test-lib-board.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,27 @@
// eslint-disable-next-line import/no-extraneous-dependencies

import '@agoric/install-ses';
import { test } from 'tape-promise/tape';
import test from 'ava';

import { makeBoard } from '../../lib/ag-solo/vats/lib-board';

test('makeBoard', async t => {
try {
const board = makeBoard();
const board = makeBoard();

const obj1 = harden({});
const obj2 = harden({});
const obj1 = harden({});
const obj2 = harden({});

t.deepEquals(board.ids(), [], `board is empty to start`);
t.deepEqual(board.ids(), [], `board is empty to start`);

const idObj1 = board.getId(obj1);
t.deepEquals(board.ids(), [idObj1], `board has one id`);
const idObj1 = board.getId(obj1);
t.deepEqual(board.ids(), [idObj1], `board has one id`);

const idObj2 = board.getId(obj2);
t.deepEquals(board.ids().length, 2, `board has two ids`);
const idObj2 = board.getId(obj2);
t.deepEqual(board.ids().length, 2, `board has two ids`);

t.deepEquals(board.getValue(idObj1), obj1, `id matches value obj1`);
t.deepEquals(board.getValue(idObj2), obj2, `id matches value obj2`);
t.deepEqual(board.getValue(idObj1), obj1, `id matches value obj1`);
t.deepEqual(board.getValue(idObj2), obj2, `id matches value obj2`);

t.deepEquals(board.getId(obj1), idObj1, `value matches id obj1`);
t.deepEquals(board.getId(obj2), idObj2, `value matches id obj2`);
} catch (e) {
t.isNot(e, e, 'unexpected exception');
} finally {
t.end();
}
t.deepEqual(board.getId(obj1), idObj1, `value matches id obj1`);
t.deepEqual(board.getId(obj2), idObj2, `value matches id obj2`);
});
Loading

0 comments on commit d4a19ce

Please sign in to comment.