parse-rest-managed-task-worker.test.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import assert from 'node:assert/strict';
  2. import test from 'node:test';
  3. import type { ParseRestClient } from '../src/db/parse-rest.client.js';
  4. import { VOC_PARSE_CLASSES } from '../src/db/parse-rest.schema.js';
  5. import {
  6. ParseRestManagedTaskQueue,
  7. startParseRestManagedTaskWorker,
  8. } from '../src/modules/managed-tasks/parse-rest-managed-task-worker.js';
  9. test('managed task queue discovers and deduplicates workspaces across both queue classes', async () => {
  10. const client = {
  11. async find(className: string) {
  12. assert.ok([
  13. VOC_PARSE_CLASSES.competitorListingRefreshRun,
  14. VOC_PARSE_CLASSES.listingScoreJob,
  15. ].includes(className as never));
  16. return className === VOC_PARSE_CLASSES.competitorListingRefreshRun
  17. ? { results: [{ workspaceId: 'workspace-b' }, { workspaceId: 'workspace-a' }] }
  18. : { results: [{ workspaceId: 'workspace-a' }, { workspaceId: '' }, {}] };
  19. },
  20. } as unknown as ParseRestClient;
  21. assert.deepEqual(
  22. await new ParseRestManagedTaskQueue(client).listPendingWorkspaceIds(),
  23. ['workspace-a', 'workspace-b'],
  24. );
  25. });
  26. test('managed task worker resumes competitor and listing jobs and stops cleanly', async () => {
  27. let queueCalls = 0;
  28. const refreshCalls: string[] = [];
  29. const scoreCalls: string[] = [];
  30. const worker = startParseRestManagedTaskWorker({
  31. queue: {
  32. async listPendingWorkspaceIds() {
  33. queueCalls += 1;
  34. return queueCalls === 1 ? ['workspace-a'] : [];
  35. },
  36. },
  37. processor: {
  38. async resumePendingRefreshes(workspaceId, platform) {
  39. refreshCalls.push(`${workspaceId}:${platform}`);
  40. return 1;
  41. },
  42. async resumePendingJobs(workspaceId) {
  43. scoreCalls.push(workspaceId);
  44. return 1;
  45. },
  46. },
  47. pollMs: 5,
  48. logger: { log() {}, error() {} },
  49. });
  50. for (let attempt = 0; attempt < 50 && queueCalls === 0; attempt += 1) {
  51. await new Promise((resolve) => setTimeout(resolve, 2));
  52. }
  53. await worker.stop();
  54. assert.deepEqual(refreshCalls, ['workspace-a:jd']);
  55. assert.deepEqual(scoreCalls, ['workspace-a']);
  56. });