load("@rules_erlang//:eunit2.bzl", "eunit")
load("@rules_erlang//:xref2.bzl", "xref")
load("@rules_erlang//:dialyze.bzl", "dialyze", "plt")
load("//:rabbitmq_home.bzl", "rabbitmq_home")
load("//:rabbitmq_run.bzl", "rabbitmq_run")
load(
    "//:rabbitmq.bzl",
    "RABBITMQ_DIALYZER_OPTS",
    "assert_suites",
    "rabbitmq_app",
    "rabbitmq_integration_suite",
    "rabbitmq_suite",
    "without",
)
load(
    ":app.bzl",
    "all_beam_files",
    "all_srcs",
    "all_test_beam_files",
    "test_suite_beam_files",
)
load(":bats.bzl", "bats")

exports_files(glob([
    "scripts/**",
]) + ["INSTALL"])

_APP_ENV = """[
	    %% See https://www.rabbitmq.com/docs/consumers#acknowledgement-timeout
	    %% 30 minutes
	    {consumer_timeout, 1800000},
	    {tcp_listeners, [5672]},
	    {num_tcp_acceptors, 10},
	    {ssl_listeners, []},
	    {num_ssl_acceptors, 10},
	    {ssl_options, []},
	    {vm_memory_high_watermark, 0.6},
	    {vm_memory_calculation_strategy, rss},
	    {disk_free_limit, 50000000}, %% 50MB
	    {backing_queue_module, rabbit_variable_queue},
	    %% 0 ("no limit") would make a better default, but that
	    %% breaks the QPid Java client
	    {frame_max, 131072},
	    %% see rabbitmq-server#1593
	    {channel_max, 2047},
	    {session_max_per_connection, 64},
	    {link_max_per_session, 256},
	    {ranch_connection_max, infinity},
	    {heartbeat, 60},
	    {msg_store_file_size_limit, 16777216},
	    {msg_store_shutdown_timeout, 600000},
	    {fhc_write_buffering, true},
	    {fhc_read_buffering, false},
	    {queue_index_max_journal_entries, 32768},
	    {queue_index_embed_msgs_below, 4096},
	    {default_user, <<"guest">>},
	    {default_pass, <<"guest">>},
	    {default_user_tags, [administrator]},
	    {default_vhost, <<"/">>},
	    {default_permissions, [<<".*">>, <<".*">>, <<".*">>]},
	    {loopback_users, [<<"guest">>]},
	    {password_hashing_module, rabbit_password_hashing_sha256},
	    {server_properties, []},
	    {collect_statistics, none},
	    {collect_statistics_interval, 5000},
	    {mnesia_table_loading_retry_timeout, 30000},
	    {mnesia_table_loading_retry_limit, 10},
	    {anonymous_login_user, <<"guest">>},
	    {anonymous_login_pass, <<"guest">>},
	    {auth_mechanisms, ['PLAIN', 'AMQPLAIN', 'ANONYMOUS']},
	    {auth_backends, [rabbit_auth_backend_internal]},
	    {delegate_count, 16},
	    {trace_vhosts, []},
	    {ssl_cert_login_from, distinguished_name},
	    {ssl_handshake_timeout, 5000},
	    {ssl_allow_poodle_attack, false},
	    {handshake_timeout, 10000},
	    {reverse_dns_lookups, false},
	    {cluster_partition_handling, ignore},
	    {cluster_keepalive_interval, 10000},
	    {autoheal_state_transition_timeout, 60000},
	    {tcp_listen_options, [{backlog,       128},
	                          {nodelay,       true},
	                          {linger,        {true, 0}},
	                          {exit_on_close, false}
	                         ]},
	    {ssl_apps, [asn1, crypto, public_key, ssl]},
	    %% see rabbitmq-server#227 and related tickets.
	    %% msg_store_credit_disc_bound only takes effect when
	    %% messages are persisted to the message store. If messages
	    %% are embedded on the queue index, then modifying this
	    %% setting has no effect because credit_flow is not used when
	    %% writing to the queue index. See the setting
	    %% queue_index_embed_msgs_below above.
	    {msg_store_credit_disc_bound, {4000, 800}},
	    {msg_store_io_batch_size, 4096},
	    %% see rabbitmq-server#143,
	    %% rabbitmq-server#949, rabbitmq-server#1098
	    {credit_flow_default_credit, {400, 200}},
	    {quorum_commands_soft_limit, 32},
	    {quorum_cluster_size, 3},
	    %% see rabbitmq-server#248
	    %% and rabbitmq-server#667
	    {channel_operation_timeout, 15000},

	    %% used by rabbit_peer_discovery_classic_config
	    {cluster_nodes, {[], disc}},

	    {config_entry_decoder, [{passphrase, undefined}]},
	    {background_gc_enabled, false},
	    {background_gc_target_interval, 60000},
	    %% rabbitmq-server#589
	    {proxy_protocol, false},
	    {disk_monitor_failure_retries, 10},
	    {disk_monitor_failure_retry_interval, 120000},
	    %% either "stop_node" or "continue".
	    %% by default we choose to not terminate the entire node if one
	    %% vhost had to shut down, see server#1158 and server#1280
	    {vhost_restart_strategy, continue},
	    %% {global, prefetch count}
	    {default_consumer_prefetch, {false, 0}},
		%% interval at which the channel can perform periodic actions
	    {channel_tick_interval, 60000},
	    %% Default max message size is 16 MB
	    {max_message_size, 16777216},
	    %% Socket writer will run GC every 1 GB of outgoing data
	    {writer_gc_threshold, 1000000000},
	    %% interval at which connection/channel tracking executes post operations
	    {tracking_execution_timeout, 15000},
	    {stream_messages_soft_limit, 256},
        {track_auth_attempt_source, false},
        {credentials_obfuscation_fallback_secret, <<"nocookie">>},
        {dead_letter_worker_consumer_prefetch, 32},
        {dead_letter_worker_publisher_confirm_timeout, 180000},
		{vhost_process_reconciliation_run_interval, 30},
        %% for testing
        {vhost_process_reconciliation_enabled, true},
        {license_line, "Licensed under the MPL 2.0. Website: https://rabbitmq.com"}
	  ]
"""

