background-tab-runtime-module.test.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const fs = require('node:fs');
  4. test('background imports tab runtime module', () => {
  5. const source = fs.readFileSync('background.js', 'utf8');
  6. assert.match(source, /background\/tab-runtime\.js/);
  7. });
  8. test('tab runtime module exposes a factory', () => {
  9. const source = fs.readFileSync('background/tab-runtime.js', 'utf8');
  10. const globalScope = {};
  11. const api = new Function('self', `${source}; return self.MultiPageBackgroundTabRuntime;`)(globalScope);
  12. assert.equal(typeof api?.createTabRuntime, 'function');
  13. });
  14. test('tab runtime waitForTabComplete waits until tab status becomes complete', async () => {
  15. const source = fs.readFileSync('background/tab-runtime.js', 'utf8');
  16. const globalScope = {};
  17. const api = new Function('self', `${source}; return self.MultiPageBackgroundTabRuntime;`)(globalScope);
  18. let getCalls = 0;
  19. const runtime = api.createTabRuntime({
  20. LOG_PREFIX: '[test]',
  21. addLog: async () => {},
  22. buildLocalhostCleanupPrefix: () => '',
  23. chrome: {
  24. tabs: {
  25. get: async () => {
  26. getCalls += 1;
  27. return {
  28. id: 9,
  29. url: 'https://example.com',
  30. status: getCalls >= 3 ? 'complete' : 'loading',
  31. };
  32. },
  33. query: async () => [],
  34. },
  35. },
  36. getSourceLabel: (source) => source || 'unknown',
  37. getState: async () => ({ tabRegistry: {}, sourceLastUrls: {} }),
  38. matchesSourceUrlFamily: () => false,
  39. normalizeLocalCpaStep9Mode: () => 'submit',
  40. parseUrlSafely: () => null,
  41. registerTab: async () => {},
  42. setState: async () => {},
  43. shouldBypassStep9ForLocalCpa: () => false,
  44. throwIfStopped: () => {},
  45. });
  46. const result = await runtime.waitForTabComplete(9, {
  47. timeoutMs: 2000,
  48. retryDelayMs: 1,
  49. });
  50. assert.equal(result?.status, 'complete');
  51. assert.equal(getCalls, 3);
  52. });
  53. test('tab runtime waitForTabComplete aborts promptly when stop is requested', async () => {
  54. const source = fs.readFileSync('background/tab-runtime.js', 'utf8');
  55. const globalScope = {};
  56. const api = new Function('self', `${source}; return self.MultiPageBackgroundTabRuntime;`)(globalScope);
  57. let throwCalls = 0;
  58. const runtime = api.createTabRuntime({
  59. LOG_PREFIX: '[test]',
  60. addLog: async () => {},
  61. chrome: {
  62. tabs: {
  63. get: async () => ({
  64. id: 9,
  65. url: 'https://example.com',
  66. status: 'loading',
  67. }),
  68. query: async () => [],
  69. },
  70. },
  71. getSourceLabel: (sourceName) => sourceName || 'unknown',
  72. getState: async () => ({ tabRegistry: {}, sourceLastUrls: {} }),
  73. matchesSourceUrlFamily: () => false,
  74. setState: async () => {},
  75. throwIfStopped: () => {
  76. throwCalls += 1;
  77. if (throwCalls >= 2) {
  78. throw new Error('Flow stopped.');
  79. }
  80. },
  81. });
  82. await assert.rejects(
  83. runtime.waitForTabComplete(9, {
  84. timeoutMs: 2000,
  85. retryDelayMs: 1,
  86. }),
  87. /Flow stopped\./
  88. );
  89. });