#!/bin/sh
set -e

# Smoke test: exercise the two offline CLI surfaces that matter to users and
# automation, without needing a database server.

out=$(/usr/bin/mysqltuner --help 2>&1)
echo "$out" | grep -qi 'MySQLTuner' || {
    echo "FAIL: --help output does not mention MySQLTuner" >&2
    exit 1
}

tmp=$(mktemp)
trap 'rm -f "$tmp"' EXIT

/usr/bin/mysqltuner --list-indicators > "$tmp"

perl -MJSON::PP -e '
    local $/;
    my $d = decode_json(<STDIN>);
    die "top-level value is not an object\n" unless ref $d eq "HASH";
    die "indicators missing or too few\n"
        unless ref $d->{indicators} eq "ARRAY" && @{ $d->{indicators} } > 100;
    die "cli_options missing or too few\n"
        unless ref $d->{cli_options} eq "HASH" && keys %{ $d->{cli_options} } > 10;
    die "version missing\n"
        unless defined $d->{version} && $d->{version} =~ /^[0-9]+\.[0-9]+/;
    print "indicators: ", scalar @{ $d->{indicators} }, ", version: $d->{version}\n";
' < "$tmp"

echo "smoke test ok"
