/ clis / douyin / _shared / text-extra.test.js
text-extra.test.js
 1  import { describe, expect, it } from 'vitest';
 2  import { parseTextExtra, extractHashtagNames } from './text-extra.js';
 3  describe('parseTextExtra', () => {
 4      it('returns empty array for text with no hashtags', () => {
 5          const result = parseTextExtra('普通文本内容', []);
 6          expect(result).toEqual([]);
 7      });
 8      it('produces type-1 entry for each hashtag', () => {
 9          const hashtags = [
10              { name: '话题', id: 12345, start: 5, end: 8 },
11          ];
12          const result = parseTextExtra('普通文本 #话题', hashtags);
13          expect(result).toHaveLength(1);
14          expect(result[0]).toMatchObject({
15              type: 1,
16              hashtag_name: '话题',
17              hashtag_id: 12345,
18              start: 5,
19              end: 8,
20          });
21      });
22      it('sets hashtag_id to 0 when not found', () => {
23          const hashtags = [
24              { name: '未知话题', id: 0, start: 0, end: 5 },
25          ];
26          const result = parseTextExtra('#未知话题', hashtags);
27          expect(result[0].hashtag_id).toBe(0);
28      });
29  });
30  describe('extractHashtagNames', () => {
31      it('extracts hashtag names from text', () => {
32          expect(extractHashtagNames('hello #foo and #bar')).toEqual(['foo', 'bar']);
33      });
34      it('returns empty array when no hashtags', () => {
35          expect(extractHashtagNames('no hashtags here')).toEqual([]);
36      });
37  });