Files
HickLab3D_Dashboard/tests/Feature/SchedulerTest.php
T
hermes 960189d14b Add settings-driven Artisan scheduler registry with Etsy sync and token refresh jobs
Introduces App\Support\DashboardSchedule (modelled on LHITS_Dashboard's
pattern) as a single settings-driven registry for recurring Artisan jobs,
replacing the two hard-coded Schedule::command() lines in
routes/console.php.

Migrated unchanged:
- integrations_refresh (integrations:refresh), every_30_seconds, withoutOverlapping(5)
- alerts_scan (alerts:scan), every_30_seconds, withoutOverlapping(5)

New:
- etsy_sync (etsy:sync), every_15_minutes - forces a fresh pull of Etsy
  receipts/listings into the existing IntegrationCache entries, decoupled
  from the bundled integrations:refresh job so it can be tuned/disabled
  independently of Parcel2Go/BamBuddy/dryer refreshes.
- etsy_token_refresh (etsy:refresh-token), every_45_minutes - proactively
  refreshes the Etsy OAuth token (tokens last ~1hr) via a new public
  Etsy::refreshAccessToken() wrapper around the existing on-demand
  accessToken() refresh logic, so the connection survives quiet periods.

Frequency is persisted to storage/app/dashboard-schedules.json (no
app_settings table exists in this app, unlike LHITS's AppSetting-backed
store) and surfaced in a new 'Scheduler' section on the existing Settings
page (ScheduleSettingsController + routes), with per-job frequency
dropdowns and manual 'Run now' buttons, matching the page's existing
Tailwind conventions.

Tests: SchedulerTest (registry/frequency behavior), ScheduleSettingsTest
(settings UI + authorization), EtsySyncScheduledJobsTest (new commands,
Http::fake only, no live Etsy calls).
2026-11-07 17:38:11 +00:00

106 lines
4.1 KiB
PHP

<?php
namespace Tests\Feature;
use App\Support\DashboardSchedule;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\File;
use Tests\TestCase;
class SchedulerTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
File::delete(DashboardSchedule::runtimeSettingsPath());
}
protected function tearDown(): void
{
// Boot (parent::setUp()) registers the schedule by reading this file
// before the next test's setUp() gets a chance to delete it, so clean
// up here too or a dirty write leaks into the next test's boot.
File::delete(DashboardSchedule::runtimeSettingsPath());
parent::tearDown();
}
public function test_registry_schedules_all_default_jobs(): void
{
$summaries = collect(app(Schedule::class)->events())
->map(fn ($event): string => $event->getSummaryForDisplay());
$this->assertCount(4, $summaries);
$this->assertScheduleContains($summaries, 'integrations:refresh');
$this->assertScheduleContains($summaries, 'alerts:scan');
$this->assertScheduleContains($summaries, 'etsy:sync');
$this->assertScheduleContains($summaries, 'etsy:refresh-token');
}
public function test_existing_jobs_keep_their_thirty_second_cadence_and_overlap_guard(): void
{
$events = collect(app(Schedule::class)->events());
$refresh = $events->first(fn ($event): bool => str_contains((string) $event->command, 'integrations:refresh'));
$alerts = $events->first(fn ($event): bool => str_contains((string) $event->command, 'alerts:scan'));
$this->assertNotNull($refresh);
$this->assertNotNull($alerts);
$this->assertSame('* * * * *', $refresh->expression);
$this->assertSame(30, $refresh->repeatSeconds);
$this->assertSame('* * * * *', $alerts->expression);
$this->assertSame(30, $alerts->repeatSeconds);
$this->assertTrue($refresh->withoutOverlapping);
$this->assertSame(5, $refresh->expiresAt);
$this->assertTrue($alerts->withoutOverlapping);
$this->assertSame(5, $alerts->expiresAt);
}
public function test_etsy_jobs_use_their_default_frequencies(): void
{
$events = collect(app(Schedule::class)->events());
$sync = $events->first(fn ($event): bool => str_contains((string) $event->command, 'etsy:sync'));
$tokenRefresh = $events->first(fn ($event): bool => str_contains((string) $event->command, 'etsy:refresh-token'));
$this->assertNotNull($sync);
$this->assertNotNull($tokenRefresh);
$this->assertSame('*/15 * * * *', $sync->expression);
$this->assertSame('*/45 * * * *', $tokenRefresh->expression);
}
public function test_changing_a_stored_frequency_changes_what_gets_scheduled(): void
{
DashboardSchedule::persistRuntimeFrequencies([
'etsy_sync' => 'hourly',
'etsy_token_refresh' => 'disabled',
]);
// The schedule singleton is built once at boot (before this test writes
// the settings file), so force a fresh Schedule and re-run the registry
// against it to see the just-persisted frequencies take effect, the way
// the next real `schedule:run` boot cycle would.
app()->forgetInstance(Schedule::class);
\Illuminate\Support\Facades\Schedule::clearResolvedInstance(Schedule::class);
DashboardSchedule::register();
$events = collect(app(Schedule::class)->events());
$sync = $events->first(fn ($event): bool => str_contains((string) $event->command, 'etsy:sync'));
$tokenRefresh = $events->first(fn ($event): bool => str_contains((string) $event->command, 'etsy:refresh-token'));
$this->assertNotNull($sync);
$this->assertSame('0 * * * *', $sync->expression);
$this->assertNull($tokenRefresh);
}
private function assertScheduleContains($summaries, string $expected): void
{
$this->assertTrue(
$summaries->contains(fn (string $summary): bool => str_contains($summary, $expected)),
"Failed asserting that the scheduler contains [{$expected}]."
);
}
}