background-account-run-history-module.test.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. const test = require('node:test');
  2. const assert = require('node:assert/strict');
  3. const fs = require('node:fs');
  4. test('background imports account run history module', () => {
  5. const source = fs.readFileSync('background.js', 'utf8');
  6. assert.match(source, /background\/account-run-history\.js/);
  7. });
  8. test('account run history module exposes a factory', () => {
  9. const source = fs.readFileSync('background/account-run-history.js', 'utf8');
  10. const globalScope = {};
  11. const api = new Function('self', `${source}; return self.MultiPageBackgroundAccountRunHistory;`)(globalScope);
  12. assert.equal(typeof api?.createAccountRunHistoryHelpers, 'function');
  13. });
  14. test('account run history helper normalizes records and persists without helper upload when local helper is disabled', async () => {
  15. const source = fs.readFileSync('background/account-run-history.js', 'utf8');
  16. const globalScope = {};
  17. const api = new Function('self', `${source}; return self.MultiPageBackgroundAccountRunHistory;`)(globalScope);
  18. let storedHistory = [{ email: 'old@example.com', password: 'old-pass', status: 'success', recordedAt: '2026-04-17T00:00:00.000Z' }];
  19. let fetchCalled = false;
  20. global.fetch = async () => {
  21. fetchCalled = true;
  22. throw new Error('should not call fetch');
  23. };
  24. const helpers = api.createAccountRunHistoryHelpers({
  25. ACCOUNT_RUN_HISTORY_STORAGE_KEY: 'accountRunHistory',
  26. addLog: async () => {},
  27. buildLocalHelperEndpoint: (baseUrl, path) => `${baseUrl}${path}`,
  28. chrome: {
  29. storage: {
  30. local: {
  31. get: async () => ({ accountRunHistory: storedHistory }),
  32. set: async (payload) => {
  33. storedHistory = payload.accountRunHistory;
  34. },
  35. },
  36. },
  37. },
  38. getErrorMessage: (error) => error?.message || String(error || ''),
  39. getState: async () => ({
  40. email: ' latest@example.com ',
  41. password: ' secret ',
  42. accountRunHistoryTextEnabled: false,
  43. accountRunHistoryHelperBaseUrl: '',
  44. }),
  45. normalizeAccountRunHistoryHelperBaseUrl: (value) => String(value || '').trim(),
  46. });
  47. const record = helpers.buildAccountRunHistoryRecord(
  48. { email: ' latest@example.com ', password: ' secret ' },
  49. ' FAILED ',
  50. ' reason '
  51. );
  52. assert.deepStrictEqual(record, {
  53. email: 'latest@example.com',
  54. password: 'secret',
  55. status: 'failed',
  56. recordedAt: record.recordedAt,
  57. reason: 'reason',
  58. });
  59. const appended = await helpers.appendAccountRunRecord('failed', null, 'boom');
  60. assert.equal(appended.email, 'latest@example.com');
  61. assert.equal(appended.status, 'failed');
  62. assert.equal(storedHistory.length, 2);
  63. assert.equal(storedHistory[1].reason, 'boom');
  64. assert.equal(fetchCalled, false);
  65. assert.equal(helpers.shouldAppendAccountRunTextFile({ accountRunHistoryTextEnabled: false, accountRunHistoryHelperBaseUrl: 'http://127.0.0.1:17373' }), false);
  66. assert.equal(helpers.shouldAppendAccountRunTextFile({ accountRunHistoryTextEnabled: true, accountRunHistoryHelperBaseUrl: 'http://127.0.0.1:17373' }), true);
  67. });