APP_MODULE = "rabbit"

APP_REGISTERED = [
    "rabbit_amqqueue_sup",
    "rabbit_direct_client_sup",
    "rabbit_log",
    "rabbit_node_monitor",
    "rabbit_router",
]

all_beam_files(name = "all_beam_files")

all_test_beam_files(name = "all_test_beam_files")

all_srcs(name = "all_srcs")

test_suite_beam_files(name = "test_suite_beam_files")

# gazelle:erlang_apps_dirs apps

# gazelle:erlang_app_extra_app sasl
# gazelle:erlang_app_extra_app os_mon
# gazelle:erlang_app_extra_app inets
# gazelle:erlang_app_extra_app compiler
# gazelle:erlang_app_extra_app crypto
# gazelle:erlang_app_extra_app public_key
# gazelle:erlang_app_extra_app ssl
# gazelle:erlang_app_extra_app syntax_tools
# gazelle:erlang_app_extra_app xmerl

# gazelle:erlang_app_dep cuttlefish
# gazelle:erlang_app_dep syslog
# gazelle:erlang_app_dep observer_cli
# gazelle:erlang_app_dep redbug
# gazelle:erlang_app_dep sysmon_handler
# gazelle:erlang_app_dep systemd

rabbitmq_app(
    name = "erlang_app",
    srcs = [":all_srcs"],
    hdrs = [":public_hdrs"],
    app_description = "RabbitMQ",
    app_env = _APP_ENV,
    app_module = APP_MODULE,
    app_name = "rabbit",
    app_registered = APP_REGISTERED,
    beam_files = [":beam_files"],
    extra_apps = [
        "compiler",
        "inets",
        "os_mon",
        "public_key",
        "sasl",
        "ssl",
        "syntax_tools",
        "xmerl",
        "crypto",
    ],
    license_files = [":license_files"],
    priv = [":priv"],
    deps = [
        "//deps/amqp10_common:erlang_app",
        "//deps/rabbit_common:erlang_app",
        "//deps/rabbitmq_prelaunch:erlang_app",
        "@cuttlefish//:erlang_app",
        "@gen_batch_server//:erlang_app",
        "@khepri//:erlang_app",
        "@khepri_mnesia_migration//:erlang_app",
        "@observer_cli//:erlang_app",
        "@osiris//:erlang_app",
        "@ra//:erlang_app",
        "@ranch//:erlang_app",
        "@recon//:erlang_app",
        "@redbug//:erlang_app",
        "@seshat//:erlang_app",
        "@stdout_formatter//:erlang_app",
        "@syslog//:erlang_app",
        "@sysmon_handler//:erlang_app",
        "@systemd//:erlang_app",
    ],
)

