auth-page-recovery.test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const { createAuthPageRecovery } = require('../content/auth-page-recovery.js');
  4. function createRetryButton() {
  5. return {
  6. disabled: false,
  7. textContent: 'Try again',
  8. getAttribute(name) {
  9. if (name === 'data-dd-action-name') return 'Try again';
  10. if (name === 'aria-disabled') return 'false';
  11. return '';
  12. },
  13. };
  14. }
  15. function createRecoveryApi(state) {
  16. const retryButton = createRetryButton();
  17. global.location = {
  18. pathname: '/log-in',
  19. href: 'https://auth.openai.com/log-in',
  20. };
  21. global.document = {
  22. title: 'Something went wrong',
  23. querySelector(selector) {
  24. if (selector === 'button[data-dd-action-name="Try again"]' && state.retryVisible) {
  25. return retryButton;
  26. }
  27. return null;
  28. },
  29. querySelectorAll() {
  30. return state.retryVisible ? [retryButton] : [];
  31. },
  32. };
  33. return createAuthPageRecovery({
  34. detailPattern: /timed out/i,
  35. getActionText: (element) => element?.textContent || '',
  36. getPageTextSnapshot: () => state.pageText,
  37. humanPause: async () => {},
  38. isActionEnabled: (element) => Boolean(element) && !element.disabled && element.getAttribute('aria-disabled') !== 'true',
  39. isVisibleElement: () => true,
  40. log: () => {},
  41. simulateClick: () => {
  42. state.clickCount += 1;
  43. if (typeof state.onClick === 'function') {
  44. state.onClick(state);
  45. return;
  46. }
  47. state.retryVisible = false;
  48. state.pageText = 'Recovered login form';
  49. },
  50. sleep: async (ms = 0) => {
  51. await new Promise((resolve) => setTimeout(resolve, Math.max(1, Math.min(5, ms))));
  52. if (typeof state.onSleep === 'function') {
  53. state.onSleep(state);
  54. }
  55. },
  56. throwIfStopped: () => {},
  57. titlePattern: /something went wrong/i,
  58. });
  59. }
  60. test('auth page recovery detects retry page state', () => {
  61. const state = {
  62. clickCount: 0,
  63. pageText: 'Something went wrong. Operation timed out.',
  64. retryVisible: true,
  65. };
  66. const api = createRecoveryApi(state);
  67. const snapshot = api.getAuthTimeoutErrorPageState({
  68. pathPatterns: [/\/log-in(?:[/?#]|$)/i],
  69. });
  70. assert.equal(Boolean(snapshot), true);
  71. assert.equal(snapshot.retryEnabled, true);
  72. assert.equal(snapshot.titleMatched, true);
  73. assert.equal(snapshot.detailMatched, true);
  74. });
  75. test('auth page recovery clicks retry and waits until page recovers', async () => {
  76. const state = {
  77. clickCount: 0,
  78. pageText: 'Something went wrong. Operation timed out.',
  79. retryVisible: true,
  80. };
  81. const api = createRecoveryApi(state);
  82. const result = await api.recoverAuthRetryPage({
  83. logLabel: '步骤 8:检测到重试页,正在点击“重试”恢复',
  84. pathPatterns: [/\/log-in(?:[/?#]|$)/i],
  85. step: 8,
  86. timeoutMs: 1000,
  87. });
  88. assert.deepStrictEqual(result, {
  89. recovered: true,
  90. clickCount: 1,
  91. url: 'https://auth.openai.com/log-in',
  92. });
  93. assert.equal(state.clickCount, 1);
  94. assert.equal(state.retryVisible, false);
  95. });
  96. test('auth page recovery can click retry twice before page recovers', async () => {
  97. const state = {
  98. clickCount: 0,
  99. pageText: 'Something went wrong. Operation timed out.',
  100. retryVisible: true,
  101. onClick(currentState) {
  102. if (currentState.clickCount >= 2) {
  103. currentState.retryVisible = false;
  104. currentState.pageText = 'Recovered login form';
  105. }
  106. },
  107. };
  108. const api = createRecoveryApi(state);
  109. const result = await api.recoverAuthRetryPage({
  110. logLabel: '步骤 8:检测到重试页,正在点击“重试”恢复',
  111. pathPatterns: [/\/log-in(?:[/?#]|$)/i],
  112. step: 8,
  113. timeoutMs: 200,
  114. waitAfterClickMs: 10,
  115. pollIntervalMs: 1,
  116. });
  117. assert.deepStrictEqual(result, {
  118. recovered: true,
  119. clickCount: 2,
  120. url: 'https://auth.openai.com/log-in',
  121. });
  122. assert.equal(state.clickCount, 2);
  123. assert.equal(state.retryVisible, false);
  124. });