#!/usr/bin/env perl
use strict;
use warnings;
no warnings 'once';
use Test::More;
use Data::Dumper;

# 1. Load MySQLTuner logic
require './mysqltuner.pl';
require './tests/MySQLTuner/TestHelper.pm';

# Mocking essential globals
$main::good = '[OK]';
$main::bad  = '[!!]';
$main::info = '[--]';
$main::deb  = '[DG]';
$main::end  = '';
our %myvar;
our %mystat;
our %mycalc;
our @adjvars;

# Test Issue #881: join_buffer_size recommendation formatting
subtest 'Issue #881: join_buffer_size formatting' => sub {
    # Case 1: join_buffer_size < 4MB
    @main::adjvars = ();
    MySQLTuner::TestHelper::reset_state();
    %main::myvar = ( %main::myvar,  join_buffer_size => 256 * 1024 ); # 256KB
    %main::mycalc = ( %main::mycalc,  joins_without_indexes_per_day => 300, joins_without_indexes => 1000 );
    %main::mystat = ( %main::mystat, );
    
    # We need to mock subheaderprint and badprint to avoid output during tests
    no warnings 'redefine';
    local *main::subheaderprint = sub { };
    local *main::badprint = sub { };
    local *main::goodprint = sub { };
    local *main::infoprint = sub { };
    local *main::push_recommendation = sub { };
    
    # Call the logic part manually (extracting from calculations sub)
    # Since we can't easily call 'calculations' without a full setup, 
    # we just verify the logic we modified.
    
    if ( $main::mycalc{'joins_without_indexes_per_day'} > 250 ) {
        if ( $main::myvar{'join_buffer_size'} < 4 * 1024 * 1024 ) {
            push( @main::adjvars,
                    "join_buffer_size (> "
                  . main::hr_bytes( $main::myvar{'join_buffer_size'} )
                  . ", or always use indexes with JOINs)" );
        }
        else {
            push( @main::adjvars, "join_buffer_size (always use indexes with JOINs)" );
        }
    }
    
    like($main::adjvars[0], qr/join_buffer_size \(> 256\.0K, or always use indexes with JOINs\)/, 'Format correct for < 4MB');

    # Case 2: join_buffer_size >= 4MB
    @main::adjvars = ();
    $main::myvar{'join_buffer_size'} = 8 * 1024 * 1024; # 8MB
    
    if ( $main::mycalc{'joins_without_indexes_per_day'} > 250 ) {
        if ( $main::myvar{'join_buffer_size'} < 4 * 1024 * 1024 ) {
            push( @main::adjvars,
                    "join_buffer_size (> "
                  . main::hr_bytes( $main::myvar{'join_buffer_size'} )
                  . ", or always use indexes with JOINs)" );
        }
        else {
            push( @main::adjvars, "join_buffer_size (always use indexes with JOINs)" );
        }
    }
    is($main::adjvars[0], "join_buffer_size (always use indexes with JOINs)", 'Format correct for >= 4MB');
};

# Test Issue #887: Suppress SSL DISABLED warning
subtest 'Issue #887: SSL DISABLED warning suppression' => sub {
    # Mock backticks or the part that executes the command
    # Actually we can test execute_system_command if we mock the backticks.
    # But Perl backticks are hard to mock without modules.
    # We can test the grep logic directly.
    
    my @input_output = (
        "mysql: [Warning] option 'ssl': boolean value 'DISABLED' wasn't recognized. Set to OFF.\n",
        "some valid output\n",
        "another line\n"
    );
    
    my @filtered = grep { !/boolean value 'DISABLED' wasn't recognized/ } @input_output;
    
    is(scalar @filtered, 2, 'Warning line filtered out');
    ok(!grep(/boolean value 'DISABLED'/, @filtered), 'Warning not present in filtered output');
};

done_testing();
