mirror of
https://github.com/jackwener/OpenCLI.git
synced 2026-09-14 18:25:42 +08:00
34f351e59f
* feat(errors,utils): 添加 LoginWallError 与 HTML-as-JSON 响应嗅探器
部分 adapter(twitter list-tweets/thread、reddit search/subreddit 等)历史上
直接 `JSON.parse(await r.text())` 或 `await r.json()` 解析响应。当服务端返回
登录墙、限流页或 WAF 拦截页(而不是 JSON)时,body 以 `<!DOCTYPE html>` 或
`<html ...>` 开头,解析直接抛出晦涩的
`SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON`,
调用方无法把它和真正的 JSON 解析失败区分开。
本 PR 添加三块共享基础设施,让 adapter 能识别 HTML 情况并抛出结构化的
`LoginWallError`(带 status / url / body 预览),而不是裸的解析栈:
- `LoginWallError`(src/errors.ts):新的 `CliError` 子类,含 `status`、
`url`、`bodyPreview` 字段,hint 提示"重新登录或等待限流过期",
退出码映射到 `EXIT_CODES.NOPERM`。
- `parseJsonOrThrowLoginWall(response)`(src/utils.ts):Node 端 helper,
供从 daemon 侧 fetch 的 adapter 使用(接收 Fetch Response)。
- `BROWSER_JSON_SNIFF_FN` + `throwIfLoginWall(value)`(src/utils.ts):
browser 端等价物。字符串片段嵌入到 `page.evaluate` 里,返回值是
解析后的 JSON,或 `{ error: status }` HTTP 形状,或 `LoginWallSignal`
哨兵对象(`{ __loginWall: true, status, url, ... }`),Node 侧拿到后
转成 `LoginWallError`。
行为是 opt-in:现有 adapter 不调用这些 helper 就完全不受影响。后续 PR
会把 reddit / twitter adapter 接入这些 helper。`src/utils.test.ts` 新增
13 个单测,覆盖 Node 端 + browser 端两条路径以及 body 预览的 100 字截断。
* feat(reddit/subscribed): 接入 LoginWallError 嗅探(#1650 的第一个 caller)
`reddit subscribed` 是从 daemon-backed browser session 调 Reddit 的 JSON API
(`/api/me.json` + `/subreddits/mine/subscriptions.json`)。原本两处 `await res.json()`
在 Reddit 返回登录墙 / WAF / over-18 拦截页(HTML body + 200 OK)时会抛
`SyntaxError: Unexpected token '<'`,被外层 try/catch 兜底成 `kind: 'exception'`
→ Node 侧报成 `CommandExecutionError: subscribed failed: SyntaxError ...`,
看不出 root cause。
接入 #1650 的 helper 后:
- **Browser 侧**:用 `BROWSER_JSON_SNIFF_FN` 提供的 `fetchJsonOrLoginWall(url, init)`
替换裸 `fetch + .json()`。helper 内部 sniff `Content-Type: text/html` 或
`<!DOCTYPE` / `<html` body 前缀,返回 `{ __loginWall: true, status, url,
contentType, bodyPreview }` 哨兵(不抛,交给调用者)。
- **Cross-boundary**:两处 fetch 站点(me.json + subscriptions.json)发现哨兵后
返回 `{ kind: 'login-wall', sentinel, where }` 透传给 Node。
- **Node 侧**:`throwIfLoginWall(result.sentinel, { url: result.where })` 把
哨兵转成结构化 `LoginWallError`(含 `status` / `url` / `bodyPreview` 字段,
exit code `EXIT_CODES.NOPERM`,hint 提示重新登录或等限流过期)。
这是 #1650 的第一个真实 caller,覆盖 3 块 export 全部(`BROWSER_JSON_SNIFF_FN`
+ `throwIfLoginWall` + `LoginWallError`)。其它 adapter 后续按这个模板逐个接入。
回归测试新增 1 个:mock evaluate 返回 `{ kind: 'login-wall', sentinel }`,
断言 Node 端抛 `LoginWallError` 且 `status` / `url` / `bodyPreview` 字段正确。
原有 12 个测试不动,全部通过。
169 lines
7.5 KiB
JavaScript
169 lines
7.5 KiB
JavaScript
import { describe, expect, it, vi } from 'vitest';
|
|
import { getRegistry } from '@jackwener/opencli/registry';
|
|
import { ArgumentError, AuthRequiredError, CommandExecutionError, EmptyResultError, LoginWallError } from '@jackwener/opencli/errors';
|
|
import { parseRedditSubscribedLimit, unwrapEvaluateResult } from './subscribed.js';
|
|
import './subscribed.js';
|
|
|
|
function subredditThing(id, overrides = {}) {
|
|
const displayName = `sub${id}`;
|
|
return {
|
|
kind: 't5',
|
|
data: {
|
|
id,
|
|
name: `t5_${id}`,
|
|
display_name: displayName,
|
|
display_name_prefixed: `r/${displayName}`,
|
|
title: `Sub ${id}`,
|
|
subscribers: 1000,
|
|
public_description: `Description ${id}`,
|
|
url: `/r/${displayName}/`,
|
|
...overrides,
|
|
},
|
|
};
|
|
}
|
|
|
|
function makePage(result) {
|
|
return {
|
|
goto: vi.fn().mockResolvedValue(undefined),
|
|
evaluate: vi.fn().mockResolvedValue(result),
|
|
};
|
|
}
|
|
|
|
describe('reddit subscribed adapter', () => {
|
|
const command = getRegistry().get('reddit/subscribed');
|
|
|
|
it('registers with id-bearing output columns', () => {
|
|
expect(command).toBeDefined();
|
|
expect(command.columns).toEqual(['id', 'subreddit', 'title', 'subscribers', 'description', 'url']);
|
|
});
|
|
|
|
it('parseRedditSubscribedLimit rejects out-of-range values without silent clamp', () => {
|
|
expect(parseRedditSubscribedLimit(undefined)).toBe(100);
|
|
expect(parseRedditSubscribedLimit(null)).toBe(100);
|
|
expect(parseRedditSubscribedLimit('')).toBe(100);
|
|
expect(parseRedditSubscribedLimit(1)).toBe(1);
|
|
expect(parseRedditSubscribedLimit(1000)).toBe(1000);
|
|
for (const bad of [0, -1, 1001, 1.5, NaN, 'abc']) {
|
|
expect(() => parseRedditSubscribedLimit(bad)).toThrow(ArgumentError);
|
|
}
|
|
});
|
|
|
|
it('rejects bad limit before navigation', async () => {
|
|
const page = makePage({ kind: 'ok', entries: [] });
|
|
await expect(command.func(page, { limit: 1001 })).rejects.toBeInstanceOf(ArgumentError);
|
|
expect(page.goto).not.toHaveBeenCalled();
|
|
expect(page.evaluate).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('unwraps Browser Bridge envelopes', () => {
|
|
const inner = { kind: 'ok', entries: [] };
|
|
expect(unwrapEvaluateResult({ session: 'browser:default', data: inner })).toBe(inner);
|
|
expect(unwrapEvaluateResult(inner)).toBe(inner);
|
|
});
|
|
|
|
it('returns subscribed subreddits from the browser-evaluated payload', async () => {
|
|
const page = makePage({
|
|
kind: 'ok',
|
|
entries: [
|
|
subredditThing('abc', {
|
|
display_name: 'programming',
|
|
display_name_prefixed: 'r/programming',
|
|
title: 'Programming',
|
|
subscribers: 6000000,
|
|
public_description: 'All things code',
|
|
url: '/r/programming/',
|
|
}),
|
|
subredditThing('def', {
|
|
display_name: 'MachineLearning',
|
|
display_name_prefixed: 'r/MachineLearning',
|
|
title: 'Machine Learning',
|
|
subscribers: 3000000,
|
|
public_description: 'ML research',
|
|
url: '/r/MachineLearning/',
|
|
}),
|
|
],
|
|
});
|
|
const result = await command.func(page, { limit: 100 });
|
|
expect(page.goto).toHaveBeenCalledWith('https://www.reddit.com');
|
|
expect(result).toEqual([
|
|
{ id: 't5_abc', subreddit: 'r/programming', title: 'Programming', subscribers: 6000000, description: 'All things code', url: 'https://www.reddit.com/r/programming/' },
|
|
{ id: 't5_def', subreddit: 'r/MachineLearning', title: 'Machine Learning', subscribers: 3000000, description: 'ML research', url: 'https://www.reddit.com/r/MachineLearning/' },
|
|
]);
|
|
});
|
|
|
|
it('throws AuthRequiredError when not logged in', async () => {
|
|
await expect(command.func(makePage({ kind: 'auth', detail: 'login required' }), { limit: 100 }))
|
|
.rejects.toBeInstanceOf(AuthRequiredError);
|
|
});
|
|
|
|
it('surfaces HTTP, malformed, exception, and unexpected envelopes as CommandExecutionError', async () => {
|
|
await expect(command.func(makePage({ kind: 'http', httpStatus: 429, where: '/subreddits/mine/subscriptions.json?limit=100' }), { limit: 100 }))
|
|
.rejects.toBeInstanceOf(CommandExecutionError);
|
|
await expect(command.func(makePage({ kind: 'malformed', detail: 'missing data.children' }), { limit: 100 }))
|
|
.rejects.toBeInstanceOf(CommandExecutionError);
|
|
await expect(command.func(makePage({ kind: 'exception', detail: 'network' }), { limit: 100 }))
|
|
.rejects.toBeInstanceOf(CommandExecutionError);
|
|
await expect(command.func(makePage({ ok: true }), { limit: 100 }))
|
|
.rejects.toBeInstanceOf(CommandExecutionError);
|
|
});
|
|
|
|
it('converts a browser-side login-wall sentinel into a typed LoginWallError', async () => {
|
|
const sentinel = {
|
|
__loginWall: true,
|
|
status: 200,
|
|
url: 'https://www.reddit.com/api/me.json?raw_json=1',
|
|
contentType: 'text/html; charset=utf-8',
|
|
bodyPreview: '<!DOCTYPE html><html><head><title>reddit.com: over 18?</title>',
|
|
};
|
|
const page = makePage({ kind: 'login-wall', sentinel, where: '/api/me.json' });
|
|
try {
|
|
await command.func(page, { limit: 100 });
|
|
throw new Error('expected LoginWallError, got success');
|
|
} catch (err) {
|
|
expect(err).toBeInstanceOf(LoginWallError);
|
|
expect(err.status).toBe(200);
|
|
expect(err.url).toBe('/api/me.json');
|
|
expect(err.bodyPreview).toContain('reddit.com: over 18');
|
|
}
|
|
});
|
|
|
|
it('throws EmptyResultError for a valid empty subscriptions list', async () => {
|
|
await expect(command.func(makePage({ kind: 'ok', entries: [] }), { limit: 100 }))
|
|
.rejects.toBeInstanceOf(EmptyResultError);
|
|
});
|
|
|
|
it('throws CommandExecutionError when rows lack subreddit identity', async () => {
|
|
await expect(command.func(makePage({ kind: 'ok', entries: [subredditThing('abc', { name: '', id: '', display_name: '', display_name_prefixed: '', url: '' })] }), { limit: 100 }))
|
|
.rejects.toBeInstanceOf(CommandExecutionError);
|
|
});
|
|
|
|
it('does not synthesize subreddit identity from a non-t5 listing item', async () => {
|
|
await expect(command.func(makePage({
|
|
kind: 'ok',
|
|
entries: [{
|
|
kind: 't3',
|
|
data: {
|
|
id: 'abc',
|
|
display_name: 'notasub',
|
|
display_name_prefixed: 'r/notasub',
|
|
url: '/r/notasub/',
|
|
},
|
|
}],
|
|
}), { limit: 100 })).rejects.toBeInstanceOf(CommandExecutionError);
|
|
});
|
|
|
|
it('respects --limit by slicing the final result', async () => {
|
|
const page = makePage({ kind: 'ok', entries: Array.from({ length: 5 }, (_, i) => subredditThing(String(i))) });
|
|
const result = await command.func(page, { limit: 3 });
|
|
expect(result).toHaveLength(3);
|
|
expect(result[0].subreddit).toBe('r/sub0');
|
|
});
|
|
|
|
it('embeds the validated limit literally in the browser script', async () => {
|
|
const page = makePage({ kind: 'ok', entries: [subredditThing('x')] });
|
|
await command.func(page, { limit: 7 });
|
|
const script = page.evaluate.mock.calls[0][0];
|
|
expect(script).toContain('const target = 7');
|
|
});
|
|
});
|