From 8de86eabbff9d5ff42f2340b1906a8595cd898e8 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 8 Dec 2023 04:51:57 -0500 Subject: [PATCH] Add spec for `CLI::Domains#crawl` command (#28271) --- lib/mastodon/cli/domains.rb | 6 ++-- spec/lib/mastodon/cli/domains_spec.rb | 47 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/lib/mastodon/cli/domains.rb b/lib/mastodon/cli/domains.rb index 329f17167..e092497dc 100644 --- a/lib/mastodon/cli/domains.rb +++ b/lib/mastodon/cli/domains.rb @@ -97,6 +97,8 @@ module Mastodon::CLI say("Removed #{custom_emojis_count} custom emojis#{dry_run_mode_suffix}", :green) end + CRAWL_SLEEP_TIME = 20 + option :concurrency, type: :numeric, default: 50, aliases: [:c] option :format, type: :string, default: 'summary', aliases: [:f] option :exclude_suspended, type: :boolean, default: false, aliases: [:x] @@ -168,8 +170,8 @@ module Mastodon::CLI pool.post(domain, &work_unit) end - sleep 20 - sleep 20 until pool.queue_length.zero? + sleep CRAWL_SLEEP_TIME + sleep CRAWL_SLEEP_TIME until pool.queue_length.zero? pool.shutdown pool.wait_for_termination(20) diff --git a/spec/lib/mastodon/cli/domains_spec.rb b/spec/lib/mastodon/cli/domains_spec.rb index a10907f76..24f341c12 100644 --- a/spec/lib/mastodon/cli/domains_spec.rb +++ b/spec/lib/mastodon/cli/domains_spec.rb @@ -28,4 +28,51 @@ describe Mastodon::CLI::Domains do end end end + + describe '#crawl' do + let(:action) { :crawl } + + context 'with accounts from the domain' do + let(:domain) { 'host.example' } + + before do + Fabricate(:account, domain: domain) + stub_request(:get, 'https://host.example/api/v1/instance').to_return(status: 200, body: {}.to_json) + stub_request(:get, 'https://host.example/api/v1/instance/peers').to_return(status: 200, body: {}.to_json) + stub_request(:get, 'https://host.example/api/v1/instance/activity').to_return(status: 200, body: {}.to_json) + stub_const('Mastodon::CLI::Domains::CRAWL_SLEEP_TIME', 0) + end + + context 'with --format of summary' do + let(:options) { { format: 'summary' } } + + it 'crawls the domains and summarizes results' do + expect { subject } + .to output_results('Visited 1 domains, 0 failed') + end + end + + context 'with --format of domains' do + let(:options) { { format: 'domains' } } + + it 'crawls the domains and summarizes results' do + expect { subject } + .to output_results(domain) + end + end + + context 'with --format of json' do + let(:options) { { format: 'json' } } + + it 'crawls the domains and summarizes results' do + expect { subject } + .to output_results(json_summary) + end + + def json_summary + Oj.dump('host.example': { activity: {} }) + end + end + end + end end