|
| 1 | +<?php |
| 2 | + |
| 3 | +use Composer\InstalledVersions; |
| 4 | +use Illuminate\Support\Facades\Http; |
| 5 | +use Laravel\Boost\Mcp\Tools\DatabaseQuery; |
| 6 | +use Laravel\Boost\Mcp\Tools\Tinker; |
| 7 | +use Laravel\Boost\Telemetry\TelemetryCollector; |
| 8 | + |
| 9 | +beforeEach(function (): void { |
| 10 | + $this->collector = app(TelemetryCollector::class); |
| 11 | + $this->collector->toolCounts = []; |
| 12 | +}); |
| 13 | + |
| 14 | +it('records tool invocations', function (): void { |
| 15 | + config(['boost.telemetry.enabled' => true]); |
| 16 | + |
| 17 | + $this->collector->record(DatabaseQuery::class); |
| 18 | + $this->collector->record(DatabaseQuery::class); |
| 19 | + $this->collector->record(Tinker::class); |
| 20 | + |
| 21 | + expect($this->collector->toolCounts)->toBe([ |
| 22 | + DatabaseQuery::class => 2, |
| 23 | + Tinker::class => 1, |
| 24 | + ]); |
| 25 | +}); |
| 26 | + |
| 27 | +it('does not record when disabled via config', function (): void { |
| 28 | + config(['boost.telemetry.enabled' => false]); |
| 29 | + |
| 30 | + $this->collector->record(DatabaseQuery::class); |
| 31 | + |
| 32 | + expect($this->collector->toolCounts)->toBe([]); |
| 33 | +}); |
| 34 | + |
| 35 | +it('auto-flushes when reaching MAX_TOOLS_PER_FLUSH', function (): void { |
| 36 | + config(['boost.telemetry.enabled' => true]); |
| 37 | + |
| 38 | + Http::fake([ |
| 39 | + '*' => Http::response(['status' => 'ok'], 200), |
| 40 | + ]); |
| 41 | + |
| 42 | + for ($i = 0; $i < 20; $i++) { |
| 43 | + $this->collector->record(Tinker::class); |
| 44 | + } |
| 45 | + |
| 46 | + expect($this->collector->toolCounts)->toHaveCount(1) |
| 47 | + ->and($this->collector->toolCounts[Tinker::class])->toBe(20); |
| 48 | + |
| 49 | + $this->collector->record(Tinker::class); |
| 50 | + |
| 51 | + expect(Http::recorded())->toHaveCount(1) |
| 52 | + ->and($this->collector->toolCounts)->toHaveCount(1) |
| 53 | + ->and($this->collector->toolCounts[Tinker::class])->toBe(1); |
| 54 | +}); |
| 55 | + |
| 56 | +it('does not auto-flush below MAX_TOOLS_PER_FLUSH', function (): void { |
| 57 | + config(['boost.telemetry.enabled' => true]); |
| 58 | + |
| 59 | + Http::fake([ |
| 60 | + '*' => Http::response(['status' => 'ok'], 200), |
| 61 | + ]); |
| 62 | + |
| 63 | + for ($i = 0; $i < 19; $i++) { |
| 64 | + $this->collector->record(Tinker::class); |
| 65 | + } |
| 66 | + |
| 67 | + expect(Http::recorded())->toHaveCount(0) |
| 68 | + ->and($this->collector->toolCounts)->toHaveCount(1) |
| 69 | + ->and($this->collector->toolCounts[Tinker::class])->toBe(19); |
| 70 | +}); |
| 71 | + |
| 72 | +it('flush sends data and clears counts', function (): void { |
| 73 | + config(['boost.telemetry.enabled' => true]); |
| 74 | + |
| 75 | + Http::fake([ |
| 76 | + '*' => Http::response(['status' => 'ok'], 200), |
| 77 | + ]); |
| 78 | + |
| 79 | + $this->collector->record(Tinker::class); |
| 80 | + $this->collector->flush(); |
| 81 | + |
| 82 | + expect(Http::recorded())->toHaveCount(1); |
| 83 | + |
| 84 | + $request = Http::recorded()[0][0]; |
| 85 | + $payload = json_decode(base64_decode((string) $request['data'], true), true); |
| 86 | + |
| 87 | + expect($request->url())->toBe(config('boost.telemetry.url')) |
| 88 | + ->and($payload['tools'][Tinker::class])->toBe(1) |
| 89 | + ->and($this->collector->toolCounts)->toBe([]); |
| 90 | +}); |
| 91 | + |
| 92 | +it('flush does nothing when toolCounts is empty', function (): void { |
| 93 | + config(['boost.telemetry.enabled' => true]); |
| 94 | + |
| 95 | + Http::fake([ |
| 96 | + '*' => Http::response(['status' => 'ok'], 200), |
| 97 | + ]); |
| 98 | + |
| 99 | + $this->collector->flush(); |
| 100 | + |
| 101 | + expect(Http::recorded())->toHaveCount(0); |
| 102 | +}); |
| 103 | + |
| 104 | +it('flush does nothing when telemetry is disabled', function (): void { |
| 105 | + config(['boost.telemetry.enabled' => false]); |
| 106 | + |
| 107 | + Http::fake([ |
| 108 | + '*' => Http::response(['status' => 'ok'], 200), |
| 109 | + ]); |
| 110 | + |
| 111 | + $this->collector->toolCounts = ['SomeTool' => 1]; |
| 112 | + $this->collector->flush(); |
| 113 | + |
| 114 | + expect(Http::recorded())->toHaveCount(0); |
| 115 | +}); |
| 116 | + |
| 117 | +it('flush fails silently on network error', function (): void { |
| 118 | + config(['boost.telemetry.enabled' => true]); |
| 119 | + |
| 120 | + Http::fake([ |
| 121 | + '*' => Http::response(null, 500), |
| 122 | + ]); |
| 123 | + |
| 124 | + $this->collector->record(Tinker::class); |
| 125 | + $this->collector->flush(); |
| 126 | + |
| 127 | + expect($this->collector->toolCounts)->toBe([]); |
| 128 | +}); |
| 129 | + |
| 130 | +it('flush fails silently on connection timeout', function (): void { |
| 131 | + config(['boost.telemetry.enabled' => true]); |
| 132 | + |
| 133 | + Http::fake(function (): void { |
| 134 | + throw new \Exception('Connection timeout'); |
| 135 | + }); |
| 136 | + |
| 137 | + $this->collector->record(Tinker::class); |
| 138 | + $this->collector->flush(); |
| 139 | + |
| 140 | + expect($this->collector->toolCounts)->toBe([]); |
| 141 | +}); |
| 142 | + |
| 143 | +it('includes buildPayload as the correct structure', function (): void { |
| 144 | + config(['boost.telemetry.enabled' => true]); |
| 145 | + |
| 146 | + Http::fake([ |
| 147 | + '*' => Http::response(['status' => 'ok'], 200), |
| 148 | + ]); |
| 149 | + |
| 150 | + $this->collector->record(Tinker::class); |
| 151 | + $this->collector->flush(); |
| 152 | + |
| 153 | + expect(Http::recorded())->toHaveCount(1); |
| 154 | + |
| 155 | + $request = Http::recorded()[0][0]; |
| 156 | + $payload = json_decode(base64_decode((string) $request['data'], true), true); |
| 157 | + |
| 158 | + expect($payload)->toHaveKeys(['session_id', 'boost_version', 'php_version', 'os', 'laravel_version', 'tools', 'timestamp']) |
| 159 | + ->and($payload['php_version'])->toBe(PHP_VERSION) |
| 160 | + ->and($payload['os'])->toBe(PHP_OS_FAMILY) |
| 161 | + ->and($payload['tools'])->toBeArray(); |
| 162 | +}); |
| 163 | + |
| 164 | +it('sends session_id as a consistent hash of base_path', function (): void { |
| 165 | + config(['boost.telemetry.enabled' => true]); |
| 166 | + |
| 167 | + Http::fake([ |
| 168 | + '*' => Http::response(['status' => 'ok'], 200), |
| 169 | + ]); |
| 170 | + |
| 171 | + $expectedSessionId = hash('sha256', base_path()); |
| 172 | + |
| 173 | + $this->collector->record(Tinker::class); |
| 174 | + $this->collector->flush(); |
| 175 | + |
| 176 | + expect(Http::recorded())->toHaveCount(1); |
| 177 | + |
| 178 | + $request = Http::recorded()[0][0]; |
| 179 | + $payload = json_decode(base64_decode((string) $request['data'], true), true); |
| 180 | + |
| 181 | + expect($payload['session_id'])->toBe($expectedSessionId); |
| 182 | +}); |
| 183 | + |
| 184 | +it('uses boost_version as InstalledVersions', function (): void { |
| 185 | + config(['boost.telemetry.enabled' => true]); |
| 186 | + |
| 187 | + Http::fake([ |
| 188 | + '*' => Http::response(['status' => 'ok'], 200), |
| 189 | + ]); |
| 190 | + |
| 191 | + $expectedVersion = InstalledVersions::getVersion('laravel/boost'); |
| 192 | + |
| 193 | + $this->collector->record(Tinker::class); |
| 194 | + $this->collector->flush(); |
| 195 | + |
| 196 | + expect(Http::recorded())->toHaveCount(1); |
| 197 | + |
| 198 | + $request = Http::recorded()[0][0]; |
| 199 | + $payload = json_decode(base64_decode((string) $request['data'], true), true); |
| 200 | + |
| 201 | + expect($payload['boost_version'])->toBe($expectedVersion); |
| 202 | +}); |
0 commit comments