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

26.08% Statements 6/23
0% Branches 0/4
20% Functions 2/10
30% Lines 6/20

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 9821x                                     21x       4x 4x   4x                                                                                                         4x                                    
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;
}) => void;
 
export const setupOcapKernelMock = (): {
  resetMocks: ResetMocks;
  setMockBehavior: SetMockBehavior;
} => {
  let isVatConfigMock = true;
  let isVatIdMock = 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),
    });
 
    return {
      isVatId: () => isVatIdMock,
      isVatConfig: () => isVatConfigMock,
      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;
    },
    setMockBehavior: (options: {
      isVatConfig?: boolean;
      isVatId?: boolean;
    }): void => {
      if (typeof options.isVatConfig === 'boolean') {
        isVatConfigMock = options.isVatConfig;
      }
      if (typeof options.isVatId === 'boolean') {
        isVatIdMock = options.isVatId;
      }
    },
  };
};