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