Files
jackwener__opencli/clis/weread/search-regression.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

441 lines
13 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { getRegistry } from '@jackwener/opencli/registry';
import './search.js';
describe('weread/search regression', () => {
beforeEach(() => {
vi.restoreAllMocks();
});
it('uses the query argument for the search API and returns reader urls from search html', async () => {
const command = getRegistry().get('weread/search');
expect(command?.func).toBeTypeOf('function');
const fetchMock = vi.fn()
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({
books: [
{
bookInfo: {
title: 'Deep Work',
author: 'Cal Newport',
bookId: 'abc123',
},
},
],
}),
})
.mockResolvedValueOnce({
ok: true,
text: () => Promise.resolve(`
<ul class="search_bookDetail_list">
<li class="wr_bookList_item">
<a class="wr_bookList_item_link" href="/web/reader/reader123"></a>
<p class="wr_bookList_item_title">Deep Work</p>
</li>
</ul>
`),
});
vi.stubGlobal('fetch', fetchMock);
const result = await command!.func!(null as any, { query: 'deep work', limit: 5 });
expect(fetchMock).toHaveBeenCalledTimes(2);
expect(String(fetchMock.mock.calls[0][0])).toContain('keyword=deep+work');
expect(String(fetchMock.mock.calls[1][0])).toContain('/web/search/books?keyword=deep+work');
expect(result).toEqual([
{
rank: 1,
title: 'Deep Work',
author: 'Cal Newport',
bookId: 'abc123',
url: 'https://weread.qq.com/web/reader/reader123',
},
]);
});
it('does not emit stale bookDetail urls when the reader url is unavailable', async () => {
const command = getRegistry().get('weread/search');
expect(command?.func).toBeTypeOf('function');
const fetchMock = vi.fn()
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({
books: [
{
bookInfo: {
title: 'Deep Work',
author: 'Cal Newport',
bookId: 'abc123',
},
},
],
}),
})
.mockResolvedValueOnce({
ok: true,
text: () => Promise.resolve('<html><body><p>no search cards</p></body></html>'),
});
vi.stubGlobal('fetch', fetchMock);
const result = await command!.func!(null as any, { query: 'deep work', limit: 5 });
expect(result).toEqual([
{
rank: 1,
title: 'Deep Work',
author: 'Cal Newport',
bookId: 'abc123',
url: '',
},
]);
});
it('matches reader urls by title queue instead of assuming identical result order', async () => {
const command = getRegistry().get('weread/search');
expect(command?.func).toBeTypeOf('function');
const fetchMock = vi.fn()
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({
books: [
{
bookInfo: {
title: 'Deep Work',
author: 'Cal Newport',
bookId: 'abc123',
},
},
{
bookInfo: {
title: 'Digital Minimalism',
author: 'Cal Newport',
bookId: 'xyz789',
},
},
],
}),
})
.mockResolvedValueOnce({
ok: true,
text: () => Promise.resolve(`
<ul class="search_bookDetail_list">
<li class="wr_bookList_item">
<a class="wr_bookList_item_link" href="/web/reader/unrelated111"></a>
<p class="wr_bookList_item_title">Atomic Habits</p>
</li>
<li class="wr_bookList_item">
<a class="wr_bookList_item_link" href="/web/reader/digital222"></a>
<p class="wr_bookList_item_title">Digital Minimalism</p>
</li>
<li class="wr_bookList_item">
<a class="wr_bookList_item_link" href="/web/reader/deep333"></a>
<p class="wr_bookList_item_title">Deep Work</p>
</li>
</ul>
`),
});
vi.stubGlobal('fetch', fetchMock);
const result = await command!.func!(null as any, { query: 'cal newport', limit: 5 });
expect(result).toEqual([
{
rank: 1,
title: 'Deep Work',
author: 'Cal Newport',
bookId: 'abc123',
url: 'https://weread.qq.com/web/reader/deep333',
},
{
rank: 2,
title: 'Digital Minimalism',
author: 'Cal Newport',
bookId: 'xyz789',
url: 'https://weread.qq.com/web/reader/digital222',
},
]);
});
it('falls back to empty urls when the search html request fails', async () => {
const command = getRegistry().get('weread/search');
expect(command?.func).toBeTypeOf('function');
const fetchMock = vi.fn()
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({
books: [
{
bookInfo: {
title: 'Deep Work',
author: 'Cal Newport',
bookId: 'abc123',
},
},
],
}),
})
.mockRejectedValueOnce(new Error('network timeout'));
vi.stubGlobal('fetch', fetchMock);
const result = await command!.func!(null as any, { query: 'deep work', limit: 5 });
expect(result).toEqual([
{
rank: 1,
title: 'Deep Work',
author: 'Cal Newport',
bookId: 'abc123',
url: '',
},
]);
});
it('binds reader urls with title and author instead of title alone', async () => {
const command = getRegistry().get('weread/search');
expect(command?.func).toBeTypeOf('function');
const fetchMock = vi.fn()
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({
books: [
{
bookInfo: {
title: '文明',
author: '作者甲',
bookId: 'book-a',
},
},
{
bookInfo: {
title: '文明',
author: '作者乙',
bookId: 'book-b',
},
},
],
}),
})
.mockResolvedValueOnce({
ok: true,
text: () => Promise.resolve(`
<ul class="search_bookDetail_list">
<li class="wr_bookList_item">
<a class="wr_bookList_item_link" href="/web/reader/book-b-reader"></a>
<p class="wr_bookList_item_title">文明</p>
<p class="wr_bookList_item_author"><a href="/web/search/books?author=%E4%BD%9C%E8%80%85%E4%B9%99">作者乙</a></p>
</li>
<li class="wr_bookList_item">
<a class="wr_bookList_item_link" href="/web/reader/book-a-reader"></a>
<p class="wr_bookList_item_title">文明</p>
<p class="wr_bookList_item_author"><a href="/web/search/books?author=%E4%BD%9C%E8%80%85%E7%94%B2">作者甲</a></p>
</li>
</ul>
`),
});
vi.stubGlobal('fetch', fetchMock);
const result = await command!.func!(null as any, { query: '文明', limit: 5 });
expect(result).toEqual([
{
rank: 1,
title: '文明',
author: '作者甲',
bookId: 'book-a',
url: 'https://weread.qq.com/web/reader/book-a-reader',
},
{
rank: 2,
title: '文明',
author: '作者乙',
bookId: 'book-b',
url: 'https://weread.qq.com/web/reader/book-b-reader',
},
]);
});
it('leaves urls empty when same-title results are ambiguous and html cards have no author', async () => {
const command = getRegistry().get('weread/search');
expect(command?.func).toBeTypeOf('function');
const fetchMock = vi.fn()
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({
books: [
{
bookInfo: {
title: '文明',
author: '作者甲',
bookId: 'book-a',
},
},
{
bookInfo: {
title: '文明',
author: '作者乙',
bookId: 'book-b',
},
},
],
}),
})
.mockResolvedValueOnce({
ok: true,
text: () => Promise.resolve(`
<ul class="search_bookDetail_list">
<li class="wr_bookList_item">
<a class="wr_bookList_item_link" href="/web/reader/book-b-reader"></a>
<p class="wr_bookList_item_title">文明</p>
</li>
<li class="wr_bookList_item">
<a class="wr_bookList_item_link" href="/web/reader/book-a-reader"></a>
<p class="wr_bookList_item_title">文明</p>
</li>
</ul>
`),
});
vi.stubGlobal('fetch', fetchMock);
const result = await command!.func!(null as any, { query: '文明', limit: 5 });
expect(result).toEqual([
{
rank: 1,
title: '文明',
author: '作者甲',
bookId: 'book-a',
url: '',
},
{
rank: 2,
title: '文明',
author: '作者乙',
bookId: 'book-b',
url: '',
},
]);
});
it('leaves urls empty when exact author matching fails and multiple html cards share the same title', async () => {
const command = getRegistry().get('weread/search');
expect(command?.func).toBeTypeOf('function');
const fetchMock = vi.fn()
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({
books: [
{
bookInfo: {
title: '文明',
author: '作者甲',
bookId: 'book-a',
},
},
],
}),
})
.mockResolvedValueOnce({
ok: true,
text: () => Promise.resolve(`
<ul class="search_bookDetail_list">
<li class="wr_bookList_item">
<a class="wr_bookList_item_link" href="/web/reader/book-a-reader"></a>
<p class="wr_bookList_item_title">文明</p>
<p class="wr_bookList_item_author"><a href="/web/search/books?author=%E4%BD%9C%E8%80%85%E4%B9%99">作者乙</a></p>
</li>
<li class="wr_bookList_item">
<a class="wr_bookList_item_link" href="/web/reader/book-a-reader-2"></a>
<p class="wr_bookList_item_title">文明</p>
</li>
</ul>
`),
});
vi.stubGlobal('fetch', fetchMock);
const result = await command!.func!(null as any, { query: '文明', limit: 5 });
expect(result).toEqual([
{
rank: 1,
title: '文明',
author: '作者甲',
bookId: 'book-a',
url: '',
},
]);
});
it('leaves urls empty when multiple results share the same title and author identity', async () => {
const command = getRegistry().get('weread/search');
expect(command?.func).toBeTypeOf('function');
const fetchMock = vi.fn()
.mockResolvedValueOnce({
ok: true,
json: () => Promise.resolve({
books: [
{
bookInfo: {
title: '文明',
author: '作者甲',
bookId: 'book-a',
},
},
{
bookInfo: {
title: '文明',
author: '作者甲',
bookId: 'book-b',
},
},
],
}),
})
.mockResolvedValueOnce({
ok: true,
text: () => Promise.resolve(`
<ul class="search_bookDetail_list">
<li class="wr_bookList_item">
<a class="wr_bookList_item_link" href="/web/reader/book-b-reader"></a>
<p class="wr_bookList_item_title">文明</p>
<p class="wr_bookList_item_author"><a href="/web/search/books?author=%E4%BD%9C%E8%80%85%E7%94%B2">作者甲</a></p>
</li>
<li class="wr_bookList_item">
<a class="wr_bookList_item_link" href="/web/reader/book-a-reader"></a>
<p class="wr_bookList_item_title">文明</p>
<p class="wr_bookList_item_author"><a href="/web/search/books?author=%E4%BD%9C%E8%80%85%E7%94%B2">作者甲</a></p>
</li>
</ul>
`),
});
vi.stubGlobal('fetch', fetchMock);
const result = await command!.func!(null as any, { query: '文明', limit: 5 });
expect(result).toEqual([
{
rank: 1,
title: '文明',
author: '作者甲',
bookId: 'book-a',
url: '',
},
{
rank: 2,
title: '文明',
author: '作者甲',
bookId: 'book-b',
url: '',
},
]);
});
});