diff --git a/CHANGELOG.md b/CHANGELOG.md index a5a86e2..70e8a7c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ All notable changes to the `indianopinion` npm package. The format follows - `brief --include coverage` returns the per-outlet framing as `coverage` (`[{outlet, framing, note}]`, the MCP `get_brief` shape) instead of under the REST name `stances`, in the CLI and in `io.sh`. With `--include`, `stances` is no longer in the output; without `--include` the REST payload is unchanged (#27). - `briefs --category` with a category that matches nothing no longer looks like a quiet day: when the category is not among those of the newest 100 briefs, the empty answer carries a `warning` and `categories_seen`, in the CLI and in `io.sh` with jq. The exit code stays 0, because the API keeps no list of categories to check against. README and SKILL.md say how `--category` matches and how to list the categories in use (#30). - README: `npm i -g github:IndianOpinion/cli` installs the `main` branch, not "the newest release" as the comment said; a `#v0.2.2` line installs the tagged release. The registry package is still unpublished (#1). +- `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 diff --git a/README.md b/README.md index f4ded51..bf59641 100644 --- a/README.md +++ b/README.md @@ -35,8 +35,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, errors from the API included (`{"ok":false,"error":...,"message":...,"status":...}` on stderr); 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 5808f27..3fce33b 100644 --- a/io.sh +++ b/io.sh @@ -9,8 +9,9 @@ # 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, and API errors as # {ok: false, error, message, status} on stderr); 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. @@ -304,7 +305,7 @@ case "$cmd" in emit "$OUT" ;; topics) case "$sub" in - ''|list) allow 'topics list' 0 limit; limit_of 10; call GET /agent/topics '' auth + ''|list) allow 'topics list' 0 limit; if [ -n "$(f limit)" ]; then needjq 'topics list --limit'; fi; limit_of 10; call GET /agent/topics '' auth if [ "$HAVE_JQ" = 1 ]; then OUT=$(printf '%s' "$OUT" | jq -c --argjson n "$LIMIT" '.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); +});