All files / kernel-test/src/vats io-vat.ts

0% Statements 0/13
0% Branches 0/3
0% Functions 0/5
0% Lines 0/13

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                                                                                         
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 testing IO kernel services.
 *
 * @param vatPowers - Special powers granted to this vat.
 * @param parameters - Initialization parameters from the vat's config object.
 * @param parameters.name - The name of the vat.
 * @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 } = {},
) {
  const name = parameters?.name ?? 'io-vat';
  const tlog = unwrapTestLogger(vatPowers, name);
  let ioService: unknown;
  const readBuffer: string[] = [];
 
  return makeDefaultExo('root', {
    async bootstrap(_vats: unknown, services: { repl: unknown }) {
      tlog('bootstrap');
      ioService = services.repl;
    },
    async doRead() {
      const line = await E(ioService).read();
      tlog(`read: ${line}`);
      readBuffer.push(String(line));
      return line;
    },
    async doWrite(data: string) {
      await E(ioService).write(data);
      tlog(`wrote: ${data}`);
    },
    async getReadBuffer() {
      return [...readBuffer];
    },
  });
}