-
Notifications
You must be signed in to change notification settings - Fork 7
/
agd-lib.test.js
267 lines (232 loc) · 7.83 KB
/
agd-lib.test.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { test } from './prepare-test-env-ava.js';
import { makeAgd, makeContainer } from '../tools/agd-lib.js';
import { makeE2ETools } from '../tools/e2e-tools.js';
import { makeDeployBuilder } from '../tools/deploy.js';
import { makeNodeBundleCache } from '@endo/bundle-source/cache.js';
import { commonSetup, ensureAccounts } from './support.js';
const contractName = 'myContract';
const chainInfoBuilder = 'proposals/revise-chain-info.builder.js';
const contractBuilder = './test/builder/init-orca.js';
const myPlan = {
name: 'myContract',
script: 'myContract.deploy.js',
permit: 'myContract-permit.json',
bundles: [{ fileName: '/home/me/.agoric/cache/b1-DEADBEEF.json' }],
};
const mockIO = t => {
const execLog = [];
const cookie = 'oothaiW3'; // from pwgen; unlikely to occur elsewhere
let latest_block_height = 100;
const files = new Set();
const { stringify } = JSON;
const readJSON = async path => {
t.is(path, `./${myPlan.permit.replace('-permit', '-plan')}`);
return myPlan;
};
/** @type {import('../tools/agd-lib.js').ExecSync} */
const execFileSync = (file, args, opts) => {
execLog.push({ file, args, opts });
if (args.includes('status')) {
return stringify({ SyncInfo: { latest_block_height } });
}
if (args.includes('keys') && args.includes('show')) {
return 'agoric1234';
}
let proposal_id = 0;
if (args.includes('gov') && args.includes('proposals')) {
proposal_id += 1;
return stringify({ proposals: [{ proposal_id }] });
}
if (args.includes('gov') && args.includes('proposal')) {
proposal_id += 1;
return stringify({ status: 'PROPOSAL_STATUS_PASSED' });
}
if (args.includes('tx')) {
return stringify({ code: 0, txhash: 'abc123', height: 123 });
}
if (args[0] === 'cp') {
files.add(args[1]);
return '';
}
if (args.includes('ls')) {
return [...files].sort().join(' ');
}
if (args.includes('agoric') && args.includes('run')) {
return `... ${myPlan.permit}`;
}
return stringify({ output: cookie });
};
/**
* @param {string} file
* @param {string[]} args
*/
const execFile = (file, args, cb) => {
try {
const stdout = execFileSync(file, args, { encoding: 'utf-8' });
cb(null, { stdout });
} catch (err) {
cb(err);
}
};
const npx = (file, args) => {
t.log('npx', file, ...args);
return Promise.resolve(
execFileSync(file, args, { encoding: 'utf-8' }),
).then(_stdout => ({ stdout: ` ${myPlan.permit}` }));
};
const logLog = [];
const log = (...args) => {
t.log(...args);
logLog.push(args);
};
/** @type {typeof fetch} */
// @ts-expect-error mock
const fetch = async (url, settings) => ({
ok: true,
text: async () => 'TEXT',
json: async () => {
if (url.endsWith('agoricNames.instance')) {
// XXX incomplete vstorage encoding
// relies on lax parsing in batchQuery.js
return { value: stringify({ value: [[contractName, null]] }) };
}
if (settings.method !== 'POST') throw Error('todo');
const body = JSON.parse(settings.body);
if ('jsonrpc' in body) {
const { jsonrpc, id } = body;
if (body.method !== 'status') throw Error('todo');
latest_block_height += 1;
return { jsonrpc, id, result: { sync_info: { latest_block_height } } };
}
throw Error('todo');
},
});
/** @type { typeof setTimeout } */
// @ts-expect-error mock
const setTimeout = (cb, ms) => Promise.resolve().then(_ => cb());
return {
execFile,
execFileSync,
execLog,
readJSON,
npx,
readFile: (...args) => readJSON(...args).then(x => stringify(x)),
log,
logLog,
cookie,
fetch,
setTimeout,
};
};
test('makeAgd handles key lookup', async t => {
const io = mockIO(t);
const { execFileSync, log } = io;
const agd = makeAgd({ execFileSync });
const data = await agd.lookup('gov1');
t.deepEqual(data, 'agoric1234');
const subCmd = 'keys show --address gov1';
t.is(io.execLog[0].args.join(' '), subCmd);
t.like(io.execLog, [{ file: 'agd', opts: { encoding: 'utf-8' } }]);
});
test('agd tx swingset install-bundle', async t => {
const io = mockIO(t);
const { execFileSync } = io;
const agd = makeAgd({ execFileSync }); // TODO: pass log explicitly
const data = await agd.tx(['swingset', 'install-bundle', '@f1'], {
from: 'acct1',
chainId: 'c1',
});
t.deepEqual(data, {
code: 0,
height: 123,
txhash: 'abc123',
});
});
test('container virtualizes agd status command', async t => {
const io = mockIO(t);
const { execFileSync, log } = io;
const c1 = makeContainer({ execFileSync, log }).withFlags({ tty: false });
const actual = c1.execFileSync('agd', ['status'], { encoding: 'utf-8' });
t.deepEqual(
actual,
JSON.stringify({ SyncInfo: { latest_block_height: 100 } }),
);
t.log(io.execLog[0].args.join(' '));
const subCmd =
'exec -i agoriclocal-genesis-0 --container validator --tty false -- agd status';
t.is(io.execLog[0].args.join(' '), subCmd);
t.like(io.execLog, [{ file: 'kubectl', opts: { encoding: 'utf-8' } }]);
t.deepEqual(io.logLog, [
['agoriclocal-genesis-0/validator$', 'agd', 'status'],
]);
});
test('agd in container', async t => {
const io = mockIO(t);
const { execFileSync, log } = io;
const c1 = makeContainer({ execFileSync, log }).withFlags({ tty: false });
const agd = makeAgd({ execFileSync: c1.execFileSync }).withOpts({
keyringBackend: 'test',
});
const data = await agd.lookup('gov1');
t.deepEqual(data, 'agoric1234');
const subCmd =
'exec -i agoriclocal-genesis-0 --container validator --tty false -- agd keys show --address gov1 --keyring-backend test';
t.is(io.execLog[0].args.join(' '), subCmd);
t.like(io.execLog, [{ file: 'kubectl', opts: { encoding: 'utf-8' } }]);
});
test('container copies files', async t => {
const io = mockIO(t);
const { execFileSync, log } = io;
const c1 = makeContainer({ execFileSync, log }).withFlags({ tty: false });
const dests = c1.copyFiles(['contract.deploy.js', 'contract-permit.json']);
t.deepEqual(dests, [
'/root/contract.deploy.js',
'/root/contract-permit.json',
]);
t.snapshot(io.execLog, 'exec log');
});
test('deploy-cli style usage', async t => {
const io = mockIO(t);
const bundleCache = null;
const { log } = io;
const c1 = makeContainer({ execFileSync: io.execFileSync, log });
const tools = makeE2ETools(log, bundleCache, {
execFileSync: c1.execFileSync,
copyFiles: c1.copyFiles,
fetch: io.fetch,
setTimeout: io.setTimeout,
});
const deployBuilder = makeDeployBuilder(tools, io.readJSON, io.npx);
await deployBuilder(chainInfoBuilder);
t.snapshot(io.execLog, 'execLog');
});
test('orca-multichain.test style usage', async t => {
const io = mockIO(t);
const bundleCache = await makeNodeBundleCache('bundles', {}, s => import(s));
const container = makeContainer({ execFileSync: io.execFileSync });
const { deployBuilder, retryUntilCondition, ...tools } = commonSetup(t, {
bundleCache,
container,
execFile: io.execFile,
fetch: io.fetch,
log: t.log,
readFile: io.readFile,
setTimeout: io.setTimeout,
});
const wallets = await ensureAccounts(tools.agd.keys);
t.deepEqual(wallets, {
agoric: 'agoric1v8qxguqqjtfyfwqr8ln2wlu858vkx4860jzf6g',
cosmoshub: 'agoric17th0tvrzmwc2fqpeneuwmrwcm8armyjlgmypuf',
osmosis: 'agoric17hglh4q5k087nthneq0kegk4y838vmt3vk80u3',
});
t.log('bundle and install contract', contractName);
await deployBuilder(contractBuilder);
const { vstorageClient } = tools;
await retryUntilCondition(
() => vstorageClient.queryData(`published.agoricNames.instance`),
res => contractName in Object.fromEntries(res),
`${contractName} instance is available`,
);
// consider snapshotting fetch, readFile access too
t.snapshot(io.execLog, 'execLog');
});