# frozen_string_literal: true require 'open3' require 'ratproto/cli' RSpec.describe 'rat CLI' do def run_cli(*arguments) executable = File.expand_path('../exe/rat', __dir__) Open3.capture3(Gem.ruby, executable, *arguments) end it 'prints the root help when invoked without arguments' do stdout, stderr, status = run_cli expect(status).to be_success expect(stderr).to eq('') expect(stdout).to include('Subcommands:') expect(stdout).to include('fetch') expect(stdout).to include('resolve') expect(stdout).to include('stream') end it 'supports the help command' do stdout, stderr, status = run_cli('help', 'stream') expect(status).to be_success expect(stderr).to eq('') expect(stdout).to include('rat stream [OPTIONS] SERVICE') expect(stdout).to include('--collection') end it 'supports command-specific help flags' do stdout, stderr, status = run_cli('resolve', '--help') expect(status).to be_success expect(stderr).to eq('') expect(stdout).to include('rat resolve [OPTIONS] TARGET') expect(stdout).to include('--pds') end it 'supports the version command and option' do command_stdout, command_stderr, command_status = run_cli('version') option_stdout, option_stderr, option_status = run_cli('--version') expect(command_status).to be_success expect(option_status).to be_success expect(command_stderr).to eq('') expect(option_stderr).to eq('') expect(command_stdout).to eq(option_stdout) expect(command_stdout).to include(RatProto::VERSION) end it 'rejects mutually exclusive resolve output options before resolving' do _stdout, stderr, status = run_cli('resolve', 'example.com', '--did', '--pds') expect(status).not_to be_success expect(stderr).to include('--pds and --did options cannot be used together') end it 'rejects malformed cursors before connecting' do _stdout, stderr, status = run_cli('stream', 'example.com', '--cursor', 'abc') expect(status).not_to be_success expect(stderr).to include('must be a decimal integer') end it 'parses repeated filters and attached short options after the host' do command = RatProto::Commands::Stream.new('rat stream') command.parse(%w[ example.com -r0 -d did:plc:one,did:plc:two -d did:plc:three -c app.bsky.feed.post -c app.bsky.actor.profile ]) expect(command.service).to eq('example.com') expect(command.cursor).to eq(0) expect(command.did_values).to eq(['did:plc:one,did:plc:two', 'did:plc:three']) expect(command.collection_values).to eq(['app.bsky.feed.post', 'app.bsky.actor.profile']) end end