background-skip-step-linking.test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const fs = require('node:fs');
  4. const source = fs.readFileSync('background.js', 'utf8');
  5. function extractFunction(name) {
  6. const markers = [`async function ${name}(`, `function ${name}(`];
  7. const start = markers
  8. .map((marker) => source.indexOf(marker))
  9. .find((index) => index >= 0);
  10. if (start < 0) {
  11. throw new Error(`missing function ${name}`);
  12. }
  13. let parenDepth = 0;
  14. let signatureEnded = false;
  15. let braceStart = -1;
  16. for (let i = start; i < source.length; i += 1) {
  17. const ch = source[i];
  18. if (ch === '(') {
  19. parenDepth += 1;
  20. } else if (ch === ')') {
  21. parenDepth -= 1;
  22. if (parenDepth === 0) {
  23. signatureEnded = true;
  24. }
  25. } else if (ch === '{' && signatureEnded) {
  26. braceStart = i;
  27. break;
  28. }
  29. }
  30. let depth = 0;
  31. let end = braceStart;
  32. for (; end < source.length; end += 1) {
  33. const ch = source[end];
  34. if (ch === '{') depth += 1;
  35. if (ch === '}') {
  36. depth -= 1;
  37. if (depth === 0) {
  38. end += 1;
  39. break;
  40. }
  41. }
  42. }
  43. return source.slice(start, end);
  44. }
  45. test('skipStep cascades from step 1 to step 5 when downstream steps are pending', async () => {
  46. const bundle = [
  47. extractFunction('isStepDoneStatus'),
  48. extractFunction('skipStep'),
  49. ].join('\n');
  50. const statuses = {
  51. 1: 'pending',
  52. 2: 'pending',
  53. 3: 'pending',
  54. 4: 'pending',
  55. 5: 'pending',
  56. 6: 'pending',
  57. 7: 'pending',
  58. 8: 'pending',
  59. 9: 'pending',
  60. 10: 'pending',
  61. };
  62. const events = {
  63. setStepStatusCalls: [],
  64. logs: [],
  65. };
  66. const api = new Function(`
  67. const STEP_IDS = [1,2,3,4,5,6,7,8,9,10];
  68. ${bundle}
  69. return { skipStep };
  70. `)();
  71. globalThis.ensureManualInteractionAllowed = async () => ({
  72. stepStatuses: { ...statuses },
  73. });
  74. globalThis.getState = async () => ({
  75. stepStatuses: { ...statuses },
  76. });
  77. globalThis.setStepStatus = async (step, status) => {
  78. events.setStepStatusCalls.push({ step, status });
  79. statuses[step] = status;
  80. };
  81. globalThis.addLog = async (message, level) => {
  82. events.logs.push({ message, level });
  83. };
  84. const result = await api.skipStep(1);
  85. assert.deepStrictEqual(result, { ok: true, step: 1, status: 'skipped' });
  86. assert.deepStrictEqual(events.setStepStatusCalls, [
  87. { step: 1, status: 'skipped' },
  88. { step: 2, status: 'skipped' },
  89. { step: 3, status: 'skipped' },
  90. { step: 4, status: 'skipped' },
  91. { step: 5, status: 'skipped' },
  92. ]);
  93. assert.equal(events.logs[0]?.message, '步骤 1 已跳过');
  94. assert.equal(events.logs[1]?.message, '步骤 1 已跳过,步骤 2、3、4、5 也已同时跳过。');
  95. });
  96. test('skipStep from step 1 skips only unfinished steps up to step 5', async () => {
  97. const bundle = [
  98. extractFunction('isStepDoneStatus'),
  99. extractFunction('skipStep'),
  100. ].join('\n');
  101. const statuses = {
  102. 1: 'pending',
  103. 2: 'completed',
  104. 3: 'running',
  105. 4: 'pending',
  106. 5: 'manual_completed',
  107. 6: 'pending',
  108. 7: 'pending',
  109. 8: 'pending',
  110. 9: 'pending',
  111. 10: 'pending',
  112. };
  113. const events = {
  114. setStepStatusCalls: [],
  115. logs: [],
  116. };
  117. const api = new Function(`
  118. const STEP_IDS = [1,2,3,4,5,6,7,8,9,10];
  119. ${bundle}
  120. return { skipStep };
  121. `)();
  122. globalThis.ensureManualInteractionAllowed = async () => ({
  123. stepStatuses: { ...statuses },
  124. });
  125. globalThis.getState = async () => ({
  126. stepStatuses: { ...statuses },
  127. });
  128. globalThis.setStepStatus = async (step, status) => {
  129. events.setStepStatusCalls.push({ step, status });
  130. statuses[step] = status;
  131. };
  132. globalThis.addLog = async (message, level) => {
  133. events.logs.push({ message, level });
  134. };
  135. await api.skipStep(1);
  136. assert.deepStrictEqual(events.setStepStatusCalls, [
  137. { step: 1, status: 'skipped' },
  138. { step: 4, status: 'skipped' },
  139. ]);
  140. assert.equal(events.logs.some(({ message }) => message === '步骤 1 已跳过,步骤 4 也已同时跳过。'), true);
  141. });