identity.test.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import assert from 'node:assert/strict';
  2. import test from 'node:test';
  3. import {
  4. makeProductKey,
  5. makeRelationKey,
  6. makeReviewKey,
  7. makeSyncIdempotencyKey,
  8. } from '../src/modules/domestic-voc/domain/identity.js';
  9. test('domestic product and relation keys are stable', () => {
  10. assert.equal(makeProductKey('JD', ' 11266507445 '), 'jd:11266507445');
  11. assert.equal(makeRelationKey('JD', 'own-1', 'competitor-2'), 'jd:own-1:competitor-2');
  12. });
  13. test('review key uses source id when available and a stable hash otherwise', () => {
  14. const sourceKey = makeReviewKey({
  15. platform: 'jd',
  16. productId: '1',
  17. reviewId: 'review-10',
  18. content: 'content',
  19. });
  20. assert.equal(sourceKey, 'jd:1:review-10');
  21. const input = { platform: 'jd', productId: '1', content: 'same content', reviewDate: '2026-07-23' };
  22. assert.equal(makeReviewKey(input), makeReviewKey(input));
  23. assert.match(makeReviewKey(input), /^jd:1:sha256:[a-f0-9]{64}$/);
  24. });
  25. test('sync idempotency ignores input ordering', () => {
  26. const first = makeSyncIdempotencyKey({
  27. workspaceId: 'demashi',
  28. platform: 'jd',
  29. productIds: ['2', '1'],
  30. scopes: ['reviews', 'product'],
  31. bucket: '2026-07-23',
  32. });
  33. const second = makeSyncIdempotencyKey({
  34. workspaceId: 'demashi',
  35. platform: 'JD',
  36. productIds: ['1', '2', '1'],
  37. scopes: ['product', 'reviews'],
  38. bucket: '2026-07-23',
  39. });
  40. assert.equal(first, second);
  41. });