|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import type { IPage } from '../../types.js'; |
| 3 | +import { getRegistry } from '../../registry.js'; |
| 4 | +import './comments.js'; |
| 5 | + |
| 6 | +function createPageMock(evaluateResult: any): IPage { |
| 7 | + return { |
| 8 | + goto: vi.fn().mockResolvedValue(undefined), |
| 9 | + evaluate: vi.fn().mockResolvedValue(evaluateResult), |
| 10 | + snapshot: vi.fn().mockResolvedValue(undefined), |
| 11 | + click: vi.fn().mockResolvedValue(undefined), |
| 12 | + typeText: vi.fn().mockResolvedValue(undefined), |
| 13 | + pressKey: vi.fn().mockResolvedValue(undefined), |
| 14 | + scrollTo: vi.fn().mockResolvedValue(undefined), |
| 15 | + getFormState: vi.fn().mockResolvedValue({ forms: [], orphanFields: [] }), |
| 16 | + wait: vi.fn().mockResolvedValue(undefined), |
| 17 | + tabs: vi.fn().mockResolvedValue([]), |
| 18 | + closeTab: vi.fn().mockResolvedValue(undefined), |
| 19 | + newTab: vi.fn().mockResolvedValue(undefined), |
| 20 | + selectTab: vi.fn().mockResolvedValue(undefined), |
| 21 | + networkRequests: vi.fn().mockResolvedValue([]), |
| 22 | + consoleMessages: vi.fn().mockResolvedValue([]), |
| 23 | + scroll: vi.fn().mockResolvedValue(undefined), |
| 24 | + autoScroll: vi.fn().mockResolvedValue(undefined), |
| 25 | + installInterceptor: vi.fn().mockResolvedValue(undefined), |
| 26 | + getInterceptedRequests: vi.fn().mockResolvedValue([]), |
| 27 | + getCookies: vi.fn().mockResolvedValue([]), |
| 28 | + screenshot: vi.fn().mockResolvedValue(''), |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +describe('xiaohongshu comments', () => { |
| 33 | + const command = getRegistry().get('xiaohongshu/comments'); |
| 34 | + |
| 35 | + it('returns ranked comment rows', async () => { |
| 36 | + const page = createPageMock({ |
| 37 | + loginWall: false, |
| 38 | + results: [ |
| 39 | + { author: 'Alice', text: 'Great note!', likes: 10, time: '2024-01-01' }, |
| 40 | + { author: 'Bob', text: 'Very helpful', likes: 0, time: '2024-01-02' }, |
| 41 | + ], |
| 42 | + }); |
| 43 | + |
| 44 | + const result = (await command!.func!(page, { 'note-id': '69aadbcb000000002202f131', limit: 5 })) as any[]; |
| 45 | + |
| 46 | + expect((page.goto as any).mock.calls[0][0]).toContain('/explore/69aadbcb000000002202f131'); |
| 47 | + expect(result).toEqual([ |
| 48 | + { rank: 1, author: 'Alice', text: 'Great note!', likes: 10, time: '2024-01-01' }, |
| 49 | + { rank: 2, author: 'Bob', text: 'Very helpful', likes: 0, time: '2024-01-02' }, |
| 50 | + ]); |
| 51 | + expect(result[0]).not.toHaveProperty('loginWall'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('strips /explore/ prefix from full URL input', async () => { |
| 55 | + const page = createPageMock({ |
| 56 | + loginWall: false, |
| 57 | + results: [{ author: 'Alice', text: 'Nice', likes: 1, time: '2024-01-01' }], |
| 58 | + }); |
| 59 | + |
| 60 | + await command!.func!(page, { |
| 61 | + 'note-id': 'https://www.xiaohongshu.com/explore/69aadbcb000000002202f131', |
| 62 | + limit: 5, |
| 63 | + }); |
| 64 | + |
| 65 | + expect((page.goto as any).mock.calls[0][0]).toContain('/explore/69aadbcb000000002202f131'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('throws AuthRequiredError when login wall is detected', async () => { |
| 69 | + const page = createPageMock({ loginWall: true, results: [] }); |
| 70 | + |
| 71 | + await expect(command!.func!(page, { 'note-id': 'abc123', limit: 5 })).rejects.toThrow( |
| 72 | + 'Note comments require login', |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it('returns empty array when no comments are found', async () => { |
| 77 | + const page = createPageMock({ loginWall: false, results: [] }); |
| 78 | + |
| 79 | + await expect(command!.func!(page, { 'note-id': 'abc123', limit: 5 })).resolves.toEqual([]); |
| 80 | + }); |
| 81 | + |
| 82 | + it('respects the limit', async () => { |
| 83 | + const manyComments = Array.from({ length: 10 }, (_, i) => ({ |
| 84 | + author: `User${i}`, |
| 85 | + text: `Comment ${i}`, |
| 86 | + likes: i, |
| 87 | + time: '2024-01-01', |
| 88 | + })); |
| 89 | + const page = createPageMock({ loginWall: false, results: manyComments }); |
| 90 | + |
| 91 | + const result = (await command!.func!(page, { 'note-id': 'abc123', limit: 3 })) as any[]; |
| 92 | + expect(result).toHaveLength(3); |
| 93 | + expect(result[0].rank).toBe(1); |
| 94 | + expect(result[2].rank).toBe(3); |
| 95 | + }); |
| 96 | +}); |
0 commit comments