-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathtest-home.js
186 lines (160 loc) · 6.21 KB
/
test-home.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* global process */
import '@agoric/install-ses/pre-bundle-source.js';
// `test.after.always` does not yet seem compatible with ses-ava
// See https://github.com/endojs/endo/issues/647
// TODO restore
// import { test } from '@agoric/swingset-vat/tools/prepare-test-env-ava';
import '@agoric/swingset-vat/tools/prepare-test-env.js';
// eslint-disable-next-line import/no-extraneous-dependencies
import test from 'ava';
import bundleSource from '@agoric/bundle-source';
import { AmountMath } from '@agoric/ertp';
import { Far } from '@agoric/marshal';
import { resolve as importMetaResolve } from 'import-meta-resolve';
import { CENTRAL_ISSUER_NAME } from '@agoric/vats/src/issuers.js';
import { makeFixture, E } from './captp-fixture.js';
const SOLO_PORT = 7999;
// This runs before all the tests.
let home;
let teardown;
test.before('setup', async t => {
const { homeP, kill } = await makeFixture(SOLO_PORT, process.env.NOISY);
teardown = kill;
home = await homeP;
t.truthy('ready');
});
// Now come the tests that use `home`...
// =========================================
test.serial('home.board', async t => {
const { board } = E.get(home);
await t.throwsAsync(
() => E(board).getValue('148'),
{ message: /board does not have id/ },
`getting a value for a fake id throws`,
);
await t.throwsAsync(
() => E(board).getValue('0000000000'),
{ message: /id is probably a typo/ },
`using a non-verified id throws`,
);
const myValue = Far('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.serial('home.wallet - transfer funds to the feePurse', async t => {
const { wallet, faucet } = E.get(home);
const feePurse = E(faucet).getFeePurse();
const feeBrand = await E(feePurse).getAllegedBrand();
const feeAmount = AmountMath.make(feeBrand, 10_000_000n);
const feePayment = await E(
E(wallet).getPurse('Agoric RUN currency'),
).withdraw(feeAmount);
const deposited = await E(feePurse).deposit(feePayment);
t.deepEqual(deposited, feeAmount, `all fees deposited to feePurse`);
});
test.serial('home.wallet - receive zoe invite', async t => {
const { wallet, zoe, board } = E.get(home);
// Setup contract in order to get an invite to use in tests
const contractUrl = await importMetaResolve(
'@agoric/zoe/src/contracts/automaticRefund.js',
import.meta.url,
);
const contractRoot = new URL(contractUrl).pathname;
t.log({ contractRoot });
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`,
);
});
test.serial('home.wallet - central issuer setup', async t => {
const { wallet } = E.get(home);
// Check that the wallet knows about the central issuer.
const issuers = await E(wallet).getIssuers();
const issuersMap = new Map(issuers);
const centralIssuer = issuersMap.get(CENTRAL_ISSUER_NAME);
const centralPurse = await E(wallet).getPurse('Agoric RUN currency');
const brandFromIssuer = await E(centralIssuer).getBrand();
const brandFromPurse = await E(centralPurse).getAllegedBrand();
t.is(brandFromPurse, brandFromIssuer);
});
test.serial('home.localTimerService makeNotifier', async t => {
const { localTimerService } = E.get(home);
const notifier = E(localTimerService).makeNotifier(1, 1);
const update1 = await E(notifier).getUpdateSince();
const firstUpdate = update1.updateCount;
t.truthy(firstUpdate > 0);
const update2 = await E(notifier).getUpdateSince(update1.updateCount);
t.is(update2.updateCount, firstUpdate + 1);
// Tests gets an actual localTimerService, which returns actual times. We
// can't verify the actual time, so we compare to make sure it's increasing.
t.truthy(update2.value > update1.value);
});
function makeHandler() {
let calls = 0;
const args = [];
return Far('wake handler', {
getCalls() {
return calls;
},
getArgs() {
return args;
},
wake(arg) {
args.push(arg);
calls += 1;
},
});
}
test.serial('home.localTimerService makeRepeater', async t => {
const { localTimerService } = E.get(home);
const timestamp = await E(localTimerService).getCurrentTimestamp();
const repeater = E(localTimerService).makeRepeater(1, 1);
const handler = makeHandler();
await E(repeater).schedule(handler);
const notifier = E(localTimerService).makeNotifier(1, 1);
await E(notifier).getUpdateSince();
t.truthy(handler.getCalls() >= 1);
t.truthy(handler.getArgs()[0] > timestamp);
});
// =========================================
// This runs after all the tests.
test.after.always('teardown', async t => {
await teardown();
t.truthy('shutdown');
});