xref(
    name = "xref",
    additional_libs = [
        "//deps/rabbitmq_cli:erlang_app",  # keep
    ],
    target = ":erlang_app",
)

plt(
    name = "deps_plt",
    apps = [
        "mnesia",  # keep
        "runtime_tools",  # keep
    ],
    for_target = ":erlang_app",
    ignore_warnings = True,
    libs = ["@rules_elixir//elixir"],  # keep
    plt = "//:base_plt",
    deps = [
        "//deps/rabbitmq_cli:erlang_app",  # keep
    ],
)

dialyze(
    name = "dialyze",
    dialyzer_opts = RABBITMQ_DIALYZER_OPTS,
    plt = ":deps_plt",
    target = ":erlang_app",
)

bats(
    srcs = glob(["test/**/*.bats"]),
    data = glob(
        ["scripts/*"],
        exclude = ["scripts/*.bat"],
    ),
    tags = ["bats"],
)

rabbitmq_home(
    name = "broker-for-tests-home",
    testonly = True,
    plugins = [
        ":test_erlang_app",
        "//deps/rabbitmq_ct_client_helpers:erlang_app",
        "//deps/rabbitmq_amqp1_0:erlang_app",
        "@inet_tcp_proxy_dist//:erlang_app",
        "@meck//:erlang_app",
    ],
)

rabbitmq_run(
    name = "rabbitmq-for-tests-run",
    testonly = True,
    home = ":broker-for-tests-home",
)

rabbitmq_suite(
    name = "amqqueue_backward_compatibility_SUITE",
    size = "small",
)

rabbitmq_integration_suite(
    name = "backing_queue_SUITE",
    size = "large",
)

rabbitmq_integration_suite(
    name = "channel_interceptor_SUITE",
    size = "medium",
    additional_beam = [
        "test/dummy_interceptor.beam",
        "test/failing_dummy_interceptor.beam",
    ],
)

rabbitmq_integration_suite(
    name = "channel_operation_timeout_SUITE",
    size = "medium",
    additional_beam = [
        "test/channel_operation_timeout_test_queue.beam",
    ],
)

