|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binaryk\LaravelRestify\Tests\MCP; |
| 4 | + |
| 5 | +use Binaryk\LaravelRestify\Contracts\RestifySearchable; |
| 6 | +use Binaryk\LaravelRestify\Filters\MatchFilter; |
| 7 | +use Binaryk\LaravelRestify\MCP\Concerns\HasMcpTools; |
| 8 | +use Binaryk\LaravelRestify\Repositories\Repository; |
| 9 | +use Binaryk\LaravelRestify\Restify; |
| 10 | +use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post; |
| 11 | +use Binaryk\LaravelRestify\Tests\IntegrationTestCase; |
| 12 | +use Illuminate\JsonSchema\JsonSchemaTypeFactory; |
| 13 | + |
| 14 | +class McpIndexToolSchemaTest extends IntegrationTestCase |
| 15 | +{ |
| 16 | + protected function tearDown(): void |
| 17 | + { |
| 18 | + Restify::$repositories = []; |
| 19 | + |
| 20 | + parent::tearDown(); |
| 21 | + } |
| 22 | + |
| 23 | + protected function repositoryWith(array $search = [], array $sort = [], array $match = []): Repository |
| 24 | + { |
| 25 | + return new class($search, $sort, $match) extends Repository |
| 26 | + { |
| 27 | + use HasMcpTools; |
| 28 | + |
| 29 | + public static $model = Post::class; |
| 30 | + |
| 31 | + public static array $search = []; |
| 32 | + |
| 33 | + public static array $sort = []; |
| 34 | + |
| 35 | + public static array $match = []; |
| 36 | + |
| 37 | + public function __construct(array $search = [], array $sort = [], array $match = []) |
| 38 | + { |
| 39 | + self::$search = $search; |
| 40 | + self::$sort = $sort; |
| 41 | + self::$match = $match; |
| 42 | + } |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + protected function schema(Repository $repository): array |
| 47 | + { |
| 48 | + return $repository::indexToolSchema(new JsonSchemaTypeFactory); |
| 49 | + } |
| 50 | + |
| 51 | + public function test_search_property_lists_searchable_fields(): void |
| 52 | + { |
| 53 | + $schema = $this->schema($this->repositoryWith(search: ['id', 'title'])); |
| 54 | + |
| 55 | + $this->assertArrayHasKey('search', $schema); |
| 56 | + |
| 57 | + $description = $schema['search']->toArray()['description']; |
| 58 | + |
| 59 | + $this->assertStringContainsString('Full-text search across: id, title', $description); |
| 60 | + $this->assertStringNotContainsString('No searchable fields available', $description); |
| 61 | + $this->assertStringNotContainsString('by name or description', $description); |
| 62 | + } |
| 63 | + |
| 64 | + public function test_search_property_is_omitted_when_no_searchables(): void |
| 65 | + { |
| 66 | + $schema = $this->schema($this->repositoryWith(search: [])); |
| 67 | + |
| 68 | + $this->assertArrayNotHasKey('search', $schema); |
| 69 | + } |
| 70 | + |
| 71 | + public function test_sort_property_lists_sort_options(): void |
| 72 | + { |
| 73 | + $schema = $this->schema($this->repositoryWith(sort: ['title', 'is_active'])); |
| 74 | + |
| 75 | + $this->assertArrayHasKey('sort', $schema); |
| 76 | + |
| 77 | + $description = $schema['sort']->toArray()['description']; |
| 78 | + |
| 79 | + $this->assertStringContainsString('title', $description); |
| 80 | + $this->assertStringContainsString('is_active', $description); |
| 81 | + $this->assertStringContainsString("Prefix with '-' for descending", $description); |
| 82 | + } |
| 83 | + |
| 84 | + public function test_matcher_property_uses_description_without_clunky_prefix(): void |
| 85 | + { |
| 86 | + $schema = $this->schema($this->repositoryWith(match: [ |
| 87 | + 'title' => RestifySearchable::MATCH_TEXT, |
| 88 | + ])); |
| 89 | + |
| 90 | + $this->assertArrayHasKey('title', $schema); |
| 91 | + |
| 92 | + $description = $schema['title']->toArray()['description']; |
| 93 | + |
| 94 | + $this->assertStringNotContainsString('Description:', $description); |
| 95 | + $this->assertStringNotContainsString('resource.', $description); |
| 96 | + $this->assertSame( |
| 97 | + "Exact match on title (title=value). Prefix with '-' to negate (-title=value).", |
| 98 | + $description |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + public function test_matcher_property_respects_custom_description(): void |
| 103 | + { |
| 104 | + $schema = $this->schema($this->repositoryWith(match: [ |
| 105 | + 'title' => MatchFilter::make() |
| 106 | + ->setType(RestifySearchable::MATCH_TEXT) |
| 107 | + ->setDescription('Filter posts by their title.'), |
| 108 | + ])); |
| 109 | + |
| 110 | + $this->assertSame( |
| 111 | + 'Filter posts by their title.', |
| 112 | + $schema['title']->toArray()['description'] |
| 113 | + ); |
| 114 | + } |
| 115 | + |
| 116 | + public function test_default_templates_per_match_type(): void |
| 117 | + { |
| 118 | + $cases = [ |
| 119 | + RestifySearchable::MATCH_TEXT => "Exact match on col (col=value). Prefix with '-' to negate (-col=value).", |
| 120 | + RestifySearchable::MATCH_INTEGER => "Exact match on col (col=value). Prefix with '-' to negate (-col=value).", |
| 121 | + RestifySearchable::MATCH_BOOL => "Boolean match on col (col=true or col=false). Prefix with '-' to negate (-col=true).", |
| 122 | + RestifySearchable::MATCH_BETWEEN => "Range match on col (col=min,max). Prefix with '-' to negate (-col=min,max).", |
| 123 | + RestifySearchable::MATCH_DATETIME => "Date match on col (col=YYYY-MM-DD, or col=start,end for a range). Prefix with '-' to negate (-col=YYYY-MM-DD).", |
| 124 | + RestifySearchable::MATCH_ARRAY => "Array match on col (col=value1,value2 matches any listed value). Prefix with '-' to negate (-col=value1,value2).", |
| 125 | + ]; |
| 126 | + |
| 127 | + foreach ($cases as $type => $expected) { |
| 128 | + $filter = MatchFilter::make()->setColumn('col')->setType($type); |
| 129 | + |
| 130 | + $this->assertSame($expected, $filter->description(), "Failed default template for [{$type}]."); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + public function test_default_templates_have_no_grammar_or_typo_bugs(): void |
| 135 | + { |
| 136 | + foreach ([ |
| 137 | + RestifySearchable::MATCH_TEXT, |
| 138 | + RestifySearchable::MATCH_BETWEEN, |
| 139 | + RestifySearchable::MATCH_DATETIME, |
| 140 | + RestifySearchable::MATCH_BOOL, |
| 141 | + RestifySearchable::MATCH_ARRAY, |
| 142 | + ] as $type) { |
| 143 | + $description = MatchFilter::make()->setColumn('col')->setType($type)->description(); |
| 144 | + |
| 145 | + $this->assertStringNotContainsString('a exact match', $description); |
| 146 | + $this->assertStringNotContainsString('acccepted', $description); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments