jd-adapters.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import assert from 'node:assert/strict';
  2. import test from 'node:test';
  3. import { adaptJdProductResponse } from '../src/modules/domestic-voc/adapters/jd-product.adapter.js';
  4. import { GatewayPayloadError } from '../src/modules/domestic-voc/adapters/jd-response.js';
  5. import { adaptJdReviewResponse } from '../src/modules/domestic-voc/adapters/jd-review.adapter.js';
  6. test('JD product adapter unwraps the company gateway envelope', () => {
  7. const product = adaptJdProductResponse({
  8. code: 200,
  9. data: {
  10. code: 200,
  11. data: {
  12. item: {
  13. itemId: '11266507445',
  14. itemName: 'Demashi commercial cooker',
  15. brandName: 'Demashi',
  16. model: '35P6-CM1',
  17. firstCategoryName: 'Commercial appliances',
  18. thirdCategoryName: 'Commercial cooker',
  19. },
  20. },
  21. },
  22. }, '11266507445');
  23. assert.equal(product.productId, '11266507445');
  24. assert.equal(product.productKey, 'jd:11266507445');
  25. assert.equal(product.title, 'Demashi commercial cooker');
  26. assert.equal(product.brand, 'Demashi');
  27. assert.equal(product.model, '35P6-CM1');
  28. assert.equal(product.source, 'fmode_gateway');
  29. });
  30. test('JD product adapter preserves an explicit competitor role', () => {
  31. const product = adaptJdProductResponse({ data: { item: { itemId: '100119862001', itemName: 'Competitor' } } }, '100119862001', 'competitor');
  32. assert.equal(product.role, 'competitor');
  33. });
  34. test('JD review adapter maps evidence and pagination without reviewer identity', () => {
  35. const page = adaptJdReviewResponse({
  36. code: 200,
  37. data: {
  38. code: 200,
  39. data: {
  40. comments: [{
  41. commentId: 'comment-1',
  42. score: 5,
  43. content: 'Easy to clean and heats quickly.',
  44. creationTime: '2026-07-20T10:00:00+08:00',
  45. nickname: 'not-exposed-by-snapshot',
  46. }],
  47. pageCount: 2,
  48. },
  49. },
  50. }, '11266507445', 1);
  51. assert.equal(page.reviews.length, 1);
  52. assert.equal(page.reviews[0]?.reviewId, 'comment-1');
  53. assert.equal(page.reviews[0]?.rating, 5);
  54. assert.equal(page.reviews[0]?.reviewDate, '2026-07-20T02:00:00.000Z');
  55. assert.match(page.reviews[0]?.reviewKey ?? '', /^jd:11266507445:/);
  56. assert.equal(page.hasNextPage, true);
  57. });
  58. test('JD adapters classify pending collection as retryable without raw body leakage', () => {
  59. assert.throws(
  60. () => adaptJdProductResponse({ code: 301, message: 'COLLECT FAILED, SEND REQUEST AGAIN', data: null }, '1'),
  61. (error: unknown) => {
  62. assert.ok(error instanceof GatewayPayloadError);
  63. assert.equal(error.retryable, true);
  64. assert.equal(error.message, 'Gateway data is not ready');
  65. assert.doesNotMatch(error.message, /COLLECT FAILED/);
  66. return true;
  67. },
  68. );
  69. });