From a44479a48a26187a1cf93ad7d7a44ac889a1ef0a Mon Sep 17 00:00:00 2001 From: Benjamin Nussbaum Date: Thu, 10 Jul 2025 23:50:41 +0200 Subject: [PATCH] fix(#62): webhook plugins are stale for 60 minutes by default --- app/Models/Plugin.php | 4 ++++ tests/Feature/Api/DeviceEndpointsTest.php | 8 ++++---- tests/Unit/Models/PluginTest.php | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/Models/Plugin.php b/app/Models/Plugin.php index 7c40805..1d07251 100644 --- a/app/Models/Plugin.php +++ b/app/Models/Plugin.php @@ -36,6 +36,10 @@ class Plugin extends Model public function isDataStale(): bool { + if ($this->data_strategy === 'webhook') { + // Treat as stale if any webhook event has occurred in the past hour + return $this->data_payload_updated_at && $this->data_payload_updated_at->gt(now()->subHour()); + } if (! $this->data_payload_updated_at || ! $this->data_stale_minutes) { return true; } diff --git a/tests/Feature/Api/DeviceEndpointsTest.php b/tests/Feature/Api/DeviceEndpointsTest.php index 53fe724..6bf1c28 100644 --- a/tests/Feature/Api/DeviceEndpointsTest.php +++ b/tests/Feature/Api/DeviceEndpointsTest.php @@ -769,7 +769,7 @@ test('device in sleep mode returns sleep image and correct refresh rate', functi ]); // Freeze time to 20:00 (within sleep window) - \Carbon\Carbon::setTestNow(\Carbon\Carbon::parse('2000-01-01 20:00:00')); + Carbon\Carbon::setTestNow(Carbon\Carbon::parse('2000-01-01 20:00:00')); $response = $this->withHeaders([ 'id' => $device->mac_address, @@ -785,7 +785,7 @@ test('device in sleep mode returns sleep image and correct refresh rate', functi ]); expect($response['refresh_rate'])->toBeGreaterThan(0); - \Carbon\Carbon::setTestNow(); // Clear test time + Carbon\Carbon::setTestNow(); // Clear test time }); test('device not in sleep mode returns normal image', function () { @@ -799,7 +799,7 @@ test('device not in sleep mode returns normal image', function () { ]); // Freeze time to 18:00 (outside sleep window) - \Carbon\Carbon::setTestNow(\Carbon\Carbon::parse('2000-01-01 18:00:00')); + Carbon\Carbon::setTestNow(Carbon\Carbon::parse('2000-01-01 18:00:00')); $response = $this->withHeaders([ 'id' => $device->mac_address, @@ -814,7 +814,7 @@ test('device not in sleep mode returns normal image', function () { 'filename' => 'test-image.bmp', ]); - \Carbon\Carbon::setTestNow(); // Clear test time + Carbon\Carbon::setTestNow(); // Clear test time }); test('device returns sleep.png and correct refresh time when paused', function () { diff --git a/tests/Unit/Models/PluginTest.php b/tests/Unit/Models/PluginTest.php index 173c5a8..cb598f6 100644 --- a/tests/Unit/Models/PluginTest.php +++ b/tests/Unit/Models/PluginTest.php @@ -71,3 +71,25 @@ test('updateDataPayload sends POST request with body when polling_verb is post', $request->body() === '{"query": "query { user { id name } }"}'; }); }); + +test('webhook plugin is stale if webhook event occurred', function () { + $plugin = Plugin::factory()->create([ + 'data_strategy' => 'webhook', + 'data_payload_updated_at' => now()->subMinutes(10), + 'data_stale_minutes' => 60, // Should be ignored for webhook + ]); + + expect($plugin->isDataStale())->toBeTrue(); + +}); + +test('webhook plugin data not stale if no webhook event occurred for 1 hour', function () { + $plugin = Plugin::factory()->create([ + 'data_strategy' => 'webhook', + 'data_payload_updated_at' => now()->subMinutes(60), + 'data_stale_minutes' => 60, // Should be ignored for webhook + ]); + + expect($plugin->isDataStale())->toBeFalse(); + +});