step9-status-diagnostics.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const fs = require('node:fs');
  4. const source = fs.readFileSync('content/vps-panel.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. if (braceStart < 0) {
  31. throw new Error(`missing body for function ${name}`);
  32. }
  33. let depth = 0;
  34. let end = braceStart;
  35. for (; end < source.length; end += 1) {
  36. const ch = source[end];
  37. if (ch === '{') depth += 1;
  38. if (ch === '}') {
  39. depth -= 1;
  40. if (depth === 0) {
  41. end += 1;
  42. break;
  43. }
  44. }
  45. }
  46. return source.slice(start, end);
  47. }
  48. const bundle = [
  49. "const STEP9_SUCCESS_STATUSES = new Set(['Authentication successful!', 'Аутентификация успешна!', '认证成功!']);",
  50. extractFunction('getInlineTextSnippet'),
  51. extractFunction('summarizeStatusBadgeEntries'),
  52. extractFunction('normalizeStep9StatusText'),
  53. extractFunction('isOAuthCallbackTimeoutFailure'),
  54. extractFunction('isStep9FailureText'),
  55. extractFunction('isStep9SuccessStatus'),
  56. extractFunction('isStep9SuccessLikeStatus'),
  57. extractFunction('buildStep9StatusDiagnostics'),
  58. ].join('\n');
  59. function createApi() {
  60. return new Function(`
  61. function isRecoverableStep9AuthFailure(text) {
  62. return /(?:认证失败|回调 URL 提交失败):\\s*/i.test(String(text || '').trim())
  63. || /oauth flow is not pending/i.test(String(text || '').trim());
  64. }
  65. ${bundle}
  66. return {
  67. buildStep9StatusDiagnostics,
  68. };
  69. `)();
  70. }
  71. test('step 9 does not treat red success badges as exact success', () => {
  72. const api = createApi();
  73. const diagnostics = api.buildStep9StatusDiagnostics([
  74. {
  75. visible: true,
  76. text: '认证成功!',
  77. className: 'status-badge text-danger',
  78. hasErrorVisualSignal: true,
  79. errorVisualSummary: 'color=rgb(220, 38, 38)',
  80. },
  81. ], [], 'page');
  82. assert.equal(diagnostics.hasSuccessLikeVisibleBadge, true);
  83. assert.equal(diagnostics.hasExactSuccessVisibleBadge, false);
  84. assert.equal(diagnostics.hasErrorStyledVisibleBadge, true);
  85. });
  86. test('step 9 keeps failure state dominant when success badge and error banner coexist', () => {
  87. const api = createApi();
  88. const diagnostics = api.buildStep9StatusDiagnostics(
  89. [
  90. {
  91. visible: true,
  92. text: '认证成功!',
  93. className: 'status-badge',
  94. hasErrorVisualSignal: false,
  95. errorVisualSummary: '',
  96. },
  97. ],
  98. [
  99. {
  100. visible: true,
  101. text: '回调 URL 提交失败: oauth flow is not pending',
  102. className: 'alert alert-danger',
  103. hasErrorVisualSignal: true,
  104. errorVisualSummary: 'color=rgb(220, 38, 38)',
  105. },
  106. ],
  107. 'page'
  108. );
  109. assert.equal(diagnostics.hasExactSuccessVisibleBadge, true);
  110. assert.equal(diagnostics.hasFailureVisibleBadge, true);
  111. assert.equal(diagnostics.failureText, '回调 URL 提交失败: oauth flow is not pending');
  112. });