/ clis / jd / item.test.js
item.test.js
 1  import { describe, expect, it } from 'vitest';
 2  import { getRegistry } from '@jackwener/opencli/registry';
 3  import { __test__ } from './item.js';
 4  import './item.js';
 5  describe('jd item adapter', () => {
 6      const command = getRegistry().get('jd/item');
 7      it('registers the command with correct shape', () => {
 8          expect(command).toBeDefined();
 9          expect(command.site).toBe('jd');
10          expect(command.name).toBe('item');
11          expect(command.domain).toBe('item.jd.com');
12          expect(command.strategy).toBe('cookie');
13          expect(typeof command.func).toBe('function');
14      });
15      it('has sku as a required positional arg', () => {
16          const skuArg = command.args.find((a) => a.name === 'sku');
17          expect(skuArg).toBeDefined();
18          expect(skuArg.required).toBe(true);
19          expect(skuArg.positional).toBe(true);
20      });
21      it('has images arg with default 10', () => {
22          const imagesArg = command.args.find((a) => a.name === 'images');
23          expect(imagesArg).toBeDefined();
24          expect(imagesArg.default).toBe(10);
25      });
26      it('includes expected columns', () => {
27          expect(command.columns).toEqual(expect.arrayContaining(['title', 'price', 'shop', 'specs', 'avifImages']));
28      });
29      it('extracts only pcpubliccms avif images and respects the limit', () => {
30          const result = __test__.extractAvifImages([
31              'https://img14.360buyimg.com/n1/jfs/t1/normal.jpg',
32              'https://img10.360buyimg.com/imgzone/jfs/t1/detail.avif',
33              'https://pcpubliccms.jd.com/image1.avif',
34              'https://pcpubliccms.jd.com/image1.avif',
35              'https://pcpubliccms.jd.com/image2.avif?x=1',
36              'https://example.com/not-jd.avif',
37          ], 2);
38          expect(result).toEqual([
39              'https://pcpubliccms.jd.com/image1.avif',
40              'https://pcpubliccms.jd.com/image2.avif?x=1',
41          ]);
42      });
43  });