UNPKG

6.67 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { KernelMessage, ServiceManager } from '@jupyterlab/services';
4import { changeKernel, KERNEL_MODELS, KernelMock, ServiceManagerMock, SessionConnectionMock } from '@jupyterlab/services/lib/testutils';
5import { UUID } from '@lumino/coreutils';
6import { AttachedProperty } from '@lumino/properties';
7import { Signal } from '@lumino/signaling';
8import { Context } from './context';
9import { TextModelFactory } from './default';
10/**
11 * Create a context for a file.
12 */
13export function createFileContext(path = UUID.uuid4() + '.txt', manager = Private.getManager()) {
14 const factory = Private.textFactory;
15 return new Context({ manager, factory, path });
16}
17export 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}
33export 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 * Create a session and return a session connection.
53 */
54export async function createSession(options) {
55 const manager = Private.getManager().sessions;
56 await manager.ready;
57 return manager.startNew(options);
58}
59/**
60 * Create a session context given a partial session model.
61 *
62 * @param model The session model to use.
63 */
64export 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 * Emit an iopub message on a session context.
71 *
72 * @param sessionContext The session context
73 * @param msg Message created with `KernelMessage.createMessage`
74 */
75export 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 * Forceably change the status of a session context.
84 * An iopub message is emitted for the change.
85 *
86 * @param sessionContext The session context of interest.
87 * @param newStatus The new kernel status.
88 */
89export 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 * A mock session context.
103 *
104 * @param session The session connection object to use
105 */
106export 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 * A namespace for module private data.
158 */
159var Private;
160(function (Private) {
161 let manager;
162 Private.textFactory = new TextModelFactory();
163 /**
164 * Get or create the service manager singleton.
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 // This list of running kernels simply mirrors the KERNEL_MODELS and KERNELSPECS lists
178 Private.RUNNING_KERNELS = KERNEL_MODELS.map((model, _) => {
179 return new KernelMock({ model });
180 });
181})(Private || (Private = {}));
182/**
183 * A mock document widget opener.
184 */
185export class DocumentWidgetOpenerMock {
186 constructor() {
187 this._opened = new Signal(this);
188 }
189 get opened() {
190 return this._opened;
191 }
192 open(widget) {
193 // no-op, just emit the signal
194 this._opened.emit(widget);
195 }
196}
197//# sourceMappingURL=testutils.js.map
\No newline at end of file