Files
jackwener__opencli/clis/xiaohongshu/creator-notes.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

213 lines
6.0 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import type { IPage } from '@jackwener/opencli/types';
import { getRegistry } from '@jackwener/opencli/registry';
import { parseCreatorNoteIdsFromHtml, parseCreatorNotesText } from './creator-notes.js';
import './creator-notes.js';
function createPageMock(evaluateResult: any, interceptedRequests: any[] = []): IPage {
const evaluate = Array.isArray(evaluateResult)
? vi.fn()
.mockResolvedValueOnce(evaluateResult[0])
.mockResolvedValue(evaluateResult[evaluateResult.length - 1])
: vi.fn().mockResolvedValue(evaluateResult);
const getInterceptedRequests = Array.isArray(interceptedRequests)
? vi.fn().mockResolvedValue(interceptedRequests)
: vi.fn().mockResolvedValue([]);
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,
getCookies: vi.fn().mockResolvedValue([]),
screenshot: vi.fn().mockResolvedValue(''),
waitForCapture: vi.fn().mockResolvedValue(undefined),
};
}
describe('xiaohongshu creator-notes', () => {
it('parses creator note text blocks into rows', () => {
const bodyText = `笔记管理
全部笔记(366)
已发布
测试笔记一
发布于 2025年12月04日 19:45
148208
324
2279
465
32
权限设置
取消置顶
编辑
删除
仅自己可见
测试笔记二
发布于 2026年03月18日 12:39
10
0
0
0
0
权限设置`;
expect(parseCreatorNotesText(bodyText)).toEqual([
{
id: '',
title: '测试笔记一',
date: '2025年12月04日 19:45',
views: 148208,
likes: 2279,
collects: 465,
comments: 324,
url: '',
},
{
id: '',
title: '测试笔记二',
date: '2026年03月18日 12:39',
views: 10,
likes: 0,
collects: 0,
comments: 0,
url: '',
},
]);
});
it('reads body text and returns ranked rows', async () => {
const cmd = getRegistry().get('xiaohongshu/creator-notes');
expect(cmd?.func).toBeTypeOf('function');
const page = createPageMock([
undefined,
{
text: `示例笔记
发布于 2026年03月19日 12:00
10
2
3
4
5
权限设置`,
html: '"noteId":"aaaaaaaaaaaaaaaaaaaaaaaa"',
},
]);
const result = await cmd!.func!(page, { limit: 1 });
expect((page.evaluate as any).mock.calls.at(-1)?.[0]).toBe('() => ({ text: document.body.innerText, html: document.body.innerHTML })');
expect(result).toEqual([
{
rank: 1,
id: 'aaaaaaaaaaaaaaaaaaaaaaaa',
title: '示例笔记',
date: '2026年03月19日 12:00',
views: 10,
likes: 3,
collects: 4,
comments: 2,
url: 'https://creator.xiaohongshu.com/statistics/note-detail?noteId=aaaaaaaaaaaaaaaaaaaaaaaa',
},
]);
});
it('prefers note card dom data when the analyze api is unavailable', async () => {
const cmd = getRegistry().get('xiaohongshu/creator-notes');
expect(cmd?.func).toBeTypeOf('function');
const page = createPageMock([
undefined,
[
{
id: 'bbbbbbbbbbbbbbbbbbbbbbbb',
title: '测试笔记一',
date: '2025年12月04日 19:45',
metrics: [148284, 319, 2280, 466, 33],
},
],
]);
const result = await cmd!.func!(page, { limit: 1 });
expect(result).toEqual([
{
rank: 1,
id: 'bbbbbbbbbbbbbbbbbbbbbbbb',
title: '测试笔记一',
date: '2025年12月04日 19:45',
views: 148284,
likes: 2280,
collects: 466,
comments: 319,
url: 'https://creator.xiaohongshu.com/statistics/note-detail?noteId=bbbbbbbbbbbbbbbbbbbbbbbb',
},
]);
});
it('prefers the creator analyze API and preserves note ids', async () => {
const cmd = getRegistry().get('xiaohongshu/creator-notes');
expect(cmd?.func).toBeTypeOf('function');
const page = createPageMock(undefined, [{
data: {
note_infos: [
{
id: 'cccccccccccccccccccccccc',
title: '示例内容复盘',
post_time: new Date('2026-03-18T20:01:00+08:00').getTime(),
read_count: 521,
like_count: 18,
fav_count: 10,
comment_count: 7,
},
],
},
}]);
const result = await cmd!.func!(page, { limit: 1 });
expect((page.installInterceptor as any).mock.calls[0][0]).toContain('/api/galaxy/creator/datacenter/note/analyze/list');
expect(result).toEqual([
{
rank: 1,
id: 'cccccccccccccccccccccccc',
title: '示例内容复盘',
date: '2026年03月18日 20:01',
views: 521,
likes: 18,
collects: 10,
comments: 7,
url: 'https://creator.xiaohongshu.com/statistics/note-detail?noteId=cccccccccccccccccccccccc',
},
]);
});
it('extracts note ids from creator note-manager html', () => {
const html = `
<div>&quot;noteId&quot;:&quot;aaaaaaaaaaaaaaaaaaaaaaaa&quot;</div>
<div>&quot;noteId&quot;:&quot;dddddddddddddddddddddddd&quot;</div>
<div>&quot;noteId&quot;:&quot;aaaaaaaaaaaaaaaaaaaaaaaa&quot;</div>
`;
expect(parseCreatorNoteIdsFromHtml(html)).toEqual([
'aaaaaaaaaaaaaaaaaaaaaaaa',
'dddddddddddddddddddddddd',
]);
});
});