mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-14 15:37:53 +00:00
fix(#62): webhook plugins are stale for 60 minutes by default
This commit is contained in:
parent
c20e1a9a58
commit
a44479a48a
3 changed files with 30 additions and 4 deletions
|
|
@ -36,6 +36,10 @@ class Plugin extends Model
|
||||||
|
|
||||||
public function isDataStale(): bool
|
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) {
|
if (! $this->data_payload_updated_at || ! $this->data_stale_minutes) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
// 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([
|
$response = $this->withHeaders([
|
||||||
'id' => $device->mac_address,
|
'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);
|
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 () {
|
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)
|
// 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([
|
$response = $this->withHeaders([
|
||||||
'id' => $device->mac_address,
|
'id' => $device->mac_address,
|
||||||
|
|
@ -814,7 +814,7 @@ test('device not in sleep mode returns normal image', function () {
|
||||||
'filename' => 'test-image.bmp',
|
'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 () {
|
test('device returns sleep.png and correct refresh time when paused', function () {
|
||||||
|
|
|
||||||
|
|
@ -71,3 +71,25 @@ test('updateDataPayload sends POST request with body when polling_verb is post',
|
||||||
$request->body() === '{"query": "query { user { id name } }"}';
|
$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();
|
||||||
|
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue