Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion clis/reddit/popular.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cli({
],
columns: ['rank', 'id', 'title', 'subreddit', 'score', 'comments', 'author', 'url', 'created_utc', 'selftext', 'post_hint', 'url_overridden_by_dest', 'preview_image_url', 'gallery_urls'],
pipeline: [
{ navigate: 'https://www.reddit.com' },
{ evaluate: `(async () => {
function decodeHtml(s) {
if (typeof s !== 'string' || !s) return '';
Expand Down Expand Up @@ -43,7 +44,17 @@ cli({
const res = await fetch('/r/popular.json?limit=' + limit + '&raw_json=1', {
credentials: 'include'
});
const d = await res.json();
const text = await res.text();
if (!res.ok) {
throw new Error('Reddit popular request failed: HTTP ' + res.status + ' ' + text.replace(/\\s+/g, ' ').slice(0, 160));
}
let d;
try {
d = JSON.parse(text);
} catch {
const kind = /^\\s*</.test(text) ? 'HTML' : 'non-JSON';
throw new Error('Reddit popular expected JSON but received ' + kind + ': ' + text.replace(/\\s+/g, ' ').slice(0, 160));
}
return (d?.data?.children || []).map(c => ({
id: c.data.id,
title: c.data.title,
Expand Down
15 changes: 12 additions & 3 deletions clis/reddit/popular.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import './popular.js';

describe('reddit popular adapter', () => {
const command = getRegistry().get('reddit/popular');
const evaluate = command?.pipeline?.find((step) => step.evaluate)?.evaluate;
const map = command?.pipeline?.find((step) => step.map)?.map;

it('exposes the full post-list shape including the 4 media columns', () => {
expect(command?.columns).toEqual([
Expand All @@ -14,13 +16,20 @@ describe('reddit popular adapter', () => {
});

it('surfaces media via extractRedditMedia in evaluate + map', () => {
expect(command?.pipeline?.[0]?.evaluate).toContain('function extractRedditMedia');
expect(command?.pipeline?.[0]?.evaluate).toContain('...extractRedditMedia(c.data)');
expect(command?.pipeline?.[1]?.map).toMatchObject({
expect(evaluate).toContain('function extractRedditMedia');
expect(evaluate).toContain('...extractRedditMedia(c.data)');
expect(map).toMatchObject({
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 }}',
});
});

it('navigates to Reddit and guards HTML responses before JSON parsing', () => {
expect(command?.pipeline?.[0]).toEqual({ navigate: 'https://www.reddit.com' });
expect(evaluate).toContain('await res.text()');
expect(evaluate).toContain('Reddit popular expected JSON');
expect(evaluate).not.toContain('await res.json()');
});
});
Loading