hashtag.test.js
1 import { beforeEach, describe, expect, it, vi } from 'vitest'; 2 const { browserFetchMock } = vi.hoisted(() => ({ 3 browserFetchMock: vi.fn(), 4 })); 5 vi.mock('./_shared/browser-fetch.js', () => ({ 6 browserFetch: browserFetchMock, 7 })); 8 import { getRegistry } from '@jackwener/opencli/registry'; 9 import './hashtag.js'; 10 describe('douyin hashtag', () => { 11 beforeEach(() => { 12 browserFetchMock.mockReset(); 13 }); 14 it('registers the hashtag command', () => { 15 const registry = getRegistry(); 16 const cmd = [...registry.values()].find(c => c.site === 'douyin' && c.name === 'hashtag'); 17 expect(cmd).toBeDefined(); 18 expect(cmd?.args.some(a => a.name === 'action')).toBe(true); 19 }); 20 it('has all expected args', () => { 21 const registry = getRegistry(); 22 const cmd = [...registry.values()].find(c => c.site === 'douyin' && c.name === 'hashtag'); 23 const argNames = cmd?.args.map(a => a.name) ?? []; 24 expect(argNames).toContain('action'); 25 expect(argNames).toContain('keyword'); 26 expect(argNames).toContain('cover'); 27 expect(argNames).toContain('limit'); 28 }); 29 it('uses COOKIE strategy', () => { 30 const registry = getRegistry(); 31 const cmd = [...registry.values()].find(c => c.site === 'douyin' && c.name === 'hashtag'); 32 expect(cmd?.strategy).toBe('cookie'); 33 }); 34 it('parses the current hotspot recommendation shape', async () => { 35 const registry = getRegistry(); 36 const command = [...registry.values()].find((cmd) => cmd.site === 'douyin' && cmd.name === 'hashtag'); 37 expect(command?.func).toBeDefined(); 38 if (!command?.func) 39 throw new Error('douyin hashtag command not registered'); 40 browserFetchMock.mockResolvedValueOnce({ 41 all_sentences: [ 42 { 43 word: '在公园花海里大晒一场', 44 hot_value: 12141172, 45 sentence_id: '2448416', 46 }, 47 ], 48 }); 49 const rows = await command.func({}, { action: 'hot', keyword: '', limit: 5 }); 50 expect(rows).toEqual([ 51 { 52 name: '在公园花海里大晒一场', 53 id: '2448416', 54 view_count: 12141172, 55 }, 56 ]); 57 }); 58 });