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

0% Statements 0/23
0% Branches 0/7
0% Functions 0/7
0% Lines 0/23

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                                                                                                                                         
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.
 *
 * The `repl` endowment is an `IOListener`, so the vat accepts connections
 * from it and keeps each one separately. `doRead`/`doWrite` name a
 * connection by index so a test can drive several peers independently.
 *
 * @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 listener: unknown;
  const connections: unknown[] = [];
  const readBuffer: string[] = [];
 
  return makeDefaultExo('root', {
    async bootstrap(_vats: unknown, services: { repl: unknown }) {
      tlog('bootstrap');
      listener = services.repl;
    },
    /**
     * Accept the next waiting connection, appending it to the list.
     *
     * @returns The index of the accepted connection, or -1 on EOF.
     */
    async doAccept() {
      const connection = await E(listener).accept();
      if (!connection) {
        tlog('accept: listener closed');
        return -1;
      }
      connections.push(connection);
      const index = connections.length - 1;
      tlog(`accepted connection ${index}`);
      return index;
    },
    async doRead(index = 0) {
      const line = await E(connections[index]).read();
      tlog(`read[${index}]: ${line}`);
      readBuffer.push(String(line));
      return line;
    },
    async doWrite(data: string, index = 0) {
      await E(connections[index]).write(data);
      tlog(`wrote[${index}]: ${data}`);
    },
    async getReadBuffer() {
      return [...readBuffer];
    },
    async getConnectionCount() {
      return connections.length;
    },
  });
}