AnyEvent-MCP
view release on metacpan or search on metacpan
t/e11_permission_ui.t view on Meta::CPAN
print {$fh} JSON::XS::encode_json (\@ARGV), "\n";
close $fh;
if (grep $_ eq "--text-info", @ARGV) {
my ($filename) = map { /\A--filename=(.*)\z/ ? $1 : () } @ARGV;
die "Viewer needs existing file\n" unless defined $filename && -f $filename;
open my $viewer, "<", $filename or die $!;
binmode $viewer;
local $/;
my $contents = <$viewer>;
close $viewer;
open my $viewlog, ">>", $ENV{PERL_ANYEVENT_MCP_TEST_VIEW_LOG} or die $!;
print {$viewlog} JSON::XS::encode_json ({
contents_hex => unpack ("H*", $contents),
mode => ((stat $filename)[2] & 0777),
}), "\n";
close $viewlog;
}
if ($response->{click_view}) {
require Text::ParseWords;
my ($index) = grep $ARGV[$_] eq "--field=$response->{click_view}...:FBTN", 0 .. $#ARGV;
die "No View stdin form button\n" unless defined $index;
my $command = $ARGV[$index + 1];
die "No View stdin action\n" unless defined $command;
my @cmd = Text::ParseWords::shellwords ($command);
die "Invalid viewer command\n" unless @cmd && $cmd[0] eq "yad";
system { $cmd[0] } @cmd;
die "Viewer did not complete\n" if $?;
}
if (grep $_ eq "--form", @ARGV) {
for my $i (0 .. $#ARGV) {
print "\x1f" if $ARGV[$i] =~ /\A--field=.*:(?:LBL|FBTN)\z/;
}
}
print $response->{output};
exit $response->{status};
};
chmod 0700, "$dir/yad" or die $!;
local %ENV = (
%ENV, PATH => "$dir:$ENV{PATH}", DISPLAY => ":test", WAYLAND_DISPLAY => "",
PERL_ANYEVENT_MCP_TEST_UI_QUEUE => "$dir/queue",
PERL_ANYEVENT_MCP_TEST_UI_LOG => "$dir/log",
PERL_ANYEVENT_MCP_TEST_VIEW_LOG => "$dir/view_log",
);
my @prompt_options;
sub prompt {
my ($request, @responses) = @_;
write_file "$dir/queue", $json->encode (\@responses);
my ($output, $error);
my $input = AnyEvent::MCP::utf8enc ($json->encode ($request));
my $status = AnyEvent::Util::run_cmd (
[$^X, "-Ilib", "-MPOSIX", "-e",
'defined POSIX::setsid () or die $!; do shift @ARGV; die $@ if $@;',
"./bin/anyevent-mcp-askpermission", @prompt_options],
"<" => \$input, ">" => \$output, "2>" => \$error,
)->recv;
is $status, 0, "helper returns a decision successfully";
my $decision = eval { JSON::XS::decode_json $output };
my $decode_error = $@;
diag "permission helper status=", (defined $status ? $status : "undef"),
" stderr=", $json->encode ($error), " stdout=", $json->encode ($output),
" decode error=", $decode_error
if !defined $status || $status != 0 || $decode_error;
die $decode_error if $decode_error;
$decision
}
is +(AnyEvent::MCP::Server::Permissions->new)->{ask}, "helper", "approval defaults to a local helper";
my $decision = prompt $request, { status => 4, output => "Do it, wrong file\x1f\n" };
is_deeply $decision, { decision => "allow-once", sandbox_grants => ["write"] },
"allow-once uses requested grants and discards denial reason";
{
open my $log, "<", "$dir/log" or die $!;
my $args = $json->decode (scalar <$log>);
close $log;
my @labels = grep /\A--field=.*:LBL\z/, @$args;
is_deeply \@labels, ["--field=Action: write:LBL", "--field=Reason: Change the file:LBL",
"--field=File: \"/one/file\":LBL"], "request context is read-only labels";
ok !(grep /\A--field=.*:CHK\z/, @$args) && !(grep /\A--field=.*:CB\z/, @$args),
"initial YAD dialog has no grant or rule-scope selectors";
ok +(grep $_ eq "--field=Denial reason (optional)", @$args),
"denial reason appears in form above decision buttons";
is_deeply [grep /\A--button=/, @$args], [
"--button=Deny once:0", "--button=Deny always...:6",
"--button=Allow always...:8", "--button=Allow once:4",
], "one-time decisions are at the outside edges";
ok +(grep /\A--text=Required grants: write\z/, @$args),
"requested grants appear as a read-only compact line";
ok +(grep $_ eq "--focus-field=4", @$args),
"initial focus is in optional message field";
}
my $multiline = { %$request, reason => "Execute in Vim\n\necho 'hello'\x1b" };
like AnyEvent::MCP::Permissions::Ask::message ($multiline),
qr/Reason: Execute in Vim\n\necho 'hello'\\u\{1B\}/,
"MCP and terminal reasons preserve newlines and escape terminal controls";
prompt $multiline, { status => 0, output => "\x1f\n" };
{
open my $log, "<", "$dir/log" or die $!;
my $line;
$line = $_ while <$log>;
close $log;
my $args = $json->decode ($line);
ok +(grep $_ eq "--field=Reason: Execute in Vim\n\necho 'hello'\\\\u{1B}:LBL", @$args),
"YAD reason preserves line breaks and protects escaped controls from YAD decoding";
}
# Display-only input can be viewed without dismissing the approval form.
# The fake YAD "clicks" View stdin, launches the separate text-info viewer,
# then continues the original approval.
my $source = "\x{03c0}\nprint \"ok\";\0\x1b"; # Unicode plus controls
my $preview = {
%$request,
details => { "View stdin" => $source },
};
my $viewed = prompt $preview,
{ status => 4, output => "Approved after viewing\x1f\n", click_view => "View stdin" },
{ status => 0, output => "" };
is_deeply $viewed, { decision => "allow-once", sandbox_grants => ["write"] },
"separate viewer leaves original approval decision unchanged";
{
( run in 0.592 second using v1.01-cache-2.11-cpan-60884a8df76 )