/ clis / bilibili / dynamic.test.js
dynamic.test.js
 1  import { beforeEach, describe, expect, it, vi } from 'vitest';
 2  const { mockApiGet } = vi.hoisted(() => ({
 3      mockApiGet: vi.fn(),
 4  }));
 5  vi.mock('./utils.js', () => ({
 6      apiGet: mockApiGet,
 7  }));
 8  import { getRegistry } from '@jackwener/opencli/registry';
 9  import './dynamic.js';
10  describe('bilibili dynamic adapter', () => {
11      const command = getRegistry().get('bilibili/dynamic');
12      beforeEach(() => {
13          mockApiGet.mockReset();
14      });
15      it('maps desc text rows from the dynamic feed payload', async () => {
16          mockApiGet.mockResolvedValue({
17              data: {
18                  items: [
19                      {
20                          id_str: '123',
21                          modules: {
22                              module_author: { name: 'Alice' },
23                              module_dynamic: { desc: { text: 'hello world' } },
24                              module_stat: { like: { count: 9 } },
25                          },
26                      },
27                  ],
28              },
29          });
30          const result = await command.func({}, { limit: 5 });
31          expect(mockApiGet).toHaveBeenCalledWith({}, '/x/polymer/web-dynamic/v1/feed/all', { params: {}, signed: false });
32          expect(result).toEqual([
33              {
34                  id: '123',
35                  author: 'Alice',
36                  text: 'hello world',
37                  likes: 9,
38                  url: 'https://t.bilibili.com/123',
39              },
40          ]);
41      });
42      it('falls back to archive title when desc text is absent', async () => {
43          mockApiGet.mockResolvedValue({
44              data: {
45                  items: [
46                      {
47                          id_str: '456',
48                          modules: {
49                              module_author: { name: 'Bob' },
50                              module_dynamic: { major: { archive: { title: 'Video title' } } },
51                              module_stat: { like: { count: 3 } },
52                          },
53                      },
54                  ],
55              },
56          });
57          const result = await command.func({}, { limit: 5 });
58          expect(result).toEqual([
59              {
60                  id: '456',
61                  author: 'Bob',
62                  text: 'Video title',
63                  likes: 3,
64                  url: 'https://t.bilibili.com/456',
65              },
66          ]);
67      });
68  });