All files / kernel-test/src/vats stepper-upper-vat.ts

0% Statements 0/33
0% Branches 0/12
0% Functions 0/4
0% Lines 0/33

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                                                                                                                                                         
import { E } from '@endo/eventual-send';
import { makeDefaultExo } from '@metamask/kernel-utils/exo';
import type { Baggage } from '@metamask/ocap-kernel';
 
/**
 * Build function for MaaS consumer.
 *
 * @param _vatPowers - Special powers granted to this vat (not used here).
 * @param parameters - Initialization parameters from the vat's config object.
 * @param parameters.name - The name of the vat.
 * @param baggage - Root of vat's persistent state.
 * @returns The root object for the new vat.
 */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function buildRootObject(
  _vatPowers: unknown,
  parameters: { name?: string },
  baggage: Baggage,
) {
  const name = parameters?.name ?? 'anonymous';
  // eslint-disable-next-line no-console
  console.log(`buildRootObject "${name}"`);
 
  return makeDefaultExo('root', {
    async bootstrap(
      _vats: unknown,
      services: { ocapURLRedemptionService?: unknown },
    ) {
      // eslint-disable-next-line no-console
      console.log(`vat ${name} is bootstrap`);
      const redeemer = services.ocapURLRedemptionService;
      if (!redeemer) {
        // eslint-disable-next-line no-console
        console.log('no ocapURLRedemptionService found');
        throw Error('ocapURLRedemptionService not available');
      }
      baggage.init('redeemer', redeemer);
    },
    async setMaas(url: string) {
      if (!baggage.has('redeemer')) {
        throw Error('ocapURLRedemptionService not available');
      }
      const redeemer = baggage.get('redeemer');
      const maas = await E(redeemer).redeem(url);
      if (!maas) {
        throw Error(`unable to redeem ${url}`);
      }
      if (baggage.has('maas')) {
        baggage.set('maas', maas);
      } else {
        baggage.init('maas', maas);
      }
      baggage.init('previous', 0);
      const result = `MaaS service URL set to ${url}`;
      // eslint-disable-next-line no-console
      console.log(result);
      return result;
    },
    async step() {
      if (!baggage.has('maas')) {
        const message = 'MaaS service address not set';
        // eslint-disable-next-line no-console
        console.log(message);
        throw Error(message);
      }
      const maas = baggage.get('maas');
      const next = await E(maas).next(name);
      const previous = baggage.get('previous') as number;
      const result = `next step: ${String(next)} (last seen was ${previous})`;
      // eslint-disable-next-line no-console
      console.log(result);
      baggage.set('previous', next);
      return result;
    },
  });
}