step8-debugger-stop.test.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const assert = require('assert');
  2. const fs = require('fs');
  3. const source = fs.readFileSync('background.js', 'utf8');
  4. function extractFunction(name) {
  5. const markers = [`async function ${name}(`, `function ${name}(`];
  6. const start = markers
  7. .map(marker => source.indexOf(marker))
  8. .find(index => index >= 0);
  9. if (start < 0) {
  10. throw new Error(`missing function ${name}`);
  11. }
  12. let parenDepth = 0;
  13. let signatureEnded = false;
  14. let braceStart = -1;
  15. for (let i = start; i < source.length; i++) {
  16. const ch = source[i];
  17. if (ch === '(') {
  18. parenDepth += 1;
  19. } else if (ch === ')') {
  20. parenDepth -= 1;
  21. if (parenDepth === 0) {
  22. signatureEnded = true;
  23. }
  24. } else if (ch === '{' && signatureEnded) {
  25. braceStart = i;
  26. break;
  27. }
  28. }
  29. if (braceStart < 0) {
  30. throw new Error(`missing body for function ${name}`);
  31. }
  32. let depth = 0;
  33. let end = braceStart;
  34. for (; end < source.length; end++) {
  35. const ch = source[end];
  36. if (ch === '{') depth += 1;
  37. if (ch === '}') {
  38. depth -= 1;
  39. if (depth === 0) {
  40. end += 1;
  41. break;
  42. }
  43. }
  44. }
  45. return source.slice(start, end);
  46. }
  47. const bundle = [
  48. extractFunction('throwIfStopped'),
  49. extractFunction('clickWithDebugger'),
  50. ].join('\n');
  51. const api = new Function(`
  52. let stopRequested = false;
  53. const STOP_ERROR_MESSAGE = '流程已被用户停止。';
  54. const commands = [];
  55. let attachCount = 0;
  56. let detachCount = 0;
  57. const chrome = {
  58. debugger: {
  59. async attach(target, version) {
  60. attachCount += 1;
  61. },
  62. async sendCommand(target, command, payload) {
  63. commands.push(command);
  64. if (command === 'Page.bringToFront') {
  65. stopRequested = true;
  66. }
  67. },
  68. async detach(target) {
  69. detachCount += 1;
  70. },
  71. },
  72. };
  73. ${bundle}
  74. return {
  75. clickWithDebugger,
  76. snapshot() {
  77. return {
  78. commands,
  79. attachCount,
  80. detachCount,
  81. };
  82. },
  83. };
  84. `)();
  85. (async () => {
  86. const error = await api.clickWithDebugger(123, { centerX: 10, centerY: 20 }).catch((err) => err);
  87. const state = api.snapshot();
  88. assert.strictEqual(error?.message, '流程已被用户停止。', 'debugger 点击过程中收到 Stop 后应终止');
  89. assert.deepStrictEqual(state.commands, ['Page.bringToFront'], 'Stop 后不应继续发送鼠标事件');
  90. assert.strictEqual(state.attachCount, 1, '应先附加 debugger');
  91. assert.strictEqual(state.detachCount, 1, '即使停止也应在 finally 中释放 debugger');
  92. console.log('step8 debugger stop tests passed');
  93. })().catch((error) => {
  94. console.error(error);
  95. process.exit(1);
  96. });