App-Dapper

 view release on metacpan or  search on metacpan

t/52-mcp-protocol.t  view on Meta::CPAN

    is($lines->[2], '{"error":{"code":-32600,"message":"Invalid Request"},"id":null,"jsonrpc":"2.0"}', 'a JSON array is an invalid request');
    is($lines->[3], $lines->[2], 'and so is a JSON string');
    is($lines->[4], '{"error":{"code":-32601,"message":"Method not found: nope"},"id":"x","jsonrpc":"2.0"}', 'an unknown method');
    is($lines->[5], '{"id":2,"jsonrpc":"2.0","result":{}}', 'the server carries on after errors');
    is($err, '', 'nothing on standard error');

    is(JSON->new->decode($lines->[2])->{error}->{code}, -32600, 'an array is an invalid request');
};

subtest 'serve: UTF-8 in and out' => sub {
    make_site({ '_source/index.md' => "---\ntitle: Home\n---\nHome.\n" });
    my $s = App::Dapper->new();
    my $request = $json->encode({ jsonrpc => '2.0', id => 1, method => 'tools/call',
        params => { name => 'write_page', arguments => { source => 'café.md', front_matter => { title => 'Crème brûlée' }, body => 'Délicieux — 美味しい' } } });
    my $read = $json->encode({ jsonrpc => '2.0', id => 2, method => 'tools/call', params => { name => 'read_page', arguments => { source => 'café.md' } } });
    my ($lines, $err, $raw) = serve_lines($s, "$request\n$read\n");
    is(scalar @$lines, 2, 'two responses');
    ok(utf8::decode(my $copy = $raw), 'the output is valid UTF-8');
    my $written = $json->decode($lines->[0]);
    ok(!$written->{result}->{isError}, 'written') or diag explain $written;
    my $page = $json->decode($lines->[1])->{result}->{structuredContent};
    is($page->{front_matter}->{title}, 'Crème brûlée', 'the title survives the round trip');
    is($page->{body}, "Délicieux — 美味しい\n", 'and the body');
    like($raw, qr/Cr\xc3\xa8me/, 'non-ASCII is sent as UTF-8 bytes, not escapes');
};

subtest 'serve: what tools print goes to standard error' => sub {
    make_site({ '_source/index.md' => "---\ntitle: Home\n---\nHome.\n" });
    my $s = App::Dapper->new();
    no warnings 'redefine';
    local *App::Dapper::MCP::site_info = sub { print "noise on standard output\n"; return { quiet => 1 } };
    my ($lines, $err) = serve_lines($s, qq({"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"site_info"}}\n));
    is(scalar @$lines, 1, 'only the response is on standard output');
    is($json->decode($lines->[0])->{result}->{structuredContent}->{quiet}, 1, 'and it is the response');
    is($err, "noise on standard output\n", 'the noise went to standard error');
};

subtest 'serve: input without a final newline, and an empty input' => sub {
    make_site({ '_source/index.md' => "---\ntitle: Home\n---\nHome.\n" });
    my $s = App::Dapper->new();
    my ($lines) = serve_lines($s, '{"jsonrpc":"2.0","id":1,"method":"ping"}');
    is_deeply($lines, ['{"id":1,"jsonrpc":"2.0","result":{}}'], 'the last line is answered');
    ($lines) = serve_lines($s, '');
    is_deeply($lines, [], 'nothing in, nothing out');
};

subtest 'dapper mcp as a process' => sub {
    make_site({ '_source/index.md' => "---\ntitle: Home\n---\nHome.\n" });
    my $dir = getcwd();
    my $input = join("\n",
        '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2025-03-26"}}',
        '{"jsonrpc":"2.0","method":"notifications/initialized"}',
        '{"jsonrpc":"2.0","id":2,"method":"tools/list"}',
        'garbage',
        '{"jsonrpc":"2.0","id":3,"method":"tools/call","params":{"name":"build","arguments":{}}}',
        '{"jsonrpc":"2.0","id":4,"method":"resources/read","params":{"uri":"dapper://config"}}',
    ) . "\n";
    spew("$io/requests", $input, ':raw');
    my $other = tempdir(CLEANUP => 1);
    chdir $other;
    my $out = `"$^X" "-I$lib" "$dapper_bin" -C "$dir" mcp < "$io/requests" 2>"$io/stderr"`;
    my $status = $? >> 8;
    chdir $dir;
    is($status, 0, 'exits 0 at the end of the input');
    my @lines = split /\n/, $out;
    is(scalar @lines, 5, 'five responses');
    my @responses = map { $json->decode($_) } @lines;
    is_deeply([ map { $_->{id} } @responses ], [1, 2, undef, 3, 4], 'in order, with their ids');
    is($responses[0]->{result}->{protocolVersion}, '2025-03-26', 'initialize');
    is(scalar @{ $responses[1]->{result}->{tools} }, 9, 'tools/list');
    is($responses[2]->{error}->{code}, -32700, 'the parse error');
    ok($responses[3]->{result}->{structuredContent}->{built}, 'build');
    ok(-f "$dir/_output/index.html", 'built in the -C directory');
    like($responses[4]->{result}->{contents}->[0]->{text}, qr/name: Test Site/, 'the project file from the -C directory');
    is(slurp("$io/stderr"), '', 'a quiet build prints nothing on standard error');
};

subtest 'dapper mcp outside a project' => sub {
    make_site({}, bare => 1);
    my $dir = getcwd();
    spew("$io/requests", qq({"jsonrpc":"2.0","id":1,"method":"tools/call","params":{"name":"site_info"}}\n{"jsonrpc":"2.0","id":2,"method":"resources/read","params":{"uri":"dapper://config"}}\n), ':raw');
    my $out = `"$^X" "-I$lib" "$dapper_bin" -C "$dir" mcp < "$io/requests" 2>/dev/null`;
    my @responses = map { $json->decode($_) } split /\n/, $out;
    is(scalar @responses, 2, 'still answers');
    ok($responses[0]->{result}->{isError}, 'tools report the missing project file as an error');
    is($responses[0]->{result}->{structuredContent}->{error}, 'could not find the project file "_config.yml". Run "dapper init" to create a new project.', 'with the message');
    is($responses[1]->{result}->{contents}->[0]->{text}, '', 'the config resource is empty');
};

done_testing();



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