Files
jackwener__opencli/clis/xiaohongshu/search.test.ts
jakevin 4fe9a73ebc refactor: migrate adapter imports to package exports (#795)
* refactor: migrate adapter imports to package exports

Replace all relative imports (../../src/registry.js, ../../browser/cdp.js, etc.)
with package exports (@jackwener/opencli/registry, @jackwener/opencli/errors, etc.)
across all 484 adapter files.

This decouples adapter import resolution from directory structure:
- User CLIs in ~/.opencli/clis/ resolve via node_modules symlink
- Internal adapters resolve via Node.js self-referencing
- No more shim files needed for import resolution

Changes:
- package.json: add sub-path exports for all public modules
- clis/**: replace relative imports with @jackwener/opencli/...
- discovery.ts: simplify ensureUserCliCompatShims to symlink-only
- registry-api.ts: export CommandArgs type
- Remove root-level shim directories (browser/, download/, pipeline/)
- Remove shim entries from tsconfig.json include and package.json files

* test: add regression tests for package exports

Prevents regressions like #788/#791 by:
1. Scanning all adapter files for forbidden relative imports
   (../../src/, ../../browser/, etc.) — fails if any remain
2. Verifying every package.json export maps to an existing source file

18 new test cases.

* fix: use junction on Windows + broaden test patterns

- discovery.ts: use 'junction' symlink type on Windows (no admin required)
- package-exports.test.ts: generalize forbidden patterns to catch any
  depth of ../ traversal (not just ../../ and ../../../)

* fix: update stale vi.mock/importActual paths in adapter tests

Test files still used old relative paths for vi.mock() and
vi.importActual() calls. Updated 5 test files to use package exports.
Also broadened regression test patterns to catch mock/importActual paths.

* fix: use rm instead of unlink for symlink cleanup, add warn on failure

Addresses review feedback from Astro-Han:
- rm() handles both symlinks and stale directories (unlink fails on dirs)
- Log a warning when symlink creation fails instead of silent catch

* docs: update import examples to use package exports

Update all documentation, contributing guides, and skills to use
@jackwener/opencli/registry instead of ../../src/registry.js.

Without this, users following the docs would write adapters with
broken imports since the old shim files are no longer created.
2026-04-05 16:02:17 +08:00

192 lines
6.9 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import type { IPage } from '@jackwener/opencli/types';
import { getRegistry } from '@jackwener/opencli/registry';
import { noteIdToDate } from './search.js';
function createPageMock(evaluateResults: any[]): IPage {
const evaluate = vi.fn();
for (const result of evaluateResults) {
evaluate.mockResolvedValueOnce(result);
}
return {
goto: vi.fn().mockResolvedValue(undefined),
evaluate,
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('xiaohongshu search', () => {
it('throws a clear error when the search page is blocked by a login wall', async () => {
const cmd = getRegistry().get('xiaohongshu/search');
expect(cmd?.func).toBeTypeOf('function');
const page = createPageMock([
// First evaluate: MutationObserver wait (login wall detected)
'login_wall',
]);
await expect(cmd!.func!(page, { query: '特斯拉', limit: 5 })).rejects.toThrow(
'Xiaohongshu search results are blocked behind a login wall'
);
// autoScroll must NOT be called when a login wall is detected early
expect(page.autoScroll).not.toHaveBeenCalled();
});
it('returns ranked results with search_result url and author_url preserved', async () => {
const cmd = getRegistry().get('xiaohongshu/search');
expect(cmd?.func).toBeTypeOf('function');
const detailUrl =
'https://www.xiaohongshu.com/search_result/68e90be80000000004022e66?xsec_token=test-token&xsec_source=';
const authorUrl =
'https://www.xiaohongshu.com/user/profile/635a9c720000000018028b40?xsec_token=user-token&xsec_source=pc_search';
const page = createPageMock([
// First evaluate: MutationObserver wait (content appeared)
'content',
// Second evaluate: main DOM extraction (returns array directly)
[
{
title: '某鱼买FSD被坑了4万',
author: '随风',
likes: '261',
url: detailUrl,
author_url: authorUrl,
},
],
]);
const result = await cmd!.func!(page, { query: '特斯拉', limit: 1 });
// Should only do one goto (the search page itself), no per-note detail navigation
expect((page.goto as any).mock.calls).toHaveLength(1);
expect(result).toEqual([
{
rank: 1,
title: '某鱼买FSD被坑了4万',
author: '随风',
likes: '261',
published_at: '2025-10-10',
url: detailUrl,
author_url: authorUrl,
},
]);
});
it('filters out results with no title and respects the limit', async () => {
const cmd = getRegistry().get('xiaohongshu/search');
expect(cmd?.func).toBeTypeOf('function');
const page = createPageMock([
// First evaluate: MutationObserver wait (content appeared)
'content',
// Second evaluate: main DOM extraction (returns array directly)
[
{
title: 'Result A',
author: 'UserA',
likes: '10',
url: 'https://www.xiaohongshu.com/search_result/aaa',
author_url: '',
},
{
title: '',
author: 'UserB',
likes: '5',
url: 'https://www.xiaohongshu.com/search_result/bbb',
author_url: '',
},
{
title: 'Result C',
author: 'UserC',
likes: '3',
url: 'https://www.xiaohongshu.com/search_result/ccc',
author_url: '',
},
],
]);
const result = (await cmd!.func!(page, { query: '测试', limit: 1 })) as any[];
// limit=1 should return only the first valid-titled result
expect(result).toHaveLength(1);
expect(result[0]).toMatchObject({ rank: 1, title: 'Result A' });
});
it('waits for content via MutationObserver before extracting', async () => {
const cmd = getRegistry().get('xiaohongshu/search');
expect(cmd?.func).toBeTypeOf('function');
const page = createPageMock([
// First evaluate: MutationObserver wait (content appeared)
'content',
// Second evaluate: extraction (returns empty array)
[],
]);
const result = (await cmd!.func!(page, { query: '测试等待', limit: 5 })) as any[];
expect(result).toHaveLength(0);
// Only one navigation, no retry
expect(page.goto).toHaveBeenCalledTimes(1);
// Two evaluate calls: wait + extraction
expect(page.evaluate).toHaveBeenCalledTimes(2);
});
});
describe('noteIdToDate (ObjectID timestamp parsing)', () => {
it('parses a known note ID to the correct China-timezone date', () => {
// 0x697f6c74 = 1769958516 → 2026-02-01 in UTC+8
expect(noteIdToDate('https://www.xiaohongshu.com/search_result/697f6c74000000002103de17')).toBe('2026-02-01');
// 0x68e90be8 → 2025-10-10 in UTC+8
expect(noteIdToDate('https://www.xiaohongshu.com/explore/68e90be80000000004022e66')).toBe('2025-10-10');
});
it('returns China date when UTC+8 crosses into the next day', () => {
// 0x69b739f0 = 2026-03-15 23:00 UTC = 2026-03-16 07:00 CST
// Without UTC+8 offset this would incorrectly return 2026-03-15
expect(noteIdToDate('https://www.xiaohongshu.com/search_result/69b739f00000000000000000')).toBe('2026-03-16');
});
it('handles /note/ path variant', () => {
expect(noteIdToDate('https://www.xiaohongshu.com/note/697f6c74000000002103de17')).toBe('2026-02-01');
});
it('handles URL with query parameters', () => {
expect(noteIdToDate('https://www.xiaohongshu.com/search_result/697f6c74000000002103de17?xsec_token=abc')).toBe('2026-02-01');
});
it('returns empty string for non-matching URLs', () => {
expect(noteIdToDate('https://www.xiaohongshu.com/user/profile/635a9c720000000018028b40')).toBe('');
expect(noteIdToDate('https://www.xiaohongshu.com/')).toBe('');
});
it('returns empty string for IDs shorter than 24 hex chars', () => {
expect(noteIdToDate('https://www.xiaohongshu.com/search_result/abcdef')).toBe('');
});
it('returns empty string when timestamp is out of range', () => {
// All zeros → ts = 0
expect(noteIdToDate('https://www.xiaohongshu.com/search_result/000000000000000000000000')).toBe('');
});
});