Files
jackwener__opencli/clis/jd/commands.test.js
jakevin d2974a9ff6 refactor(adapters): convert adapter layer from TypeScript to JavaScript (#928)
* refactor(adapters): convert adapter layer from TypeScript to JavaScript

Core framework stays TypeScript; adapter layer moves to JS-first.
Adapters are essentially "executable config + browser scripts" that
barely use TS features — this simplifies the build/distribution pipeline
by removing the dist/clis/ intermediate compilation step.

Changes:
- Convert all 753 adapter files in clis/ from .ts to .js
- Update tsconfig to exclude clis/ from compilation
- Simplify build-manifest to scan clis/*.js directly (no dist/clis/)
- Update discovery, main, fetch-adapters to load JS adapters from clis/
- Update generate-verified to output .js artifacts
- Update package.json files field: dist/clis/ → clis/
- Fix all test files for the .ts → .js transition

* fix(main): use findPackageRoot for BUILTIN_CLIS path

The previous relative path (../../clis from __dirname) only worked for
dist/src/main.js but broke dev mode (tsx src/main.ts) where __dirname
is <repo>/src — resolving to /clis instead of <repo>/clis.

Use findPackageRoot() which works for both dev and prod paths.
2026-04-10 14:52:18 +08:00

65 lines
2.8 KiB
JavaScript

import { describe, expect, it, vi } from 'vitest';
import { getRegistry } from '@jackwener/opencli/registry';
import './search.js';
import './detail.js';
import './reviews.js';
import './cart.js';
import './add-cart.js';
function createPageMock() {
return {
goto: vi.fn().mockResolvedValue(undefined),
evaluate: vi.fn().mockResolvedValue({ title: 'Demo', price: '¥99' }),
snapshot: vi.fn().mockResolvedValue(undefined),
click: vi.fn().mockResolvedValue(undefined),
typeText: vi.fn().mockResolvedValue(undefined),
pressKey: vi.fn().mockResolvedValue(undefined),
scrollTo: vi.fn().mockResolvedValue(undefined),
getFormState: vi.fn().mockResolvedValue({ forms: [], orphanFields: [] }),
wait: vi.fn().mockResolvedValue(undefined),
tabs: vi.fn().mockResolvedValue([]),
selectTab: vi.fn().mockResolvedValue(undefined),
networkRequests: vi.fn().mockResolvedValue([]),
consoleMessages: vi.fn().mockResolvedValue([]),
scroll: vi.fn().mockResolvedValue(undefined),
autoScroll: vi.fn().mockResolvedValue(undefined),
installInterceptor: vi.fn().mockResolvedValue(undefined),
getInterceptedRequests: vi.fn().mockResolvedValue([]),
getCookies: vi.fn().mockResolvedValue([]),
screenshot: vi.fn().mockResolvedValue(''),
waitForCapture: vi.fn().mockResolvedValue(undefined),
};
}
describe('jd command registration', () => {
it('registers all jd shopping commands', () => {
for (const name of ['search', 'detail', 'reviews', 'cart', 'add-cart']) {
expect(getRegistry().get(`jd/${name}`)).toBeDefined();
}
});
});
describe('jd command safety', () => {
it('rejects invalid numeric sku before evaluating page scripts', async () => {
const page = createPageMock();
const detail = getRegistry().get('jd/detail');
await expect(detail.func(page, { sku: 'abc' })).rejects.toMatchObject({
name: 'ArgumentError',
code: 'ARGUMENT',
});
expect(page.goto).not.toHaveBeenCalled();
expect(page.evaluate).not.toHaveBeenCalled();
});
it('supports dry-run for add-cart without mutating the cart', async () => {
const page = createPageMock();
const addCart = getRegistry().get('jd/add-cart');
const result = await addCart.func(page, { sku: '100291143898', 'dry-run': true });
expect(result).toEqual([
expect.objectContaining({
status: 'dry-run',
sku: '100291143898',
}),
]);
expect(page.goto).toHaveBeenCalledTimes(1);
expect(page.goto).toHaveBeenCalledWith('https://item.jd.com/100291143898.html');
expect(page.evaluate).toHaveBeenCalledTimes(1);
});
});