@jackwener/opencli 1.5.4 → 1.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +27 -2
- package/README.zh-CN.md +36 -4
- package/dist/browser/daemon-client.d.ts +5 -1
- package/dist/browser/page.d.ts +6 -0
- package/dist/browser/page.js +15 -0
- package/dist/cli-manifest.json +1284 -67
- package/dist/cli.js +14 -14
- package/dist/clis/antigravity/serve.js +2 -2
- package/dist/clis/band/bands.d.ts +1 -0
- package/dist/clis/band/bands.js +72 -0
- package/dist/clis/band/mentions.d.ts +1 -0
- package/dist/clis/band/mentions.js +127 -0
- package/dist/clis/band/post.d.ts +1 -0
- package/dist/clis/band/post.js +175 -0
- package/dist/clis/band/posts.d.ts +1 -0
- package/dist/clis/band/posts.js +94 -0
- package/dist/clis/doubao/detail.d.ts +1 -0
- package/dist/clis/doubao/detail.js +33 -0
- package/dist/clis/doubao/detail.test.d.ts +1 -0
- package/dist/clis/doubao/detail.test.js +42 -0
- package/dist/clis/doubao/history.d.ts +1 -0
- package/dist/clis/doubao/history.js +28 -0
- package/dist/clis/doubao/history.test.d.ts +1 -0
- package/dist/clis/doubao/history.test.js +37 -0
- package/dist/clis/doubao/meeting-summary.d.ts +1 -0
- package/dist/clis/doubao/meeting-summary.js +39 -0
- package/dist/clis/doubao/meeting-transcript.d.ts +1 -0
- package/dist/clis/doubao/meeting-transcript.js +36 -0
- package/dist/clis/doubao/utils.d.ts +27 -0
- package/dist/clis/doubao/utils.js +317 -0
- package/dist/clis/doubao/utils.test.d.ts +1 -0
- package/dist/clis/doubao/utils.test.js +24 -0
- package/dist/clis/douyin/_shared/public-api.d.ts +33 -0
- package/dist/clis/douyin/_shared/public-api.js +29 -0
- package/dist/clis/douyin/user-videos.d.ts +5 -0
- package/dist/clis/douyin/user-videos.js +74 -0
- package/dist/clis/douyin/user-videos.test.d.ts +1 -0
- package/dist/clis/douyin/user-videos.test.js +108 -0
- package/dist/clis/ones/common.d.ts +32 -0
- package/dist/clis/ones/common.js +144 -0
- package/dist/clis/ones/enrich-tasks.d.ts +5 -0
- package/dist/clis/ones/enrich-tasks.js +37 -0
- package/dist/clis/ones/login.d.ts +1 -0
- package/dist/clis/ones/login.js +80 -0
- package/dist/clis/ones/logout.d.ts +1 -0
- package/dist/clis/ones/logout.js +17 -0
- package/dist/clis/ones/me.d.ts +1 -0
- package/dist/clis/ones/me.js +30 -0
- package/dist/clis/ones/my-tasks.d.ts +1 -0
- package/dist/clis/ones/my-tasks.js +120 -0
- package/dist/clis/ones/resolve-labels.d.ts +10 -0
- package/dist/clis/ones/resolve-labels.js +64 -0
- package/dist/clis/ones/task-helpers.d.ts +29 -0
- package/dist/clis/ones/task-helpers.js +212 -0
- package/dist/clis/ones/task-helpers.test.d.ts +1 -0
- package/dist/clis/ones/task-helpers.test.js +12 -0
- package/dist/clis/ones/task.d.ts +1 -0
- package/dist/clis/ones/task.js +66 -0
- package/dist/clis/ones/tasks.d.ts +1 -0
- package/dist/clis/ones/tasks.js +79 -0
- package/dist/clis/ones/token-info.d.ts +1 -0
- package/dist/clis/ones/token-info.js +42 -0
- package/dist/clis/ones/worklog.d.ts +11 -0
- package/dist/clis/ones/worklog.js +267 -0
- package/dist/clis/ones/worklog.test.d.ts +1 -0
- package/dist/clis/ones/worklog.test.js +20 -0
- package/dist/clis/sinafinance/rolling-news.d.ts +4 -0
- package/dist/clis/sinafinance/rolling-news.js +40 -0
- package/dist/clis/sinafinance/stock.d.ts +8 -0
- package/dist/clis/sinafinance/stock.js +117 -0
- package/dist/clis/spotify/spotify.d.ts +1 -0
- package/dist/clis/spotify/spotify.js +316 -0
- package/dist/clis/spotify/utils.d.ts +21 -0
- package/dist/clis/spotify/utils.js +66 -0
- package/dist/clis/spotify/utils.test.d.ts +1 -0
- package/dist/clis/spotify/utils.test.js +67 -0
- package/dist/clis/tieba/commands.test.d.ts +4 -0
- package/dist/clis/tieba/commands.test.js +79 -0
- package/dist/clis/tieba/hot.d.ts +1 -0
- package/dist/clis/tieba/hot.js +48 -0
- package/dist/clis/tieba/posts.d.ts +1 -0
- package/dist/clis/tieba/posts.js +85 -0
- package/dist/clis/tieba/read.d.ts +1 -0
- package/dist/clis/tieba/read.js +140 -0
- package/dist/clis/tieba/search.d.ts +1 -0
- package/dist/clis/tieba/search.js +108 -0
- package/dist/clis/tieba/utils.d.ts +101 -0
- package/dist/clis/tieba/utils.js +240 -0
- package/dist/clis/tieba/utils.test.d.ts +1 -0
- package/dist/clis/tieba/utils.test.js +290 -0
- package/dist/clis/weread/book.js +100 -13
- package/dist/clis/weread/commands.test.js +221 -0
- package/dist/clis/weread/private-api-regression.test.d.ts +1 -0
- package/dist/{weread-private-api-regression.test.js → clis/weread/private-api-regression.test.js} +92 -30
- package/dist/clis/weread/search-regression.test.d.ts +1 -0
- package/dist/clis/weread/search-regression.test.js +407 -0
- package/dist/clis/weread/search.js +143 -7
- package/dist/clis/weread/shelf.js +13 -95
- package/dist/clis/weread/utils.d.ts +46 -0
- package/dist/clis/weread/utils.js +214 -7
- package/dist/clis/weread/utils.test.js +71 -1
- package/dist/clis/xiaohongshu/publish.d.ts +1 -1
- package/dist/clis/xiaohongshu/publish.js +78 -31
- package/dist/clis/xiaohongshu/publish.test.js +66 -1
- package/dist/clis/xiaohongshu/user-helpers.d.ts +1 -0
- package/dist/clis/xiaohongshu/user-helpers.js +2 -0
- package/dist/clis/xiaohongshu/user-helpers.test.js +18 -0
- package/dist/clis/xueqiu/comments.d.ts +118 -0
- package/dist/clis/xueqiu/comments.js +354 -0
- package/dist/clis/xueqiu/comments.test.d.ts +1 -0
- package/dist/clis/xueqiu/comments.test.js +696 -0
- package/dist/clis/youtube/transcript.js +2 -4
- package/dist/clis/youtube/utils.d.ts +9 -0
- package/dist/clis/youtube/utils.js +67 -3
- package/dist/clis/youtube/utils.test.d.ts +1 -0
- package/dist/clis/youtube/utils.test.js +37 -0
- package/dist/clis/youtube/video.js +16 -15
- package/dist/clis/zsxq/dynamics.d.ts +1 -0
- package/dist/clis/zsxq/dynamics.js +47 -0
- package/dist/clis/zsxq/groups.d.ts +1 -0
- package/dist/clis/zsxq/groups.js +32 -0
- package/dist/clis/zsxq/search.d.ts +1 -0
- package/dist/clis/zsxq/search.js +43 -0
- package/dist/clis/zsxq/search.test.d.ts +1 -0
- package/dist/clis/zsxq/search.test.js +24 -0
- package/dist/clis/zsxq/topic.d.ts +1 -0
- package/dist/clis/zsxq/topic.js +47 -0
- package/dist/clis/zsxq/topic.test.d.ts +1 -0
- package/dist/clis/zsxq/topic.test.js +29 -0
- package/dist/clis/zsxq/topics.d.ts +1 -0
- package/dist/clis/zsxq/topics.js +25 -0
- package/dist/clis/zsxq/topics.test.d.ts +1 -0
- package/dist/clis/zsxq/topics.test.js +24 -0
- package/dist/clis/zsxq/utils.d.ts +97 -0
- package/dist/clis/zsxq/utils.js +230 -0
- package/dist/commanderAdapter.js +27 -4
- package/dist/commanderAdapter.test.js +39 -0
- package/dist/daemon.js +5 -4
- package/dist/errors.d.ts +29 -1
- package/dist/errors.js +49 -11
- package/dist/external-clis.yaml +17 -0
- package/dist/external.js +3 -3
- package/dist/main.js +2 -1
- package/dist/tui.js +2 -1
- package/dist/types.d.ts +5 -0
- package/docs/.vitepress/config.mts +3 -0
- package/docs/adapters/browser/band.md +63 -0
- package/docs/adapters/browser/ones.md +59 -0
- package/docs/adapters/browser/sinafinance.md +56 -6
- package/docs/adapters/browser/spotify.md +62 -0
- package/docs/adapters/browser/tieba.md +45 -0
- package/docs/adapters/browser/xueqiu.md +5 -0
- package/docs/adapters/browser/zsxq.md +49 -0
- package/docs/adapters/index.md +5 -2
- package/docs/adapters-doc/ones.md +32 -0
- package/extension/dist/background.js +1 -2
- package/extension/manifest.json +1 -1
- package/extension/package.json +1 -1
- package/extension/src/background.ts +17 -1
- package/extension/src/cdp.ts +42 -0
- package/extension/src/protocol.ts +5 -1
- package/package.json +1 -1
- package/scripts/postinstall.js +16 -0
- package/src/browser/daemon-client.ts +5 -1
- package/src/browser/page.ts +16 -0
- package/src/cli.ts +14 -14
- package/src/clis/antigravity/serve.ts +2 -2
- package/src/clis/band/bands.ts +76 -0
- package/src/clis/band/mentions.ts +134 -0
- package/src/clis/band/post.ts +187 -0
- package/src/clis/band/posts.ts +106 -0
- package/src/clis/doubao/detail.test.ts +53 -0
- package/src/clis/doubao/detail.ts +41 -0
- package/src/clis/doubao/history.test.ts +45 -0
- package/src/clis/doubao/history.ts +32 -0
- package/src/clis/doubao/meeting-summary.ts +53 -0
- package/src/clis/doubao/meeting-transcript.ts +48 -0
- package/src/clis/doubao/utils.test.ts +45 -0
- package/src/clis/doubao/utils.ts +371 -0
- package/src/clis/douyin/_shared/public-api.ts +84 -0
- package/src/clis/douyin/user-videos.test.ts +122 -0
- package/src/clis/douyin/user-videos.ts +101 -0
- package/src/clis/ones/common.ts +187 -0
- package/src/clis/ones/enrich-tasks.ts +47 -0
- package/src/clis/ones/login.ts +103 -0
- package/src/clis/ones/logout.ts +19 -0
- package/src/clis/ones/me.ts +34 -0
- package/src/clis/ones/my-tasks.ts +148 -0
- package/src/clis/ones/resolve-labels.ts +80 -0
- package/src/clis/ones/task-helpers.test.ts +14 -0
- package/src/clis/ones/task-helpers.ts +214 -0
- package/src/clis/ones/task.ts +79 -0
- package/src/clis/ones/tasks.ts +92 -0
- package/src/clis/ones/token-info.ts +46 -0
- package/src/clis/ones/worklog.test.ts +24 -0
- package/src/clis/ones/worklog.ts +306 -0
- package/src/clis/sinafinance/rolling-news.ts +42 -0
- package/src/clis/sinafinance/stock.ts +127 -0
- package/src/clis/spotify/spotify.ts +328 -0
- package/src/clis/spotify/utils.test.ts +87 -0
- package/src/clis/spotify/utils.ts +92 -0
- package/src/clis/tieba/commands.test.ts +86 -0
- package/src/clis/tieba/hot.ts +52 -0
- package/src/clis/tieba/posts.ts +108 -0
- package/src/clis/tieba/read.ts +158 -0
- package/src/clis/tieba/search.ts +119 -0
- package/src/clis/tieba/utils.test.ts +322 -0
- package/src/clis/tieba/utils.ts +348 -0
- package/src/clis/weread/book.ts +116 -13
- package/src/clis/weread/commands.test.ts +249 -0
- package/src/{weread-private-api-regression.test.ts → clis/weread/private-api-regression.test.ts} +108 -30
- package/src/clis/weread/search-regression.test.ts +440 -0
- package/src/clis/weread/search.ts +189 -9
- package/src/clis/weread/shelf.ts +20 -122
- package/src/clis/weread/utils.test.ts +81 -1
- package/src/clis/weread/utils.ts +264 -7
- package/src/clis/xiaohongshu/publish.test.ts +79 -1
- package/src/clis/xiaohongshu/publish.ts +84 -30
- package/src/clis/xiaohongshu/user-helpers.test.ts +23 -0
- package/src/clis/xiaohongshu/user-helpers.ts +4 -0
- package/src/clis/xueqiu/comments.test.ts +823 -0
- package/src/clis/xueqiu/comments.ts +461 -0
- package/src/clis/youtube/transcript.ts +2 -4
- package/src/clis/youtube/utils.test.ts +43 -0
- package/src/clis/youtube/utils.ts +69 -0
- package/src/clis/youtube/video.ts +16 -15
- package/src/clis/zsxq/dynamics.ts +60 -0
- package/src/clis/zsxq/groups.ts +41 -0
- package/src/clis/zsxq/search.test.ts +29 -0
- package/src/clis/zsxq/search.ts +54 -0
- package/src/clis/zsxq/topic.test.ts +34 -0
- package/src/clis/zsxq/topic.ts +68 -0
- package/src/clis/zsxq/topics.test.ts +29 -0
- package/src/clis/zsxq/topics.ts +36 -0
- package/src/clis/zsxq/utils.ts +351 -0
- package/src/commanderAdapter.test.ts +47 -0
- package/src/commanderAdapter.ts +26 -3
- package/src/daemon.ts +5 -4
- package/src/errors.ts +71 -10
- package/src/external-clis.yaml +17 -0
- package/src/external.ts +3 -3
- package/src/main.ts +2 -1
- package/src/tui.ts +2 -1
- package/src/types.ts +5 -0
- package/tests/e2e/band-auth.test.ts +20 -0
- package/tests/e2e/browser-auth-helpers.ts +18 -0
- package/tests/e2e/browser-auth.test.ts +35 -47
- package/tests/e2e/browser-public.test.ts +288 -0
- package/tests/e2e/management.test.ts +1 -1
- package/tests/e2e/plugin-management.test.ts +1 -1
- package/vitest.config.ts +1 -0
- package/SKILL.md +0 -879
- package/dist/weread-private-api-regression.test.d.ts +0 -1
- package/dist/weread-search-regression.test.d.ts +0 -1
- package/dist/weread-search-regression.test.js +0 -39
- package/src/weread-search-regression.test.ts +0 -44
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { getRegistry } from '../../registry.js';
|
|
3
|
+
import './search.js';
|
|
4
|
+
|
|
5
|
+
describe('weread/search regression', () => {
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
vi.restoreAllMocks();
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('uses the query argument for the search API and returns reader urls from search html', async () => {
|
|
11
|
+
const command = getRegistry().get('weread/search');
|
|
12
|
+
expect(command?.func).toBeTypeOf('function');
|
|
13
|
+
|
|
14
|
+
const fetchMock = vi.fn()
|
|
15
|
+
.mockResolvedValueOnce({
|
|
16
|
+
ok: true,
|
|
17
|
+
json: () => Promise.resolve({
|
|
18
|
+
books: [
|
|
19
|
+
{
|
|
20
|
+
bookInfo: {
|
|
21
|
+
title: 'Deep Work',
|
|
22
|
+
author: 'Cal Newport',
|
|
23
|
+
bookId: 'abc123',
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
}),
|
|
28
|
+
})
|
|
29
|
+
.mockResolvedValueOnce({
|
|
30
|
+
ok: true,
|
|
31
|
+
text: () => Promise.resolve(`
|
|
32
|
+
<ul class="search_bookDetail_list">
|
|
33
|
+
<li class="wr_bookList_item">
|
|
34
|
+
<a class="wr_bookList_item_link" href="/web/reader/reader123"></a>
|
|
35
|
+
<p class="wr_bookList_item_title">Deep Work</p>
|
|
36
|
+
</li>
|
|
37
|
+
</ul>
|
|
38
|
+
`),
|
|
39
|
+
});
|
|
40
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
41
|
+
|
|
42
|
+
const result = await command!.func!(null as any, { query: 'deep work', limit: 5 });
|
|
43
|
+
|
|
44
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
45
|
+
expect(String(fetchMock.mock.calls[0][0])).toContain('keyword=deep+work');
|
|
46
|
+
expect(String(fetchMock.mock.calls[1][0])).toContain('/web/search/books?keyword=deep+work');
|
|
47
|
+
expect(result).toEqual([
|
|
48
|
+
{
|
|
49
|
+
rank: 1,
|
|
50
|
+
title: 'Deep Work',
|
|
51
|
+
author: 'Cal Newport',
|
|
52
|
+
bookId: 'abc123',
|
|
53
|
+
url: 'https://weread.qq.com/web/reader/reader123',
|
|
54
|
+
},
|
|
55
|
+
]);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('does not emit stale bookDetail urls when the reader url is unavailable', async () => {
|
|
59
|
+
const command = getRegistry().get('weread/search');
|
|
60
|
+
expect(command?.func).toBeTypeOf('function');
|
|
61
|
+
|
|
62
|
+
const fetchMock = vi.fn()
|
|
63
|
+
.mockResolvedValueOnce({
|
|
64
|
+
ok: true,
|
|
65
|
+
json: () => Promise.resolve({
|
|
66
|
+
books: [
|
|
67
|
+
{
|
|
68
|
+
bookInfo: {
|
|
69
|
+
title: 'Deep Work',
|
|
70
|
+
author: 'Cal Newport',
|
|
71
|
+
bookId: 'abc123',
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
],
|
|
75
|
+
}),
|
|
76
|
+
})
|
|
77
|
+
.mockResolvedValueOnce({
|
|
78
|
+
ok: true,
|
|
79
|
+
text: () => Promise.resolve('<html><body><p>no search cards</p></body></html>'),
|
|
80
|
+
});
|
|
81
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
82
|
+
|
|
83
|
+
const result = await command!.func!(null as any, { query: 'deep work', limit: 5 });
|
|
84
|
+
|
|
85
|
+
expect(result).toEqual([
|
|
86
|
+
{
|
|
87
|
+
rank: 1,
|
|
88
|
+
title: 'Deep Work',
|
|
89
|
+
author: 'Cal Newport',
|
|
90
|
+
bookId: 'abc123',
|
|
91
|
+
url: '',
|
|
92
|
+
},
|
|
93
|
+
]);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('matches reader urls by title queue instead of assuming identical result order', async () => {
|
|
97
|
+
const command = getRegistry().get('weread/search');
|
|
98
|
+
expect(command?.func).toBeTypeOf('function');
|
|
99
|
+
|
|
100
|
+
const fetchMock = vi.fn()
|
|
101
|
+
.mockResolvedValueOnce({
|
|
102
|
+
ok: true,
|
|
103
|
+
json: () => Promise.resolve({
|
|
104
|
+
books: [
|
|
105
|
+
{
|
|
106
|
+
bookInfo: {
|
|
107
|
+
title: 'Deep Work',
|
|
108
|
+
author: 'Cal Newport',
|
|
109
|
+
bookId: 'abc123',
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
bookInfo: {
|
|
114
|
+
title: 'Digital Minimalism',
|
|
115
|
+
author: 'Cal Newport',
|
|
116
|
+
bookId: 'xyz789',
|
|
117
|
+
},
|
|
118
|
+
},
|
|
119
|
+
],
|
|
120
|
+
}),
|
|
121
|
+
})
|
|
122
|
+
.mockResolvedValueOnce({
|
|
123
|
+
ok: true,
|
|
124
|
+
text: () => Promise.resolve(`
|
|
125
|
+
<ul class="search_bookDetail_list">
|
|
126
|
+
<li class="wr_bookList_item">
|
|
127
|
+
<a class="wr_bookList_item_link" href="/web/reader/unrelated111"></a>
|
|
128
|
+
<p class="wr_bookList_item_title">Atomic Habits</p>
|
|
129
|
+
</li>
|
|
130
|
+
<li class="wr_bookList_item">
|
|
131
|
+
<a class="wr_bookList_item_link" href="/web/reader/digital222"></a>
|
|
132
|
+
<p class="wr_bookList_item_title">Digital Minimalism</p>
|
|
133
|
+
</li>
|
|
134
|
+
<li class="wr_bookList_item">
|
|
135
|
+
<a class="wr_bookList_item_link" href="/web/reader/deep333"></a>
|
|
136
|
+
<p class="wr_bookList_item_title">Deep Work</p>
|
|
137
|
+
</li>
|
|
138
|
+
</ul>
|
|
139
|
+
`),
|
|
140
|
+
});
|
|
141
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
142
|
+
|
|
143
|
+
const result = await command!.func!(null as any, { query: 'cal newport', limit: 5 });
|
|
144
|
+
|
|
145
|
+
expect(result).toEqual([
|
|
146
|
+
{
|
|
147
|
+
rank: 1,
|
|
148
|
+
title: 'Deep Work',
|
|
149
|
+
author: 'Cal Newport',
|
|
150
|
+
bookId: 'abc123',
|
|
151
|
+
url: 'https://weread.qq.com/web/reader/deep333',
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
rank: 2,
|
|
155
|
+
title: 'Digital Minimalism',
|
|
156
|
+
author: 'Cal Newport',
|
|
157
|
+
bookId: 'xyz789',
|
|
158
|
+
url: 'https://weread.qq.com/web/reader/digital222',
|
|
159
|
+
},
|
|
160
|
+
]);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('falls back to empty urls when the search html request fails', async () => {
|
|
164
|
+
const command = getRegistry().get('weread/search');
|
|
165
|
+
expect(command?.func).toBeTypeOf('function');
|
|
166
|
+
|
|
167
|
+
const fetchMock = vi.fn()
|
|
168
|
+
.mockResolvedValueOnce({
|
|
169
|
+
ok: true,
|
|
170
|
+
json: () => Promise.resolve({
|
|
171
|
+
books: [
|
|
172
|
+
{
|
|
173
|
+
bookInfo: {
|
|
174
|
+
title: 'Deep Work',
|
|
175
|
+
author: 'Cal Newport',
|
|
176
|
+
bookId: 'abc123',
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
],
|
|
180
|
+
}),
|
|
181
|
+
})
|
|
182
|
+
.mockRejectedValueOnce(new Error('network timeout'));
|
|
183
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
184
|
+
|
|
185
|
+
const result = await command!.func!(null as any, { query: 'deep work', limit: 5 });
|
|
186
|
+
|
|
187
|
+
expect(result).toEqual([
|
|
188
|
+
{
|
|
189
|
+
rank: 1,
|
|
190
|
+
title: 'Deep Work',
|
|
191
|
+
author: 'Cal Newport',
|
|
192
|
+
bookId: 'abc123',
|
|
193
|
+
url: '',
|
|
194
|
+
},
|
|
195
|
+
]);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('binds reader urls with title and author instead of title alone', async () => {
|
|
199
|
+
const command = getRegistry().get('weread/search');
|
|
200
|
+
expect(command?.func).toBeTypeOf('function');
|
|
201
|
+
|
|
202
|
+
const fetchMock = vi.fn()
|
|
203
|
+
.mockResolvedValueOnce({
|
|
204
|
+
ok: true,
|
|
205
|
+
json: () => Promise.resolve({
|
|
206
|
+
books: [
|
|
207
|
+
{
|
|
208
|
+
bookInfo: {
|
|
209
|
+
title: '文明',
|
|
210
|
+
author: '作者甲',
|
|
211
|
+
bookId: 'book-a',
|
|
212
|
+
},
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
bookInfo: {
|
|
216
|
+
title: '文明',
|
|
217
|
+
author: '作者乙',
|
|
218
|
+
bookId: 'book-b',
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
],
|
|
222
|
+
}),
|
|
223
|
+
})
|
|
224
|
+
.mockResolvedValueOnce({
|
|
225
|
+
ok: true,
|
|
226
|
+
text: () => Promise.resolve(`
|
|
227
|
+
<ul class="search_bookDetail_list">
|
|
228
|
+
<li class="wr_bookList_item">
|
|
229
|
+
<a class="wr_bookList_item_link" href="/web/reader/book-b-reader"></a>
|
|
230
|
+
<p class="wr_bookList_item_title">文明</p>
|
|
231
|
+
<p class="wr_bookList_item_author"><a href="/web/search/books?author=%E4%BD%9C%E8%80%85%E4%B9%99">作者乙</a></p>
|
|
232
|
+
</li>
|
|
233
|
+
<li class="wr_bookList_item">
|
|
234
|
+
<a class="wr_bookList_item_link" href="/web/reader/book-a-reader"></a>
|
|
235
|
+
<p class="wr_bookList_item_title">文明</p>
|
|
236
|
+
<p class="wr_bookList_item_author"><a href="/web/search/books?author=%E4%BD%9C%E8%80%85%E7%94%B2">作者甲</a></p>
|
|
237
|
+
</li>
|
|
238
|
+
</ul>
|
|
239
|
+
`),
|
|
240
|
+
});
|
|
241
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
242
|
+
|
|
243
|
+
const result = await command!.func!(null as any, { query: '文明', limit: 5 });
|
|
244
|
+
|
|
245
|
+
expect(result).toEqual([
|
|
246
|
+
{
|
|
247
|
+
rank: 1,
|
|
248
|
+
title: '文明',
|
|
249
|
+
author: '作者甲',
|
|
250
|
+
bookId: 'book-a',
|
|
251
|
+
url: 'https://weread.qq.com/web/reader/book-a-reader',
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
rank: 2,
|
|
255
|
+
title: '文明',
|
|
256
|
+
author: '作者乙',
|
|
257
|
+
bookId: 'book-b',
|
|
258
|
+
url: 'https://weread.qq.com/web/reader/book-b-reader',
|
|
259
|
+
},
|
|
260
|
+
]);
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it('leaves urls empty when same-title results are ambiguous and html cards have no author', async () => {
|
|
264
|
+
const command = getRegistry().get('weread/search');
|
|
265
|
+
expect(command?.func).toBeTypeOf('function');
|
|
266
|
+
|
|
267
|
+
const fetchMock = vi.fn()
|
|
268
|
+
.mockResolvedValueOnce({
|
|
269
|
+
ok: true,
|
|
270
|
+
json: () => Promise.resolve({
|
|
271
|
+
books: [
|
|
272
|
+
{
|
|
273
|
+
bookInfo: {
|
|
274
|
+
title: '文明',
|
|
275
|
+
author: '作者甲',
|
|
276
|
+
bookId: 'book-a',
|
|
277
|
+
},
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
bookInfo: {
|
|
281
|
+
title: '文明',
|
|
282
|
+
author: '作者乙',
|
|
283
|
+
bookId: 'book-b',
|
|
284
|
+
},
|
|
285
|
+
},
|
|
286
|
+
],
|
|
287
|
+
}),
|
|
288
|
+
})
|
|
289
|
+
.mockResolvedValueOnce({
|
|
290
|
+
ok: true,
|
|
291
|
+
text: () => Promise.resolve(`
|
|
292
|
+
<ul class="search_bookDetail_list">
|
|
293
|
+
<li class="wr_bookList_item">
|
|
294
|
+
<a class="wr_bookList_item_link" href="/web/reader/book-b-reader"></a>
|
|
295
|
+
<p class="wr_bookList_item_title">文明</p>
|
|
296
|
+
</li>
|
|
297
|
+
<li class="wr_bookList_item">
|
|
298
|
+
<a class="wr_bookList_item_link" href="/web/reader/book-a-reader"></a>
|
|
299
|
+
<p class="wr_bookList_item_title">文明</p>
|
|
300
|
+
</li>
|
|
301
|
+
</ul>
|
|
302
|
+
`),
|
|
303
|
+
});
|
|
304
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
305
|
+
|
|
306
|
+
const result = await command!.func!(null as any, { query: '文明', limit: 5 });
|
|
307
|
+
|
|
308
|
+
expect(result).toEqual([
|
|
309
|
+
{
|
|
310
|
+
rank: 1,
|
|
311
|
+
title: '文明',
|
|
312
|
+
author: '作者甲',
|
|
313
|
+
bookId: 'book-a',
|
|
314
|
+
url: '',
|
|
315
|
+
},
|
|
316
|
+
{
|
|
317
|
+
rank: 2,
|
|
318
|
+
title: '文明',
|
|
319
|
+
author: '作者乙',
|
|
320
|
+
bookId: 'book-b',
|
|
321
|
+
url: '',
|
|
322
|
+
},
|
|
323
|
+
]);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
it('leaves urls empty when exact author matching fails and multiple html cards share the same title', async () => {
|
|
327
|
+
const command = getRegistry().get('weread/search');
|
|
328
|
+
expect(command?.func).toBeTypeOf('function');
|
|
329
|
+
|
|
330
|
+
const fetchMock = vi.fn()
|
|
331
|
+
.mockResolvedValueOnce({
|
|
332
|
+
ok: true,
|
|
333
|
+
json: () => Promise.resolve({
|
|
334
|
+
books: [
|
|
335
|
+
{
|
|
336
|
+
bookInfo: {
|
|
337
|
+
title: '文明',
|
|
338
|
+
author: '作者甲',
|
|
339
|
+
bookId: 'book-a',
|
|
340
|
+
},
|
|
341
|
+
},
|
|
342
|
+
],
|
|
343
|
+
}),
|
|
344
|
+
})
|
|
345
|
+
.mockResolvedValueOnce({
|
|
346
|
+
ok: true,
|
|
347
|
+
text: () => Promise.resolve(`
|
|
348
|
+
<ul class="search_bookDetail_list">
|
|
349
|
+
<li class="wr_bookList_item">
|
|
350
|
+
<a class="wr_bookList_item_link" href="/web/reader/book-a-reader"></a>
|
|
351
|
+
<p class="wr_bookList_item_title">文明</p>
|
|
352
|
+
<p class="wr_bookList_item_author"><a href="/web/search/books?author=%E4%BD%9C%E8%80%85%E4%B9%99">作者乙</a></p>
|
|
353
|
+
</li>
|
|
354
|
+
<li class="wr_bookList_item">
|
|
355
|
+
<a class="wr_bookList_item_link" href="/web/reader/book-a-reader-2"></a>
|
|
356
|
+
<p class="wr_bookList_item_title">文明</p>
|
|
357
|
+
</li>
|
|
358
|
+
</ul>
|
|
359
|
+
`),
|
|
360
|
+
});
|
|
361
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
362
|
+
|
|
363
|
+
const result = await command!.func!(null as any, { query: '文明', limit: 5 });
|
|
364
|
+
|
|
365
|
+
expect(result).toEqual([
|
|
366
|
+
{
|
|
367
|
+
rank: 1,
|
|
368
|
+
title: '文明',
|
|
369
|
+
author: '作者甲',
|
|
370
|
+
bookId: 'book-a',
|
|
371
|
+
url: '',
|
|
372
|
+
},
|
|
373
|
+
]);
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
it('leaves urls empty when multiple results share the same title and author identity', async () => {
|
|
377
|
+
const command = getRegistry().get('weread/search');
|
|
378
|
+
expect(command?.func).toBeTypeOf('function');
|
|
379
|
+
|
|
380
|
+
const fetchMock = vi.fn()
|
|
381
|
+
.mockResolvedValueOnce({
|
|
382
|
+
ok: true,
|
|
383
|
+
json: () => Promise.resolve({
|
|
384
|
+
books: [
|
|
385
|
+
{
|
|
386
|
+
bookInfo: {
|
|
387
|
+
title: '文明',
|
|
388
|
+
author: '作者甲',
|
|
389
|
+
bookId: 'book-a',
|
|
390
|
+
},
|
|
391
|
+
},
|
|
392
|
+
{
|
|
393
|
+
bookInfo: {
|
|
394
|
+
title: '文明',
|
|
395
|
+
author: '作者甲',
|
|
396
|
+
bookId: 'book-b',
|
|
397
|
+
},
|
|
398
|
+
},
|
|
399
|
+
],
|
|
400
|
+
}),
|
|
401
|
+
})
|
|
402
|
+
.mockResolvedValueOnce({
|
|
403
|
+
ok: true,
|
|
404
|
+
text: () => Promise.resolve(`
|
|
405
|
+
<ul class="search_bookDetail_list">
|
|
406
|
+
<li class="wr_bookList_item">
|
|
407
|
+
<a class="wr_bookList_item_link" href="/web/reader/book-b-reader"></a>
|
|
408
|
+
<p class="wr_bookList_item_title">文明</p>
|
|
409
|
+
<p class="wr_bookList_item_author"><a href="/web/search/books?author=%E4%BD%9C%E8%80%85%E7%94%B2">作者甲</a></p>
|
|
410
|
+
</li>
|
|
411
|
+
<li class="wr_bookList_item">
|
|
412
|
+
<a class="wr_bookList_item_link" href="/web/reader/book-a-reader"></a>
|
|
413
|
+
<p class="wr_bookList_item_title">文明</p>
|
|
414
|
+
<p class="wr_bookList_item_author"><a href="/web/search/books?author=%E4%BD%9C%E8%80%85%E7%94%B2">作者甲</a></p>
|
|
415
|
+
</li>
|
|
416
|
+
</ul>
|
|
417
|
+
`),
|
|
418
|
+
});
|
|
419
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
420
|
+
|
|
421
|
+
const result = await command!.func!(null as any, { query: '文明', limit: 5 });
|
|
422
|
+
|
|
423
|
+
expect(result).toEqual([
|
|
424
|
+
{
|
|
425
|
+
rank: 1,
|
|
426
|
+
title: '文明',
|
|
427
|
+
author: '作者甲',
|
|
428
|
+
bookId: 'book-a',
|
|
429
|
+
url: '',
|
|
430
|
+
},
|
|
431
|
+
{
|
|
432
|
+
rank: 2,
|
|
433
|
+
title: '文明',
|
|
434
|
+
author: '作者甲',
|
|
435
|
+
bookId: 'book-b',
|
|
436
|
+
url: '',
|
|
437
|
+
},
|
|
438
|
+
]);
|
|
439
|
+
});
|
|
440
|
+
});
|
|
@@ -1,5 +1,155 @@
|
|
|
1
1
|
import { cli, Strategy } from '../../registry.js';
|
|
2
|
-
import { fetchWebApi } from './utils.js';
|
|
2
|
+
import { fetchWebApi, WEREAD_UA, WEREAD_WEB_ORIGIN } from './utils.js';
|
|
3
|
+
|
|
4
|
+
interface SearchHtmlEntry {
|
|
5
|
+
title: string;
|
|
6
|
+
author: string;
|
|
7
|
+
url: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function decodeHtmlText(value: string): string {
|
|
11
|
+
return value
|
|
12
|
+
.replace(/<[^>]+>/g, '')
|
|
13
|
+
.replace(/&#x([0-9a-fA-F]+);/gi, (_, n) => String.fromCharCode(parseInt(n, 16)))
|
|
14
|
+
.replace(/&#(\d+);/g, (_, n) => String.fromCharCode(Number(n)))
|
|
15
|
+
.replace(/ /g, ' ')
|
|
16
|
+
.replace(/&/g, '&')
|
|
17
|
+
.replace(/"/g, '"')
|
|
18
|
+
.trim();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function normalizeSearchTitle(value: string): string {
|
|
22
|
+
return value.replace(/\s+/g, ' ').trim();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function buildSearchIdentity(title: string, author: string): string {
|
|
26
|
+
return `${normalizeSearchTitle(title)}\u0000${normalizeSearchTitle(author)}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function countSearchTitles(entries: Array<{ title: string }>): Map<string, number> {
|
|
30
|
+
const counts = new Map<string, number>();
|
|
31
|
+
for (const entry of entries) {
|
|
32
|
+
const key = normalizeSearchTitle(entry.title);
|
|
33
|
+
if (!key) continue;
|
|
34
|
+
counts.set(key, (counts.get(key) || 0) + 1);
|
|
35
|
+
}
|
|
36
|
+
return counts;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function countSearchIdentities(entries: Array<{ title: string; author: string }>): Map<string, number> {
|
|
40
|
+
const counts = new Map<string, number>();
|
|
41
|
+
for (const entry of entries) {
|
|
42
|
+
const key = buildSearchIdentity(entry.title, entry.author);
|
|
43
|
+
if (!normalizeSearchTitle(entry.title) || !normalizeSearchTitle(entry.author)) continue;
|
|
44
|
+
counts.set(key, (counts.get(key) || 0) + 1);
|
|
45
|
+
}
|
|
46
|
+
return counts;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function isUniqueCount(counts: Map<string, number>, key: string): boolean {
|
|
50
|
+
return (counts.get(key) || 0) <= 1;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Build exact and title-only queues separately.
|
|
55
|
+
* Exact title+author matches are preferred; title-only matching is used only
|
|
56
|
+
* when the HTML card did not expose an author field.
|
|
57
|
+
*/
|
|
58
|
+
function buildSearchUrlQueues(entries: SearchHtmlEntry[]): {
|
|
59
|
+
exactQueues: Map<string, string[]>;
|
|
60
|
+
titleOnlyQueues: Map<string, string[]>;
|
|
61
|
+
} {
|
|
62
|
+
const exactQueues = new Map<string, string[]>();
|
|
63
|
+
const titleOnlyQueues = new Map<string, string[]>();
|
|
64
|
+
for (const entry of entries) {
|
|
65
|
+
const titleKey = normalizeSearchTitle(entry.title);
|
|
66
|
+
if (!titleKey || !entry.url) continue;
|
|
67
|
+
const queueMap = entry.author ? exactQueues : titleOnlyQueues;
|
|
68
|
+
const queueKey = entry.author ? buildSearchIdentity(entry.title, entry.author) : titleKey;
|
|
69
|
+
const current = queueMap.get(queueKey);
|
|
70
|
+
if (current) {
|
|
71
|
+
current.push(entry.url);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
queueMap.set(queueKey, [entry.url]);
|
|
75
|
+
}
|
|
76
|
+
return { exactQueues, titleOnlyQueues };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function resolveSearchResultUrl(params: {
|
|
80
|
+
exactQueues: Map<string, string[]>;
|
|
81
|
+
titleOnlyQueues: Map<string, string[]>;
|
|
82
|
+
apiIdentityCounts: Map<string, number>;
|
|
83
|
+
htmlIdentityCounts: Map<string, number>;
|
|
84
|
+
apiTitleCounts: Map<string, number>;
|
|
85
|
+
htmlTitleCounts: Map<string, number>;
|
|
86
|
+
title: string;
|
|
87
|
+
author: string;
|
|
88
|
+
}): string {
|
|
89
|
+
const {
|
|
90
|
+
exactQueues,
|
|
91
|
+
titleOnlyQueues,
|
|
92
|
+
apiIdentityCounts,
|
|
93
|
+
htmlIdentityCounts,
|
|
94
|
+
apiTitleCounts,
|
|
95
|
+
htmlTitleCounts,
|
|
96
|
+
title,
|
|
97
|
+
author,
|
|
98
|
+
} = params;
|
|
99
|
+
const identityKey = buildSearchIdentity(title, author);
|
|
100
|
+
if (isUniqueCount(apiIdentityCounts, identityKey) && isUniqueCount(htmlIdentityCounts, identityKey)) {
|
|
101
|
+
const exactUrl = exactQueues.get(identityKey)?.shift();
|
|
102
|
+
if (exactUrl) return exactUrl;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const titleKey = normalizeSearchTitle(title);
|
|
106
|
+
if (!isUniqueCount(apiTitleCounts, titleKey) || !isUniqueCount(htmlTitleCounts, titleKey)) {
|
|
107
|
+
return '';
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return titleOnlyQueues.get(titleKey)?.shift() ?? '';
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Extract rendered search result reader URLs from the server-rendered search page.
|
|
115
|
+
* The public JSON API still returns bookId, but the current web app links results
|
|
116
|
+
* through /web/reader/<opaque-id> rather than /web/bookDetail/<bookId>.
|
|
117
|
+
*/
|
|
118
|
+
async function loadSearchHtmlEntries(query: string): Promise<SearchHtmlEntry[]> {
|
|
119
|
+
const url = new URL('/web/search/books', WEREAD_WEB_ORIGIN);
|
|
120
|
+
url.searchParams.set('keyword', query);
|
|
121
|
+
|
|
122
|
+
let html = '';
|
|
123
|
+
try {
|
|
124
|
+
const resp = await fetch(url.toString(), {
|
|
125
|
+
headers: { 'User-Agent': WEREAD_UA },
|
|
126
|
+
});
|
|
127
|
+
if (!resp.ok) return [];
|
|
128
|
+
html = await resp.text();
|
|
129
|
+
} catch {
|
|
130
|
+
return [];
|
|
131
|
+
}
|
|
132
|
+
const items = Array.from(
|
|
133
|
+
html.matchAll(/<li[^>]*class="wr_bookList_item"[^>]*>([\s\S]*?)<\/li>/g),
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
return items.map((match) => {
|
|
137
|
+
const chunk = match[1];
|
|
138
|
+
const hrefMatch = chunk.match(/<a[^>]*href="([^"]+)"[^>]*class="wr_bookList_item_link"[^>]*>|<a[^>]*class="wr_bookList_item_link"[^>]*href="([^"]+)"[^>]*>/);
|
|
139
|
+
const titleMatch = chunk.match(/<p[^>]*class="wr_bookList_item_title"[^>]*>([\s\S]*?)<\/p>/);
|
|
140
|
+
const authorMatch = chunk.match(/<p[^>]*class="wr_bookList_item_author"[^>]*>([\s\S]*?)<\/p>/);
|
|
141
|
+
|
|
142
|
+
const href = hrefMatch?.[1] || hrefMatch?.[2] || '';
|
|
143
|
+
const title = decodeHtmlText(titleMatch?.[1] || '');
|
|
144
|
+
const author = decodeHtmlText(authorMatch?.[1] || '');
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
author,
|
|
148
|
+
url: href ? new URL(href, WEREAD_WEB_ORIGIN).toString() : '',
|
|
149
|
+
title,
|
|
150
|
+
};
|
|
151
|
+
}).filter((item) => item.url && item.title);
|
|
152
|
+
}
|
|
3
153
|
|
|
4
154
|
cli({
|
|
5
155
|
site: 'weread',
|
|
@@ -14,14 +164,44 @@ cli({
|
|
|
14
164
|
],
|
|
15
165
|
columns: ['rank', 'title', 'author', 'bookId', 'url'],
|
|
16
166
|
func: async (_page, args) => {
|
|
17
|
-
const data = await
|
|
167
|
+
const [data, htmlEntries] = await Promise.all([
|
|
168
|
+
fetchWebApi('/search/global', { keyword: args.query }),
|
|
169
|
+
loadSearchHtmlEntries(String(args.query ?? '')),
|
|
170
|
+
]);
|
|
18
171
|
const books: any[] = data?.books ?? [];
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
172
|
+
const { exactQueues, titleOnlyQueues } = buildSearchUrlQueues(htmlEntries);
|
|
173
|
+
const apiIdentityCounts = countSearchIdentities(
|
|
174
|
+
books.map((item: any) => ({
|
|
175
|
+
title: item.bookInfo?.title ?? '',
|
|
176
|
+
author: item.bookInfo?.author ?? '',
|
|
177
|
+
})),
|
|
178
|
+
);
|
|
179
|
+
const htmlIdentityCounts = countSearchIdentities(
|
|
180
|
+
htmlEntries.filter((entry) => entry.author),
|
|
181
|
+
);
|
|
182
|
+
const apiTitleCounts = countSearchTitles(
|
|
183
|
+
books.map((item: any) => ({ title: item.bookInfo?.title ?? '' })),
|
|
184
|
+
);
|
|
185
|
+
const htmlTitleCounts = countSearchTitles(htmlEntries);
|
|
186
|
+
return books.slice(0, Number(args.limit)).map((item: any, i: number) => {
|
|
187
|
+
const title = item.bookInfo?.title ?? '';
|
|
188
|
+
const author = item.bookInfo?.author ?? '';
|
|
189
|
+
return {
|
|
190
|
+
rank: i + 1,
|
|
191
|
+
title,
|
|
192
|
+
author,
|
|
193
|
+
bookId: item.bookInfo?.bookId ?? '',
|
|
194
|
+
url: resolveSearchResultUrl({
|
|
195
|
+
exactQueues,
|
|
196
|
+
titleOnlyQueues,
|
|
197
|
+
apiIdentityCounts,
|
|
198
|
+
htmlIdentityCounts,
|
|
199
|
+
apiTitleCounts,
|
|
200
|
+
htmlTitleCounts,
|
|
201
|
+
title,
|
|
202
|
+
author,
|
|
203
|
+
}),
|
|
204
|
+
};
|
|
205
|
+
});
|
|
26
206
|
},
|
|
27
207
|
});
|