Files
jackwener__opencli/clis/reddit/reply.test.js
Gaurav Saxena 150551be8c feat(reddit): add reply command for replying to comments (#1428)
* feat(reddit): add reply command for replying to comments

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

* fix(reddit/reply): replace silent-sentinel rows with typed errors

reply.js originally mirror-copied comment.js's failure pattern: returning
[{ status: 'failed', message: 'HTTP 403' }] on auth/HTTP/Reddit errors and
relying on the caller to inspect the row instead of throwing. That's the
'silent-sentinel' anti-pattern from typed-errors.md — failures should
surface as typed errors so an agent can actually branch on them.

Round 21 lesson (f) — "grandfathered-not-exempt + helper-refactor boundary
is new" — applies: comment.js / upvote.js / save.js can stay grandfathered,
but a brand-new file does not inherit that exemption.

Changes:
- Throw AuthRequiredError when /api/me.json or /api/comment returns 401/403,
  or when /api/me.json returns 200 but data.name is missing (stale anon
  session — empty modhash alone isn't a strong enough signal).
- Throw CommandExecutionError for non-2xx HTTP and for non-empty
  data.json.errors (e.g. RATELIMIT, NO_TEXT, TOO_OLD).
- Drop the over-defensive `if (!page) throw ...` — registry guarantees a
  page object when browser:true.
- Intermediate result object uses `kind` discriminator + `detail` /
  `httpStatus` / `where` keys that don't overlap with columns
  ['status','message'], so the silent-column-drop audit stays quiet
  (per PR #1329 sediment).

Verified:
- npx tsc --noEmit clean
- node scripts/check-typed-error-lint.mjs → 189/189, 0 new
- node scripts/check-silent-column-drop.mjs → 103/103, 0 new
- npx vitest run clis/reddit src/convention-audit → 11/11 pass
- node ./dist/src/main.js validate → 0 errors

Success path is unchanged: still returns
[{ status: 'success', message: 'Reply posted on t1_<id>' }].

* fix(reddit): harden reply command contract

* fix(reddit): reject suffixed reply urls

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-authored-by: jackwener <jakevingoo@gmail.com>
2026-05-12 02:33:09 +08:00

90 lines
4.6 KiB
JavaScript

import { describe, expect, it, vi } from 'vitest';
import { getRegistry } from '@jackwener/opencli/registry';
import { ArgumentError, AuthRequiredError, CommandExecutionError } from '@jackwener/opencli/errors';
import { normalizeRedditCommentFullname, requireReplyText } from './reply.js';
import './reply.js';
function makePage(result = { kind: 'ok', detail: 'Reply posted on t1_okf3s7u as t1_reply123' }) {
return {
goto: vi.fn().mockResolvedValue(undefined),
evaluate: vi.fn().mockResolvedValue(result),
};
}
describe('reddit reply command', () => {
const command = getRegistry().get('reddit/reply');
it('normalizes bare ids, fullnames, and exact reddit comment URLs', () => {
expect(normalizeRedditCommentFullname('okf3s7u')).toBe('t1_okf3s7u');
expect(normalizeRedditCommentFullname('T1_OKF3S7U')).toBe('t1_okf3s7u');
expect(normalizeRedditCommentFullname('https://www.reddit.com/r/opencli/comments/1abc23/title_slug/okf3s7u/?context=3')).toBe('t1_okf3s7u');
expect(normalizeRedditCommentFullname('https://old.reddit.com/r/opencli/comments/1abc23/title_slug/okf3s7u/')).toBe('t1_okf3s7u');
});
it('rejects invalid or ambiguous comment identities before navigation', async () => {
const page = makePage();
for (const value of [
'',
't3_1abc23',
'abc/def',
'https://reddit.com.evil.com/r/opencli/comments/1abc23/title_slug/okf3s7u/',
'http://www.reddit.com/r/opencli/comments/1abc23/title_slug/okf3s7u/',
'https://www.reddit.com/r/opencli/comments/1abc23/title_slug/',
'https://www.reddit.com/r/opencli/comments/1abc23/title_slug/okf3s7u/evil',
]) {
await expect(command.func(page, { 'comment-id': value, text: 'hello' })).rejects.toBeInstanceOf(ArgumentError);
}
expect(page.goto).not.toHaveBeenCalled();
expect(page.evaluate).not.toHaveBeenCalled();
});
it('rejects blank reply text before navigation', async () => {
const page = makePage();
await expect(command.func(page, { 'comment-id': 'okf3s7u', text: ' ' })).rejects.toBeInstanceOf(ArgumentError);
expect(page.goto).not.toHaveBeenCalled();
expect(page.evaluate).not.toHaveBeenCalled();
expect(() => requireReplyText('hello')).not.toThrow();
});
it('posts to the normalized t1 fullname and returns success only on ok result', async () => {
const page = makePage();
const rows = await command.func(page, {
'comment-id': 'https://www.reddit.com/r/opencli/comments/1abc23/title_slug/okf3s7u/',
text: 'hello',
});
expect(page.goto).toHaveBeenCalledWith('https://www.reddit.com');
const script = page.evaluate.mock.calls[0][0];
expect(script).toContain('const fullname = "t1_okf3s7u"');
expect(script).toContain('const text = "hello"');
expect(rows).toEqual([{ status: 'success', message: 'Reply posted on t1_okf3s7u as t1_reply123' }]);
});
it('maps auth, http, reddit, exception, and postcondition failures to typed errors', async () => {
await expect(command.func(makePage({ kind: 'auth', detail: 'login required' }), { 'comment-id': 'okf3s7u', text: 'hello' }))
.rejects.toBeInstanceOf(AuthRequiredError);
await expect(command.func(makePage({ kind: 'http', httpStatus: 500, where: '/api/comment' }), { 'comment-id': 'okf3s7u', text: 'hello' }))
.rejects.toBeInstanceOf(CommandExecutionError);
await expect(command.func(makePage({ kind: 'reddit-error', detail: 'RATELIMIT: try later' }), { 'comment-id': 'okf3s7u', text: 'hello' }))
.rejects.toBeInstanceOf(CommandExecutionError);
await expect(command.func(makePage({ kind: 'exception', detail: 'bad json' }), { 'comment-id': 'okf3s7u', text: 'hello' }))
.rejects.toBeInstanceOf(CommandExecutionError);
await expect(command.func(makePage({ kind: 'postcondition', detail: 'Reddit comment response did not include a created reply id' }), { 'comment-id': 'okf3s7u', text: 'hello' }))
.rejects.toBeInstanceOf(CommandExecutionError);
});
it('requires the Reddit response to include a created reply id', async () => {
const page = makePage();
await command.func(page, { 'comment-id': 'okf3s7u', text: 'hello' });
expect(page.evaluate.mock.calls[0][0]).toContain('Reddit comment response did not include a created reply id');
expect(page.evaluate.mock.calls[0][0]).toContain("String(thing?.data?.name || '').startsWith('t1_')");
});
});