AnyEvent-MCP
view release on metacpan or search on metacpan
t/e23_sctl.t view on Meta::CPAN
is_deeply read_rpc ($call_id)->{result}{structuredContent}{result}, { scripts => [] },
"business result forwarded to MCP";
my ($browser) = pair_peer ("browser", "22" x 16, "chrome-test");
$list = tool ("list")-> {result}{structuredContent}{instances};
is @$list, 2, "browser and ScriptCat remain connected together";
$call_id = ++$serial;
send_rpc $call_id, "tools/call", {
name => "call", arguments => {
instance => "chrome-test", method => "debugger.send", input => {
tabId => 7, method => "Runtime.evaluate", params => { expression => "2+2" }
}
},
};
$command = ws_recv ($browser);
is $command->{method}, "debugger.send", "CDP forwarded as opaque debugger.send RPC";
is $command->{params}{input}{params}{expression}, "2+2", "CDP parameters preserved";
ws_send $browser, { jsonrpc => "2.0", id => $command->{id}, result => { result => { value => 4 } } };
is read_rpc ($call_id)->{result}{structuredContent}{result}{result}{value}, 4,
"CDP result forwarded unchanged";
my $again = upgrade ();
my $challenge = ws_recv ($again);
my $nonceD = $challenge->{params}{nonceD};
my $nonceE = "cd" x 32;
my $instance_id = "11" x 16;
ws_send $again, {
jsonrpc => "2.0", id => $challenge->{id},
result => {
mode => "session", nonceE => $nonceE,
hmac => hmac_sha256_hex ("scriptcat-rpc-v1/instance-ext" . $instance_id . $nonceD . $nonceE, $key),
peer => { kind => "scriptcat", instanceId => $instance_id },
},
};
my $authenticated = ws_recv ($again);
is $authenticated->{method}, '$session.authenticated', "existing shared key authenticates reconnect";
is $authenticated->{params}{hmac},
hmac_sha256_hex ("scriptcat-rpc-v1/instance-daemon" . $instance_id . $nonceE . $nonceD, $key),
"reconnect mutually authenticates the daemon";
ok !exists $authenticated->{params}{key}, "session does not deliver another key";
is ws_recv ($again)->{method}, '$session.hello', "reconnected client receives hello";
ws_send $again, {
jsonrpc => "2.0", id => "rejoin", method => '$session.capabilities',
params => {
schemaVersion => "1.0.0", methods => ["scripts.list"],
peer => { name => "scriptcat-test", extensionVersion => "test" },
}
};
is ws_recv ($again)->{id}, "rejoin", "reconnected instance replaces old connection";
$list = tool ("list")-> {result}{structuredContent}{instances};
is @$list, 2, "reconnection preserves separate browser and ScriptCat peers";
my $logs = "";
while (IO::Select->new ($err)->can_read (.05)) {
my $n = sysread $err, my $chunk, 8192;
last unless $n;
$logs .= $chunk;
}
like $logs, qr/pairing window opened \(120s, 3 attempts\)/, "pairing activity logged without code";
like $logs, qr/listening on 127\.0\.0\.1:\d+ \(MCP HTTP \+ sctl WebSocket\)/, "startup log identifies listening endpoint";
like $logs, qr/list: 2 connected instances/, "instance discovery logs result count";
like $logs, qr/search: 1 matching methods/, "method discovery logs result count";
like $logs, qr/"scriptcat-test"#11111111: paired and connected \(1 methods\)/,
"connection log identifies the named extension with abbreviated instance ID";
like $logs, qr/call .*method="scripts.list"/, "generic MCP call log includes forwarded method";
like $logs, qr/"scriptcat-test"#11111111: scripts.list scripts=0 \([0-9.]+s\)/,
"successful call logs script count and elapsed time";
like $logs, qr/call .*method="debugger.send"/, "CDP operation method appears in activity log";
unlike $logs, qr/2\+2/, "arbitrary CDP expression stays out of operator log";
done_testing;
( run in 2.609 seconds using v1.01-cache-2.11-cpan-1bcd28dbaf1 )