tracking-retention.test.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import assert from 'node:assert/strict';
  2. import { test } from 'node:test';
  3. import { startTrackingRetentionWorker } from '../src/tracking-retention.js';
  4. test('retention worker prunes on startup and interval and stops cleanly', async () => {
  5. const calls = [];
  6. const worker = startTrackingRetentionWorker({
  7. enabled: true,
  8. days: 45,
  9. intervalMs: 10,
  10. prune(options) {
  11. calls.push(options.days);
  12. return 1;
  13. },
  14. logger: { warn() {} }
  15. });
  16. await waitFor(() => calls.length >= 2);
  17. assert.deepEqual(calls.slice(0, 2), [45, 45]);
  18. worker.stop();
  19. const stoppedAt = calls.length;
  20. await new Promise((resolve) => setTimeout(resolve, 30));
  21. assert.equal(calls.length, stoppedAt);
  22. });
  23. test('retention worker can be disabled', () => {
  24. let calls = 0;
  25. const worker = startTrackingRetentionWorker({
  26. enabled: false,
  27. prune() {
  28. calls += 1;
  29. }
  30. });
  31. assert.equal(worker, null);
  32. assert.equal(calls, 0);
  33. });
  34. async function waitFor(predicate, timeoutMs = 500) {
  35. const startedAt = Date.now();
  36. while (!predicate()) {
  37. if (Date.now() - startedAt > timeoutMs) throw new Error('Timed out waiting for retention worker.');
  38. await new Promise((resolve) => setTimeout(resolve, 5));
  39. }
  40. }