Files
jackwener__opencli/clis/reddit/subreddit.test.js
Ocean bcd9c124c4 perf(reddit): 删除冗余的首页预导航步,每命令双导航→单导航 (#1987)
reddit 的 popular / subreddit / search / read 命令都先用框架 navigateBefore(domain=reddit.com → 302 到 www.reddit.com)把页面带到 reddit origin,**又**额外硬编码一步导航到 `https://www.reddit.com` 首页,然后才发相对 fetch(`/r/popular.json`、`/comments/<id>.json` 等)。两次导航到同一站点首页纯属冗余 —— 框架那次已经够让相对 fetch 工作。

实测:一次串行抓取仅 reddit 就因此重复导航首页约 24 次(每次 6-15s)。

## 改动

- 删掉 popular / subreddit / search 的 pipeline `{ navigate: 'https://www.reddit.com' }` 步;
- 删掉 read.js func 里的 `await page.goto('https://www.reddit.com')`;
- 框架 navigateBefore 仍把页面带到 reddit origin,相对 fetch 照常工作。

## 配套测试调整

- `read.test.js`:原断言「导航到首页」改为**反向回归保护**(`page.goto` 不再以首页 URL 被调用),锁住本次优化不被回退;
- `popular / search / subreddit.test.js`:删掉一个 pipeline 步后 evaluate / map 的索引各前移一位(`[1]→[0]`、`[2]→[1]`),同步更新断言。

## 验证

- 逐一实测 `opencli reddit popular / subreddit / search / read` 均正常返回数据;
- reddit 全量适配器测试 88 通过;`tsc --noEmit` 干净;`npm run build` 干净。

Co-authored-by: ml-scout <ml-scout@anthropic.com>
2026-06-21 14:28:19 +08:00

32 lines
1.3 KiB
JavaScript

import { describe, expect, it } from 'vitest';
import { getRegistry } from '@jackwener/opencli/registry';
import './subreddit.js';
describe('reddit subreddit adapter', () => {
const command = getRegistry().get('reddit/subreddit');
it('exposes the full subreddit-list shape including the 4 media columns', () => {
expect(command?.columns).toEqual([
'id', 'title', 'subreddit', 'author', 'upvotes', 'comments', 'url',
'created_utc', 'selftext',
'post_hint', 'url_overridden_by_dest', 'preview_image_url', 'gallery_urls',
]);
});
it('shapes children into the intermediate-object pattern with media spread in', () => {
expect(command?.pipeline?.[0]?.evaluate).toContain('function extractRedditMedia');
expect(command?.pipeline?.[0]?.evaluate).toContain('...extractRedditMedia(c.data)');
expect(command?.pipeline?.[1]?.map).toMatchObject({
title: '${{ item.title }}',
author: '${{ item.author }}',
upvotes: '${{ item.upvotes }}',
comments: '${{ item.comments }}',
url: '${{ item.url }}',
post_hint: '${{ item.post_hint }}',
url_overridden_by_dest: '${{ item.url_overridden_by_dest }}',
preview_image_url: '${{ item.preview_image_url }}',
gallery_urls: '${{ item.gallery_urls }}',
});
});
});