import { describe, expect, it, vi } from 'vitest'; import { JSDOM } from 'jsdom'; import { getRegistry } from '@jackwener/opencli/registry'; import { AuthRequiredError } from '@jackwener/opencli/errors'; import { __test__ } from './item.js'; import './item.js'; const originalPerformance = globalThis.performance; const originalWindow = globalThis.window; const originalDocument = globalThis.document; function restoreGlobals() { globalThis.performance = originalPerformance; globalThis.window = originalWindow; globalThis.document = originalDocument; } describe('jd item adapter', () => { const command = getRegistry().get('jd/item'); it('registers the command with correct shape', () => { expect(command).toBeDefined(); expect(command.site).toBe('jd'); expect(command.name).toBe('item'); expect(command.domain).toBe('item.jd.com'); expect(command.strategy).toBe('cookie'); expect(typeof command.func).toBe('function'); }); it('has sku as a required positional arg', () => { const skuArg = command.args.find((a) => a.name === 'sku'); expect(skuArg).toBeDefined(); expect(skuArg.required).toBe(true); expect(skuArg.positional).toBe(true); }); it('has images arg with default 200', () => { const imagesArg = command.args.find((a) => a.name === 'images'); expect(imagesArg).toBeDefined(); expect(imagesArg.default).toBe(200); }); it('fails fast when JD blocks the item page', async () => { const page = { evaluate: vi.fn() .mockResolvedValueOnce('https://item.jd.com/100328272886.html') .mockResolvedValueOnce({ looksBlocked: true }) .mockResolvedValueOnce({ error: 'JD page is blocked by login/security verification', pageState: { looksBlocked: true }, }), goto: vi.fn(), wait: vi.fn(), }; await expect(command.func(page, { sku: '100328272886', images: 5 })).rejects.toBeInstanceOf(AuthRequiredError); expect(page.goto).not.toHaveBeenCalled(); }); it('fails fast when a loaded product page has no detail images', async () => { const page = { evaluate: vi.fn() .mockResolvedValueOnce('https://item.jd.com/100328272886.html') .mockResolvedValueOnce({ looksBlocked: false }) .mockResolvedValueOnce(undefined) .mockResolvedValueOnce(null) .mockResolvedValueOnce({ title: 'JD product', price: '100', shop: '京东自营', specs: {}, mainImages: ['https://img10.360buyimg.com/pcpubliccms/jfs/t1/main.jpg'], detailImages: [], pageState: { isProductPage: true, looksBlocked: false }, }), goto: vi.fn(), wait: vi.fn(), }; await expect(command.func(page, { sku: '100328272886', images: 5 })).rejects.toThrow('JD item detail images were not found'); }); it('allows zero-image requests to skip detail image fail-fast', async () => { const data = { title: 'JD product', price: '100', shop: '京东自营', specs: {}, mainImages: [], detailImages: [], pageState: { isProductPage: true, looksBlocked: false }, }; const page = { evaluate: vi.fn() .mockResolvedValueOnce('https://item.jd.com/100328272886.html') .mockResolvedValueOnce({ looksBlocked: false }) .mockResolvedValueOnce(undefined) .mockResolvedValueOnce(null) .mockResolvedValueOnce(data), goto: vi.fn(), wait: vi.fn(), }; await expect(command.func(page, { sku: '100328272886', images: 0 })).resolves.toEqual([data]); }); it('normalizes JD item URL input to a SKU before building selectors', () => { expect(__test__.normalizeJdSkuInput('100328272886')).toBe('100328272886'); expect(__test__.normalizeJdSkuInput('https://item.jd.com/100328272886.html?purchasetab=gfgm')).toBe('100328272886'); expect(__test__.normalizeJdSkuInput('skuId=10218494560141')).toBe('10218494560141'); }); it('includes expected columns', () => { expect(command.columns).toEqual(expect.arrayContaining(['title', 'price', 'shop', 'specs', 'mainImages', 'detailImages'])); expect(command.columns).not.toContain('avifImages'); }); it('extracts only detail avif images and respects the limit', () => { const result = __test__.extractAvifImages([ 'https://img14.360buyimg.com/n1/jfs/t1/normal.jpg', 'https://img10.360buyimg.com/imgzone/jfs/t1/detail.avif', 'https://pcpubliccms.jd.com/image1.avif', 'https://pcpubliccms.jd.com/image1.avif', 'https://pcpubliccms.jd.com/image2.avif?x=1', 'https://example.com/not-jd.avif', ], 2); expect(result).toEqual([ 'https://img10.360buyimg.com/imgzone/jfs/t1/detail.avif', ]); }); it('treats WareGraphic sku images as detail images only for WareGraphic fallback', () => { expect(__test__.isJdDetailImage('https://img10.360buyimg.com/sku/jfs/t1/color-option.gif')).toBe(false); expect(__test__.isJdWareGraphicDetailImage('https://img10.360buyimg.com/sku/jfs/t1/ware-graphic.jpg')).toBe(true); expect(__test__.isJdWareGraphicDetailImage('https://img10.360buyimg.com/sku/s228x228_jfs/t1/thumb.jpg')).toBe(false); }); it('keeps main gallery images while ignoring unrelated contexts for detail images', () => { const dom = new JSDOM(`
not image
`,
},
}),
}));
try {
await expect(__test__.extractDetailImagesFromPage(10)).resolves.toEqual([
'https://img30.360buyimg.com/sku/jfs/t1/ware-a.jpg',
'https://img30.360buyimg.com/sku/jfs/t1/ware-b.png',
]);
}
finally {
globalThis.fetch = previousFetch;
restoreGlobals();
}
});
it('extracts selected specs from the newer JD spec-list DOM', () => {
const dom = new JSDOM(`
洗烘套装