@lvce-editor/test-worker 1.2.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -134,7 +134,6 @@ const getErrorConstructor = (message, type) => {
134
134
  if (type) {
135
135
  switch (type) {
136
136
  case DomException:
137
- // @ts-ignore
138
137
  return DOMException;
139
138
  case TypeError$1:
140
139
  return TypeError;
@@ -159,7 +158,6 @@ const getErrorConstructor = (message, type) => {
159
158
  };
160
159
  const constructError = (message, type, name) => {
161
160
  const ErrorConstructor = getErrorConstructor(message, type);
162
- // @ts-ignore
163
161
  if (ErrorConstructor === DOMException && name) {
164
162
  return new ErrorConstructor(message, name);
165
163
  }
@@ -175,6 +173,13 @@ const constructError = (message, type, name) => {
175
173
  const getNewLineIndex$2 = (string, startIndex = undefined) => {
176
174
  return string.indexOf(NewLine$3, startIndex);
177
175
  };
176
+ const getParentStack = error => {
177
+ let parentStack = error.stack || error.data || error.message || '';
178
+ if (parentStack.startsWith(' at')) {
179
+ parentStack = error.message + NewLine$3 + parentStack;
180
+ }
181
+ return parentStack;
182
+ };
178
183
  const joinLines$1 = lines => {
179
184
  return lines.join(NewLine$3);
180
185
  };
@@ -183,18 +188,11 @@ const Custom = -32001;
183
188
  const splitLines$1 = lines => {
184
189
  return lines.split(NewLine$3);
185
190
  };
186
- const getParentStack = error => {
187
- let parentStack = error.stack || error.data || error.message || '';
188
- if (parentStack.startsWith(' at')) {
189
- parentStack = error.message + NewLine$3 + parentStack;
190
- }
191
- return parentStack;
192
- };
193
191
  const restoreJsonRpcError = error => {
194
192
  if (error && error instanceof Error) {
195
193
  return error;
196
194
  }
197
- const currentStack = joinLines$1(splitLines$1(new Error().stack).slice(1));
195
+ const currentStack = joinLines$1(splitLines$1(new Error().stack || '').slice(1));
198
196
  if (error && error.code && error.code === MethodNotFound) {
199
197
  const restoredError = new JsonRpcError(error.message);
200
198
  const parentStack = getParentStack(error);
@@ -223,7 +221,6 @@ const restoreJsonRpcError = error => {
223
221
  }
224
222
  } else {
225
223
  if (error.stack) {
226
- // TODO accessing stack might be slow
227
224
  const lowerStack = restoredError.stack || '';
228
225
  // @ts-ignore
229
226
  const indexNewLine = getNewLineIndex$2(lowerStack);
@@ -253,6 +250,67 @@ const unwrapJsonRpcResult = responseMessage => {
253
250
  }
254
251
  throw new JsonRpcError('unexpected response message');
255
252
  };
253
+ const isMessagePort = value => {
254
+ return typeof MessagePort !== 'undefined' && value instanceof MessagePort;
255
+ };
256
+ const isInstanceOf = (value, constructorName) => {
257
+ return value?.constructor?.name === constructorName;
258
+ };
259
+ const isMessagePortMain = value => {
260
+ return isInstanceOf(value, 'MessagePortMain');
261
+ };
262
+ const isOffscreenCanvas = value => {
263
+ return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
264
+ };
265
+ const isSocket = value => {
266
+ return isInstanceOf(value, 'Socket');
267
+ };
268
+ const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
269
+ const isTransferrable = value => {
270
+ for (const fn of transferrables) {
271
+ if (fn(value)) {
272
+ return true;
273
+ }
274
+ }
275
+ return false;
276
+ };
277
+ const walkValue = (value, transferrables) => {
278
+ if (!value) {
279
+ return;
280
+ }
281
+ if (isTransferrable(value)) {
282
+ transferrables.push(value);
283
+ }
284
+ if (Array.isArray(value)) {
285
+ for (const item of value) {
286
+ walkValue(item, transferrables);
287
+ }
288
+ return;
289
+ }
290
+ if (typeof value === 'object') {
291
+ for (const property of Object.values(value)) {
292
+ walkValue(property, transferrables);
293
+ }
294
+ }
295
+ };
296
+ const getTransferrables = value => {
297
+ const transferrables = [];
298
+ walkValue(value, transferrables);
299
+ return transferrables;
300
+ };
301
+ const isSingleTransferrable = value => {
302
+ return isSocket(value);
303
+ };
304
+ const getTransferrableParams = value => {
305
+ const transferrables = getTransferrables(value);
306
+ if (transferrables.length === 0) {
307
+ return undefined;
308
+ }
309
+ if (isSingleTransferrable(transferrables[0])) {
310
+ return transferrables[0];
311
+ }
312
+ return transferrables;
313
+ };
256
314
  const create$1 = (message, error) => {
257
315
  return {
258
316
  jsonrpc: Two,
@@ -305,7 +363,42 @@ const getResponse = async (message, ipc, execute, preparePrettyError, logError,
305
363
  return getErrorResponse(message, error, preparePrettyError, logError);
306
364
  }
307
365
  };
308
- const handleJsonRpcMessage = async (ipc, message, execute, resolve, preparePrettyError, logError, requiresSocket) => {
366
+ const defaultPreparePrettyError = error => {
367
+ return error;
368
+ };
369
+ const defaultLogError = () => {
370
+ // ignore
371
+ };
372
+ const defaultRequiresSocket = () => {
373
+ return false;
374
+ };
375
+ const defaultResolve = resolve;
376
+ const handleJsonRpcMessage = async (...args) => {
377
+ let message;
378
+ let ipc;
379
+ let execute;
380
+ let preparePrettyError;
381
+ let logError;
382
+ let resolve;
383
+ let requiresSocket;
384
+ if (args.length === 1) {
385
+ const arg = args[0];
386
+ message = arg.message;
387
+ ipc = arg.ipc;
388
+ execute = arg.execute;
389
+ preparePrettyError = arg.preparePrettyError || defaultPreparePrettyError;
390
+ logError = arg.logError || defaultLogError;
391
+ requiresSocket = arg.requiresSocket || defaultRequiresSocket;
392
+ resolve = arg.resolve || defaultResolve;
393
+ } else {
394
+ ipc = args[0];
395
+ message = args[1];
396
+ execute = args[2];
397
+ resolve = args[3];
398
+ preparePrettyError = args[4];
399
+ logError = args[5];
400
+ requiresSocket = args[6];
401
+ }
309
402
  if ('id' in message) {
310
403
  if ('method' in message) {
311
404
  const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
@@ -337,14 +430,44 @@ const invoke$1 = async (ipc, method, ...params) => {
337
430
  return result;
338
431
  };
339
432
 
433
+ // TODO deprecated old typings,
434
+ // always use automatic transferrable detection
435
+ const invokeAndTransfer$1 = async (ipc, handle, method, ...params) => {
436
+ let transfer = handle;
437
+ if (typeof handle === 'string') {
438
+ params = [method, ...params];
439
+ method = handle;
440
+ transfer = getTransferrableParams(params);
441
+ }
442
+ const {
443
+ message,
444
+ promise
445
+ } = create$2(method, params);
446
+ ipc.sendAndTransfer(message, transfer);
447
+ const responseMessage = await promise;
448
+ const result = unwrapJsonRpcResult(responseMessage);
449
+ return result;
450
+ };
451
+
340
452
  const invoke = (method, ...params) => {
341
453
  const ipc = get$1();
342
454
  return invoke$1(ipc, method, ...params);
343
455
  };
456
+ const invokeAndTransfer = (method, ...params) => {
457
+ const ipc = get$1();
458
+ return invokeAndTransfer$1(ipc, method, ...params);
459
+ };
344
460
  const listen$4 = ipc => {
345
461
  set$1(ipc);
346
462
  };
347
463
 
464
+ const Rpc = {
465
+ __proto__: null,
466
+ invoke,
467
+ invokeAndTransfer,
468
+ listen: listen$4
469
+ };
470
+
348
471
  const Fail = 'fail';
349
472
  const Pass = 'pass';
350
473
 
@@ -504,9 +627,13 @@ const Assert = {
504
627
  }
505
628
  };
506
629
  const expect$1 = locator => {
630
+ const {
631
+ invoke
632
+ } = locator.webView || Rpc;
507
633
  return {
508
634
  async checkSingleElementCondition(fnName, options) {
509
635
  Assert.string(fnName);
636
+ // TODO add rpcId property to locator instead
510
637
  return invoke('TestFrameWork.checkSingleElementCondition', locator, fnName, options);
511
638
  },
512
639
  async checkMultiElementCondition(fnName, options) {
@@ -587,9 +714,6 @@ class AssertionError extends Error {
587
714
  this.name = 'AssertionError';
588
715
  }
589
716
  }
590
-
591
- // TODO treeshake out this whole module in production
592
-
593
717
  const getType = value => {
594
718
  switch (typeof value) {
595
719
  case 'number':
@@ -605,9 +729,6 @@ const getType = value => {
605
729
  if (Array.isArray(value)) {
606
730
  return 'array';
607
731
  }
608
- if (value instanceof Uint32Array) {
609
- return 'uint32array';
610
- }
611
732
  return 'object';
612
733
  case 'boolean':
613
734
  return 'boolean';
@@ -668,6 +789,9 @@ const Locator = function (selector, {
668
789
  this._hasText = hasText;
669
790
  };
670
791
  const performAction = async (locator, fnName, options) => {
792
+ const {
793
+ invoke
794
+ } = locator.webView || Rpc;
671
795
  return invoke('TestFrameWork.performAction', locator, fnName, options);
672
796
  };
673
797
  const toButtonNumber = buttonType => {
@@ -833,13 +957,13 @@ const TestFrameWorkComponentBaseUrl = {
833
957
  getBaseUrl
834
958
  };
835
959
 
836
- const execute$2 = async (id, ...args) => {
960
+ const execute$3 = async (id, ...args) => {
837
961
  return invoke(id, ...args);
838
962
  };
839
963
 
840
964
  const TestFrameWorkComponentCommand = {
841
965
  __proto__: null,
842
- execute: execute$2
966
+ execute: execute$3
843
967
  };
844
968
 
845
969
  const selectItem$1 = async text => {
@@ -1398,6 +1522,87 @@ const TestFrameWorkComponentTitleBarMenuBar = {
1398
1522
  toggleMenu
1399
1523
  };
1400
1524
 
1525
+ const getPortTuple = () => {
1526
+ const {
1527
+ port1,
1528
+ port2
1529
+ } = new MessageChannel();
1530
+ return {
1531
+ port1,
1532
+ port2
1533
+ };
1534
+ };
1535
+
1536
+ const sendPortToWebView = async (webviewId, port) => {
1537
+ await invokeAndTransfer('Transferrable.transferToRendererProcess', webviewId, port);
1538
+ console.log('did send port to renderer process');
1539
+ // TODO ask renderer process to transfer the port to the webview
1540
+ };
1541
+
1542
+ const preparePrettyError$1 = error => {
1543
+ return error;
1544
+ };
1545
+ const logError$1 = () => {
1546
+ // ignore
1547
+ };
1548
+ const execute$2 = () => {};
1549
+ const requiresSocket$1 = () => {
1550
+ return false;
1551
+ };
1552
+ const createPortIpc = async webViewId => {
1553
+ const {
1554
+ port1,
1555
+ port2
1556
+ } = getPortTuple();
1557
+ const firstEventPromise = new Promise(resolve => {
1558
+ port1.onmessage = resolve;
1559
+ });
1560
+ await sendPortToWebView(webViewId, port2);
1561
+ const firstEvent = await firstEventPromise;
1562
+ // @ts-ignore
1563
+ if (firstEvent.data !== 'ready') {
1564
+ throw new Error('unexpected first message');
1565
+ }
1566
+ const handleOtherMessage = async event => {
1567
+ // @ts-ignore
1568
+ await handleJsonRpcMessage(ipc, event.data, resolve, preparePrettyError$1, execute$2, logError$1, requiresSocket$1);
1569
+ };
1570
+ port1.onmessage = handleOtherMessage;
1571
+ const ipc = {
1572
+ send(message) {
1573
+ port1.postMessage(message);
1574
+ }
1575
+ };
1576
+ return ipc;
1577
+ };
1578
+
1579
+ const fromId = async webViewId => {
1580
+ const ipc = await createPortIpc(webViewId);
1581
+ // TODO
1582
+ // 1. create messagechannel
1583
+ // 2. send one message port to webview
1584
+ // 3. setup rpc connection and wait for webview to be ready
1585
+ // 4. send test commands like locator.toBeVisible to webview
1586
+ const webViewRpc = {
1587
+ invoke(method, ...params) {
1588
+ return invoke$1(ipc, method, ...params);
1589
+ }
1590
+ };
1591
+ return {
1592
+ locator(selector, options) {
1593
+ const baseLocator = create(selector, options);
1594
+ baseLocator.webView = webViewRpc;
1595
+ return baseLocator;
1596
+ },
1597
+ expect: expect$1
1598
+ };
1599
+ };
1600
+
1601
+ const TestFrameWorkComponentWebView = {
1602
+ __proto__: null,
1603
+ fromId
1604
+ };
1605
+
1401
1606
  const setPath = async path => {
1402
1607
  await invoke('Workspace.setPath', path);
1403
1608
  };
@@ -1431,6 +1636,7 @@ const TestFrameWorkComponent = {
1431
1636
  SourceControl: TestFrameWorkComponentSourceControl,
1432
1637
  StatusBar: TestFrameWorkComponentStatusBar,
1433
1638
  TitleBarMenuBar: TestFrameWorkComponentTitleBarMenuBar,
1639
+ WebView: TestFrameWorkComponentWebView,
1434
1640
  Workspace: TestFrameWorkComponentWorkspace
1435
1641
  };
1436
1642
 
@@ -1514,14 +1720,14 @@ const handleIpc = ipc => {
1514
1720
  ipc.addEventListener('message', handleMessage);
1515
1721
  };
1516
1722
 
1517
- const MessagePort = 1;
1723
+ const MessagePort$1 = 1;
1518
1724
  const ModuleWorker = 2;
1519
1725
  const ReferencePort = 3;
1520
1726
  const ModuleWorkerAndMessagePort = 8;
1521
1727
  const Auto = () => {
1522
1728
  // @ts-ignore
1523
1729
  if (globalThis.acceptPort) {
1524
- return MessagePort;
1730
+ return MessagePort$1;
1525
1731
  }
1526
1732
  // @ts-ignore
1527
1733
  if (globalThis.acceptReferencePort) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/test-worker",
3
- "version": "1.2.0",
3
+ "version": "1.4.0",
4
4
  "description": "",
5
5
  "main": "dist/testWorkerMain.js",
6
6
  "type": "module",
@@ -66,6 +66,5 @@
66
66
  "ignores": [
67
67
  "distmin"
68
68
  ]
69
- },
70
- "dependencies": {}
69
+ }
71
70
  }