rabbitmq_integration_suite(
    name = "classic_queue_prop_SUITE",
    size = "large",
    deps = [
        "@proper//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "cluster_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "clustering_events_SUITE",
    size = "medium",
    additional_beam = [
        ":test_event_recorder_beam",
    ],
)

rabbitmq_integration_suite(
    name = "quorum_queue_member_reconciliation_SUITE",
    size = "medium",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
)

rabbitmq_integration_suite(
    name = "cluster_limit_SUITE",
    size = "medium",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
)

rabbitmq_integration_suite(
    name = "clustering_management_SUITE",
    size = "large",
    additional_beam = [
        ":test_clustering_utils_beam",
    ],
    shard_count = 45,
    sharding_method = "case",
)

rabbitmq_integration_suite(
    name = "clustering_recovery_SUITE",
    size = "medium",
    additional_beam = [
        ":test_clustering_utils_beam",
    ],
    shard_count = 8,
    sharding_method = "case",
)

rabbitmq_integration_suite(
    name = "config_schema_SUITE",
    size = "medium",
    data = [
        "test/definition_import_SUITE_data/case1.json",
    ],
)

rabbitmq_integration_suite(
    name = "confirms_rejects_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "consumer_timeout_SUITE",
    size = "medium",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
)

rabbitmq_integration_suite(
    name = "crashing_queues_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "dead_lettering_SUITE",
    size = "large",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
    shard_count = 6,
)

rabbitmq_integration_suite(
    name = "amqpl_consumer_ack_SUITE",
)

rabbitmq_integration_suite(
    name = "message_containers_deaths_v2_SUITE",
    size = "medium",
    shard_count = 1,
)

rabbitmq_integration_suite(
    name = "definition_import_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "deprecated_features_SUITE",
    size = "medium",
    additional_beam = [
        ":feature_flags_v2_SUITE_beam_files",
    ],
)

rabbitmq_integration_suite(
    name = "disconnect_detected_during_alarm_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "disk_monitor_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "dynamic_qq_SUITE",
    size = "large",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
    deps = [
        "@proper//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "feature_flags_SUITE",
    size = "large",
    additional_beam = [
        ":test_clustering_utils_beam",
    ],
    flaky = True,
    shard_count = 5,
    runtime_deps = [
        "//deps/rabbit/test/feature_flags_SUITE_data/my_plugin:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "feature_flags_v2_SUITE",
    size = "large",
)

rabbitmq_integration_suite(
    name = "msg_size_metrics_SUITE",
    runtime_deps = [
        "//deps/rabbitmq_amqp_client:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "list_consumers_sanity_check_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "list_queues_online_and_offline_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "logging_SUITE",
    runtime_deps = [
        "@syslog//:erlang_app",
    ],
)

rabbitmq_suite(
    name = "lqueue_SUITE",
    size = "small",
    deps = [
        "@proper//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "maintenance_mode_SUITE",
    size = "medium",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
)

rabbitmq_integration_suite(
    name = "rabbit_message_interceptor_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "message_size_limit_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "per_node_limit_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "metrics_SUITE",
    size = "medium",
    deps = [
        "@proper//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "mirrored_supervisor_SUITE",
    size = "small",
    additional_beam = [
        "test/mirrored_supervisor_SUITE_gs.beam",
    ],
)

rabbitmq_suite(
    name = "msg_store_SUITE",
    size = "small",
    deps = [
        "//deps/rabbit_common:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "peer_discovery_classic_config_SUITE",
    size = "large",
)

rabbitmq_integration_suite(
    name = "peer_discovery_dns_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "peer_discovery_tmp_hidden_node_SUITE",
    size = "large",
)

rabbitmq_integration_suite(
    name = "per_user_connection_channel_limit_partitions_SUITE",
    size = "large",
)

rabbitmq_integration_suite(
    name = "per_user_connection_channel_limit_SUITE",
    size = "medium",
    shard_count = 4,
)

rabbitmq_integration_suite(
    name = "per_user_connection_channel_tracking_SUITE",
    size = "medium",
    shard_count = 4,
)

rabbitmq_integration_suite(
    name = "per_user_connection_tracking_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "per_vhost_connection_limit_partitions_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "per_vhost_connection_limit_SUITE",
    size = "medium",
    shard_count = 5,
)

rabbitmq_integration_suite(
    name = "per_vhost_msg_store_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "per_vhost_queue_limit_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "policy_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "priority_queue_recovery_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "priority_queue_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "product_info_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "proxy_protocol_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "publisher_confirms_parallel_SUITE",
    size = "medium",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
)

rabbitmq_integration_suite(
    name = "queue_length_limits_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "queue_parallel_SUITE",
    size = "large",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
    shard_count = 3,
)

rabbitmq_integration_suite(
    name = "queue_type_SUITE",
    size = "medium",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
)

rabbitmq_integration_suite(
    name = "quorum_queue_SUITE",
    size = "large",
    additional_beam = [
        ":test_queue_utils_beam",
        ":test_clustering_utils_beam",
    ],
    shard_count = 6,
)

rabbitmq_integration_suite(
    name = "classic_queue_SUITE",
    size = "medium",
)

rabbitmq_suite(
    name = "rabbit_confirms_SUITE",
    size = "small",
    deps = [
        "//deps/rabbit_common:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "rabbit_core_metrics_gc_SUITE",
    size = "medium",
)

rabbitmq_suite(
    name = "rabbit_cuttlefish_SUITE",
)

rabbitmq_suite(
    name = "rabbit_fifo_int_SUITE",
    size = "medium",
    additional_beam = [
        ":test_test_util_beam",
    ],
    deps = [
        "//deps/rabbit_common:erlang_app",
        "//deps/rabbitmq_ct_helpers:erlang_app",
        "@aten//:erlang_app",
        "@gen_batch_server//:erlang_app",
        "@meck//:erlang_app",
        "@ra//:erlang_app",
    ],
)

rabbitmq_suite(
    name = "rabbit_fifo_prop_SUITE",
    size = "large",
    additional_beam = [
        ":test_test_util_beam",
    ],
    deps = [
        "//deps/rabbit_common:erlang_app",
        "@meck//:erlang_app",
        "@proper//:erlang_app",
        "@ra//:erlang_app",
    ],
)

rabbitmq_suite(
    name = "rabbit_fifo_dlx_SUITE",
    size = "small",
    deps = [
        "//deps/rabbit_common:erlang_app",
    ],
)

rabbitmq_suite(
    name = "rabbit_fifo_q_SUITE",
    size = "small",
    deps = [
        "//deps/rabbit_common:erlang_app",
        "@proper//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "rabbit_fifo_dlx_integration_SUITE",
    size = "medium",
    additional_beam = [
        ":test_test_util_beam",
        ":test_queue_utils_beam",
        ":quorum_queue_SUITE_beam_files",
    ],
    deps = [
        "@proper//:erlang_app",
        "@ra//:erlang_app",
    ],
)

rabbitmq_suite(
    name = "rabbit_fifo_SUITE",
    size = "medium",
    additional_beam = [
        ":test_test_util_beam",
        ":rabbit_fifo_v0_SUITE_beam_files",
    ],
    deps = [
        "//deps/rabbit_common:erlang_app",
        "@meck//:erlang_app",
        "@ra//:erlang_app",
    ],
)

rabbitmq_suite(
    name = "rabbit_fifo_v0_SUITE",
    size = "medium",
    additional_beam = [
        ":test_test_util_beam",
    ],
    deps = [
        "//deps/rabbit_common:erlang_app",
        "@meck//:erlang_app",
        "@ra//:erlang_app",
    ],
)

rabbitmq_suite(
    name = "mc_unit_SUITE",
    size = "small",
    deps = [
        "//deps/amqp10_common:erlang_app",
        "//deps/rabbit_common:erlang_app",
    ],
)

rabbitmq_suite(
    name = "rabbit_stream_coordinator_SUITE",
    deps = [
        "//deps/rabbit_common:erlang_app",
    ],
)

rabbitmq_suite(
    name = "rabbit_stream_sac_coordinator_SUITE",
    runtime_deps = [
        "@meck//:erlang_app",
    ],
    deps = [
        "//deps/rabbit_common:erlang_app",
    ],
)

rabbitmq_suite(
    name = "rabbit_access_control_SUITE",
    runtime_deps = [
        "@meck//:erlang_app",
    ],
    deps = [
        "//deps/rabbit_common:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "rabbit_stream_queue_SUITE",
    size = "large",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
    shard_count = 20,
    deps = [
        "@proper//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "rabbitmq_4_0_deprecations_SUITE",
    size = "large",
)

rabbitmq_integration_suite(
    name = "rabbitmq_queues_cli_integration_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "rabbitmqctl_integration_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "rabbitmqctl_shutdown_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "signal_handling_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "single_active_consumer_SUITE",
    size = "medium",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
)

rabbitmq_integration_suite(
    name = "term_to_binary_compat_prop_SUITE",
    deps = [
        "@proper//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "topic_permission_SUITE",
    size = "medium",
    additional_beam = [
        ":test_amqp_utils_beam",
    ],
    runtime_deps = [
        "//deps/rabbitmq_amqp_client:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "transactions_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "unit_access_control_authn_authz_context_propagation_SUITE",
    size = "medium",
    additional_beam = [
        "test/rabbit_auth_backend_context_propagation_mock.beam",
        "test/rabbit_foo_protocol_connection_info.beam",
    ],
)

rabbitmq_integration_suite(
    name = "unit_access_control_credential_validation_SUITE",
    size = "medium",
    deps = [
        "@proper//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "unit_access_control_SUITE",
    size = "medium",
    additional_beam = [
        "test/rabbit_dummy_protocol_connection_info.beam",
    ],
)

rabbitmq_suite(
    name = "unit_amqp091_content_framing_SUITE",
    size = "small",
    deps = [
        "//deps/rabbit_common:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "unit_amqp091_server_properties_SUITE",
    size = "medium",
)

rabbitmq_suite(
    name = "unit_quorum_queue_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "unit_app_management_SUITE",
    size = "medium",
)

rabbitmq_suite(
    name = "unit_cluster_formation_locking_mocks_SUITE",
    size = "small",
    deps = [
        "@meck//:erlang_app",
    ],
)

rabbitmq_suite(
    name = "unit_cluster_formation_sort_nodes_SUITE",
    size = "small",
    deps = [
        "@meck//:erlang_app",
    ],
)

rabbitmq_suite(
    name = "unit_collections_SUITE",
    size = "small",
    deps = [
        "//deps/rabbit_common:erlang_app",
    ],
)

rabbitmq_suite(
    name = "unit_config_value_encryption_SUITE",
    size = "medium",
    deps = [
        "//deps/rabbit_common:test_erlang_app",
        "//deps/rabbitmq_prelaunch:test_erlang_app",
        "@credentials_obfuscation//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "unit_connection_tracking_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "unit_credit_flow_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "unit_disk_monitor_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "unit_file_handle_cache_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "unit_gen_server2_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "unit_log_management_SUITE",
    size = "medium",
)

rabbitmq_suite(
    name = "unit_msg_size_metrics_SUITE",
    size = "small",
)

rabbitmq_suite(
    name = "unit_operator_policy_SUITE",
    size = "small",
    deps = [
        "//deps/rabbit_common:test_erlang_app",
    ],
)

rabbitmq_suite(
    name = "unit_pg_local_SUITE",
    size = "small",
)

rabbitmq_suite(
    name = "unit_plugin_directories_SUITE",
    size = "small",
    deps = [
        "//deps/rabbit_common:test_erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "unit_plugin_versioning_SUITE",
    size = "small",
)

rabbitmq_integration_suite(
    name = "unit_policy_validators_SUITE",
    size = "small",
)

rabbitmq_suite(
    name = "unit_priority_queue_SUITE",
    size = "small",
)

rabbitmq_suite(
    name = "unit_queue_consumers_SUITE",
    size = "small",
)

rabbitmq_suite(
    name = "unit_queue_location_SUITE",
    size = "small",
    deps = [
        "@meck//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "unit_stats_and_metrics_SUITE",
    size = "medium",
    additional_beam = [
        "test/dummy_event_receiver.beam",
    ],
)

rabbitmq_suite(
    name = "unit_supervisor2_SUITE",
    size = "small",
    additional_beam = [
        "test/dummy_supervisor2.beam",
    ],
)

rabbitmq_integration_suite(
    name = "unit_vm_memory_monitor_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "upgrade_preparation_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "vhost_SUITE",
    size = "medium",
    additional_beam = [
        "test/test_rabbit_event_handler.beam",
    ],
)

rabbitmq_integration_suite(
    name = "direct_exchange_routing_v2_SUITE",
    size = "medium",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
)

rabbitmq_integration_suite(
    name = "rabbit_local_random_exchange_SUITE",
    size = "small",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
)

rabbitmq_integration_suite(
    name = "rabbit_direct_reply_to_prop_SUITE",
    size = "medium",
    deps = [
        "@proper//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "unicode_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "exchanges_SUITE",
    size = "medium",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
)

rabbitmq_integration_suite(
    name = "bindings_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "rabbit_db_queue_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "rabbit_db_maintenance_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "rabbit_db_topic_exchange_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "rabbit_db_exchange_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "rabbit_db_binding_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "rabbit_db_msup_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "rabbit_db_policy_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "runtime_parameters_SUITE",
    size = "small",
    additional_beam = [
        "test/dummy_runtime_parameters.beam",
    ],
)

rabbitmq_integration_suite(
    name = "metadata_store_clustering_SUITE",
    size = "large",
    shard_count = 19,
    sharding_method = "case",
)

rabbitmq_integration_suite(
    name = "metadata_store_phase1_SUITE",
    size = "small",
    deps = [
        "@khepri//:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "metadata_store_migration_SUITE",
    size = "small",
)

rabbitmq_integration_suite(
    name = "routing_SUITE",
    size = "large",
)

rabbitmq_integration_suite(
    name = "cli_forget_cluster_node_SUITE",
    size = "medium",
    additional_beam = [
        ":test_clustering_utils_beam",
    ],
)

rabbitmq_integration_suite(
    name = "cluster_minority_SUITE",
    size = "medium",
    additional_beam = [
        ":test_clustering_utils_beam",
    ],
)

rabbitmq_integration_suite(
    name = "cluster_upgrade_SUITE",
    size = "medium",
    additional_beam = [
        ":test_queue_utils_beam",
    ],
)

rabbitmq_integration_suite(
    name = "amqp_client_SUITE",
    size = "large",
    additional_beam = [
        ":test_amqp_utils_beam",
        ":test_event_recorder_beam",
    ],
    shard_count = 3,
    runtime_deps = [
        "//deps/rabbitmq_amqp_client:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "amqp_filtex_SUITE",
    additional_beam = [
        ":test_amqp_utils_beam",
    ],
    runtime_deps = [
        "//deps/rabbitmq_amqp_client:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "amqp_proxy_protocol_SUITE",
    size = "medium",
)

rabbitmq_integration_suite(
    name = "amqp_system_SUITE",
    flaky = True,
    shard_count = 2,
    tags = [
        "dotnet",
    ],
    test_env = {
        "TMPDIR": "$TEST_TMPDIR",
    },
)

rabbitmq_integration_suite(
    name = "amqp_auth_SUITE",
    additional_beam = [
        ":test_amqp_utils_beam",
        ":test_event_recorder_beam",
    ],
    shard_count = 2,
    runtime_deps = [
        "//deps/rabbitmq_amqp_client:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "amqp_address_SUITE",
    additional_beam = [
        ":test_amqp_utils_beam",
    ],
    shard_count = 2,
    runtime_deps = [
        "//deps/rabbitmq_amqp_client:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "amqp_credit_api_v2_SUITE",
    runtime_deps = [
        "//deps/rabbitmq_amqp_client:erlang_app",
    ],
)

rabbitmq_integration_suite(
    name = "amqpl_direct_reply_to_SUITE",
)

assert_suites()

filegroup(
    name = "manpages",
    srcs = glob([
        "docs/*.1",
        "docs/*.2",
        "docs/*.3",
        "docs/*.4",
        "docs/*.5",
        "docs/*.6",
        "docs/*.7",
        "docs/*.8",
        "docs/*.9",
    ]),
)

genrule(
    name = "manpages-dir",
    srcs = [":manpages"],
    outs = ["manpages.tar"],
    cmd = """set -euo pipefail

DESTDIR=share/man
mkdir -p $${DESTDIR}
for mp in $(SRCS); do
    section=$${mp##*.}
    mkdir -p $${DESTDIR}/man$$section
    gzip < $$mp \\
        > $${DESTDIR}/man$$section/$$(basename $$mp).gz
done
tar -cf $@ share
rm -dr share
""",
    visibility = ["//visibility:public"],
)

genrule(
    name = "web-manpages",
    srcs = [":manpages"],
    outs = ["web-manpages.tar"],
    cmd = """set -euo pipefail

mkdir web-manpages-tmp
for mp in $(SRCS); do
    d=web-manpages-tmp/$$(basename $${mp}).html
    echo "Converting $$mp to $$d..."
    mandoc -T html -O 'fragment,man=%N.%S.html' "$$mp" | \\
        awk '\\
        /^<table class="head">$$/ { remove_table=1; next; } \\
        /^<table class="foot">$$/ { remove_table=1; next; } \\
        /^<\\/table>$$/ { if (remove_table) { remove_table=0; next; } } \\
        { if (!remove_table) { \\
            line=$$0; \\
            gsub(/<h2/, "<h3", line); \\
            gsub(/<\\/h2>/, "</h3>", line); \\
            gsub(/<h1/, "<h2", line); \\
            gsub(/<\\/h1>/, "</h2>", line); \\
            gsub(/class="D1"/, "class=\"D1 lang-bash\"", line); \\
            gsub(/class="Bd Bd-indent"/, "class=\"Bd Bd-indent lang-bash\"", line); \\
            gsub(/&#[xX]201[cCdD];/, "\\&quot;", line); \\
            print line; \\
        } } \\
        ' > "$$d"
done
tar --strip-components 1 -cf $@ web-manpages-tmp/*
rm -dr web-manpages-tmp
""",
    visibility = ["//visibility:public"],
)

alias(
    name = "rabbit",
    actual = ":erlang_app",
    visibility = ["//visibility:public"],
)

eunit(
    name = "eunit",
    compiled_suites = [
        ":test_channel_operation_timeout_test_queue_beam",
        ":test_dummy_event_receiver_beam",
        ":test_dummy_interceptor_beam",
        ":test_dummy_runtime_parameters_beam",
        ":test_dummy_supervisor2_beam",
        ":test_failing_dummy_interceptor_beam",
        ":test_mirrored_supervisor_SUITE_gs_beam",
        ":test_queue_utils_beam",
        ":test_rabbit_auth_backend_context_propagation_mock_beam",
        ":test_rabbit_dummy_protocol_connection_info_beam",
        ":test_rabbit_foo_protocol_connection_info_beam",
        ":test_test_util_beam",
        ":test_test_rabbit_event_handler_beam",
        ":test_clustering_utils_beam",
        ":test_event_recorder_beam",
        ":test_rabbit_ct_hook_beam",
        ":test_amqp_utils_beam",
    ],
    target = ":test_erlang_app",
    test_env = {
        "COVERDATA_TO_LCOV_APPS_DIRS": "deps:deps/rabbit/apps",
    },
)
