Skip to content

Commit

Permalink
fix(lint): tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mujahidkay committed Sep 20, 2024
1 parent c101de1 commit c11a48e
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 24 deletions.
14 changes: 13 additions & 1 deletion contract/test/test-build-proposal.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
/* eslint-disable import/order -- https://github.com/endojs/endo/issues/1235 */
import { test } from './prepare-test-env-ava.js';
import { test as anyTest } from './prepare-test-env-ava.js';
import { execFile } from 'node:child_process';
import { promises as fs } from 'node:fs';
import { Buffer } from 'buffer';
import { gzip } from 'zlib';
import { promisify } from 'util';
import { makeNodeBundleCache } from '@endo/bundle-source/cache.js';

/**
* @typedef {{
* compressBundle: (name: string) => Promise<{ bundle: any; compressed: Buffer; }>,
* $: (file: string, ...args: string[]) => Promise<string>,
* runPackageScript: (scriptName: string, ...args: string[]) => Promise<string>,
* listBundles: (bundleDir?: string) => Promise<string[]>,
* }} TestContext
*/

const test = /** @type {import('ava').TestFn<TestContext>}} */ (anyTest);

test.before(async t => {
const bundleCache = await makeNodeBundleCache('bundles', {}, s => import(s));

Expand All @@ -15,6 +26,7 @@ test.before(async t => {
*/
const compressBundle = async name => {
const rootPath = undefined; // not needed since bundle is already cached
// @ts-expect-error intentionally passing undefined
const bundle = await bundleCache.load(rootPath, name);
const fileContents = JSON.stringify(bundle);
const buffer = Buffer.from(fileContents, 'utf-8');
Expand Down
4 changes: 3 additions & 1 deletion contract/test/test-bundle-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import { E, passStyleOf } from '@endo/far';
import { makeZoeKitForTest } from '@agoric/zoe/tools/setup-zoe.js';

const myRequire = createRequire(import.meta.url);
const contractPath = myRequire.resolve(`../src/sell-concert-tickets.contract.js`);
const contractPath = myRequire.resolve(
`../src/sell-concert-tickets.contract.js`,
);

test('bundleSource() bundles the contract for use with zoe', async t => {
const bundle = await bundleSource(contractPath);
Expand Down
72 changes: 65 additions & 7 deletions contract/test/test-postalSvc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// @ts-check
/* global setTimeout, fetch */
// XXX what's the state-of-the-art in ava setup?
// eslint-disable-next-line import/order
import { test as anyTest } from './prepare-test-env-ava.js';
Expand Down Expand Up @@ -28,8 +27,65 @@ import {
makeAgoricNames,
} from '../tools/ui-kit-goals/name-service-client.js';

/** @type {import('ava').TestFn<Awaited<ReturnType<makeTestContext>>>} */
const test = anyTest;
/**
* @import {CachedBundle} from './boot-tools.js';
* @import {RemotableBrand} from '@endo/eventual-send';
* @import {Payment, Brand} from '@agoric/ertp/src/types.js';
* @import {OfferSpec} from '@agoric/smart-wallet/src/offers.js';
* @import {UpdateRecord} from '@agoric/smart-wallet/src/smartWallet.js';
* @import {WellKnown} from './market-actors.js';
*/

/** @typedef {Awaited<ReturnType<import('@endo/bundle-source/cache.js').makeNodeBundleCache>>} BundleCache */

/**
* @typedef {{
* makeQueryTool: () => {
* toCapData: (x: any) => any;
* fromCapData: (d: any) => any;
* queryData: (path: any) => Promise<any>;
* } & RemotableBrand<{}, {
* toCapData: (x: any) => any;
* fromCapData: (d: any) => any;
* queryData: (path: any) => Promise<any>;
* }>,
* installBundles: (bundleRoots: Record<string, string>, progress: typeof console.log) => Promise<Record<string, CachedBundle>>,
* runCoreEval: ({ behavior, config, entryFile, name}: {
* behavior: any;
* config: any;
* entryFile: any;
* name: any;
* }) => Promise<{
* proposal_id: number;
* content: {
* '@type': string;
* };
* status: string;
* voting_end_time: string;}>,
* provisionSmartWallet: (addr: any, balances: any) => Promise<{
* deposit: {
* receive: (pmt: Payment) => Promise<any>;
* } & RemotableBrand<{}, {
* receive: (pmt: Payment) => Promise<any>;
* }>;
* offers:{
* executeOffer: (offerSpec: OfferSpec) => AsyncGenerator<UpdateRecord>;
* tryExit: (id: any) => any;
* } & RemotableBrand<{}, {
* executeOffer: (offerSpec: OfferSpec) => AsyncGenerator<UpdateRecord>;
* tryExit: (id: any) => any;
* }>;
* peek: {
* purseUpdates: (brand: Brand) => AsyncGenerator<any, void, unknown>;
* } & RemotableBrand<{}, {
* purseUpdates: (brand: Brand) => AsyncGenerator<any, void, unknown>;
* }>;}>,
* bundleCache: BundleCache,
* shared: Record<string, any>,
* }} TestContext
*/

const test = /** @type {import('ava').TestFn<TestContext>}} */ (anyTest);

const nodeRequire = createRequire(import.meta.url);

Expand All @@ -41,7 +97,6 @@ const scriptRoots = {
postalService: nodeRequire.resolve('../src/postal-service.proposal.js'),
};

/** @param {import('ava').ExecutionContext} t */
const makeTestContext = async t => {
const bc = await makeBundleCacheContext(t);

Expand Down Expand Up @@ -83,6 +138,8 @@ test.before(async t => (t.context = await makeTestContext(t)));
test.serial('well-known brand (ATOM) is available', async t => {
const { makeQueryTool } = t.context;
const hub0 = makeAgoricNames(makeQueryTool());
/** @type {WellKnown} */
// @ts-expect-error cast
const agoricNames = makeNameProxy(hub0);
await null;
const brand = {
Expand Down Expand Up @@ -137,6 +194,8 @@ test.serial('deploy contract with core eval: postalService / send', async t => {
test.serial('agoricNames.instances has contract: postalService', async t => {
const { makeQueryTool } = t.context;
const hub0 = makeAgoricNames(makeQueryTool());
/** @type {WellKnown} */
// @ts-expect-error cast
const agoricNames = makeNameProxy(hub0);
await null;
const instance = await agoricNames.instance.postalService;
Expand All @@ -150,7 +209,8 @@ test.serial('deliver payment using offer', async t => {
const { provisionSmartWallet, makeQueryTool } = t.context;
const qt = makeQueryTool();
const hub0 = makeAgoricNames(qt);
/** @type {import('./market-actors.js').WellKnown} */
/** @type {WellKnown} */
// @ts-expect-error cast
const agoricNames = makeNameProxy(hub0);

await null;
Expand Down Expand Up @@ -210,8 +270,6 @@ test('send invitation* from contract using publicFacet of postalService', async
smartWalletIssuers,
);

/** @type {StartedInstanceKit<import('../src/postal-service.contract.js').PostalServiceFn>['instance']} */
// @ts-expect-error not (yet?) in BootstrapPowers
const instance = await powers.instance.consume.postalService;

const shared = {
Expand Down
28 changes: 20 additions & 8 deletions contract/test/test-sell-concert-tickets-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,30 @@ import {
} from '../src/platform-goals/board-aux.core.js';
import { extract } from '@agoric/vats/src/core/utils.js';

/**
* @import {ERef} from '@endo/far';
* @import {Instance} from '@agoric/zoe/src/zoeService/utils.js';
* @import {Purse, NatValue} from '@agoric/ertp/src/types.js';
*/
/** @typedef {typeof import('../src/sell-concert-tickets.contract.js').start} AssetContractFn */

/** @typedef {Awaited<ReturnType<import('@endo/bundle-source/cache.js').makeNodeBundleCache>>} BundleCache */

/**
* @typedef {{
* zoe: ZoeService,
* bundle: any,
* bundleCache: BundleCache,
* feeMintAccess: FeeMintAccess
* }} TestContext
*/

const myRequire = createRequire(import.meta.url);
const contractPath = myRequire.resolve(
`../src/sell-concert-tickets.contract.js`,
);

/** @type {import('ava').TestFn<Awaited<ReturnType<makeTestContext>>>} */
const test = anyTest;
const test = /** @type {import('ava').TestFn<TestContext>}} */ (anyTest);

const UNIT6 = 1_000_000n;
const CENT = UNIT6 / 100n;
Expand All @@ -47,8 +62,7 @@ const CENT = UNIT6 / 100n;
*
* See test-bundle-source.js for basic use of bundleSource().
* Here we use a bundle cache to optimize running tests multiple times.
*
* @param {unknown} _t
* @param {import('ava').ExecutionContext} _t
*/
const makeTestContext = async _t => {
const { zoeService: zoe, feeMintAccess } = makeZoeKitForTest();
Expand Down Expand Up @@ -108,7 +122,7 @@ test('Start the contract', async t => {
*
* @param {import('ava').ExecutionContext} t
* @param {ERef<ZoeService>} zoe
* @param {ERef<import('@agoric/zoe/src/zoeService/utils').Instance<AssetContractFn>>} instance
* @param {ERef<Instance<AssetContractFn>>} instance
* @param {Purse} purse
* @param {[string, NatValue][]} choices
* @param {boolean} expectSuccessfulTrade
Expand All @@ -125,7 +139,6 @@ const alice = async (
expectSuccessfulTrade = true,
) => {
const publicFacet = E(zoe).getPublicFacet(instance);
// @ts-expect-error Promise<Instance> seems to work
const terms = await E(zoe).getTerms(instance);
const { issuers, brands } = terms;

Expand Down Expand Up @@ -241,8 +254,7 @@ test('use the code that will go on chain to start the contract', async t => {
options: { sellConcertTickets: { bundleID } },
}),
]);
/** @type {import('../src/sell-concert-tickets.proposal.js').SellTicketsSpace} */
// @ts-expect-error cast

const sellSpace = powers;
const instance = await sellSpace.instance.consume.sellConcertTickets;

Expand Down
30 changes: 28 additions & 2 deletions contract/test/test-swap-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,36 @@ import {
assets as govAssets,
} from './lib-gov-test/puppet-gov.js';

/**
* @import {BootstrapPowers} from '../src/types.js';
* @import {Amount} from '@agoric/ertp/src/types.js';
*/

/** @typedef {import('./wallet-tools.js').MockWallet} MockWallet */

/** @type {import('ava').TestFn<Awaited<ReturnType<makeTestContext>>>} */
const test = anyTest;
/** @typedef {Awaited<ReturnType<import('@endo/bundle-source/cache.js').makeNodeBundleCache>>} BundleCache */

/**
* @typedef {{
* getExitMessage: () => any;
* getHasExited: () => boolean;
* getExitWithFailure: () => any;
* installBundle: (id: any, bundle: any) => any;
* installNamedBundle: (name: any, id: any, bundle: any) => any;
* getCriticalVatKey: () => {};
* getVatPowers: () => { D: (bcap: any) => { getBundle: () => any } };
* }} FakeVatAdminState
*/

/**
* @typedef {{
* bundleCache: BundleCache,
* powers: BootstrapPowers,
* vatAdminState: FakeVatAdminState,
* shared: Record<string, any>,
* }} TestContext
*/
const test = /** @type {import('ava').TestFn<TestContext>}} */ (anyTest);

const nodeRequire = createRequire(import.meta.url);

Expand Down
39 changes: 34 additions & 5 deletions contract/test/test-vote-by-committee.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,39 @@ import { mockWalletFactory, seatLike } from './wallet-tools.js';
import { INVITATION_MAKERS_DESC } from '../src/platform-goals/start-governed-contract.js';
import { installGovContracts } from './lib-gov-test/puppet-gov.js';

/**
* @import {BootstrapPowers} from '../src/types.js';
* @import {Brand} from '@agoric/ertp/src/types.js';
* @import {QuestionDetails} from '@agoric/governance/src/types.js';
* @import {OfferSpec} from '@agoric/smart-wallet/src/offers.js';
*/

/** @typedef {import('./wallet-tools.js').MockWallet} MockWallet */

/** @type {import('ava').TestFn<Awaited<ReturnType<makeTestContext>>>} */
const test = anyTest;
/** @typedef {Awaited<ReturnType<import('@endo/bundle-source/cache.js').makeNodeBundleCache>>} BundleCache */

/**
* @typedef {{
* getExitMessage: () => any;
* getHasExited: () => boolean;
* getExitWithFailure: () => any;
* installBundle: (id: any, bundle: any) => any;
* installNamedBundle: (name: any, id: any, bundle: any) => any;
* getCriticalVatKey: () => {};
* getVatPowers: () => { D: (bcap: any) => { getBundle: () => any } };
* }} FakeVatAdminState
*/

/**
* @typedef {{
* bundleCache: BundleCache,
* powers: BootstrapPowers,
* vatAdminState: FakeVatAdminState,
* shared: Record<string, any>,
* }} TestContext
*/

const test = /** @type {import('ava').TestFn<TestContext>}} */ (anyTest);

const nodeRequire = createRequire(import.meta.url);

Expand Down Expand Up @@ -58,7 +87,7 @@ const makeVoter = (t, wallet, wellKnown) => {
};

const acceptInvitation = async (offerId, { instance, description }) => {
/** @type {import('./wallet-tools.js').OfferSpec} */
/** @type {OfferSpec} */
const offer = {
id: offerId,
invitationSpec: {
Expand Down Expand Up @@ -118,7 +147,7 @@ const makeVoter = (t, wallet, wellKnown) => {
const vote = async (offerId, details, position) => {
const chosenPositions = [details.positions[position]];

/** @type {import('./wallet-tools.js').OfferSpec} */
/** @type {OfferSpec} */
const offer = {
id: offerId,
invitationSpec: {
Expand Down Expand Up @@ -239,7 +268,7 @@ test.serial('vote to change swap fee', async t => {
/** @type {ReturnType<typeof makeVoter>} */
const victor = t.context.shared.victor;

/** @type {BootstrapPowers & import('../src/swaparoo.proposal.js').SwaparooSpace */
/** @type {BootstrapPowers & import('../src/swaparoo.proposal.js').SwaparooSpace} */
// @ts-expect-error cast
const swapPowers = powers;
const swapPub = E(zoe).getPublicFacet(
Expand Down

0 comments on commit c11a48e

Please sign in to comment.