auth-page-recovery.test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. state.retryVisible = false;
  44. state.pageText = 'Recovered login form';
  45. },
  46. sleep: async () => {},
  47. throwIfStopped: () => {},
  48. titlePattern: /something went wrong/i,
  49. });
  50. }
  51. test('auth page recovery detects retry page state', () => {
  52. const state = {
  53. clickCount: 0,
  54. pageText: 'Something went wrong. Operation timed out.',
  55. retryVisible: true,
  56. };
  57. const api = createRecoveryApi(state);
  58. const snapshot = api.getAuthTimeoutErrorPageState({
  59. pathPatterns: [/\/log-in(?:[/?#]|$)/i],
  60. });
  61. assert.equal(Boolean(snapshot), true);
  62. assert.equal(snapshot.retryEnabled, true);
  63. assert.equal(snapshot.titleMatched, true);
  64. assert.equal(snapshot.detailMatched, true);
  65. });
  66. test('auth page recovery clicks retry and waits until page recovers', async () => {
  67. const state = {
  68. clickCount: 0,
  69. pageText: 'Something went wrong. Operation timed out.',
  70. retryVisible: true,
  71. };
  72. const api = createRecoveryApi(state);
  73. const result = await api.recoverAuthRetryPage({
  74. logLabel: '步骤 8:检测到重试页,正在点击“重试”恢复',
  75. pathPatterns: [/\/log-in(?:[/?#]|$)/i],
  76. step: 8,
  77. timeoutMs: 1000,
  78. });
  79. assert.deepStrictEqual(result, {
  80. recovered: true,
  81. clickCount: 1,
  82. url: 'https://auth.openai.com/log-in',
  83. });
  84. assert.equal(state.clickCount, 1);
  85. assert.equal(state.retryVisible, false);
  86. });