xiaohongshu-profile.test.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import {
  4. extractLatestXiaohongshuFansCount,
  5. extractXiaohongshuProfileInfo,
  6. } from '../src/utils/xiaohongshu.js';
  7. test('extracts profile info from creator personal_info snake_case response', () => {
  8. const profile = extractXiaohongshuProfileInfo({
  9. data: {
  10. name: 'AAA',
  11. avatar: 'https://example.test/avatar.jpg',
  12. red_num: '63535021536',
  13. fans_count: 1288,
  14. note_count: 19,
  15. },
  16. });
  17. assert.deepEqual(profile, {
  18. name: 'AAA',
  19. avatar: 'https://example.test/avatar.jpg',
  20. redNum: '63535021536',
  21. userId: undefined,
  22. fansCount: 1288,
  23. worksCount: 19,
  24. });
  25. });
  26. test('extracts profile info from creator storage camelCase response', () => {
  27. const profile = extractXiaohongshuProfileInfo({
  28. userName: 'O_O',
  29. userAvatar: 'https://example.test/camel.jpg',
  30. redId: '495513171',
  31. fansCount: '1.2万',
  32. notes: '20',
  33. });
  34. assert.deepEqual(profile, {
  35. name: 'O_O',
  36. avatar: 'https://example.test/camel.jpg',
  37. redNum: '495513171',
  38. userId: undefined,
  39. fansCount: 12000,
  40. worksCount: 20,
  41. });
  42. });
  43. test('extracts profile info from nested creator profile response', () => {
  44. const profile = extractXiaohongshuProfileInfo({
  45. data: {
  46. core_user_info: {
  47. nickName: 'Nested Name',
  48. avatarUrl: 'https://example.test/nested.jpg',
  49. redId: 'nested_123',
  50. followers_count: '2,345',
  51. },
  52. },
  53. });
  54. assert.deepEqual(profile, {
  55. name: 'Nested Name',
  56. avatar: 'https://example.test/nested.jpg',
  57. redNum: 'nested_123',
  58. userId: undefined,
  59. fansCount: 2345,
  60. worksCount: undefined,
  61. });
  62. });
  63. test('extracts latest fans count from overall_new response', () => {
  64. const count = extractLatestXiaohongshuFansCount({
  65. data: {
  66. thirty: {
  67. fans_list: [
  68. { date: 1777190400000, count: 110 },
  69. { date: 1777276800000, count: '1,234' },
  70. ],
  71. },
  72. },
  73. });
  74. assert.equal(count, 1234);
  75. });