parse-rest-competitor-sync.test.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 { ParseRestCompetitorSyncService } from '../src/modules/domestic-voc/services/parse-rest-competitor-sync.service.js';
  5. import type { GatewayRequestClient } from '../src/modules/domestic-voc/services/jd-sync.service.js';
  6. import type { ParseRestVocRepository } from '../src/modules/saas-platform/parse-rest-voc.repository.js';
  7. test('competitor sync creates mapped placeholders and persists returned products as competitors', async () => {
  8. const placeholders: string[] = [];
  9. const products: Array<{ productId: string; role: string }> = [];
  10. const client = {
  11. async findAll() {
  12. return [
  13. { competitorProductId: 'c1', competitorBrand: 'Brand A', category: 'Category' },
  14. { competitorProductId: 'c1', competitorBrand: 'Brand A', category: 'Category' },
  15. { competitorProductId: 'c2', competitorBrand: 'Brand B', category: 'Category' },
  16. ];
  17. },
  18. async findOne() { return null; },
  19. } as unknown as ParseRestClient;
  20. const repository = {
  21. async ensureCompetitorProduct(input: { productId: string }) { placeholders.push(input.productId); },
  22. async upsertProduct(_workspaceId: string, product: { productId: string; role: string }) {
  23. products.push({ productId: product.productId, role: product.role });
  24. },
  25. } as unknown as ParseRestVocRepository;
  26. const gateway: GatewayRequestClient = {
  27. async request(_path, init) {
  28. const productId = String(init?.params?.itemId);
  29. return { code: 200, data: { code: 200, data: { item: { itemId: productId, itemName: `Product ${productId}` } } } } as never;
  30. },
  31. };
  32. const result = await new ParseRestCompetitorSyncService(client, repository, gateway).sync({
  33. workspaceId: 'demashi',
  34. platform: 'jd',
  35. concurrency: 2,
  36. });
  37. assert.deepEqual(placeholders.sort(), ['c1', 'c2']);
  38. assert.deepEqual(products.sort((left, right) => left.productId.localeCompare(right.productId)), [
  39. { productId: 'c1', role: 'competitor' },
  40. { productId: 'c2', role: 'competitor' },
  41. ]);
  42. assert.deepEqual(result, {
  43. total: 2,
  44. placeholders: 2,
  45. succeeded: 2,
  46. skipped: 0,
  47. failed: 0,
  48. failedProductIds: [],
  49. });
  50. });