mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
64ac362a40
* feat(rednote): add rednote.com adapter mirroring xiaohongshu read commands (#1136) Implements rednote.com support as discussed in issue #1136. The mainland xiaohongshu adapter stays in place; international users redirected to www.rednote.com now have a CLI without a copy-pasted adapter. Issue #1136 documents that xiaohongshu and rednote share DOM selectors, URL paths, API paths, response schema, cookies, and the xsec_token auth mechanism. The only material differences: Layer xiaohongshu rednote Web host www.xiaohongshu.com www.rednote.com API host edith.xiaohongshu.com webapi.rednote.com Security host fe-static.xhscdn.com as.rednote.com Cookie root .xiaohongshu.com .rednote.com Search gate Inline text Full-screen modal + text ## Architecture (minimal) `clis/xiaohongshu/*` keep all selector / regex / extraction logic. Each command file is touched minimally to export the IIFE or pipeline so the sibling adapter can reuse it: search.js + export const buildSearchExtractJs(webHost) + export const command = cli({...}) note.js + export const NOTE_EXTRACT_JS + export const command = cli({...}) comments.js + export function buildCommentsExtractJs(withReplies) + export parseCommentLimit + export const command = cli({...}) download.js + export function buildDownloadExtractJs(noteId) (CDN allowlist now includes rednote alongside xhscdn) + export const command = cli({...}) user.js + export const USER_SNAPSHOT_JS + export const command = cli({...}) feed.js + export function buildFeedPipeline(webHost) + export const command = cli({...}) notifications.js + export function buildNotificationsPipeline(webHost) + export const command = cli({...}) note-helpers.js buildNoteUrl now accepts `cookieRoot` + `signedUrlHint` options (defaults preserved so xhs callers and tests are unchanged) user-helpers.js buildXhsNoteUrl / extractXhsUserNotes accept an optional `webHost` argument (default xhs) The `export const command = cli({...})` pattern matches twitter/lists.js and clis/discord-app/*; without it the build-manifest scanner attributes xhs's command to whichever rednote sibling triggered the transitive import first. ## clis/rednote/ — thin shims Each rednote command file imports the relevant builder / constant from its xiaohongshu sibling and calls `cli()` with the rednote host triple. No selectors, regexes, or extraction logic are duplicated. search.js imports buildSearchExtractJs + noteIdToDate declares its own WAIT_FOR_CONTENT_JS (modal + text login-gate variants — the one xhs behaviour that genuinely differs) note.js imports NOTE_EXTRACT_JS + buildNoteUrl + parseNoteId comments.js imports buildCommentsExtractJs + parseCommentLimit + buildNoteUrl + parseNoteId download.js imports buildDownloadExtractJs + buildNoteUrl + parseNoteId user.js imports USER_SNAPSHOT_JS + extractXhsUserNotes + normalizeXhsUserId ## Scope (initial) Ships the five commands verified live against the user's logged-in rednote.com session: search / note / comments / user / download. `feed` and `notifications` are intentionally left out. Both rely on intercepting the xiaohongshu Pinia store at the `homefeed` / `you` capture pattern; live verification on rednote returns `tap → dict (error)` for the feed step, so shipping them would surface a broken contract. The mainland xiaohongshu commands continue to work. Adding the rednote-side feed / notifications is straightforward follow-up work once someone with rednote access maps the network surface. Creator-center commands (publish, creator-*) have no rednote counterpart and stay xiaohongshu-only, per the reporter's note in #1136. ## Verification - clis/xiaohongshu/ + clis/rednote/: 103/103 tests green - npx tsc --noEmit: clean - npm run build: 807 manifest entries (xhs 13 + rednote 5 + everything else preserved) - silent-column-drop / typed-error-lint: 103 / 189 baseline entries, no new violations - Live verify against the user's rednote.com session: rednote search "travel" --limit 1 → real note row rednote note <signed-url> → 7 field/value rows rednote comments <signed-url> --limit 3 → 3 top-level rows rednote user 5b21f6564eacab3b38f05c39 --limit 2 → 2 profile notes Spaced 15–30s between runs per the xhs/rednote rate-limit guidance; no write commands invoked. Regression check: xiaohongshu/feed on the existing mainland session still returns the standard 6-field rows after the refactor. Closes #1136 * fix(rednote): tighten adapter failure boundaries --------- Co-authored-by: jackwener <jakevingoo@gmail.com>
158 lines
6.4 KiB
JavaScript
158 lines
6.4 KiB
JavaScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const { mockDownloadMedia, mockFormatCookieHeader } = vi.hoisted(() => ({
|
|
mockDownloadMedia: vi.fn(),
|
|
mockFormatCookieHeader: vi.fn(() => 'sid=secret'),
|
|
}));
|
|
|
|
vi.mock('@jackwener/opencli/download/media-download', () => ({
|
|
downloadMedia: mockDownloadMedia,
|
|
}));
|
|
|
|
vi.mock('@jackwener/opencli/download', () => ({
|
|
formatCookieHeader: mockFormatCookieHeader,
|
|
}));
|
|
|
|
import { getRegistry } from '@jackwener/opencli/registry';
|
|
import './comments.js';
|
|
import './download.js';
|
|
import './feed.js';
|
|
import './notifications.js';
|
|
import './note.js';
|
|
import './search.js';
|
|
import './user.js';
|
|
|
|
function createPageMock(evaluateResult) {
|
|
return {
|
|
goto: vi.fn().mockResolvedValue(undefined),
|
|
evaluate: vi.fn().mockResolvedValue(evaluateResult),
|
|
wait: vi.fn().mockResolvedValue(undefined),
|
|
autoScroll: vi.fn().mockResolvedValue(undefined),
|
|
getCookies: vi.fn().mockResolvedValue([{ name: 'sid', value: 'secret', domain: 'www.rednote.com' }]),
|
|
};
|
|
}
|
|
|
|
describe('rednote note URL identity', () => {
|
|
const download = getRegistry().get('rednote/download');
|
|
const comments = getRegistry().get('rednote/comments');
|
|
|
|
beforeEach(() => {
|
|
mockDownloadMedia.mockReset();
|
|
mockDownloadMedia.mockResolvedValue([{ index: 1, type: 'image', status: 'success', size: '1 KB' }]);
|
|
mockFormatCookieHeader.mockClear();
|
|
});
|
|
|
|
it('rejects xhslink short links before browser navigation', async () => {
|
|
const page = createPageMock({ media: [] });
|
|
await expect(download.func(page, {
|
|
'note-id': 'https://xhslink.com/o/4MKEjsZnhCz',
|
|
output: './out',
|
|
})).rejects.toMatchObject({
|
|
code: 'ARGUMENT',
|
|
message: expect.stringContaining('signed URL'),
|
|
hint: expect.stringContaining('rednote.com'),
|
|
});
|
|
expect(page.goto).not.toHaveBeenCalled();
|
|
expect(mockDownloadMedia).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('rejects signed xiaohongshu URLs before browser navigation', async () => {
|
|
const page = createPageMock({ media: [] });
|
|
await expect(comments.func(page, {
|
|
'note-id': 'https://www.xiaohongshu.com/search_result/69aadbcb000000002202f131?xsec_token=abc',
|
|
limit: 20,
|
|
})).rejects.toMatchObject({
|
|
code: 'ARGUMENT',
|
|
message: expect.stringContaining('signed URL'),
|
|
hint: expect.stringContaining('rednote.com'),
|
|
});
|
|
expect(page.goto).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('uses URL-scoped rednote cookies when downloading media', async () => {
|
|
const page = createPageMock({
|
|
noteId: '69bc166f000000001a02069a',
|
|
media: [{ type: 'image', url: 'https://ci.rednote.com/example.jpg' }],
|
|
});
|
|
await download.func(page, {
|
|
'note-id': 'https://www.rednote.com/search_result/69bc166f000000001a02069a?xsec_token=abc',
|
|
output: './out',
|
|
});
|
|
expect(page.getCookies).toHaveBeenCalledWith({ url: 'https://www.rednote.com' });
|
|
expect(mockDownloadMedia).toHaveBeenCalledWith([{ type: 'image', url: 'https://ci.rednote.com/example.jpg' }], expect.objectContaining({
|
|
cookies: 'sid=secret',
|
|
subdir: '69bc166f000000001a02069a',
|
|
}));
|
|
});
|
|
|
|
it('throws empty-result instead of returning a failed success row when no media exists', async () => {
|
|
const page = createPageMock({ noteId: '69bc166f000000001a02069a', media: [] });
|
|
let caught;
|
|
try {
|
|
await download.func(page, {
|
|
'note-id': 'https://www.rednote.com/search_result/69bc166f000000001a02069a?xsec_token=abc',
|
|
output: './out',
|
|
});
|
|
}
|
|
catch (error) {
|
|
caught = error;
|
|
}
|
|
expect(caught).toMatchObject({ code: 'EMPTY_RESULT' });
|
|
expect(caught?.hint).toContain('No downloadable media');
|
|
expect(mockDownloadMedia).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('rednote argument validation', () => {
|
|
const comments = getRegistry().get('rednote/comments');
|
|
const feed = getRegistry().get('rednote/feed');
|
|
const notifications = getRegistry().get('rednote/notifications');
|
|
const user = getRegistry().get('rednote/user');
|
|
|
|
it.each([
|
|
['rednote/comments', comments, { 'note-id': 'https://www.rednote.com/search_result/69aadbcb000000002202f131?xsec_token=abc', limit: 0 }],
|
|
['rednote/feed', feed, { limit: 0 }],
|
|
['rednote/notifications', notifications, { limit: 0 }],
|
|
['rednote/user', user, { id: 'user123', limit: 0 }],
|
|
])('%s rejects invalid --limit before browser navigation', async (_name, command, kwargs) => {
|
|
const page = createPageMock({});
|
|
await expect(command.func(page, kwargs)).rejects.toMatchObject({ code: 'ARGUMENT' });
|
|
expect(page.goto).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('rejects unknown notification types before browser navigation', async () => {
|
|
const page = createPageMock({});
|
|
await expect(notifications.func(page, { type: 'all', limit: 20 })).rejects.toMatchObject({
|
|
code: 'ARGUMENT',
|
|
message: expect.stringContaining('--type'),
|
|
});
|
|
expect(page.goto).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('rednote Pinia store failures', () => {
|
|
it('maps feed store read failure to CommandExecutionError', async () => {
|
|
const command = getRegistry().get('rednote/feed');
|
|
const page = createPageMock({ error: 'no_pinia' });
|
|
await expect(command.func(page, { limit: 20 })).rejects.toMatchObject({
|
|
code: 'COMMAND_EXEC',
|
|
message: expect.stringContaining('no_pinia'),
|
|
});
|
|
});
|
|
|
|
it('maps notification action failure to CommandExecutionError', async () => {
|
|
const command = getRegistry().get('rednote/notifications');
|
|
const page = createPageMock({ error: 'action_failed', detail: 'blocked' });
|
|
await expect(command.func(page, { type: 'mentions', limit: 20 })).rejects.toMatchObject({
|
|
code: 'COMMAND_EXEC',
|
|
message: expect.stringContaining('action_failed'),
|
|
});
|
|
});
|
|
|
|
it('allows an empty notification list after a successful store read', async () => {
|
|
const command = getRegistry().get('rednote/notifications');
|
|
const page = createPageMock({ items: [] });
|
|
await expect(command.func(page, { type: 'mentions', limit: 20 })).resolves.toEqual([]);
|
|
});
|
|
});
|