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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 | 68x 40x 39x 39x 39x 16x 16x 67x 67x 1x 67x 67x 67x 3x 414x 414x 414x 414x 414x 414x 414x 3x 45x 419x 7x 45x 4314x 224x 224x 4x 4x 18x 70x 70x 70x 18x 4x 34x 4x | // eslint-disable-next-line spaced-comment
/// <reference types="vite/client" />
import type { KernelDatabase } from '@metamask/kernel-store';
import { stringify, waitUntilQuiescent } from '@metamask/kernel-utils';
import {
Logger,
makeArrayTransport,
makeConsoleTransport,
} from '@metamask/logger';
import type { LogEntry } from '@metamask/logger';
import { Kernel, kunser } from '@metamask/ocap-kernel';
import type { ClusterConfig, PlatformServices } from '@metamask/ocap-kernel';
import { NodejsPlatformServices } from '@ocap/nodejs';
import { vi } from 'vitest';
/**
* Construct a bundle path URL from a bundle name.
*
* @param bundleName - The name of the bundle.
*
* @returns a path string for the named bundle.
*/
export function getBundleSpec(bundleName: string): string {
return new URL(`./vats/${bundleName}.bundle`, import.meta.url).toString();
}
/**
* Run the set of test vats.
*
* @param kernel - The kernel to run in.
* @param config - Subcluster configuration telling what vats to run.
*
* @returns the bootstrap result.
*/
export async function runTestVats(
kernel: Kernel,
config: ClusterConfig,
): Promise<unknown> {
const { bootstrapResult } = await kernel.launchSubcluster(config);
await waitUntilQuiescent();
Iif (bootstrapResult === undefined) {
throw Error(`this can't happen but eslint is stupid`);
}
return kunser(bootstrapResult);
}
/**
* Send the `resume message to the root of one of the test vats.
*
* @param kernel - Our kernel.
* @param rootRef - KRef of the object to which the message is sent.
*
* @returns the result returned from `resume`.
*/
export async function runResume(
kernel: Kernel,
rootRef: string,
): Promise<unknown> {
const resumeResultRaw = await kernel.queueMessage(rootRef, 'resume', []);
return kunser(resumeResultRaw);
}
/**
* Handle all the boilerplate to set up a kernel instance.
*
* @param kernelDatabase - The database that will hold the persistent state.
* @param resetStorage - If true, reset the database as part of setting up.
* @param logger - The logger to use for the kernel.
* @param workerFilePath - The path to the worker file to use for the vat workers.
* @param platformServices - The platform services client to use for the kernel.
* @param keySeed - Optional seed for libp2p key generation.
*
* @returns the new kernel instance.
*/
export async function makeKernel(
kernelDatabase: KernelDatabase,
resetStorage: boolean,
logger: Logger,
workerFilePath?: string,
platformServices?: PlatformServices,
keySeed?: string,
): Promise<Kernel> {
const platformServicesConfig: { logger: Logger; workerFilePath?: string } = {
logger: logger.subLogger({ tags: ['vat-worker-manager'] }),
};
if (workerFilePath) {
platformServicesConfig.workerFilePath = workerFilePath;
}
const platformServicesClient =
platformServices ?? new NodejsPlatformServices(platformServicesConfig);
const kernel = await Kernel.make(platformServicesClient, kernelDatabase, {
resetStorage,
logger,
keySeed,
});
return kernel;
}
/**
* De-interleave various vats' output to squeeze out interprocess I/O
* non-determinism in CI.
*
* @param logs - An array of log lines.
*
* @returns `logs` sorted by vat.
*/
export function sortLogs(logs: string[]): string[] {
logs.sort((a: string, b: string): number => {
const colonA = a.indexOf(':');
Iif (colonA < 0) {
return 0;
}
const prefixA = a.substring(0, colonA);
const colonB = b.indexOf(':');
Iif (colonB < 0) {
return 0;
}
const prefixB = b.substring(0, colonB);
return prefixA.localeCompare(prefixB);
});
return logs;
}
/**
* Convert a list of log entries into a list of lines suitable for examination.
*
* @param entries - The list of log entries to convert.
* @param withTags - The tags to filter by.
*
* @returns the relevant contents of `entries`, massaged for use.
*/
export function extractTestLogs(
entries: LogEntry[],
...withTags: string[]
): string[] {
const hasTag =
withTags.length > 0
? (tags: string[]) => withTags.some((tag) => tags.includes(tag))
: () => true;
return entries
.filter(({ tags }) => tags.includes('test') && hasTag(tags))
.map(({ message }) => message ?? '')
.filter((message) => message.length > 0);
}
/**
* Parse a message body into a JSON object.
*
* @param body - The message body to parse.
*
* @returns The parsed JSON object, or the original body if parsing fails.
*/
export function parseReplyBody(body: string): unknown {
try {
return JSON.parse(body.slice(1));
} catch {
return body;
}
}
/**
* Debug the database.
*
* @param kernelDatabase - The database to debug.
* @param logger - The logger to use for the database.
*/
export function logDatabase(
kernelDatabase: KernelDatabase,
logger: Logger = console as unknown as Logger,
): void {
const result = kernelDatabase.executeQuery('SELECT * FROM kv');
logger.log('kv result', stringify(result));
}
/**
* Create a logger that records log entries in an array.
*
* @returns A logger that records log entries in an array.
*/
export const makeTestLogger = (): { logger: Logger; entries: LogEntry[] } => {
const entries: LogEntry[] = [];
const logger = new Logger({
transports: [makeConsoleTransport(), makeArrayTransport(entries)],
});
return { logger, entries };
};
/**
* Create a mock logger that can be used to spy on the logger methods.
* Derived sub-loggers will invoke the parent logger methods directly.
* The injectStream method is a no-op.
*
* @returns A mock logger.
*/
export const makeMockLogger = (): Logger => {
const mockLogger = {
log: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
info: vi.fn(),
debug: vi.fn(),
subLogger: vi.fn(() => mockLogger),
injectStream: vi.fn(),
} as unknown as Logger;
return mockLogger;
};
|