ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-MCP/t/c13_vim_stdio.t
Revision: 1.1
Committed: Sat Nov 7 20:37:08 2026 UTC (2 weeks, 5 days ago) by root
Content type: application/x-troff
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 use common::sense;
2     use Test::More;
3     use JSON::XS ();
4     use IPC::Open3 ();
5     use IO::Select ();
6     use IO::Socket::INET ();
7     use POSIX qw(WNOHANG);
8     use Time::HiRes ();
9     use File::Temp ();
10     use File::Path ();
11     use Symbol ();
12    
13     my $dir = File::Temp::tempdir (CLEANUP => 1);
14     File::Path::mkpath (["$dir/home", "$dir/config"], 0, 0700);
15     local $ENV{HOME} = "$dir/home";
16     local $ENV{XDG_CONFIG_HOME} = "$dir/config";
17    
18     sub free_port {
19     my $socket = IO::Socket::INET->new (
20     LocalAddr => "127.0.0.1", LocalPort => 0,
21     Proto => "tcp", Listen => 1,
22     ) or die "cannot allocate TCP test port: $!";
23     my $port = $socket->sockport;
24     close $socket;
25     $port
26     }
27    
28     sub start_child {
29     my ($use_env) = @_;
30     my $port = free_port;
31     local $ENV{PERL_ANYEVENT_MCP_MULTIBROKER_PROTOCOL} = $use_env ? "stdio" : "";
32     my $err = Symbol::gensym;
33     my $pid = IPC::Open3::open3 (my $in, my $out, $err,
34     $^X, "-Ilib", "-e",
35     'unshift @INC, sub { die "WebSocket must not be loaded\n" if $_[1] =~ m{^AnyEvent/WebSocket/}; return }; do shift @ARGV; die $@ if $@;',
36     "bin/mcp-vim", "--bind", "127.0.0.1:$port", ($use_env ? () : ("--stdio")));
37     my $old = select $in;
38     $| = 1;
39     select $old;
40     ($pid, $in, $out, $err, $port)
41     }
42    
43     sub rpc {
44     my ($in, $out, $id, $method, $params) = @_;
45     local $SIG{PIPE} = "IGNORE";
46     print {$in} JSON::XS::encode_json ({
47     jsonrpc => "2.0", id => $id, method => $method, params => $params,
48     }), "\n" or return;
49     return unless IO::Select->new ($out)->can_read (30);
50     my $line = <$out>;
51     return unless defined $line;
52     eval { JSON::XS::decode_json $line }
53     }
54    
55     sub wait_exit {
56     my ($pid) = @_;
57     for (1 .. 1500) {
58     my $w = waitpid ($pid, WNOHANG);
59     return $? >> 8 if $w == $pid;
60     Time::HiRes::sleep (0.02);
61     }
62     kill TERM => $pid;
63     waitpid ($pid, 0);
64     -1
65     }
66    
67     for my $use_env (0, 1) {
68     my $kind = $use_env ? "multibroker environment" : "explicit --stdio";
69     my ($pid, $in, $out, $err, $port) = start_child ($use_env);
70    
71     my $init = rpc ($in, $out, 1, "initialize", {
72     protocolVersion => "2025-11-25",
73     capabilities => {}, clientInfo => { name => "stdio-test", version => "0" },
74     });
75     unless ($init) {
76     close $in;
77     my $exit = wait_exit ($pid);
78     my $stderr = do { local $/; <$err> };
79     diag "$kind: mcp-vim child exit=$exit, stderr: " . ($stderr // "");
80     fail "$kind: JSONL initialize response";
81     close $out;
82     close $err;
83     next;
84     }
85    
86     is $init->{id}, 1, "$kind: JSONL initialize response";
87     ok !$init->{error} && $init->{result}{serverInfo},
88     "$kind: valid MCP server metadata";
89    
90     my $tools = rpc ($in, $out, 2, "tools/list", {});
91     is $tools->{id}, 2, "$kind: JSONL tools/list response";
92     is_deeply [sort map $_->{name}, @{$tools->{result}{tools} || []}],
93     [sort qw(targets state open read-lines grep apply-diff substitute undo redo save set-cursor ex meta-instructions)],
94     "$kind: exposes thirteen unprefixed Vim tools including metadata";
95     my ($substitute) = grep $_->{name} eq "substitute", @{$tools->{result}{tools} || []};
96     ok defined $substitute && index ($substitute->{description}, q{\%^\_.*\%$}) >= 0,
97     "$kind: substitute description preserves Vim regex backslashes and end anchor";
98     ok !$tools->{error},
99     "$kind: tools/list returned without an error";
100    
101     # The stdio transport must not prevent the normal Vim/browser TCP listener.
102     my $sock;
103     for (1 .. 1500) {
104     $sock = IO::Socket::INET->new (
105     PeerAddr => "127.0.0.1", PeerPort => $port,
106     Proto => "tcp", Timeout => 1,
107     );
108     last if $sock;
109     Time::HiRes::sleep (0.02);
110     }
111     ok $sock, "$kind: TCP listener remains available";
112     if ($sock) {
113     $sock->autoflush (1);
114     print {$sock}
115     "POST / HTTP/1.1\r\nHost: localhost\r\n"
116     . "Content-Type: application/json\r\nContent-Length: 2\r\n\r\n{}";
117     my $response = IO::Select->new ($sock)->can_read (30) ? <$sock> : undef;
118     like $response, qr{\AHTTP/1\.1 401\b},
119     "$kind: TCP listener still serves authenticated MCP HTTP";
120     close $sock;
121     } else {
122     fail "$kind: HTTP listener response";
123     }
124    
125     close $in;
126     is wait_exit ($pid), 0, "$kind: stdin EOF stops the child cleanly";
127     my $extra = <$out>;
128     ok !defined $extra, "$kind: no extraneous stdout after final MCP response";
129     close $out;
130     close $err;
131     }
132    
133     done_testing;