|
| 1 | +import { makeSQLKernelDatabase } from '@metamask/kernel-store/sqlite/nodejs'; |
| 2 | +import { Logger } from '@metamask/logger'; |
| 3 | +import type { LogEntry } from '@metamask/logger'; |
| 4 | +import type { Kernel } from '@metamask/ocap-kernel'; |
| 5 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 6 | + |
| 7 | +import { |
| 8 | + extractTestLogs, |
| 9 | + getBundleSpec, |
| 10 | + makeKernel, |
| 11 | + makeTestLogger, |
| 12 | +} from './utils.ts'; |
| 13 | + |
| 14 | +describe('cluster initialization', { timeout: 4_000 }, () => { |
| 15 | + let logger: Logger; |
| 16 | + let entries: LogEntry[]; |
| 17 | + let kernel: Kernel; |
| 18 | + |
| 19 | + type When = 'global' | 'build' | 'bootstrap'; |
| 20 | + type What = 'throw' | 'uncaught-rejection'; |
| 21 | + |
| 22 | + beforeEach(async () => { |
| 23 | + const testLogger = makeTestLogger(); |
| 24 | + logger = testLogger.logger; |
| 25 | + entries = testLogger.entries; |
| 26 | + const database = await makeSQLKernelDatabase({}); |
| 27 | + kernel = await makeKernel( |
| 28 | + database, |
| 29 | + true, |
| 30 | + logger.subLogger({ tags: ['test'] }), |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + const launch = async (scenario: `${When}-${What}`) => |
| 35 | + kernel.launchSubcluster({ |
| 36 | + bootstrap: 'main', |
| 37 | + vats: { |
| 38 | + main: { |
| 39 | + bundleSpec: getBundleSpec(`error-${scenario}`), |
| 40 | + parameters: {}, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }); |
| 44 | + |
| 45 | + it('throws if globals scope throws', async () => { |
| 46 | + await expect(launch('global-throw')).rejects.toThrow(/from global scope/u); |
| 47 | + |
| 48 | + const vatLogs = extractTestLogs(entries, 'console'); |
| 49 | + expect(vatLogs).toStrictEqual(['global throw']); |
| 50 | + }); |
| 51 | + |
| 52 | + it.todo('throws if global scope has an uncaught rejection', async () => { |
| 53 | + await expect(launch('global-uncaught-rejection')).rejects.toThrow( |
| 54 | + expect.objectContaining({ |
| 55 | + message: expect.stringContaining('Subcluster initialization failed'), |
| 56 | + cause: expect.stringMatching( |
| 57 | + /[Uu]nknown(.)+uncaught promise rejection/u, |
| 58 | + ), |
| 59 | + }), |
| 60 | + ); |
| 61 | + |
| 62 | + const vatLogs = extractTestLogs(entries, 'console'); |
| 63 | + expect(vatLogs).toStrictEqual(['global uncaught rejection']); |
| 64 | + }); |
| 65 | + |
| 66 | + it('throws if buildRootObject throws', async () => { |
| 67 | + await expect(launch('build-throw')).rejects.toThrow( |
| 68 | + /from buildRootObject/u, |
| 69 | + ); |
| 70 | + |
| 71 | + const vatLogs = extractTestLogs(entries, 'console'); |
| 72 | + expect(vatLogs).toStrictEqual(['build throw', 'buildRootObject']); |
| 73 | + }); |
| 74 | + |
| 75 | + it.todo('throws if buildRootObject has an uncaught rejection', async () => { |
| 76 | + await expect(launch('build-uncaught-rejection')).rejects.toThrow( |
| 77 | + expect.objectContaining({ |
| 78 | + message: expect.stringContaining('Subcluster initialization failed'), |
| 79 | + cause: expect.stringMatching( |
| 80 | + /[Uu]nknown(.)+uncaught promise rejection/u, |
| 81 | + ), |
| 82 | + }), |
| 83 | + ); |
| 84 | + |
| 85 | + const vatLogs = extractTestLogs(entries, 'console'); |
| 86 | + expect(vatLogs).toStrictEqual([ |
| 87 | + 'build uncaught rejection', |
| 88 | + 'buildRootObject', |
| 89 | + 'bootstrap', |
| 90 | + ]); |
| 91 | + }); |
| 92 | + |
| 93 | + it('throws if bootstrap throws', async () => { |
| 94 | + await expect(launch('bootstrap-throw')).rejects.toThrow(/from bootstrap/u); |
| 95 | + |
| 96 | + const vatLogs = extractTestLogs(entries, 'console'); |
| 97 | + expect(vatLogs).toStrictEqual([ |
| 98 | + 'bootstrap throw', |
| 99 | + 'buildRootObject', |
| 100 | + 'bootstrap', |
| 101 | + ]); |
| 102 | + }); |
| 103 | + |
| 104 | + it.todo('throws if bootstrap has an uncaught rejection', async () => { |
| 105 | + await expect(launch('bootstrap-uncaught-rejection')).rejects.toThrow( |
| 106 | + expect.objectContaining({ |
| 107 | + message: expect.stringContaining('Subcluster initialization failed'), |
| 108 | + cause: expect.stringMatching( |
| 109 | + /[Uu]nknown(.)+uncaught promise rejection/u, |
| 110 | + ), |
| 111 | + }), |
| 112 | + ); |
| 113 | + |
| 114 | + const vatLogs = extractTestLogs(entries, 'console'); |
| 115 | + expect(vatLogs).toStrictEqual([ |
| 116 | + 'bootstrap uncaught rejection', |
| 117 | + 'buildRootObject', |
| 118 | + 'bootstrap', |
| 119 | + ]); |
| 120 | + }); |
| 121 | +}); |
0 commit comments