From 5934343fe6f5b0bff0351383e5ec5e9e0cbe06a6 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Fri, 8 Sep 2023 09:51:03 -0700 Subject: [PATCH] test: convert supports to .ts --- .../bench-vaults-performance.js | 2 +- .../bootstrapTests/bench-vaults-stripped.js | 2 +- packages/boot/test/bootstrapTests/drivers.ts | 2 +- .../boot/test/bootstrapTests/liquidation.js | 2 +- .../{supports.js => supports.ts} | 173 ++++++++---------- .../test/bootstrapTests/test-demo-config.ts | 3 +- .../test/bootstrapTests/test-vats-restart.js | 2 +- .../bootstrapTests/test-vaults-integration.js | 2 +- .../bootstrapTests/test-vaults-upgrade.js | 2 +- .../test/bootstrapTests/test-zcf-upgrade.js | 2 +- .../boot/test/upgrading/test-upgrade-vats.js | 2 +- packages/vats/src/core/lib-boot.js | 1 + 12 files changed, 88 insertions(+), 107 deletions(-) rename packages/boot/test/bootstrapTests/{supports.js => supports.ts} (77%) diff --git a/packages/boot/test/bootstrapTests/bench-vaults-performance.js b/packages/boot/test/bootstrapTests/bench-vaults-performance.js index 4e57e10a5be4..a7553154ae93 100644 --- a/packages/boot/test/bootstrapTests/bench-vaults-performance.js +++ b/packages/boot/test/bootstrapTests/bench-vaults-performance.js @@ -12,7 +12,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 { makeSwingsetTestKit } from './supports.ts'; import { makeWalletFactoryDriver } from './drivers.ts'; /** diff --git a/packages/boot/test/bootstrapTests/bench-vaults-stripped.js b/packages/boot/test/bootstrapTests/bench-vaults-stripped.js index 09432e636ee5..0fa7cac3e7c2 100644 --- a/packages/boot/test/bootstrapTests/bench-vaults-stripped.js +++ b/packages/boot/test/bootstrapTests/bench-vaults-stripped.js @@ -8,7 +8,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 { makeSwingsetTestKit } from './supports.ts'; import { makeWalletFactoryDriver } from './drivers.ts'; // presently all these tests use one collateral manager diff --git a/packages/boot/test/bootstrapTests/drivers.ts b/packages/boot/test/bootstrapTests/drivers.ts index be8ddc9338e5..7abcd957e7c3 100644 --- a/packages/boot/test/bootstrapTests/drivers.ts +++ b/packages/boot/test/bootstrapTests/drivers.ts @@ -22,7 +22,7 @@ import type { WalletFactoryStartResult } from '@agoric/vats/src/core/startWallet 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'; +import type { RunUtils, SwingsetTestKit } from './supports.ts'; export const makeWalletFactoryDriver = async ( runUtils: RunUtils, diff --git a/packages/boot/test/bootstrapTests/liquidation.js b/packages/boot/test/bootstrapTests/liquidation.js index 15670928706e..41060d2fc45c 100644 --- a/packages/boot/test/bootstrapTests/liquidation.js +++ b/packages/boot/test/bootstrapTests/liquidation.js @@ -9,7 +9,7 @@ import { makePriceFeedDriver, makeWalletFactoryDriver, } from './drivers.ts'; -import { makeSwingsetTestKit } from './supports.js'; +import { makeSwingsetTestKit } from './supports.ts'; export const scale6 = x => BigInt(Math.round(x * 1_000_000)); diff --git a/packages/boot/test/bootstrapTests/supports.js b/packages/boot/test/bootstrapTests/supports.ts similarity index 77% rename from packages/boot/test/bootstrapTests/supports.js rename to packages/boot/test/bootstrapTests/supports.ts index 2c11710131d9..cbf2794cbf64 100644 --- a/packages/boot/test/bootstrapTests/supports.js +++ b/packages/boot/test/bootstrapTests/supports.ts @@ -1,43 +1,40 @@ -// @ts-check +/* eslint-disable jsdoc/require-param-type, jsdoc/require-param, @jessie.js/safe-await-separator */ /* global process */ +import childProcessAmbient from 'child_process'; import { promises as fsAmbientPromises } from 'fs'; import { resolve as importMetaResolve } from 'import-meta-resolve'; import { basename } from 'path'; import { inspect } from 'util'; -import childProcessAmbient from 'child_process'; -import { Fail } from '@agoric/assert'; +import { Fail, NonNullish } from '@agoric/assert'; import { buildSwingset } from '@agoric/cosmic-swingset/src/launch-chain.js'; -import { BridgeId, makeTracer, VBankAccount } from '@agoric/internal'; +import { BridgeId, VBankAccount, makeTracer } from '@agoric/internal'; import { unmarshalFromVstorage } from '@agoric/internal/src/marshal.js'; -import { makeFakeStorageKit } from '@agoric/internal/src/storage-test-utils.js'; +import { + FakeStorageKit, + makeFakeStorageKit, +} from '@agoric/internal/src/storage-test-utils.js'; import { initSwingStore } from '@agoric/swing-store'; import { kunser } from '@agoric/swingset-liveslots/test/kmarshal.js'; import { loadSwingsetConfigFile } from '@agoric/swingset-vat'; -import { E } from '@endo/eventual-send'; -import { makeQueue } from '@endo/stream'; -import { TimeMath } from '@agoric/time'; +import { TimeMath, Timestamp } from '@agoric/time'; import { boardSlottingMarshaller, slotToBoardRemote, } from '@agoric/vats/tools/board-utils.js'; +import { makeQueue } from '@endo/stream'; -// to retain for ESlint, used by typedef -E; +import type { SwingsetController } from '@agoric/swingset-vat/src/controller/controller.js'; +import type { BootstrapRootObject } from '@agoric/vats/src/core/lib-boot'; +import type { ExecutionContext } from 'ava'; +import type { E } from '@endo/eventual-send'; const sink = () => {}; const trace = makeTracer('BSTSupport', false); -/** - * @typedef {Awaited< - * ReturnType - * >} BootstrapRootObject - */ - -/** @type {{ [P in keyof BootstrapRootObject]: P }} */ -export const bootstrapMethods = { +export const bootstrapMethods: { [P in keyof BootstrapRootObject]: P } = { bootstrap: 'bootstrap', consumeItem: 'consumeItem', produceItem: 'produceItem', @@ -49,50 +46,41 @@ export const bootstrapMethods = { snapshotStore: 'snapshotStore', }; -/** - * @template {PropertyKey} K - * @template V - * @param {K[]} keys - * @param {(key: K, i: number) => V} valueMaker - */ -const keysToObject = (keys, valueMaker) => { +const keysToObject = ( + keys: K[], + valueMaker: (key: K, i: number) => V, +) => { return Object.fromEntries(keys.map((key, i) => [key, valueMaker(key, i)])); }; /** * AVA's default t.deepEqual() is nearly unreadable for sorted arrays of * strings. - * - * @param {{ - * deepEqual: (a: unknown, b: unknown, message?: string) => void; - * }} t - * @param {PropertyKey[]} a - * @param {PropertyKey[]} b - * @param {string} [message] */ -export const keyArrayEqual = (t, a, b, message) => { +export const keyArrayEqual = ( + t: ExecutionContext, + a: PropertyKey[], + b: PropertyKey[], + message?: string, +) => { const aobj = keysToObject(a, () => 1); const bobj = keysToObject(b, () => 1); return t.deepEqual(aobj, bobj, message); }; -/** - * @param {import('@agoric/swingset-vat/src/controller/controller').SwingsetController} controller - * @param {(..._: any[]) => any} log - */ -export const makeRunUtils = (controller, log = (..._) => {}) => { +export const makeRunUtils = ( + controller: SwingsetController, + log = (..._) => {}, +) => { let cranksRun = 0; const mutex = makeQueue(); mutex.put(controller.run()); - /** - * @template {() => any} T - * @param {T} thunk - * @returns {Promise>} - */ - const runThunk = async thunk => { + const runThunk = async any>( + thunk: T, + ): Promise> => { try { // this promise for the last lock may fail await mutex.get(); @@ -111,7 +99,7 @@ export const makeRunUtils = (controller, log = (..._) => {}) => { return result; }; - const runMethod = async (method, args = []) => { + const runMethod = async (method: string, args: object[] = []) => { log('runMethod', method, args, 'at', cranksRun); assert(Array.isArray(args)); @@ -132,15 +120,13 @@ export const makeRunUtils = (controller, log = (..._) => {}) => { } }; - /** - * @type {typeof E & { - * sendOnly: (presence: unknown) => Record void>; - * vat: (name: string) => Record Promise>; - * rawBoot: Record Promise>; - * }} - */ - // @ts-expect-error cast, approximate - const EV = presence => + type EVProxy = typeof E & { + sendOnly: (presence: unknown) => Record void>; + vat: (name: string) => Record Promise>; + rawBoot: Record Promise>; + }; + // @ts-expect-error XXX casting + const EV: EVProxy = presence => new Proxy(harden({}), { get: (_t, methodName, _rx) => harden((...args) => @@ -151,6 +137,7 @@ export const makeRunUtils = (controller, log = (..._) => {}) => { new Proxy(harden({}), { get: (_t, methodName, _rx) => harden((...args) => { + assert.string(methodName); if (name === 'meta') { return runMethod(methodName, args); } @@ -159,6 +146,7 @@ export const makeRunUtils = (controller, log = (..._) => {}) => { }); EV.rawBoot = new Proxy(harden({}), { get: (_t, methodName, _rx) => + // @ts-expect-error FIXME runMethod takes string but proxy allows symbol harden((...args) => runMethod(methodName, args)), }); // @ts-expect-error xxx @@ -174,7 +162,7 @@ export const makeRunUtils = (controller, log = (..._) => {}) => { ]), }, ); - // @ts-expect-error xxx + // @ts-expect-error 'get' is a read-only property EV.get = presence => new Proxy(harden({}), { get: (_t, pathElement, _rx) => @@ -183,7 +171,7 @@ export const makeRunUtils = (controller, log = (..._) => {}) => { return harden({ runThunk, EV }); }; -/** @typedef {ReturnType} RunUtils */ +export type RunUtils = ReturnType; export const getNodeTestVaultsConfig = async ( bundleDir = 'bundles', @@ -192,10 +180,9 @@ export const getNodeTestVaultsConfig = async ( const fullPath = await importMetaResolve(specifier, import.meta.url).then( u => new URL(u).pathname, ); - const config = /** @type {SwingSetConfig & { coreProposals?: any[] }} */ ( - await loadSwingsetConfigFile(fullPath) + const config: SwingSetConfig & { coreProposals?: any[] } = NonNullish( + await loadSwingsetConfigFile(fullPath), ); - assert(config); // speed up (e.g. 80s vs 133s with xs-worker in production config) config.defaultManagerType = 'local'; @@ -219,12 +206,12 @@ export const getNodeTestVaultsConfig = async ( return testConfigPath; }; -/** - * @param {object} powers - * @param {Pick} powers.childProcess - * @param {typeof import('node:fs/promises')} powers.fs - */ -export const makeProposalExtractor = ({ childProcess, fs }) => { +interface Powers { + childProcess: Pick; + fs: typeof import('node:fs/promises'); +} + +export const makeProposalExtractor = ({ childProcess, fs }: Powers) => { const getPkgPath = (pkg, fileName = '') => new URL(`../../../${pkg}/${fileName}`, import.meta.url).pathname; @@ -241,8 +228,7 @@ export const makeProposalExtractor = ({ childProcess, fs }) => { harden(JSON.parse(await fs.readFile(filePath, 'utf8'))); // XXX parses the output to find the files but could write them to a path that can be traversed - /** @param {string} txt */ - const parseProposalParts = txt => { + const parseProposalParts = (txt: string) => { const evals = [ ...txt.matchAll(/swingset-core-eval (?\S+) (?