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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. });
  45. const result = await runtime.waitForTabComplete(9, {
  46. timeoutMs: 2000,
  47. retryDelayMs: 1,
  48. });
  49. assert.equal(result?.status, 'complete');
  50. assert.equal(getCalls, 3);
  51. });