-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathkernel-captp.test.ts
More file actions
94 lines (76 loc) · 2.65 KB
/
kernel-captp.test.ts
File metadata and controls
94 lines (76 loc) · 2.65 KB
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
import type { Kernel } from '@metamask/ocap-kernel';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { makeKernelCapTP } from './kernel-captp.ts';
import type { CapTPMessage } from './kernel-captp.ts';
describe('makeKernelCapTP', () => {
const mockKernel: Kernel = {} as unknown as Kernel;
let sendMock: (message: CapTPMessage) => void;
beforeEach(() => {
sendMock = vi.fn();
});
it('returns object with dispatch and abort', () => {
const capTP = makeKernelCapTP({
kernel: mockKernel,
send: sendMock,
});
expect(capTP).toHaveProperty('dispatch');
expect(capTP).toHaveProperty('abort');
expect(typeof capTP.dispatch).toBe('function');
expect(typeof capTP.abort).toBe('function');
});
it('dispatch returns boolean', () => {
const capTP = makeKernelCapTP({
kernel: mockKernel,
send: sendMock,
});
// Dispatch a dummy message - will return false since it's not valid
const result = capTP.dispatch({ type: 'unknown' });
expect(typeof result).toBe('boolean');
});
it('processes valid CapTP messages without errors', () => {
const capTP = makeKernelCapTP({
kernel: mockKernel,
send: sendMock,
});
// Dispatch a valid CapTP message format
// CapTP uses array-based message format internally
// A CTP_CALL message triggers method calls on the bootstrap object
const callMessage: CapTPMessage = {
type: 'CTP_CALL',
questionID: 1,
target: 0, // Bootstrap slot
method: 'ping',
args: { body: '[]', slots: [] },
};
// Should not throw when processing a message
expect(() => capTP.dispatch(callMessage)).not.toThrow();
});
it('abort does not throw', () => {
const capTP = makeKernelCapTP({
kernel: mockKernel,
send: sendMock,
});
expect(() => capTP.abort()).not.toThrow();
});
it('abort can be called with a reason', () => {
const capTP = makeKernelCapTP({
kernel: mockKernel,
send: sendMock,
});
expect(() => capTP.abort({ reason: 'test shutdown' })).not.toThrow();
});
describe('kref marshalling', () => {
it('creates kernel CapTP with custom import/export tables', () => {
// Verify that makeKernelCapTP with the custom tables doesn't throw
const capTP = makeKernelCapTP({
kernel: mockKernel,
send: sendMock,
});
expect(capTP).toBeDefined();
expect(capTP.dispatch).toBeDefined();
expect(capTP.abort).toBeDefined();
// The custom tables are internal to CapTP, so we can't test them directly
// Integration tests will verify the end-to-end kref marshalling functionality
});
});
});