1 |
|
2 |
|
3 | import { KernelMessage, ServiceManager } from '@jupyterlab/services';
|
4 | import { changeKernel, KERNEL_MODELS, KernelMock, ServiceManagerMock, SessionConnectionMock } from '@jupyterlab/services/lib/testutils';
|
5 | import { UUID } from '@lumino/coreutils';
|
6 | import { AttachedProperty } from '@lumino/properties';
|
7 | import { Signal } from '@lumino/signaling';
|
8 | import { Context } from './context';
|
9 | import { TextModelFactory } from './default';
|
10 |
|
11 |
|
12 |
|
13 | export function createFileContext(path = UUID.uuid4() + '.txt', manager = Private.getManager()) {
|
14 | const factory = Private.textFactory;
|
15 | return new Context({ manager, factory, path });
|
16 | }
|
17 | export async function createFileContextWithKernel(path = UUID.uuid4() + '.txt', manager = Private.getManager()) {
|
18 | var _a;
|
19 | const factory = Private.textFactory;
|
20 | const specsManager = manager.kernelspecs;
|
21 | await specsManager.ready;
|
22 | return new Context({
|
23 | manager,
|
24 | factory,
|
25 | path,
|
26 | kernelPreference: {
|
27 | shouldStart: true,
|
28 | canStart: true,
|
29 | name: (_a = specsManager.specs) === null || _a === void 0 ? void 0 : _a.default
|
30 | }
|
31 | });
|
32 | }
|
33 | export async function createFileContextWithMockedServices(startKernel = false, manager) {
|
34 | const path = UUID.uuid4() + '.txt';
|
35 | manager = manager || new ServiceManagerMock();
|
36 | const factory = new TextModelFactory();
|
37 | const context = new Context({
|
38 | manager: manager || new ServiceManagerMock(),
|
39 | factory,
|
40 | path,
|
41 | kernelPreference: {
|
42 | shouldStart: startKernel,
|
43 | canStart: startKernel,
|
44 | autoStartDefault: startKernel
|
45 | }
|
46 | });
|
47 | await context.initialize(true);
|
48 | await context.sessionContext.initialize();
|
49 | return context;
|
50 | }
|
51 |
|
52 |
|
53 |
|
54 | export async function createSession(options) {
|
55 | const manager = Private.getManager().sessions;
|
56 | await manager.ready;
|
57 | return manager.startNew(options);
|
58 | }
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | export function createSimpleSessionContext(model = {}) {
|
65 | const kernel = new KernelMock({ model: (model === null || model === void 0 ? void 0 : model.kernel) || {} });
|
66 | const session = new SessionConnectionMock({ model }, kernel);
|
67 | return new SessionContextMock({}, session);
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | export function emitIopubMessage(context, msg) {
|
76 | const kernel = context.session.kernel;
|
77 | const msgId = Private.lastMessageProperty.get(kernel);
|
78 | msg.parent_header.session = kernel.clientId;
|
79 | msg.parent_header.msg_id = msgId;
|
80 | kernel.iopubMessage.emit(msg);
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | export function updateKernelStatus(sessionContext, newStatus) {
|
90 | const kernel = sessionContext.session.kernel;
|
91 | kernel.status = newStatus;
|
92 | sessionContext.statusChanged.emit(newStatus);
|
93 | const msg = KernelMessage.createMessage({
|
94 | session: kernel.clientId,
|
95 | channel: 'iopub',
|
96 | msgType: 'status',
|
97 | content: { execution_state: newStatus }
|
98 | });
|
99 | emitIopubMessage(sessionContext, msg);
|
100 | }
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | export const SessionContextMock = jest.fn((options, connection) => {
|
107 | const session = connection ||
|
108 | new SessionConnectionMock({
|
109 | model: {
|
110 | path: options.path || '',
|
111 | type: options.type || '',
|
112 | name: options.name || ''
|
113 | }
|
114 | }, null);
|
115 | const thisObject = {
|
116 | ...jest.requireActual('@jupyterlab/apputils'),
|
117 | ...options,
|
118 | path: session.path,
|
119 | type: session.type,
|
120 | name: session.name,
|
121 | session,
|
122 | dispose: jest.fn(),
|
123 | initialize: jest.fn(() => Promise.resolve(false)),
|
124 | ready: Promise.resolve(),
|
125 | changeKernel: jest.fn(partialModel => {
|
126 | return changeKernel(session.kernel || Private.RUNNING_KERNELS[0], partialModel);
|
127 | }),
|
128 | shutdown: jest.fn(() => Promise.resolve())
|
129 | };
|
130 | const disposedSignal = new Signal(thisObject);
|
131 | const propertyChangedSignal = new Signal(thisObject);
|
132 | const statusChangedSignal = new Signal(thisObject);
|
133 | const kernelChangedSignal = new Signal(thisObject);
|
134 | const iopubMessageSignal = new Signal(thisObject);
|
135 | session.statusChanged.connect((_, args) => {
|
136 | statusChangedSignal.emit(args);
|
137 | }, thisObject);
|
138 | session.iopubMessage.connect((_, args) => {
|
139 | iopubMessageSignal.emit(args);
|
140 | });
|
141 | session.kernelChanged.connect((_, args) => {
|
142 | kernelChangedSignal.emit(args);
|
143 | });
|
144 | session.pendingInput.connect((_, args) => {
|
145 | thisObject.pendingInput = args;
|
146 | });
|
147 | thisObject.statusChanged = statusChangedSignal;
|
148 | thisObject.kernelChanged = kernelChangedSignal;
|
149 | thisObject.iopubMessage = iopubMessageSignal;
|
150 | thisObject.propertyChanged = propertyChangedSignal;
|
151 | thisObject.disposed = disposedSignal;
|
152 | thisObject.session = session;
|
153 | thisObject.pendingInput = false;
|
154 | return thisObject;
|
155 | });
|
156 |
|
157 |
|
158 |
|
159 | var Private;
|
160 | (function (Private) {
|
161 | let manager;
|
162 | Private.textFactory = new TextModelFactory();
|
163 | |
164 |
|
165 |
|
166 | function getManager() {
|
167 | if (!manager) {
|
168 | manager = new ServiceManager({ standby: 'never' });
|
169 | }
|
170 | return manager;
|
171 | }
|
172 | Private.getManager = getManager;
|
173 | Private.lastMessageProperty = new AttachedProperty({
|
174 | name: 'lastMessageId',
|
175 | create: () => ''
|
176 | });
|
177 |
|
178 | Private.RUNNING_KERNELS = KERNEL_MODELS.map((model, _) => {
|
179 | return new KernelMock({ model });
|
180 | });
|
181 | })(Private || (Private = {}));
|
182 |
|
183 |
|
184 |
|
185 | export class DocumentWidgetOpenerMock {
|
186 | constructor() {
|
187 | this._opened = new Signal(this);
|
188 | }
|
189 | get opened() {
|
190 | return this._opened;
|
191 | }
|
192 | open(widget) {
|
193 |
|
194 | this._opened.emit(widget);
|
195 | }
|
196 | }
|
197 |
|
\ | No newline at end of file |