AnyEvent-MCP

 view release on metacpan or  search on metacpan

t/c18_form.t  view on Meta::CPAN

use common::sense;
use Test::More;
use AnyEvent::Util ();
use AnyEvent::MCP ();
use File::Temp ();
use JSON::XS ();
use POSIX ();

my $dir = File::Temp::tempdir (CLEANUP => 1);
my $json = JSON::XS->new->canonical;
sub write_file {
   my ($path, $text) = @_;
   open my $fh, ">", $path or die "$path: $!";
   print {$fh} $text;
   close $fh or die "$path: $!";
}
write_file "$dir/yad", "#!$^X\n" . q{
use common::sense;
use JSON::XS ();
local $/;
open my $fh, "<", $ENV{MCP_TEST_FORM_QUEUE} or die $!;
my $queue = JSON::XS::decode_json <$fh>;
close $fh;
my $reply = shift @$queue or die "Unexpected form";
open $fh, ">", $ENV{MCP_TEST_FORM_QUEUE} or die $!;
print {$fh} JSON::XS::encode_json $queue;
close $fh;
utf8::decode ($_) for @ARGV;
open $fh, ">>", $ENV{MCP_TEST_FORM_LOG} or die $!;
print {$fh} JSON::XS::encode_json (\@ARGV), "\n";
close $fh;
my @fields = grep /\A--field=/, @ARGV;
my @quoted = map {
   my $value = $_;
   my $field = shift @fields;
   $value =~ s/([\x09\x0a\x5c])/chr (92) . (ord $1 == 10 ? "n" : ord $1 == 9 ? "t" : chr (92))/ge
      if $field =~ /:TXT\z/;
   $value =~ s/'/chr (39) . chr (92) . chr (39) . chr (39)/ge;
   "'$value'"
} @{$reply->{values} || []};
my $output = join " ", @quoted;
utf8::encode $output;
print $output, "\n";
exit $reply->{status};
};
chmod 0700, "$dir/yad" or die $!;
local $ENV{PATH} = "$dir:$ENV{PATH}";
local $ENV{MCP_TEST_FORM_QUEUE} = "$dir/queue";
local $ENV{MCP_TEST_FORM_LOG} = "$dir/log";

sub run_form {
   my ($params, $queue, @options) = @_;

   write_file "$dir/queue", $json->encode ($queue || []);
   my ($output, $error);
   my $status = AnyEvent::Util::run_cmd (
      [$^X, "-Ilib", "-MPOSIX", "-e",
         'defined POSIX::setsid () or die $!; do shift @ARGV; die $@ if $@;',
         "./bin/anyevent-mcp-form-elicitation", @options],
      "<" => \(JSON::XS::encode_json $params), ">" => \$output, "2>" => \$error,
   )->recv;
   ($status, length $output ? JSON::XS::decode_json ($output) : undef, $error)
}
sub dialogs {
   open my $fh, "<", "$dir/log" or die $!;
   my @dialogs = map JSON::XS::decode_json ($_), <$fh>;
   close $fh;
   @dialogs
}

my $form = {
   mode => "form", message => "A real question\nwith a second line",
   requestedSchema => {
      type => "object", required => ["a_choice", "b_text", "c_number", "d_boolean"],
      properties => {
         a_choice => { type => "string", oneOf => [
            { const => "literal!\x1f\n", title => "Selected: !" },
            { const => "other", title => "Other" },
         ], default => "other" },
         b_text => { type => "string", title => "Answer:FBTN", default => "--command=not-an-option", maxLength => 4096 },
         c_number => { type => "number", minimum => 0, maximum => 10, default => 5 },
         d_boolean => { type => "boolean", default => JSON::XS::false },
         e_optional => { type => "integer" },
      },
   },
};
my $text = "first line\nsecond 'quoted' \\ line\x1f\x{3c0}\tC:\\tmp\\maps literal \\n and \\t";
my ($status, $response, $error) = run_form ($form, [{
   status => 2, values => ["2", $text, "3.5", "3", ""],
}]);
is $status, 0, "form helper returns a valid response";
is $error, "", "successful form has no diagnostics";
is_deeply $response, {
   action => "accept", content => {
      a_choice => "literal!\x1f\n", b_text => $text, c_number => 3.5, d_boolean => JSON::XS::true,
   },
}, "values preserve enum identity, multiline Unicode, quotes, backslashes and separator characters";
ok JSON::XS::is_bool ($response->{content}{d_boolean}), "boolean answer retains its JSON type";
unlike $json->encode ($response), qr/"c_number":"3.5"/, "numeric answer is a JSON number";
{
   my ($args) = dialogs;
   ok +(grep $_ eq "--quoted-output", @$args), "form requests shell-quoted output";



( run in 0.693 second using v1.01-cache-2.11-cpan-1bcd28dbaf1 )