|
| 1 | +<?php |
| 2 | + |
| 3 | +use Illuminate\Support\Facades\File; |
| 4 | +use Illuminate\Support\Facades\Process; |
| 5 | +use Laravel\AiAssistant\Mcp\Tools\LogReader; |
| 6 | +use Laravel\Mcp\Tools\ToolResponse; |
| 7 | + |
| 8 | +it('calls process with the correct log path when one is provided', function () { |
| 9 | + Process::fake([ |
| 10 | + '*' => Process::result(output: 'test output'), |
| 11 | + ]); |
| 12 | + |
| 13 | + File::shouldReceive('exists')->andReturn(true); |
| 14 | + File::shouldReceive('isReadable')->andReturn(true); |
| 15 | + |
| 16 | + $tool = new LogReader(); |
| 17 | + |
| 18 | + $tool->handle([ |
| 19 | + 'lines' => 10, |
| 20 | + 'log_path' => 'my/custom/log.log', |
| 21 | + ]); |
| 22 | + |
| 23 | + Process::assertRan(function ($process) { |
| 24 | + return $process->command === ['tail', '-n', '10', base_path('my/custom/log.log')]; |
| 25 | + }); |
| 26 | +}); |
| 27 | + |
| 28 | +it('calls process with the correct log path when an absolute path is provided', function () { |
| 29 | + Process::fake([ |
| 30 | + '*' => Process::result(output: 'test output'), |
| 31 | + ]); |
| 32 | + |
| 33 | + File::shouldReceive('exists')->andReturn(true); |
| 34 | + File::shouldReceive('isReadable')->andReturn(true); |
| 35 | + |
| 36 | + $tool = new LogReader(); |
| 37 | + $absolutePath = '/var/logs/my-app.log'; |
| 38 | + $tool->handle([ |
| 39 | + 'lines' => 10, |
| 40 | + 'log_path' => $absolutePath, |
| 41 | + ]); |
| 42 | + |
| 43 | + Process::assertRan(function ($process) use ($absolutePath) { |
| 44 | + return $process->command === ['tail', '-n', '10', $absolutePath]; |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +it('calls process with the default log path when none is provided', function () { |
| 49 | + Process::fake([ |
| 50 | + '*' => Process::result(output: 'test output'), |
| 51 | + ]); |
| 52 | + |
| 53 | + File::shouldReceive('exists')->andReturn(true); |
| 54 | + File::shouldReceive('isReadable')->andReturn(true); |
| 55 | + |
| 56 | + $tool = new LogReader(); |
| 57 | + $tool->handle([ |
| 58 | + 'lines' => 10, |
| 59 | + ]); |
| 60 | + |
| 61 | + Process::assertRan(function ($process) { |
| 62 | + return $process->command === ['tail', '-n', '10', storage_path('logs/laravel.log')]; |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +it('calls process with the default log path when an empty string is provided', function () { |
| 67 | + Process::fake([ |
| 68 | + '*' => Process::result(output: 'test output'), |
| 69 | + ]); |
| 70 | + |
| 71 | + File::shouldReceive('exists')->andReturn(true); |
| 72 | + File::shouldReceive('isReadable')->andReturn(true); |
| 73 | + |
| 74 | + $tool = new LogReader(); |
| 75 | + $tool->handle([ |
| 76 | + 'lines' => 10, |
| 77 | + 'log_path' => '', |
| 78 | + ]); |
| 79 | + |
| 80 | + Process::assertRan(function ($process) { |
| 81 | + return $process->command === ['tail', '-n', '10', storage_path('logs/laravel.log')]; |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +it('calls process with grep pattern when provided', function () { |
| 86 | + Process::fake([ |
| 87 | + '*' => Process::result(output: 'test output'), |
| 88 | + ]); |
| 89 | + |
| 90 | + File::shouldReceive('exists')->andReturn(true); |
| 91 | + File::shouldReceive('isReadable')->andReturn(true); |
| 92 | + |
| 93 | + $tool = new LogReader(); |
| 94 | + $tool->handle([ |
| 95 | + 'lines' => 10, |
| 96 | + 'grep' => 'error', |
| 97 | + ]); |
| 98 | + |
| 99 | + Process::assertRan(function ($process) { |
| 100 | + $logPath = escapeshellarg(storage_path('logs/laravel.log')); |
| 101 | + $grepPattern = escapeshellarg('error'); |
| 102 | + $expectedCommand = "grep {$grepPattern} {$logPath} | tail -n 10"; |
| 103 | + |
| 104 | + return $process->command === ['sh', '-c', $expectedCommand]; |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +it('returns an error if the log file does not exist', function () { |
| 109 | + File::shouldReceive('exists')->andReturn(false); |
| 110 | + |
| 111 | + $tool = new LogReader(); |
| 112 | + $response = $tool->handle([ |
| 113 | + 'lines' => 10, |
| 114 | + ]); |
| 115 | + |
| 116 | + $logPath = storage_path('logs/laravel.log'); |
| 117 | + expect($response)->toEqual(new ToolResponse("Log file not found or is not readable: {$logPath}")); |
| 118 | +}); |
| 119 | + |
| 120 | +it('returns an error if the log file is not readable', function () { |
| 121 | + File::shouldReceive('exists')->andReturn(true); |
| 122 | + File::shouldReceive('isReadable')->andReturn(false); |
| 123 | + |
| 124 | + $tool = new LogReader(); |
| 125 | + $response = $tool->handle([ |
| 126 | + 'lines' => 10, |
| 127 | + ]); |
| 128 | + |
| 129 | + $logPath = storage_path('logs/laravel.log'); |
| 130 | + expect($response)->toEqual(new ToolResponse("Log file not found or is not readable: {$logPath}")); |
| 131 | +}); |
| 132 | + |
| 133 | +it('returns an error if the process fails', function () { |
| 134 | + Process::fake([ |
| 135 | + '*' => Process::result( |
| 136 | + output: '', |
| 137 | + errorOutput: 'Something went wrong', |
| 138 | + exitCode: 1 |
| 139 | + ), |
| 140 | + ]); |
| 141 | + |
| 142 | + File::shouldReceive('exists')->andReturn(true); |
| 143 | + File::shouldReceive('isReadable')->andReturn(true); |
| 144 | + |
| 145 | + $tool = new LogReader(); |
| 146 | + $response = $tool->handle([ |
| 147 | + 'lines' => 10, |
| 148 | + ]); |
| 149 | + |
| 150 | + expect($response)->toEqual(new ToolResponse("Failed to read log file. Error: Something went wrong")); |
| 151 | +}); |
| 152 | + |
| 153 | +it('returns a message if no log entries match the grep pattern', function () { |
| 154 | + Process::fake([ |
| 155 | + '*' => Process::result(output: ' '), |
| 156 | + ]); |
| 157 | + |
| 158 | + File::shouldReceive('exists')->andReturn(true); |
| 159 | + File::shouldReceive('isReadable')->andReturn(true); |
| 160 | + |
| 161 | + $tool = new LogReader(); |
| 162 | + $response = $tool->handle([ |
| 163 | + 'lines' => 10, |
| 164 | + 'grep' => 'non_existent_pattern', |
| 165 | + ]); |
| 166 | + |
| 167 | + expect($response)->toEqual(new ToolResponse("No log entries found matching pattern: non_existent_pattern")); |
| 168 | +}); |
| 169 | + |
| 170 | +it('returns a message if the log file is empty', function () { |
| 171 | + Process::fake([ |
| 172 | + '*' => Process::result(output: ''), |
| 173 | + ]); |
| 174 | + |
| 175 | + File::shouldReceive('exists')->andReturn(true); |
| 176 | + File::shouldReceive('isReadable')->andReturn(true); |
| 177 | + |
| 178 | + $tool = new LogReader(); |
| 179 | + $response = $tool->handle([ |
| 180 | + 'lines' => 10, |
| 181 | + ]); |
| 182 | + |
| 183 | + expect($response)->toEqual(new ToolResponse('Log file is empty or no entries found.')); |
| 184 | +}); |
| 185 | + |
| 186 | +it('returns the log content on success', function () { |
| 187 | + Process::fake([ |
| 188 | + '*' => Process::result(output: " log line 1 \n log line 2 "), |
| 189 | + ]); |
| 190 | + |
| 191 | + File::shouldReceive('exists')->andReturn(true); |
| 192 | + File::shouldReceive('isReadable')->andReturn(true); |
| 193 | + |
| 194 | + $tool = new LogReader(); |
| 195 | + $response = $tool->handle([ |
| 196 | + 'lines' => 10, |
| 197 | + ]); |
| 198 | + |
| 199 | + expect($response)->toEqual(new ToolResponse("log line 1 \n log line 2")); |
| 200 | +}); |
0 commit comments