selftest-xhs.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import assert from 'node:assert/strict';
  2. import { extractDeclaredNotesCountFromPostedResponse, extractNotesCountFromPostedResponse } from '../utils/xiaohongshu.js';
  3. const sample = {
  4. success: true,
  5. data: {
  6. notes: new Array(12).fill(null).map((_, i) => ({ id: String(i + 1) })),
  7. tags: [
  8. { id: 'special.note_time_desc', notes_count: 368 },
  9. { id: 'other', notes_count: 12 },
  10. ],
  11. },
  12. };
  13. assert.equal(extractNotesCountFromPostedResponse(sample), 368);
  14. assert.equal(extractDeclaredNotesCountFromPostedResponse(sample), 368);
  15. const sampleMaxTag = {
  16. data: {
  17. notes: [],
  18. tags: [
  19. { id: 'a', notes_count: 10 },
  20. { id: 'b', notes_count: 20 },
  21. ],
  22. },
  23. };
  24. assert.equal(extractNotesCountFromPostedResponse(sampleMaxTag), 20);
  25. assert.equal(extractDeclaredNotesCountFromPostedResponse(sampleMaxTag), 20);
  26. const sampleTotalOnly = {
  27. data: {
  28. notes: [{ id: '1' }],
  29. total_count: 99,
  30. },
  31. };
  32. assert.equal(extractNotesCountFromPostedResponse(sampleTotalOnly), 99);
  33. assert.equal(extractDeclaredNotesCountFromPostedResponse(sampleTotalOnly), 99);
  34. const sampleFallback = {
  35. data: {
  36. notes: [{ id: '1' }, { id: '2' }],
  37. },
  38. };
  39. assert.equal(extractNotesCountFromPostedResponse(sampleFallback), 2);
  40. assert.equal(extractDeclaredNotesCountFromPostedResponse(sampleFallback), 0);
  41. console.log('selftest-xhs passed');