AnyEvent-MCP

 view release on metacpan or  search on metacpan

t/e19_local_forms.t  view on Meta::CPAN

}

for my $mode (qw(tty yad)) {
   local $env{PERL_ANYEVENT_MCP_ELICITATION_HELPER} = "$dir/not-a-custom-helper";
   for my $through_broker (0, 1) {
      $client = $through_broker ? broker ($mode, {}) : standalone ($mode, {});
      $result = question $client, $through_broker ? "questions.ask-user" : "ask-user";
      is $result->{structuredContent}{answer}, "Keep", "$mode works through the selected built-in form helper";
      open my $log, "<", "$dir/modes" or die $!;
      my @modes = map JSON::XS::decode_json ($_), <$log>;
      close $log;
      is_deeply $modes[-1], ["--$mode"], "$mode is forced without consulting the custom helper";
      $client->close;
   }
}

SKIP: {
   skip "extended form helper cancellation tests (PERL_ANYEVENT_MCP_TESTS=1)", 3 unless ($ENV{PERL_ANYEVENT_MCP_TESTS} || 0) >= 1;

   $client = broker ("helper", {});
   $args->{question} = "Wait for cancellation";
   my $cancelled = AE::cv;
   my $operation = $client->call ("questions.ask-user", $args, sub { $cancelled->send ([@_]) });
   my $poll;
   wait_for (sub {
      my $done = shift;
      $poll = AE::timer 0, 0.01, sub {
         return unless -s "$dir/waiting-pid";
         undef $poll;
         $done->(1);
      };
   });
   open my $pidfile, "<", "$dir/waiting-pid" or die $!;
   my $helper_pid = <$pidfile>;
   close $pidfile;
   $client->cancel ($operation);
   my $cancel = $cancelled->recv;
   ok !$cancel->[0] && $cancel->[1]{cancelled}, "broker question cancellation reaches the caller";
   wait_for (sub {
      my $done = shift;
      $poll = AE::timer 0, 0.01, sub {
         return if kill 0, $helper_pid;
         undef $poll;
         $done->(1);
      };
   });
   ok !kill (0, $helper_pid), "broker cancels and reaps the active form helper";
   $args->{question} = "Keep or replace?";
   $result = question $client, "questions.ask-user";
   is $result->{structuredContent}{answer}, "Keep", "prompt queue remains usable after cancellation";
   $client->close;

}

# Independently running HTTP servers delegate to the broker too.
my $listener = IO::Socket::INET->new (
   LocalAddr => "127.0.0.1", LocalPort => 0, Proto => "tcp", Listen => 1,
) or die $!;
my $port = $listener->sockport;
close $listener;
my ($pid, $stdout, $stderr);
my $server;
{
   local %ENV = (%ENV, %env);
   $server = AnyEvent::Util::run_cmd (
      [$^X, "-Ilib", "bin/mcp-ask-user", "--ask", "mcp", "--bind", "127.0.0.1:$port", "--token", "form-test"],
      "<" => "/dev/null", ">" => \$stdout, "2>" => \$stderr, '$$' => \$pid,
   );
}
my $ready;
for (1 .. 1500) {
   $ready = IO::Socket::INET->new (PeerAddr => "127.0.0.1", PeerPort => $port, Proto => "tcp");
   last if $ready;
   Time::HiRes::sleep (0.02);
}
ok $ready, "standalone HTTP question server is listening";
close $ready if $ready;
$client = broker ("helper", {}, 0, "http://127.0.0.1:$port/form-test/");
$result = question $client, "questions.ask-user";
is $result->{structuredContent}{answer}, "Keep", "broker handles an independent HTTP child's form locally";
$client->close;
kill TERM => $pid if $pid;
$server->recv;

done_testing;



( run in 0.605 second using v1.01-cache-2.11-cpan-60884a8df76 )