|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +require "open3" |
| 6 | +require "json" |
| 7 | +require "timeout" |
| 8 | +require "tempfile" |
| 9 | +require "fileutils" |
| 10 | + |
| 11 | +require "readme_test_helper" |
| 12 | + |
| 13 | +# Run tests on the files in the `examples` directory to ensure they work as expected. |
| 14 | +# These are not intended to be comprehensive; they are just sanity checks. |
| 15 | +class ExamplesTest < ActiveSupport::TestCase |
| 16 | + include ReadmeTestHelper |
| 17 | + |
| 18 | + make_my_diffs_pretty! |
| 19 | + |
| 20 | + test "examples/stdio_server.rb example works exactly as documented in README" do |
| 21 | + command_line, *input_lines = extract_readme_code_snippet("running_stdio_server", language: "console").lines(chomp: true) |
| 22 | + |
| 23 | + command = command_line.delete_prefix("$ ") |
| 24 | + stdin_data = input_lines.join("\n") |
| 25 | + |
| 26 | + stdout, stderr, status = Open3.capture3(command, chdir: project_root, stdin_data:) |
| 27 | + assert_predicate(status, :success?, "Expected #{command} to exit with success, but got exit status #{status.exitstatus}\n\nSTDOUT: #{stdout}\n\nSTDERR: #{stderr}") |
| 28 | + assert_empty(stderr, "Expected no stderr in: #{stderr}") |
| 29 | + refute_empty(stdout, "Expected stdout not to be empty") |
| 30 | + |
| 31 | + assert_equal( |
| 32 | + [ |
| 33 | + { jsonrpc: "2.0", id: "1", result: {} }, |
| 34 | + { |
| 35 | + jsonrpc: "2.0", |
| 36 | + id: "2", |
| 37 | + result: { |
| 38 | + tools: [ |
| 39 | + { |
| 40 | + name: "example_tool", |
| 41 | + description: "A simple example tool that adds two numbers", |
| 42 | + inputSchema: { type: "object", properties: { a: { type: "number" }, b: { type: "number" } }, required: ["a", "b"] }, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "echo", |
| 46 | + description: "A simple example tool that echoes back its arguments", |
| 47 | + inputSchema: { type: "object", properties: { message: { type: "string" } }, required: ["message"] }, |
| 48 | + }, |
| 49 | + ], |
| 50 | + }, |
| 51 | + }, |
| 52 | + ], |
| 53 | + stdout.lines.map { |line| JSON.parse(line, symbolize_names: true) }, |
| 54 | + ) |
| 55 | + end |
| 56 | +end |
0 commit comments