signup-page-tab-cleanup.test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 += 1) {
  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 += 1) {
  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('getTabRegistry'),
  49. extractFunction('parseUrlSafely'),
  50. extractFunction('isSignupPageHost'),
  51. extractFunction('isSignupEntryHost'),
  52. extractFunction('matchesSourceUrlFamily'),
  53. extractFunction('closeConflictingTabsForSource'),
  54. ].join('\n');
  55. const api = new Function(`
  56. let currentState = {
  57. sourceLastUrls: {},
  58. tabRegistry: {},
  59. };
  60. let currentTabs = [];
  61. const removedBatches = [];
  62. const logMessages = [];
  63. const chrome = {
  64. tabs: {
  65. async query() {
  66. return currentTabs;
  67. },
  68. async remove(ids) {
  69. removedBatches.push(ids);
  70. currentTabs = currentTabs.filter((tab) => !ids.includes(tab.id));
  71. },
  72. },
  73. };
  74. async function getState() {
  75. return currentState;
  76. }
  77. async function setState(updates) {
  78. currentState = { ...currentState, ...updates };
  79. }
  80. async function addLog(message, level = 'info') {
  81. logMessages.push({ message, level });
  82. }
  83. function getSourceLabel(source) {
  84. return source;
  85. }
  86. ${bundle}
  87. return {
  88. matchesSourceUrlFamily,
  89. closeConflictingTabsForSource,
  90. reset({ tabs, state }) {
  91. currentTabs = tabs;
  92. removedBatches.length = 0;
  93. logMessages.length = 0;
  94. currentState = {
  95. sourceLastUrls: {},
  96. tabRegistry: {},
  97. ...(state || {}),
  98. };
  99. },
  100. snapshot() {
  101. return {
  102. currentState,
  103. currentTabs,
  104. removedBatches,
  105. logMessages,
  106. };
  107. },
  108. };
  109. `)();
  110. (async () => {
  111. assert.strictEqual(
  112. api.matchesSourceUrlFamily('signup-page', 'https://chatgpt.com/', 'https://chatgpt.com/'),
  113. true,
  114. 'signup-page family should include chatgpt.com'
  115. );
  116. assert.strictEqual(
  117. api.matchesSourceUrlFamily('signup-page', 'https://chat.openai.com/', 'https://auth.openai.com/authorize'),
  118. true,
  119. 'signup-page family should include legacy chat.openai.com'
  120. );
  121. api.reset({
  122. tabs: [
  123. { id: 1, url: 'https://chatgpt.com/' },
  124. { id: 2, url: 'https://chat.openai.com/' },
  125. { id: 3, url: 'https://auth.openai.com/authorize?client_id=test' },
  126. { id: 4, url: 'https://example.com/' },
  127. ],
  128. state: {
  129. sourceLastUrls: {
  130. 'signup-page': 'https://chatgpt.com/',
  131. },
  132. tabRegistry: {
  133. 'signup-page': { tabId: 3, ready: true },
  134. },
  135. },
  136. });
  137. await api.closeConflictingTabsForSource('signup-page', 'https://auth.openai.com/authorize', {
  138. excludeTabIds: [3],
  139. });
  140. let snapshot = api.snapshot();
  141. assert.deepStrictEqual(
  142. snapshot.removedBatches,
  143. [[1, 2]],
  144. 'opening auth page should clean up stale ChatGPT entry tabs'
  145. );
  146. assert.deepStrictEqual(
  147. snapshot.currentTabs,
  148. [
  149. { id: 3, url: 'https://auth.openai.com/authorize?client_id=test' },
  150. { id: 4, url: 'https://example.com/' },
  151. ],
  152. 'non-signup tabs and excluded current tab should remain'
  153. );
  154. api.reset({
  155. tabs: [
  156. { id: 11, url: 'https://chatgpt.com/' },
  157. { id: 12, url: 'https://auth.openai.com/authorize?client_id=test' },
  158. ],
  159. state: {
  160. sourceLastUrls: {
  161. 'signup-page': 'https://auth.openai.com/authorize?client_id=test',
  162. },
  163. tabRegistry: {
  164. 'signup-page': { tabId: 11, ready: true },
  165. },
  166. },
  167. });
  168. await api.closeConflictingTabsForSource('signup-page', 'https://chatgpt.com/');
  169. snapshot = api.snapshot();
  170. assert.deepStrictEqual(
  171. snapshot.removedBatches,
  172. [[11, 12]],
  173. 'opening ChatGPT entry should remove older signup-family tabs'
  174. );
  175. assert.strictEqual(
  176. snapshot.currentState.tabRegistry['signup-page'],
  177. null,
  178. 'registry should be cleared when the tracked signup tab is removed'
  179. );
  180. console.log('signup page tab cleanup tests passed');
  181. })().catch((error) => {
  182. console.error(error);
  183. process.exit(1);
  184. });