use v6.d;
use Test;
use Test::Output;
#test-output-verbosity(:on);

plan 10;

use RakuDoc::Render;
use RakuDoc::To::HTML-Extra;
# remove config in case of previous test failure
my $fn-io = 'rakudoc-config.raku'.IO;
$fn-io.unlink if $fn-io ~~ :e;
my @generic = RakuDoc::Processor.new.default-text-templates.keys;
my RakuDoc::To::HTML-Extra $html-extra-instance .= new;
my @html-extra = $html-extra-instance.html-templates.keys.Slip, $html-extra-instance.templates.keys.Slip;

ok @generic (<=) @html-extra, 'HTML redefines all the keys in generic';
cmp-ok (@html-extra (-) @generic), '(==)', <VERSION footer top-of-page head-block html-root escape-code main-content favicon>, 'other templates RakuDoc::To::HTML & ::HTML-Extra additionally defines';

my RakuDoc::Processor $rdp := $html-extra-instance.rdp;
my $internet = run <ping -c 2 8.8.8.8>, :merge;
subtest 'live internet tests' => sub {
    plan :skip-all<no internet connection>
        if $internet.exitcode;
    plan 4;
    my $ast;
    my $rv;
    $ast = Q:to/QAST/.AST;
        =begin rakudoc
        =TITLE Test document
        =head Item testing

        Some text
        =end rakudoc
        QAST
    $rv = $rdp.render( $ast );
    like $rv, /
        '<title>Test document</title>'
    /, 'got simple render using online Bulma';
    $ast = Q:to/QAST/.AST;
        =begin rakudoc
        =place https://github.com/Raku/RakuDoc-GAMMA/raw/main/compliance-files/fanciful-disclaimer.txt :caption<Text placement> :headlevel(2)
        =end rakudoc
        QAST
    $rv = $rdp.render( $ast );
    like $rv, /
        'ABSOLUTELY NO WARRANTY IS IMPLIED. NOT EVEN OF ANY KIND.'
        /, 'compliance text file obtained';
    $ast = Q:to/QAST/.AST;
        =begin rakudoc

        some prior text

        =place https://github.com/Raku/RakuDoc-GAMMA/raw/main/compliance-files/bootiful-disclaimer.rakudoc :caption<RakuDoc placement> :headlevel(2)
        =end rakudoc
        QAST
    $rv = $rdp.render( $ast );
    like $rv, /
        'Not even of any kind'
        /, 'rakudoc included and rendered';
    unlike $rv, /
        'Not even of any kind' .+ 'Not even of any kind'
        /, 'rakudoc included and rendered only once';
}

# test plugin config

$fn-io.spurt: '<FontAwesome Non-existent-plugin ListFiles>';
my $out = output-from { $html-extra-instance .= new };

like $out, /
    'Plugins required' .+ 'FontAwesome'
    /, 'plugin config accepted';
unlike $out, /
    'Could not enable' .+ 'FontAwesome'
    /, 'FontAwesome was enabled';
like $out, /
    'Could not enable' .+ 'Non-existent-plugin'
    /, 'could not find Non-existent';

$fn-io.unlink if $fn-io ~~ :e;
$fn-io.spurt: q:to/CONFIG/;
    use RakuDoc::To::HTML-Extra;
    my @plugs = @HTML-Extra-plugins;
    @plugs.append: 'NonExistentPlugin';
    CONFIG
$out = output-from { $html-extra-instance .= new };

like $out, /
    'Plugins required' .+ 'FontAwesome'
    /, 'plugin config accepted';
unlike $out, /
    'Could not enable' .+ 'FontAwesome'
    /, 'FontAwesome was enabled';
like $out, /
    'Could not enable' .+ 'NonExistentPlugin'
    /, 'could not find Non-existent';

$fn-io.unlink if $fn-io ~~ :e;
$fn-io.spurt: q:to/CONFIG/;
    use RakuDoc::To::HTML-Extra;
    my @plugs = @HTML-Extra-plugins.grep({ $_ ne 'FontAwesome' });
    CONFIG
$out = output-from { $html-extra-instance .= new };

unlike $out, /
    'Plugins required' .+ 'FontAwesome'
    /, 'default plugin was disabled';

$fn-io.unlink if $fn-io ~~ :e;

done-testing;
