From cd94068f50e908957bd5b431e2a2b31302add047 Mon Sep 17 00:00:00 2001 From: makriman <9696567+makriman@users.noreply.github.com> Date: Sat, 5 Dec 2026 07:19:46 +0000 Subject: [PATCH] io.sh: topics list --limit without jq is a usage error, not an ignored flag Without jq, io.sh topics list --limit N printed every suggestion and exited 0. Slicing the suggested array needs a JSON parser, so the flag now fails before the request with the needs-jq usage error (exit 2), as digest --limit already does. --- CHANGELOG.md | 6 ++ README.md | 5 +- io.sh | 9 ++- test/topics-limit-no-jq.test.mjs | 106 +++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 test/topics-limit-no-jq.test.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index 63a97e8..f740168 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to the `indianopinion` npm package. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow [Semantic Versioning](https://semver.org/). +## [Unreleased] + +### Fixed + +- `io.sh topics list --limit N` without jq is a usage error (exit 2) that says the flag needs jq, before any request, as `digest --limit` already is. It used to print every suggestion and exit 0, so the limit was silently ignored. + ## [0.2.2] - 2026-10-22 ### Fixed diff --git a/README.md b/README.md index 8d01474..842ea18 100644 --- a/README.md +++ b/README.md @@ -29,8 +29,9 @@ mkdir -p ~/bin && curl -fsSL https://indianopinion.org/agents/cli/io.sh -o ~/bin ``` With jq installed, `io.sh` prints the same JSON as the CLI; without jq it prints the API's JSON -unchanged, and the few flags that need reshaping (`digest --limit`, `breaking recent --limit` or -`--would-send`, `brief --include`) say they need jq. A flag a command does not take is a usage error +unchanged, and the few flags that need reshaping (`digest --limit`, `topics list --limit`, +`breaking recent --limit` or `--would-send`, `brief --include`) say they need jq and exit 2 before +any request. A flag a command does not take is a usage error (exit 2) in both. ## Use diff --git a/io.sh b/io.sh index ca1dc80..f88440c 100644 --- a/io.sh +++ b/io.sh @@ -8,8 +8,9 @@ # ~/bin/io digest | jq '.stories[] | {title, url}' # Output: with jq installed, the same JSON as the indianopinion CLI (briefs as # {ok, count, items, next_cursor} with post_id, summary and so on); without jq, -# the API's JSON unchanged. Flags that need reshaping (digest --limit, breaking -# recent --limit or --would-send, brief --include) need jq and say so. +# the API's JSON unchanged. Flags that need reshaping (digest --limit, topics +# list --limit, breaking recent --limit or --would-send, brief --include) need +# jq and say so. # A flag a command does not take is a usage error (exit 2), never ignored. # Exit codes: 0 ok, 1 unexpected, 2 usage, 3 auth, 4 not found, 5 rate # limited, 6 validation, 7 network or server, 8 needs a choice. @@ -269,7 +270,9 @@ case "$cmd" in emit "$OUT" ;; topics) case "$sub" in - ''|list) allow 'topics list' 0 limit; call GET /agent/topics '' auth + ''|list) allow 'topics list' 0 limit + if [ -n "$(f limit)" ]; then needjq 'topics list --limit'; fi + call GET /agent/topics '' auth if [ "$HAVE_JQ" = 1 ]; then l=$(limit_of 10); OUT=$(printf '%s' "$OUT" | jq -c --argjson n "$l" '.suggested = ((.suggested // [])[0:$n])'); fi emit "$OUT" ;; follow) diff --git a/test/topics-limit-no-jq.test.mjs b/test/topics-limit-no-jq.test.mjs new file mode 100644 index 0000000..73bce9f --- /dev/null +++ b/test/topics-limit-no-jq.test.mjs @@ -0,0 +1,106 @@ +// io.sh topics list --limit without jq: a usage error (exit 2) before the request, never a +// silently ignored limit. Node CLI and io.sh against the REST stub; no network. + +import { test, before, after } from 'node:test'; +import assert from 'node:assert/strict'; +import { spawn, spawnSync } from 'node:child_process'; +import fs from 'node:fs'; +import os from 'node:os'; +import path from 'node:path'; +import { fileURLToPath } from 'node:url'; +import { startStub, GOOD_TOKEN } from './stub-rest.mjs'; + +const HERE = path.dirname(fileURLToPath(import.meta.url)); +const ROOT = path.join(HERE, '..'); +const BIN = path.join(ROOT, 'bin', 'indianopinion.mjs'); +const IOSH = path.join(ROOT, 'io.sh'); +const HAVE_CURL = spawnSync('bash', ['-c', 'command -v curl']).status === 0; +const HAVE_JQ = spawnSync('bash', ['-c', 'command -v jq']).status === 0; +let stub; let home; let noJqPath; + +// A PATH with every command of the current one except jq: directories without jq are kept as they +// are, and a directory that holds jq is replaced by a scratch directory of links to everything else. +function pathWithoutJq() { + const shadow = fs.mkdtempSync(path.join(os.tmpdir(), 'io-nojq-bin-')); + const dirs = []; + for (const dir of (process.env.PATH || '').split(path.delimiter).filter(Boolean)) { + if (!fs.existsSync(path.join(dir, 'jq'))) { dirs.push(dir); continue; } + let names = []; + try { names = fs.readdirSync(dir); } catch { continue; } + for (const name of names) { + if (name === 'jq' || fs.existsSync(path.join(shadow, name))) continue; + try { fs.symlinkSync(path.join(dir, name), path.join(shadow, name)); } catch { /* skip */ } + } + if (!dirs.includes(shadow)) dirs.push(shadow); + } + return dirs.join(path.delimiter); +} + +function run(args, { env = {}, bin = 'node' } = {}) { + const [cmd, argv] = bin === 'node' ? [process.execPath, [BIN, ...args]] : ['bash', [IOSH, ...args]]; + return new Promise((resolve) => { + const child = spawn(cmd, argv, { env: { PATH: process.env.PATH, HOME: home, XDG_CONFIG_HOME: path.join(home, '.config'), IO_API: stub.api, IO_TOKEN: GOOD_TOKEN, ...env } }); + let out = ''; let errs = ''; + child.stdout.on('data', (d) => { out += d; }); + child.stderr.on('data', (d) => { errs += d; }); + child.stdin.end(); + child.on('close', (code) => { + let json = null; let errJson = null; + try { json = JSON.parse(out); } catch { /* text */ } + try { errJson = JSON.parse(errs); } catch { /* text */ } + resolve({ code, out, err: errs, json, errJson }); + }); + }); +} + +const topicsRequests = () => stub.requests.filter((x) => x.path.endsWith('/agent/topics')).length; + +before(async () => { + stub = await startStub(); + home = fs.mkdtempSync(path.join(os.tmpdir(), 'io-topics-limit-')); + noJqPath = pathWithoutJq(); +}); +after(async () => { await stub.close(); }); + +test('the stub has more suggestions than the limit under test', async () => { + const r = await run(['topics', 'list']); + assert.equal(r.code, 0, r.err); + assert.ok(r.json.suggested.length >= 2); + assert.equal((await run(['topics', 'list', '--limit', '1'])).json.suggested.length, 1, 'the CLI applies --limit'); +}); + +test('io.sh topics list --limit with jq hidden from PATH: exit 2, needs jq, no request', { skip: !HAVE_CURL }, async () => { + assert.notEqual(spawnSync('bash', ['-c', 'command -v jq'], { env: { PATH: noJqPath } }).status, 0, 'jq is hidden'); + for (const limit of ['1', '100', 'abc']) { + const before2 = topicsRequests(); + const r = await run(['topics', 'list', '--limit', limit], { bin: 'bash', env: { PATH: noJqPath } }); + assert.equal(r.code, 2, `--limit ${limit}: ${r.out}${r.err}`); + assert.equal(r.errJson.error, 'usage'); + assert.match(r.errJson.message, /topics list --limit needs jq/); + assert.equal(r.out, '', 'nothing on stdout'); + assert.equal(topicsRequests(), before2, 'no request is made'); + } +}); + +test('io.sh topics list --limit with IO_SH_NO_JQ: the same usage error', { skip: !HAVE_CURL }, async () => { + const r = await run(['topics', 'list', '--limit', '1'], { bin: 'bash', env: { IO_SH_NO_JQ: '1' } }); + assert.equal(r.code, 2); + assert.match(r.errJson.message, /needs jq/); + const bare = await run(['topics', '--limit', '1'], { bin: 'bash', env: { IO_SH_NO_JQ: '1' } }); + assert.equal(bare.code, 2, 'topics with no subcommand is topics list'); +}); + +test('io.sh topics list without --limit and without jq still prints the API JSON', { skip: !HAVE_CURL }, async () => { + const r = await run(['topics', 'list'], { bin: 'bash', env: { PATH: noJqPath } }); + assert.equal(r.code, 0, r.err); + assert.ok(Array.isArray(r.json.suggested)); + assert.ok(Array.isArray(r.json.following)); +}); + +test('io.sh topics list --limit with jq keeps the CLI behaviour', { skip: !(HAVE_CURL && HAVE_JQ) }, async () => { + const sh = await run(['topics', 'list', '--limit', '1'], { bin: 'bash' }); + const cli = await run(['topics', 'list', '--limit', '1']); + assert.equal(sh.code, 0, sh.err); + assert.equal(sh.json.suggested.length, 1); + assert.deepEqual(sh.json.suggested, cli.json.suggested); +});