const { test } = require('node:test'); const assert = require('node:assert/strict'); const { execFileSync, spawn, spawnSync } = require('node:child_process'); const fs = require('node:fs'); const http = require('node:http'); const os = require('node:os'); const path = require('node:path'); const CLI = path.join(__dirname, '..', 'src', 'cli.js'); const pkg = require('../package.json'); function run(args, env = {}) { return spawnSync(process.execPath, [CLI, ...args], { encoding: 'utf8', env: { ...process.env, ...env }, }); } test('--version matches package.json', () => { const out = execFileSync(process.execPath, [CLI, '--version'], { encoding: 'utf8' }); assert.equal(out.trim(), pkg.version); }); test('--help lists every top-level command', () => { const out = execFileSync(process.execPath, [CLI, '--help'], { encoding: 'utf8' }); for (const cmd of ['auth', 'repo', 'issue', 'pr', 'branch', 'collaborator', 'org', 'user']) { assert.match(out, new RegExp(`^\\s+${cmd}`, 'm'), `missing command: ${cmd}`); } }); test('unauthenticated commands fail with a login hint', () => { const missing = path.join(os.tmpdir(), `stoke-none-${process.pid}.json`); const res = run(['repo', 'list'], { STOKE_CONFIG_FILE: missing }); assert.equal(res.status, 1); assert.match(res.stderr, /stoke auth login/); }); test('global --config flag overrides the config location', () => { // Point --config at a nonexistent file: auth status must report // "Not authenticated" instead of silently using the default config. const missing = path.join(os.tmpdir(), `stoke-missing-${process.pid}.json`); const res = run(['--config', missing, 'auth', 'status']); assert.equal(res.status, 0); assert.match(res.stdout, /Not authenticated/); }); test('invalid --limit is rejected before any network call', () => { const res = run(['repo', 'list', '-l', 'abc']); assert.equal(res.status, 1); assert.match(res.stderr, /Limit must be a non-negative integer/); }); test('invalid --team-id is rejected before any network call', () => { const res = run(['org', 'team', 'member-add', '--team-id', 'zero', '-u', 'x']); assert.equal(res.status, 1); assert.match(res.stderr, /Id must be a positive integer/); }); test('pr merge validates --number before any network call', () => { const res = run(['pr', 'merge', '-o', 'o', '-r', 'r', '-n', 'seven']); assert.equal(res.status, 1); assert.match(res.stderr, /Id must be a positive integer/); }); test('issue create --body-file reports unreadable files cleanly', () => { const cfg = path.join(os.tmpdir(), `stoke-cfg-${process.pid}.json`); fs.writeFileSync(cfg, JSON.stringify({ url: 'https://forge.test', token: 'tok' })); try { const res = run( ['issue', 'create', '-o', 'o', '-r', 'r', '-t', 't', '--body-file', '/nonexistent/body.md'], { STOKE_CONFIG_FILE: cfg }, ); assert.equal(res.status, 1); assert.match(res.stderr, /Could not read body file/); } finally { fs.unlinkSync(cfg); } }); test('pr show validates --number before any network call', () => { const cfg = path.join(os.tmpdir(), `stoke-cfg-${process.pid}.json`); fs.writeFileSync(cfg, JSON.stringify({ url: 'https://forge.test', token: 'tok' })); try { const res = run(['pr', 'show', '-o', 'o', '-r', 'r', '-n', 'zero'], { STOKE_CONFIG_FILE: cfg }); assert.equal(res.status, 1); assert.match(res.stderr, /Id must be a positive integer/); } finally { fs.unlinkSync(cfg); } }); test('pr comment rejects a missing body before any network call', () => { const cfg = path.join(os.tmpdir(), `stoke-cfg-${process.pid}.json`); fs.writeFileSync(cfg, JSON.stringify({ url: 'https://forge.test', token: 'tok' })); try { const res = run(['pr', 'comment', '-o', 'o', '-r', 'r', '-n', '1'], { STOKE_CONFIG_FILE: cfg }); assert.equal(res.status, 1); assert.match(res.stderr, /Comment body is required/); } finally { fs.unlinkSync(cfg); } }); test('pr review rejects an invalid event before any network call', () => { const cfg = path.join(os.tmpdir(), `stoke-cfg-${process.pid}.json`); fs.writeFileSync(cfg, JSON.stringify({ url: 'https://forge.test', token: 'tok' })); try { const res = run(['pr', 'review', '-o', 'o', '-r', 'r', '-n', '1', '--event', 'nope'], { STOKE_CONFIG_FILE: cfg }); assert.equal(res.status, 1); assert.match(res.stderr, /Invalid review event/); } finally { fs.unlinkSync(cfg); } }); test('pr review request-changes rejects a missing body before any network call', () => { const cfg = path.join(os.tmpdir(), `stoke-cfg-${process.pid}.json`); fs.writeFileSync(cfg, JSON.stringify({ url: 'https://forge.test', token: 'tok' })); try { const res = run(['pr', 'review', '-o', 'o', '-r', 'r', '-n', '1', '--event', 'request-changes'], { STOKE_CONFIG_FILE: cfg }); assert.equal(res.status, 1); assert.match(res.stderr, /requires a non-empty body/); } finally { fs.unlinkSync(cfg); } }); test('pr review comment rejects a whitespace-only body before any network call', () => { const cfg = path.join(os.tmpdir(), `stoke-cfg-${process.pid}.json`); fs.writeFileSync(cfg, JSON.stringify({ url: 'https://forge.test', token: 'tok' })); try { const res = run(['pr', 'review', '-o', 'o', '-r', 'r', '-n', '1', '--event', 'comment', '-b', ' '], { STOKE_CONFIG_FILE: cfg }); assert.equal(res.status, 1); assert.match(res.stderr, /requires a non-empty body/); } finally { fs.unlinkSync(cfg); } }); test('pr review approve allows an empty body before any network call', () => { const cfg = path.join(os.tmpdir(), `stoke-cfg-${process.pid}.json`); fs.writeFileSync(cfg, JSON.stringify({ url: 'https://forge.test', token: 'tok' })); try { const res = run(['pr', 'review', '-o', 'o', '-r', 'r', '-n', '1', '--event', 'approve'], { STOKE_CONFIG_FILE: cfg }); // It fails at the network call, not at validation. assert.equal(res.status, 1); assert.doesNotMatch(res.stderr, /requires a non-empty body/); } finally { fs.unlinkSync(cfg); } }); function spawnAsync(args, env = {}) { return new Promise((resolve, reject) => { const child = spawn(process.execPath, [CLI, ...args], { env: { ...process.env, ...env }, }); let stdout = ''; let stderr = ''; child.stdout.setEncoding('utf8'); child.stderr.setEncoding('utf8'); child.stdout.on('data', (chunk) => { stdout += chunk; }); child.stderr.on('data', (chunk) => { stderr += chunk; }); child.on('error', reject); child.on('close', (status) => resolve({ status, stdout, stderr })); }); } test('pr review preserves exact body-file whitespace through the CLI boundary', async () => { const cfg = path.join(os.tmpdir(), `stoke-cfg-${process.pid}.json`); const bodyFile = path.join(os.tmpdir(), `stoke-body-${process.pid}.md`); const rawBody = ' leading spaces\nline\ntrailing newline\n'; fs.writeFileSync(bodyFile, rawBody, 'utf8'); const captured = await new Promise((resolve, reject) => { const server = http.createServer((req, res) => { let data = ''; req.setEncoding('utf8'); req.on('data', (chunk) => { data += chunk; }); req.on('end', () => { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ id: 99 })); server.close(() => resolve({ url: req.url, body: data })); }); }); server.listen(0, '127.0.0.1', async () => { const { port } = server.address(); fs.writeFileSync(cfg, JSON.stringify({ url: `http://127.0.0.1:${port}`, token: 'tok' })); try { const res = await spawnAsync( ['pr', 'review', '-o', 'o', '-r', 'r', '-n', '7', '--event', 'request-changes', '--body-file', bodyFile], { STOKE_CONFIG_FILE: cfg }, ); if (res.status !== 0) { server.close(() => reject(new Error(`CLI failed: ${res.stderr}`))); } } catch (err) { server.close(() => reject(err)); } }); }); try { assert.equal(captured.url, '/api/v1/repos/o/r/pulls/7/reviews'); const json = JSON.parse(captured.body); assert.equal(json.event, 'REQUEST_CHANGES'); assert.equal(json.body, rawBody); } finally { fs.unlinkSync(cfg); fs.unlinkSync(bodyFile); } });