All files / repo-tools/src/test-utils/env mock-kernel.ts

65.62% Statements 21/32
37.5% Branches 3/8
38.46% Functions 5/13
75% Lines 21/28

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 109 110 111 112 113 11421x                                       21x       4x 4x 4x   4x 1x 1x 1x 1x           1x           1x   1x                                                                                       4x   10x 10x 10x             1x     1x     1x 1x          
import {
  object,
  define,
  literal,
  string,
  boolean,
  record,
  exactOptional,
  array,
  type,
} from '@metamask/superstruct';
import { vi } from 'vitest';
 
type ResetMocks = () => void;
type SetMockBehavior = (options: {
  isVatConfig?: boolean;
  isVatId?: boolean;
  isKRef?: boolean;
}) => void;
 
export const setupOcapKernelMock = (): {
  resetMocks: ResetMocks;
  setMockBehavior: SetMockBehavior;
} => {
  let isVatConfigMock = true;
  let isVatIdMock = true;
  let isKRefMock = true;
  // Mock implementation
  vi.doMock('@metamask/ocap-kernel', () => {
    const VatIdStruct = define<unknown>('VatId', () => isVatIdMock);
    const VatConfigStruct = define<unknown>('VatConfig', () => isVatConfigMock);
    const SubclusterIdStruct = define<unknown>('SubclusterId', () => true);
    const ClusterConfigStruct = object({
      bootstrap: string(),
      forceReset: exactOptional(boolean()),
      vats: record(string(), VatConfigStruct),
      bundles: exactOptional(record(string(), VatConfigStruct)),
    });
    const SubclusterStruct = object({
      id: SubclusterIdStruct,
      config: ClusterConfigStruct,
      vats: array(VatIdStruct),
    });
 
    const KRefStruct = define<unknown>('KRef', () => isKRefMock);
 
    return {
      isVatId: () => isVatIdMock,
      isVatConfig: () => isVatConfigMock,
      isKRef: () => isKRefMock,
      insistKRef: (value: unknown) => {
        if (!isKRefMock) {
          throw new Error(`Expected KRef, got ${String(value)}`);
        }
      },
      KRefStruct,
      VatIdStruct,
      VatConfigStruct,
      SubclusterIdStruct,
      SubclusterStruct,
      CapDataStruct: object({
        body: string(),
        slots: array(string()),
      }),
      ClusterConfigStruct,
      KernelStatusStruct: type({
        subclusters: array(SubclusterStruct),
        vats: array(
          object({
            id: VatIdStruct,
            config: VatConfigStruct,
            subclusterId: SubclusterIdStruct,
          }),
        ),
      }),
      KernelSendMessageStruct: object({
        id: literal('v0'),
        payload: object({
          method: literal('ping'),
          params: literal(null),
        }),
      }),
      PlatformServicesCommandMethod: {
        launch: 'launch',
        terminate: 'terminate',
        terminateAll: 'terminateAll',
      },
    };
  });
 
  return {
    resetMocks: (): void => {
      isVatConfigMock = true;
      isVatIdMock = true;
      isKRefMock = true;
    },
    setMockBehavior: (options: {
      isVatConfig?: boolean;
      isVatId?: boolean;
      isKRef?: boolean;
    }): void => {
      Iif (typeof options.isVatConfig === 'boolean') {
        isVatConfigMock = options.isVatConfig;
      }
      Iif (typeof options.isVatId === 'boolean') {
        isVatIdMock = options.isVatId;
      }
      Eif (typeof options.isKRef === 'boolean') {
        isKRefMock = options.isKRef;
      }
    },
  };
};