|
| 1 | +import { describe, expect, it, vi } from 'vitest'; |
| 2 | +import { getRegistry } from '@jackwener/opencli/registry'; |
| 3 | +import type { IPage } from '../../src/types.js'; |
| 4 | +import './search.js'; |
| 5 | +import './detail.js'; |
| 6 | +import './reviews.js'; |
| 7 | +import './cart.js'; |
| 8 | +import './add-cart.js'; |
| 9 | + |
| 10 | +function createPageMock() { |
| 11 | + return { |
| 12 | + goto: vi.fn().mockResolvedValue(undefined), |
| 13 | + evaluate: vi.fn().mockResolvedValue({ title: 'Demo', price: '¥99' }), |
| 14 | + snapshot: vi.fn().mockResolvedValue(undefined), |
| 15 | + click: vi.fn().mockResolvedValue(undefined), |
| 16 | + typeText: vi.fn().mockResolvedValue(undefined), |
| 17 | + pressKey: vi.fn().mockResolvedValue(undefined), |
| 18 | + scrollTo: vi.fn().mockResolvedValue(undefined), |
| 19 | + getFormState: vi.fn().mockResolvedValue({ forms: [], orphanFields: [] }), |
| 20 | + wait: vi.fn().mockResolvedValue(undefined), |
| 21 | + tabs: vi.fn().mockResolvedValue([]), |
| 22 | + selectTab: vi.fn().mockResolvedValue(undefined), |
| 23 | + networkRequests: vi.fn().mockResolvedValue([]), |
| 24 | + consoleMessages: vi.fn().mockResolvedValue([]), |
| 25 | + scroll: vi.fn().mockResolvedValue(undefined), |
| 26 | + autoScroll: vi.fn().mockResolvedValue(undefined), |
| 27 | + installInterceptor: vi.fn().mockResolvedValue(undefined), |
| 28 | + getInterceptedRequests: vi.fn().mockResolvedValue([]), |
| 29 | + getCookies: vi.fn().mockResolvedValue([]), |
| 30 | + screenshot: vi.fn().mockResolvedValue(''), |
| 31 | + waitForCapture: vi.fn().mockResolvedValue(undefined), |
| 32 | + } as unknown as IPage & { goto: ReturnType<typeof vi.fn>; evaluate: ReturnType<typeof vi.fn> }; |
| 33 | +} |
| 34 | + |
| 35 | +describe('jd command registration', () => { |
| 36 | + it('registers all jd shopping commands', () => { |
| 37 | + for (const name of ['search', 'detail', 'reviews', 'cart', 'add-cart']) { |
| 38 | + expect(getRegistry().get(`jd/${name}`)).toBeDefined(); |
| 39 | + } |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe('jd command safety', () => { |
| 44 | + it('rejects invalid numeric sku before evaluating page scripts', async () => { |
| 45 | + const page = createPageMock(); |
| 46 | + const detail = getRegistry().get('jd/detail'); |
| 47 | + await expect(detail!.func!(page, { sku: 'abc' })).rejects.toMatchObject({ |
| 48 | + name: 'ArgumentError', |
| 49 | + code: 'ARGUMENT', |
| 50 | + }); |
| 51 | + expect(page.goto).not.toHaveBeenCalled(); |
| 52 | + expect(page.evaluate).not.toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('supports dry-run for add-cart without mutating the cart', async () => { |
| 56 | + const page = createPageMock(); |
| 57 | + const addCart = getRegistry().get('jd/add-cart'); |
| 58 | + |
| 59 | + const result = await addCart!.func!(page, { sku: '100291143898', 'dry-run': true }); |
| 60 | + |
| 61 | + expect(result).toEqual([ |
| 62 | + expect.objectContaining({ |
| 63 | + status: 'dry-run', |
| 64 | + sku: '100291143898', |
| 65 | + }), |
| 66 | + ]); |
| 67 | + expect(page.goto).toHaveBeenCalledTimes(1); |
| 68 | + expect(page.goto).toHaveBeenCalledWith('https://item.jd.com/100291143898.html'); |
| 69 | + expect(page.evaluate).toHaveBeenCalledTimes(1); |
| 70 | + }); |
| 71 | +}); |
0 commit comments