AnyEvent-MCP

 view release on metacpan or  search on metacpan

t/c02_config.t  view on Meta::CPAN

ok $http{host} eq "127.0.0.1" && $http{port} == 43002, "full CLI bind overrides named bind";
%http = $config->http_settings ("demo", ":43003");
ok $http{host} eq "127.0.0.1" && $http{port} == 43003, "port-only bind defaults to loopback";
%http = $config->http_settings ("demo", "localhost:43005");
ok $http{url_host} eq "localhost" && $http{port} == 43005
   && ($http{host} eq "127.0.0.1" || $http{host} eq "::1"), "hostname bind resolves to numeric address";
ok $config->http_url ("demo", "127.0.0.1:43004", path => "/") eq
   "http://127.0.0.1:43004/" . $config->user_credentials ("demo")->[0][2] . "/",
   "printable URL uses effective settings and stable token";
eval { $config->http_url ("demo", ":0", path => "/") };
ok $@ =~ /port is not known/, "port zero cannot yield an offline URL";
my $special = "user\@doom.example+test";
ok AnyEvent::MCP::instance_name ($special) eq $special, "instance names accept email-like characters";
$config->{section}{"instance $special"}{bind} = "127.0.0.1:43006";
my %special_http = $config->http_settings ($special);
ok $special_http{port} == 43006 && $special_http{host} eq "127.0.0.1", "email-like name selects INI section";
ok $config->user_credentials ($special)->[0][2] ne $config->user_credentials ("user")->[0][2],
   "email-like name derives distinct credentials";
my $punct = ".@+-";
ok !(grep { !eval { AnyEvent::MCP::instance_name ("x${_}y"); 1 } } split //, $punct),
   "small unquoted-shell-friendly punctuation set is allowed";
ok AnyEvent::MCP::instance_name ("2test") eq "2test"
   && AnyEvent::MCP::instance_name ("_private") eq "_private", "leading digit or underscore is valid";
ok !(grep { eval { AnyEvent::MCP::instance_name ($_); 1 } }
   ("\@host", ".hidden", "bad/name", "bad!name", "bad%name", "bad=expr", "bad^name", "bad#name", "bad`name")),
   "invalid leading punctuation, path separators and shell metacharacters rejected";
for my $name ("bad name", "bad\\name", "bad:colon", "bad\"quote") {
   eval { AnyEvent::MCP::instance_name ($name) };
   ok $@ =~ /Invalid MCP server name/, "invalid instance name $name rejected";
}
eval { $config->http_settings ("demo", ":65536") };
ok $@ =~ /Invalid MCP bind/, "invalid explicit bind ports are rejected";
my ($n, $b, $u, @tokens);
my %getopt = AnyEvent::MCP::Config::http_getopt (\$n, \$b, \$u, \@tokens);
ok $getopt{"name=s"} == \$n && $getopt{"bind=s"} == \$b && $getopt{"print-url|print-urls:s"} == \$u
   && $getopt{"token=s@"} == \@tokens, "shared Getopt options refer directly to caller lexicals";

ok $config->{section}{"mcp-fs"}{utility} eq "/path with spaces/tool", "arbitrary section and spaces";
ok $config->{section}{"mcp-fs"}{exports} eq "/one, /two", "uninterpreted list value";
ok $config->{section}{"mcp-fs"}{literal} eq "value;#still-value", "literal comment characters";

%http = $config->http_settings ("agni");
ok join (",", @{$http{tokens}}) eq "first,second", "named tokens preserve order";
ok $config->http_url ("agni", "127.0.0.1:43004", path => "/") eq
   "http://127.0.0.1:43004/first/", "named URL uses first token";
ok $config->http_url ("agni", "127.0.0.1:43004", path => "/", tokens => ["override"]) eq
   "http://127.0.0.1:43004/override/", "explicit tokens override named URL token";

chmod 0644, $file or die $!;
eval { AnyEvent::MCP::Config->new (file => $file)->load };
ok !$@, "user-selected configuration permissions are accepted";

sub command {
   my @args = @_;
   my $err = Symbol::gensym;
   my $pid = IPC::Open3::open3 (my $in, my $out, $err,
      $^X, "-Ilib", "bin/anyevent-mcp", "--config", $file, @args);
   close $in;
   local $/;
   my $stdout = <$out>;
   my $stderr = <$err>;
   waitpid $pid, 0;

   ($? >> 8, $stdout, $stderr)
}

my ($status, $output) = command ("password", "agni", "alice");
ok $status, "Basic password command is removed";
($status, $output) = command ("token", "agni");
ok $status && !$output, "token command removed";
($status, $output) = command ("url", "agni");
ok $status && !$output, "URL command removed";
($status, $output) = command ("--help");
ok !$status && $output =~ /Usage: anyevent-mcp/, "help command";
($status, $output) = command ("unknown");
ok $status, "invalid command";
($status, $output) = command ("init");
ok !$status, "init command idempotently ensures existing configuration";

{
   local $ENV{HOME};
   local $ENV{XDG_CONFIG_HOME};
   ok (AnyEvent::MCP::Config->new (file => $file)->load->credential ("token", "agni", "alice") eq $token,
      "explicit config needs no HOME");
}

ok !Pod::Checker::podchecker ("lib/AnyEvent/MCP/Config.pm"), "configuration POD";
ok !Pod::Checker::podchecker ("bin/anyevent-mcp"), "utility POD";
($status, $output) = command ("cookie", "agni");
ok $status && !$output, "cookie command removed";

ok length (AnyEvent::MCP::random_bytes 64) == 64, "random byte count";
ok AnyEvent::MCP::random_bytes (0) eq "", "zero random bytes";
eval { AnyEvent::MCP::random_bytes (-1) };
ok $@, "invalid random byte count";

# verify the framed application/program/instance domains against direct HMAC.
my $master = pack "H*", $config->{section}{global}{secret};
my $expected_token = MIME::Base64::encode_base64 (
   substr (Digest::SHA::hmac_sha512 (
      (pack "(N/a*)*", "anyevent-mcp", "token", AnyEvent::MCP::Config::program_name (), "agni", "alice") . "\1", $master
   ), 0, 32), ""
);
$expected_token =~ y!+/=!-_!d;
ok $token eq $expected_token, "credential context contains namespace, purpose, program, instance and username";
ok $port == 16384 + ((unpack "n", Digest::SHA::hmac_sha512 (
   (pack "(N/a*)*", "anyevent-mcp", "port", AnyEvent::MCP::Config::program_name (), "demo") . "\1", $master
)) & 16383), "port context contains namespace, purpose, program and instance";

ok AnyEvent::MCP::instance_name (undef) eq "default", "omitted instance name is default";
ok AnyEvent::MCP::instance_name ("default") eq AnyEvent::MCP::instance_name (undef),
   "explicit and omitted default names agree";
eval { AnyEvent::MCP::Config::user_name ("foo\@bar") };
ok $@ =~ /Invalid MCP user name/, "managed usernames keep strict grammar";
{
   local $0 = "/some/path/mcp-fs.pl";
   ok AnyEvent::MCP::Config::program_name () eq "mcp-fs", "program identity uses invoked basename without Perl suffix";
   my $fs_token = $config->credential ("token", "default");
   local $0 = "/some/path/mcp-vim";
   ok $fs_token ne $config->credential ("token", "default"), "default credentials differ across programs";
}
{
   local $0 = "/some/path/program-one";
   my $one = $config->server_port ("default");



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