Skip to content

Commit

Permalink
WIP test: convert to .ts
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Sep 8, 2023
1 parent d665e78 commit 178d4ac
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import engineGC from '@agoric/internal/src/lib-nodejs/engine-gc.js';
import { eventLoopIteration } from '@agoric/internal/src/testing-utils.js';
import { makeAgoricNamesRemotesFromFakeStorage } from '@agoric/vats/tools/board-utils.js';
import { makeSwingsetTestKit } from './supports.js';
import { makeWalletFactoryDriver } from './drivers.js';
import { makeWalletFactoryDriver } from './drivers.ts';

/**
* @type {import('ava').TestFn<
Expand Down
2 changes: 1 addition & 1 deletion packages/boot/test/bootstrapTests/bench-vaults-stripped.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Offers } from '@agoric/inter-protocol/src/clientSupport.js';
import { eventLoopIteration } from '@agoric/internal/src/testing-utils.js';
import { makeAgoricNamesRemotesFromFakeStorage } from '@agoric/vats/tools/board-utils.js';
import { makeSwingsetTestKit } from './supports.js';
import { makeWalletFactoryDriver } from './drivers.js';
import { makeWalletFactoryDriver } from './drivers.ts';

// presently all these tests use one collateral manager
const collateralBrandKey = 'ATOM';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
// @ts-check
/* eslint-disable jsdoc/require-param */
import { Fail, NonNullish } from '@agoric/assert';
import { Offers } from '@agoric/inter-protocol/src/clientSupport.js';
import { SECONDS_PER_MINUTE } from '@agoric/inter-protocol/src/proposals/econ-behaviors.js';
import { unmarshalFromVstorage } from '@agoric/internal/src/marshal.js';
import { slotToRemotable } from '@agoric/internal/src/storage-test-utils.js';
import {
FakeStorageKit,
slotToRemotable,
} from '@agoric/internal/src/storage-test-utils.js';
import { instanceNameFor } from '@agoric/inter-protocol/src/proposals/price-feed-proposal.js';

import { boardSlottingMarshaller } from '@agoric/vats/tools/board-utils.js';
import {
AgoricNamesRemotes,
boardSlottingMarshaller,
} from '@agoric/vats/tools/board-utils.js';
import type {
CurrentWalletRecord,
SmartWallet,
UpdateRecord,
} from '@agoric/smart-wallet/src/smartWallet.js';
import type { WalletFactoryStartResult } from '@agoric/vats/src/core/startWalletFactory.js';
import type { OfferSpec } from '@agoric/smart-wallet/src/offers.js';
import type { TimerService } from '@agoric/time/src/types.js';
import type { OfferMaker } from '@agoric/smart-wallet/src/types.js';
import type { RunUtils, SwingsetTestKit } from './supports.js';

