Files
pricebuddy/tests/Feature/Models/ProductNextCheckTest.php
Jeremy 8ae1af1bcf fix: address code review findings
- Scope create-form tag IDs to the current user before syncing (ownership guard)
- Validate URL change before persisting price_factor in the urls edit action
- Promote next-check label to days when rounded hours reach 24 (no more "24h")
- Add aria-label/expanded/controls to the category collapse toggle
- Group dashboard products by canonical tag signature to avoid duplicate groups
- Wrap product reorder weight updates in a transaction
2026-07-02 16:53:34 +10:00

108 lines
3.8 KiB
PHP

<?php
namespace Tests\Feature\Models;
use App\Models\Product;
use App\Services\Helpers\SettingsHelper;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ProductNextCheckTest extends TestCase
{
use RefreshDatabase;
public function test_next_check_estimate_uses_custom_interval_when_set(): void
{
$product = Product::factory()->create(['refresh_interval' => 3600]);
$product->forceFill(['next_check_at' => now()->addMinutes(30)])->saveQuietly();
$this->assertTrue(
$product->nextCheckEstimate()->equalTo($product->next_check_at)
);
}
public function test_next_check_estimate_falls_back_to_global_cron(): void
{
SettingsHelper::setSetting('scrape_schedule', '0 6 * * *');
$product = Product::factory()->create(['refresh_interval' => null]);
$estimate = $product->nextCheckEstimate();
$this->assertNotNull($estimate);
$this->assertTrue($estimate->isFuture());
$this->assertSame(6, $estimate->hour);
$this->assertSame(0, $estimate->minute);
}
public function test_next_check_estimate_is_null_when_paused(): void
{
$product = Product::factory()->create(['refresh_interval' => 3600, 'paused' => true]);
$product->forceFill(['next_check_at' => now()->addMinutes(30)])->saveQuietly();
$this->assertNull($product->nextCheckEstimate());
}
public function test_current_check_period_start_for_custom_interval(): void
{
$product = Product::factory()->create(['refresh_interval' => 3600]);
$next = now()->addMinutes(20);
$product->forceFill(['next_check_at' => $next])->saveQuietly();
$start = $product->currentCheckPeriodStart();
// start = next_check_at - refresh_interval
$this->assertSame(
$next->copy()->subSeconds(3600)->toDateTimeString(),
$start->toDateTimeString()
);
}
public function test_current_check_period_start_is_null_when_paused(): void
{
$product = Product::factory()->create(['paused' => true]);
$this->assertNull($product->currentCheckPeriodStart());
}
public function test_next_check_short_label_rounds_to_hours(): void
{
$product = Product::factory()->create(['refresh_interval' => 7200]);
$product->forceFill(['next_check_at' => now()->addMinutes(70)])->saveQuietly();
$this->assertSame('1h', $product->nextCheckShortLabel());
}
public function test_next_check_short_label_rounds_to_days(): void
{
$product = Product::factory()->create(['refresh_interval' => 172800]);
$product->forceFill(['next_check_at' => now()->addHours(47)])->saveQuietly();
$this->assertSame('2d', $product->nextCheckShortLabel());
}
public function test_next_check_short_label_promotes_to_one_day_when_hours_round_up(): void
{
$product = Product::factory()->create(['refresh_interval' => 86400]);
// 23h40m rounds to 24 hours, which must display as "1d", not "24h".
$product->forceFill(['next_check_at' => now()->addMinutes((23 * 60) + 40)])->saveQuietly();
$this->assertSame('1d', $product->nextCheckShortLabel());
}
public function test_next_check_short_label_uses_less_than_hour(): void
{
$product = Product::factory()->create(['refresh_interval' => 3600]);
$product->forceFill(['next_check_at' => now()->addMinutes(20)])->saveQuietly();
$this->assertSame('<1h', $product->nextCheckShortLabel());
}
public function test_next_check_short_label_is_null_when_paused(): void
{
$product = Product::factory()->create(['refresh_interval' => 3600, 'paused' => true]);
$product->forceFill(['next_check_at' => now()->addMinutes(30)])->saveQuietly();
$this->assertNull($product->nextCheckShortLabel());
}
}