All files / kernel-test/src/vats promise-arg-vat.ts

0% Statements 0/43
0% Branches 0/14
0% Functions 0/10
0% Lines 0/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                       
import { E } from '@endo/eventual-send';
import { makeDefaultExo } from '@metamask/kernel-utils/exo';
 
import { unwrapTestLogger } from '../test-powers.ts';
import type { TestPowers } from '../test-powers.ts';
 
/**
 * Build function for vats that will run various tests.
 *
 * @param vatPowers - Special powers granted to this vat.
 * @param vatPowers.logger - The logger for the vat.
 * @param parameters - Initialization parameters from the vat's config object.
 * @param parameters.name - The name of the vat.
 * @param parameters.test - The test to run.
 * @param _baggage - Root of vat's persistent state (not used here).
 * @returns The root object for the new vat.
 */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function buildRootObject(
  vatPowers: TestPowers,
  parameters: { name?: string; test?: string } = {},
  _baggage: unknown = null,
) {
  const name = parameters?.name ?? 'anonymous';
  const test = parameters?.test ?? 'unspecified';
  const tlog = unwrapTestLogger(vatPowers, name);
 
  /**
   * Print a message to the log.
   *
   * @param message - The message to print.
   */
  function log(message: string): void {
    // eslint-disable-next-line no-console
    console.log(`${name}: ${message}`);
  }
 
  log(`buildRootObject`);
  log(`configuration parameters: ${JSON.stringify(parameters)}`);
 
  return makeDefaultExo('root', {
    async bootstrap(vats: { bob: unknown }) {
      log(`bootstrap start`);
      tlog(`running test ${test}`);
      let doneP: Promise<unknown> = Promise.resolve('no activity');
      if (!['promiseArg1', 'promiseArg2', 'promiseArg3'].includes(test)) {
        throw Error(`unknown test ${test}`);
      }
      let resolver: ((value: unknown) => void) | undefined;
      const param = new Promise((resolve, _reject) => {
        resolver = resolve;
      });
      if (test === 'promiseArg2') {
        tlog(`resolving the promise that will be sent to Bob`);
        resolver?.(`${name} said hi before send`);
      }
      tlog(`sending the promise to Bob`);
      const responseFromBob = E(vats.bob).hereIsAPromise(param);
      if (test === 'promiseArg1') {
        tlog(`resolving the promise that was sent to Bob`);
        resolver?.(`${name} said hi after send`);
      }
      tlog(`awaiting Bob's response`);
      doneP = responseFromBob.then(
        async (res: unknown) => {
          const [bobDoneP, bobDoneMsg] = res as [Promise<unknown>, string];
          tlog(`Bob's response to hereIsAPromise: '${bobDoneMsg}'`);
          if (test === 'promiseArg3') {
            tlog(`resolving the promise that was sent to Bob`);
            resolver?.(`${name} said hi after Bob's reply`);
          }
          return bobDoneP;
        },
        (rej: unknown) => {
          tlog(`Bob's response to hereIsAPromise rejected as '${String(rej)}'`);
          return 'bobFail';
        },
      );
      await E(vats.bob).loopback();
      return await doneP;
    },
 
    // This is a hack that effectively does the job of stdout.flush() even
    // though we don't have access to stdout itself here. It makes sure we
    // capture all the log output prior to the return value from `bootstrap`
    // resolving.
    loopback() {
      return undefined;
    },
 
    async hereIsAPromise(promise: Promise<unknown>) {
      log(`hereIsAPromise start`);
      const doneP = promise.then(
        (res: unknown) => {
          tlog(`the promise parameter resolved to '${String(res)}'`);
          return 'bobPSucc';
        },
        (rej: unknown) => {
          tlog(`the promise parameter rejected as '${String(rej)}'`);
          return 'bobPFail';
        },
      );
      log(`hereIsAPromise done`);
      return [doneP, `${name}.hereIsAPromise done`];
    },
  });
}