/**
* @param {ReturnType<typeof import('./supports.js').makeRunUtils>} runUtils
* @param {import('@agoric/internal/src/storage-test-utils.js').FakeStorageKit} storage
* @param {import('@agoric/vats/tools/board-utils.js').AgoricNamesRemotes} agoricNamesRemotes
*/
export const makeWalletFactoryDriver = async (
runUtils,
storage,
agoricNamesRemotes,
runUtils: RunUtils,
storage: FakeStorageKit,
agoricNamesRemotes: AgoricNamesRemotes,
) => {
const { EV } = runUtils;

/** @type {import('@agoric/vats/src/core/startWalletFactory.js').WalletFactoryStartResult} */
const walletFactoryStartResult = await EV.vat('bootstrap').consumeItem(
'walletFactoryStartResult',
const walletFactoryStartResult: WalletFactoryStartResult = await EV.vat(
'bootstrap',
).consumeItem('walletFactoryStartResult');
const bankManager: ERef<BankManager> = await EV.vat('bootstrap').consumeItem(
'bankManager',
);
/** @type {ERef<BankManager>} */
const bankManager = await EV.vat('bootstrap').consumeItem('bankManager');
const namesByAddressAdmin = await EV.vat('bootstrap').consumeItem(
'namesByAddressAdmin',
);

const marshaller = boardSlottingMarshaller(slotToRemotable);

/**
* @param {string} walletAddress
* @param {import('@agoric/smart-wallet/src/smartWallet.js').SmartWallet} walletPresence
* @param {boolean} isNew
*/
const makeWalletDriver = (walletAddress, walletPresence, isNew) => ({
const makeWalletDriver = (
walletAddress: string,
walletPresence: SmartWallet,
isNew: boolean,
) => ({
isNew,

/**
* @param {import('@agoric/smart-wallet/src/offers.js').OfferSpec} offer
* @returns {Promise<void>}
*/
executeOffer(offer) {
executeOffer(offer: OfferSpec): Promise<void> {
const offerCapData = marshaller.toCapData(
harden({
method: 'executeOffer',
Expand All @@ -53,11 +59,7 @@ export const makeWalletFactoryDriver = async (
);
return EV(walletPresence).handleBridgeAction(offerCapData, true);
},
/**
* @param {import('@agoric/smart-wallet/src/offers.js').OfferSpec} offer
* @returns {Promise<void>}
*/
sendOffer(offer) {
sendOffer(offer: OfferSpec): Promise<void> {
const offerCapData = marshaller.toCapData(
harden({
method: 'executeOffer',
Expand All @@ -67,8 +69,7 @@ export const makeWalletFactoryDriver = async (

return EV.sendOnly(walletPresence).handleBridgeAction(offerCapData, true);
},
/** @param {string} offerId */
tryExitOffer(offerId) {
tryExitOffer(offerId: string) {
const capData = marshaller.toCapData(
harden({
method: 'tryExitOffer',
Expand All @@ -77,33 +78,24 @@ export const makeWalletFactoryDriver = async (
);
return EV(walletPresence).handleBridgeAction(capData, true);
},
/**
* @template {import('@agoric/smart-wallet/src/types.js').OfferMaker} M
* offer maker function
* @param {M} makeOffer
* @param {Parameters<M>[1]} firstArg
* @param {Parameters<M>[2]} [secondArg]
* @returns {Promise<void>}
*/
executeOfferMaker(makeOffer, firstArg, secondArg) {
executeOfferMaker<M extends OfferMaker>(
makeOffer: M,
firstArg: Parameters<M>[1],
secondArg?: Parameters<M>[2],
): Promise<void> {
const offer = makeOffer(agoricNamesRemotes, firstArg, secondArg);
return this.executeOffer(offer);
},
/**
* @template {import('@agoric/smart-wallet/src/types.js').OfferMaker} M
* offer maker function
* @param {M} makeOffer
* @param {Parameters<M>[1]} firstArg
* @param {Parameters<M>[2]} [secondArg]
* @returns {Promise<void>}
*/
sendOfferMaker(makeOffer, firstArg, secondArg) {
sendOfferMaker<M extends OfferMaker>(
makeOffer: M,
firstArg: Parameters<M>[1],
secondArg?: Parameters<M>[2],
): Promise<void> {
const offer = makeOffer(agoricNamesRemotes, firstArg, secondArg);
return this.sendOffer(offer);
},

/** @returns {import('@agoric/smart-wallet/src/smartWallet.js').CurrentWalletRecord} */
getCurrentWalletRecord() {
getCurrentWalletRecord(): CurrentWalletRecord {
const fromCapData = (...args) =>
Reflect.apply(marshaller.fromCapData, marshaller, args);
return unmarshalFromVstorage(
Expand All @@ -114,8 +106,7 @@ export const makeWalletFactoryDriver = async (
);
},

/** @returns {import('@agoric/smart-wallet/src/smartWallet.js').UpdateRecord} */
getLatestUpdateRecord() {
getLatestUpdateRecord(): UpdateRecord {
const fromCapData = (...args) =>
Reflect.apply(marshaller.fromCapData, marshaller, args);
return unmarshalFromVstorage(
Expand All @@ -130,11 +121,10 @@ export const makeWalletFactoryDriver = async (
return {
/**
* Skip the provisionPool for tests
*
* @param {string} walletAddress
* @returns {Promise<ReturnType<typeof makeWalletDriver>>}
*/
async provideSmartWallet(walletAddress) {
async provideSmartWallet(
walletAddress: string,
): Promise<ReturnType<typeof makeWalletDriver>> {
const bank = await EV(bankManager).getBankForAddress(walletAddress);
return EV(walletFactoryStartResult.creatorFacet)
.provideSmartWallet(walletAddress, bank, namesByAddressAdmin)
Expand All @@ -144,18 +134,15 @@ export const makeWalletFactoryDriver = async (
},
};
};
export type WalletFactoryDriver = Awaited<
ReturnType<typeof makeWalletFactoryDriver>
>;

/**
* @param {string} collateralBrandKey
* @param {import('@agoric/vats/tools/board-utils.js').AgoricNamesRemotes} agoricNamesRemotes
* @param {Awaited<ReturnType<typeof makeWalletFactoryDriver>>} walletFactoryDriver
* @param {string[]} oracleAddresses
*/
export const makePriceFeedDriver = async (
collateralBrandKey,
agoricNamesRemotes,
walletFactoryDriver,
oracleAddresses,
collateralBrandKey: string,
agoricNamesRemotes: AgoricNamesRemotes,
walletFactoryDriver: WalletFactoryDriver,
oracleAddresses: string[],
) => {
const priceFeedName = instanceNameFor(collateralBrandKey, 'USD');

Expand Down Expand Up @@ -185,8 +172,7 @@ export const makePriceFeedDriver = async (
// zero is the initial lastReportedRoundId so causes an error: cannot report on previous rounds
let roundId = 1n;
return {
/** @param {number} price */
async setPrice(price) {
async setPrice(price: number) {
await Promise.all(
oracleWallets.map(w =>
w.executeOfferMaker(
Expand All @@ -208,28 +194,19 @@ export const makePriceFeedDriver = async (
};
};

/** @typedef {Awaited<ReturnType<import('./supports.js').makeSwingsetTestKit>>} SwingsetTestKit */

/**
* @param {SwingsetTestKit} testKit
* @param {import('@agoric/vats/tools/board-utils.js').AgoricNamesRemotes} agoricNamesRemotes
* @param {Awaited<ReturnType<typeof makeWalletFactoryDriver>>} walletFactoryDriver
* @param {string[]} committeeAddresses
*/
export const makeGovernanceDriver = async (
testKit,
agoricNamesRemotes,
walletFactoryDriver,
committeeAddresses,
testKit: SwingsetTestKit,
agoricNamesRemotes: AgoricNamesRemotes,
walletFactoryDriver: WalletFactoryDriver,
committeeAddresses: string[],
) => {
const { EV } = testKit.runUtils;
const charterMembershipId = 'charterMembership';
const committeeMembershipId = 'committeeMembership';

/** @type {ERef<import('@agoric/time/src/types.js').TimerService>} */
const chainTimerService = await EV.vat('bootstrap').consumeItem(
'chainTimerService',
);
const chainTimerService: ERef<TimerService> = await EV.vat(
'bootstrap',
).consumeItem('chainTimerService');

let invitationsAccepted = false;

Expand Down Expand Up @@ -328,12 +305,7 @@ export const makeGovernanceDriver = async (
};

return {
/**
* @param {Instance} instance
* @param {object} params
* @param {object} [path]
*/
async changeParams(instance, params, path) {
async changeParams(instance: Instance, params: Object, path?: object) {
instance || Fail`missing instance`;
await ensureInvitationsAccepted();
await proposeParams(instance, params, path);
Expand All @@ -343,8 +315,7 @@ export const makeGovernanceDriver = async (
};
};

/** @param {SwingsetTestKit} testKit */
export const makeZoeDriver = async testKit => {
export const makeZoeDriver = async (testKit: SwingsetTestKit) => {
const { EV } = testKit.runUtils;
const zoe = await EV.vat('bootstrap').consumeItem('zoe');
const chainStorage = await EV.vat('bootstrap').consumeItem('chainStorage');
Expand Down
2 changes: 1 addition & 1 deletion packages/boot/test/bootstrapTests/liquidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
makeGovernanceDriver,
makePriceFeedDriver,
makeWalletFactoryDriver,
} from './drivers.js';
} from './drivers.ts';
import { makeSwingsetTestKit } from './supports.js';

export const scale6 = x => BigInt(Math.round(x * 1_000_000));
Expand Down
2 changes: 2 additions & 0 deletions packages/boot/test/bootstrapTests/supports.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ export const makeRunUtils = (controller, log = (..._) => {}) => {

return harden({ runThunk, EV });
};
/** @typedef {ReturnType<typeof makeRunUtils>} RunUtils */

export const getNodeTestVaultsConfig = async (
bundleDir = 'bundles',
Expand Down Expand Up @@ -495,3 +496,4 @@ export const makeSwingsetTestKit = async (
timer,
};
};
/** @typedef {Awaited<ReturnType<import('./supports.js').makeSwingsetTestKit>>} SwingsetTestKit */
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// @ts-check
import { test as anyTest } from '@agoric/zoe/tools/prepare-test-env-ava.js';

import { PowerFlags } from '@agoric/vats/src/walletFlags.js';

import { makeSwingsetTestKit, keyArrayEqual } from './supports.js';

import type { TestFn } from 'ava';

const { keys } = Object;
/**
* @type {import('ava').TestFn<
* Awaited<ReturnType<typeof makeDefaultTestContext>>
* >}
*/
const test = anyTest;

type DefaultTestContext = Awaited<ReturnType<typeof makeDefaultTestContext>>;

const test: TestFn<DefaultTestContext> = anyTest;

const makeDefaultTestContext = async t => {
const swingsetTestKit = await makeSwingsetTestKit(t, 'bundles/demo-config', {
Expand Down
2 changes: 1 addition & 1 deletion packages/boot/test/bootstrapTests/test-vats-restart.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { promises as fsAmbientPromises } from 'fs';

import { Offers } from '@agoric/inter-protocol/src/clientSupport.js';
import { makeAgoricNamesRemotesFromFakeStorage } from '@agoric/vats/tools/board-utils.js';
import { makeWalletFactoryDriver } from './drivers.js';
import { makeWalletFactoryDriver } from './drivers.ts';
import { makeProposalExtractor, makeSwingsetTestKit } from './supports.js';

const { Fail } = assert;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
makeAgoricNamesRemotesFromFakeStorage,
slotToBoardRemote,
} from '@agoric/vats/tools/board-utils.js';
import { makeWalletFactoryDriver } from './drivers.js';
import { makeWalletFactoryDriver } from './drivers.ts';
import { makeSwingsetTestKit } from './supports.js';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/boot/test/bootstrapTests/test-vaults-upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Far, makeMarshal } from '@endo/marshal';
import { SECONDS_PER_YEAR } from '@agoric/inter-protocol/src/interest.js';
import { makeAgoricNamesRemotesFromFakeStorage } from '@agoric/vats/tools/board-utils.js';
import { makeSwingsetTestKit } from './supports.js';
import { makeWalletFactoryDriver } from './drivers.js';
import { makeWalletFactoryDriver } from './drivers.ts';

// presently all these tests use one collateral manager
const collateralBrandKey = 'ATOM';
Expand Down
2 changes: 1 addition & 1 deletion packages/boot/test/bootstrapTests/test-zcf-upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import processAmbient from 'child_process';
import { promises as fsAmbientPromises } from 'fs';

import { makeAgoricNamesRemotesFromFakeStorage } from '@agoric/vats/tools/board-utils.js';
import { makeZoeDriver } from './drivers.js';
import { makeZoeDriver } from './drivers.ts';
import { makeProposalExtractor, makeSwingsetTestKit } from './supports.js';

const filename = new URL(import.meta.url).pathname;
Expand Down

0 comments on commit 178d4ac

Please sign in to comment.