This commit is contained in:
DariusIII
2026-08-11 10:53:59 +02:00
19 changed files with 697 additions and 79 deletions
@@ -121,7 +121,7 @@ class AdditionalProcessingOrchestratorClaimTest extends TestCase
$output = new RecordingConsoleOutputService;
$orchestrator = new AdditionalProcessingOrchestrator(
$this->makeConfig(['queryLimit' => 25, 'minSizeMB' => 0, 'maxSizeGB' => 100]),
$this->makeConfig(['queryLimit' => 25, 'minSizeBytes' => 0, 'maxSizeBytes' => 107374182400]),
$processor,
$tempWorkspace,
$output
@@ -202,7 +202,7 @@ class AdditionalProcessingOrchestratorClaimTest extends TestCase
$output = new RecordingConsoleOutputService;
$orchestrator = new AdditionalProcessingOrchestrator(
$this->makeConfig(['queryLimit' => 25, 'minSizeMB' => 0, 'maxSizeGB' => 100]),
$this->makeConfig(['queryLimit' => 25, 'minSizeBytes' => 0, 'maxSizeBytes' => 107374182400]),
$processor,
$tempWorkspace,
$output
+107
View File
@@ -109,6 +109,92 @@ class AdminGroupControllerTest extends TestCase
$this->assertSame('No group list provided.', $response->getData()['groupmsglist']);
}
public function test_edit_submit_converts_min_size_unit_to_bytes(): void
{
$this->createUsenetGroupsTable();
$groupId = DB::table('usenet_groups')->insertGetId([
'name' => 'alt.binaries.test',
'description' => 'Test group',
'active' => 1,
'backfill' => 1,
'minsizetoformrelease' => null,
'minfilestoformrelease' => null,
]);
$request = Request::create('/admin/group-edit', 'POST', [
'action' => 'submit',
'id' => (string) $groupId,
'name' => 'alt.binaries.test',
'description' => 'Test group',
'backfill_target' => '1',
'first_record' => '0',
'last_record' => '0',
'active' => '1',
'backfill' => '1',
'minsizetoformrelease' => '2',
'minsizetoformrelease_unit' => 'GB',
'minfilestoformrelease' => '1',
]);
$response = app(AdminGroupController::class)->edit($request);
$this->assertTrue($response->isRedirect());
$this->assertSame(2147483648, (int) DB::table('usenet_groups')->where('id', $groupId)->value('minsizetoformrelease'));
}
public function test_edit_submit_keeps_blank_min_size_as_null(): void
{
$this->createUsenetGroupsTable();
$groupId = DB::table('usenet_groups')->insertGetId([
'name' => 'alt.binaries.test',
'description' => 'Test group',
'active' => 1,
'backfill' => 1,
'minsizetoformrelease' => 1048576,
'minfilestoformrelease' => null,
]);
$request = Request::create('/admin/group-edit', 'POST', [
'action' => 'submit',
'id' => (string) $groupId,
'name' => 'alt.binaries.test',
'description' => 'Test group',
'backfill_target' => '1',
'first_record' => '0',
'last_record' => '0',
'active' => '1',
'backfill' => '1',
'minsizetoformrelease' => '',
'minsizetoformrelease_unit' => 'MB',
'minfilestoformrelease' => '1',
]);
app(AdminGroupController::class)->edit($request);
$this->assertNull(DB::table('usenet_groups')->where('id', $groupId)->value('minsizetoformrelease'));
}
public function test_edit_view_exposes_group_min_size_split_into_value_and_unit(): void
{
$this->createUsenetGroupsTable();
$groupId = DB::table('usenet_groups')->insertGetId([
'name' => 'alt.binaries.test',
'description' => 'Test group',
'active' => 1,
'backfill' => 1,
'minsizetoformrelease' => 2147483648,
'minfilestoformrelease' => null,
]);
$request = Request::create('/admin/group-edit', 'GET', ['id' => (string) $groupId]);
$response = app(AdminGroupController::class)->edit($request);
$this->assertInstanceOf(View::class, $response);
$this->assertSame(['value' => 2, 'unit' => 'GB'], $response->getData()['groupMinSize']);
$this->assertSame(['MB', 'GB'], $response->getData()['sizeUnits']);
}
private function setEnvironmentValue(string $key, ?string $value): void
{
if ($value === null) {
@@ -148,6 +234,27 @@ class AdminGroupControllerTest extends TestCase
}
}
private function createUsenetGroupsTable(): void
{
if (Schema::hasTable('usenet_groups')) {
return;
}
Schema::create('usenet_groups', function (Blueprint $table): void {
$table->increments('id');
$table->string('name')->default('');
$table->string('description')->default('');
$table->unsignedBigInteger('first_record')->default(0);
$table->unsignedBigInteger('last_record')->default(0);
$table->dateTime('last_updated')->nullable();
$table->boolean('active')->default(false);
$table->boolean('backfill')->default(false);
$table->unsignedBigInteger('minsizetoformrelease')->nullable();
$table->unsignedBigInteger('minfilestoformrelease')->nullable();
$table->integer('backfill_target')->default(1);
});
}
private function seedSettings(): void
{
DB::table('settings')->upsert([
+210
View File
@@ -0,0 +1,210 @@
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Http\Controllers\Admin\AdminSiteController;
use App\View\Composers\GlobalDataComposer;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\View\View;
use PDO;
use ReflectionClass;
use Tests\TestCase;
class AdminSiteControllerTest extends TestCase
{
private string $databasePath;
/**
* @var array<string, string|false>
*/
private array $originalEnvironment = [];
public function createApplication()
{
$this->databasePath = sys_get_temp_dir().'/nntmux-admin-site-test.sqlite';
$this->originalEnvironment = [
'APP_ENV' => getenv('APP_ENV'),
'DB_CONNECTION' => getenv('DB_CONNECTION'),
'DB_DATABASE' => getenv('DB_DATABASE'),
];
if (file_exists($this->databasePath)) {
unlink($this->databasePath);
}
$pdo = new PDO('sqlite:'.$this->databasePath);
$pdo->exec('CREATE TABLE settings (name VARCHAR PRIMARY KEY, value TEXT NULL)');
$pdo->exec("INSERT INTO settings (name, value) VALUES
('categorizeforeign', '0'),
('catwebdl', '0'),
('title', 'NNTmux Test'),
('home_link', '/')");
$this->setEnvironmentValue('APP_ENV', 'testing');
$this->setEnvironmentValue('DB_CONNECTION', 'sqlite');
$this->setEnvironmentValue('DB_DATABASE', $this->databasePath);
$app = require __DIR__.'/../../bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
return $app;
}
protected function setUp(): void
{
parent::setUp();
config([
'database.default' => 'sqlite',
'database.connections.sqlite.database' => $this->databasePath,
'app.key' => 'base64:'.base64_encode(random_bytes(32)),
]);
DB::purge();
DB::reconnect();
Cache::flush();
$this->createSchema();
$this->seedSettings();
$this->resetGlobalComposerState();
}
protected function tearDown(): void
{
if ($this->databasePath !== '' && file_exists($this->databasePath)) {
unlink($this->databasePath);
}
parent::tearDown();
foreach ($this->originalEnvironment as $key => $value) {
$this->setEnvironmentValue($key, $value === false ? null : $value);
}
}
public function test_submit_converts_size_units_to_bytes(): void
{
$request = Request::create('/admin/site-edit', 'POST', [
'action' => 'submit',
'minsizetoformrelease' => '500',
'minsizetoformrelease_unit' => 'MB',
'maxsizetoformrelease' => '1.5',
'maxsizetoformrelease_unit' => 'GB',
'minsizetopostprocess' => '2',
'minsizetopostprocess_unit' => 'MB',
'maxsizetopostprocess' => '50',
'maxsizetopostprocess_unit' => 'GB',
'minsizetoprocessnfo' => '0',
'minsizetoprocessnfo_unit' => 'MB',
'maxsizetoprocessnfo' => '10',
'maxsizetoprocessnfo_unit' => 'GB',
]);
$response = app(AdminSiteController::class)->edit($request);
$this->assertSame('524288000', $this->settingValue('minsizetoformrelease'));
$this->assertSame('1610612736', $this->settingValue('maxsizetoformrelease'));
$this->assertSame('2097152', $this->settingValue('minsizetopostprocess'));
$this->assertSame('53687091200', $this->settingValue('maxsizetopostprocess'));
$this->assertSame('0', $this->settingValue('minsizetoprocessnfo'));
$this->assertSame('10737418240', $this->settingValue('maxsizetoprocessnfo'));
$this->assertNull(DB::table('settings')->where('name', 'minsizetopostprocess_unit')->value('value'));
$this->assertTrue($response->isRedirect());
}
public function test_submit_defaults_to_mb_when_unit_is_missing(): void
{
$request = Request::create('/admin/site-edit', 'POST', [
'action' => 'submit',
'minsizetopostprocess' => '3',
]);
app(AdminSiteController::class)->edit($request);
$this->assertSame('3145728', $this->settingValue('minsizetopostprocess'));
}
public function test_view_exposes_size_fields_split_into_value_and_unit(): void
{
DB::table('settings')->where('name', 'maxsizetopostprocess')->update(['value' => '53687091200']);
DB::table('settings')->where('name', 'minsizetopostprocess')->update(['value' => '524288000']);
DB::table('settings')->where('name', 'minsizetoformrelease')->update(['value' => '1572864']);
Cache::flush();
$request = Request::create('/admin/site-edit', 'GET');
$response = app(AdminSiteController::class)->edit($request);
$this->assertInstanceOf(View::class, $response);
$sizeFields = $response->getData()['sizeFields'];
$this->assertSame(['value' => 50, 'unit' => 'GB'], $sizeFields['maxsizetopostprocess']);
$this->assertSame(['value' => 500, 'unit' => 'MB'], $sizeFields['minsizetopostprocess']);
$this->assertSame(['value' => 1.5, 'unit' => 'MB'], $sizeFields['minsizetoformrelease']);
$this->assertSame(['value' => 0, 'unit' => 'MB'], $sizeFields['maxsizetoformrelease']);
$this->assertSame(['MB', 'GB'], $response->getData()['sizeUnits']);
}
private function settingValue(string $name): ?string
{
$value = DB::table('settings')->where('name', $name)->value('value');
return $value === null ? null : (string) $value;
}
private function setEnvironmentValue(string $key, ?string $value): void
{
if ($value === null) {
putenv($key);
unset($_ENV[$key], $_SERVER[$key]);
return;
}
putenv($key.'='.$value);
$_ENV[$key] = $value;
$_SERVER[$key] = $value;
}
private function createSchema(): void
{
if (! Schema::hasTable('settings')) {
Schema::create('settings', function (Blueprint $table): void {
$table->string('name')->primary();
$table->text('value')->nullable();
});
}
}
private function seedSettings(): void
{
DB::table('settings')->upsert([
['name' => 'title', 'value' => 'NNTmux Test'],
['name' => 'home_link', 'value' => '/'],
['name' => 'categorizeforeign', 'value' => '0'],
['name' => 'catwebdl', 'value' => '0'],
['name' => 'minsizetoformrelease', 'value' => '0'],
['name' => 'maxsizetoformrelease', 'value' => '0'],
['name' => 'minsizetopostprocess', 'value' => '1048576'],
['name' => 'maxsizetopostprocess', 'value' => '107374182400'],
['name' => 'minsizetoprocessnfo', 'value' => '1048576'],
['name' => 'maxsizetoprocessnfo', 'value' => '107374182400'],
], ['name'], ['value']);
}
private function resetGlobalComposerState(): void
{
$reflection = new ReflectionClass(GlobalDataComposer::class);
$property = $reflection->getProperty('resolvedData');
$property->setValue(null, null);
}
}
@@ -31,8 +31,8 @@ trait CreatesProcessingConfiguration
'segmentsToDownload' => 2,
'maximumRarSegments' => 3,
'maximumRarPasswordChecks' => 1,
'maxSizeGB' => 100,
'minSizeMB' => 0,
'maxSizeBytes' => 107374182400,
'minSizeBytes' => 0,
'alternateNNTP' => false,
'ffmpegDuration' => 5,
'addPAR2Files' => false,
+83
View File
@@ -0,0 +1,83 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Support;
use App\Support\SizeUnit;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
class SizeUnitTest extends TestCase
{
public function test_to_bytes_converts_mb(): void
{
$this->assertSame(1048576, SizeUnit::toBytes(1, 'MB'));
$this->assertSame(5242880, SizeUnit::toBytes('5', 'MB'));
}
public function test_to_bytes_converts_gb(): void
{
$this->assertSame(1073741824, SizeUnit::toBytes(1, 'GB'));
$this->assertSame(107374182400, SizeUnit::toBytes(100, 'GB'));
}
public function test_to_bytes_supports_decimal_values(): void
{
$this->assertSame(1610612736, SizeUnit::toBytes(1.5, 'GB'));
$this->assertSame(1572864, SizeUnit::toBytes('1.5', 'MB'));
}
public function test_to_bytes_maps_empty_and_non_positive_values_to_zero(): void
{
$this->assertSame(0, SizeUnit::toBytes(null, 'MB'));
$this->assertSame(0, SizeUnit::toBytes('', 'GB'));
$this->assertSame(0, SizeUnit::toBytes(0, 'GB'));
$this->assertSame(0, SizeUnit::toBytes('0', 'MB'));
$this->assertSame(0, SizeUnit::toBytes(-5, 'GB'));
$this->assertSame(0, SizeUnit::toBytes('not-a-number', 'MB'));
}
public function test_to_bytes_rejects_unknown_units(): void
{
$this->expectException(InvalidArgumentException::class);
SizeUnit::toBytes(1, 'TB');
}
public function test_from_bytes_prefers_gb_when_evenly_divisible(): void
{
$this->assertSame(['value' => 1, 'unit' => 'GB'], SizeUnit::fromBytes(1073741824));
$this->assertSame(['value' => 100, 'unit' => 'GB'], SizeUnit::fromBytes(107374182400));
}
public function test_from_bytes_uses_mb_when_not_evenly_divisible_by_gb(): void
{
$this->assertSame(['value' => 1, 'unit' => 'MB'], SizeUnit::fromBytes(1048576));
$this->assertSame(['value' => 500, 'unit' => 'MB'], SizeUnit::fromBytes(524288000));
}
public function test_from_bytes_rounds_to_two_decimals_for_odd_byte_counts(): void
{
$this->assertSame(['value' => 1.5, 'unit' => 'MB'], SizeUnit::fromBytes(1572864));
$this->assertSame(['value' => 1.43, 'unit' => 'MB'], SizeUnit::fromBytes(1500000));
}
public function test_from_bytes_maps_empty_and_non_positive_values_to_zero_mb(): void
{
$this->assertSame(['value' => 0, 'unit' => 'MB'], SizeUnit::fromBytes(null));
$this->assertSame(['value' => 0, 'unit' => 'MB'], SizeUnit::fromBytes(''));
$this->assertSame(['value' => 0, 'unit' => 'MB'], SizeUnit::fromBytes(0));
$this->assertSame(['value' => 0, 'unit' => 'MB'], SizeUnit::fromBytes(-1));
}
public function test_round_trip_preserves_values(): void
{
foreach ([['2', 'GB'], ['250', 'MB'], ['1.5', 'GB'], ['0.5', 'MB']] as [$value, $unit]) {
$bytes = SizeUnit::toBytes($value, $unit);
$display = SizeUnit::fromBytes($bytes);
$this->assertSame($bytes, SizeUnit::toBytes($display['value'], $display['unit']));
}
}
}