dir = sys_get_temp_dir().'/inhaus-state-'.bin2hex(random_bytes(4)); mkdir($this->dir); } protected function tearDown(): void { Filesystem::deleteDirectory($this->dir); parent::tearDown(); } public function test_sha_is_read_and_written(): void { $version = new InstalledVersion($this->dir.'/.version'); $this->assertNull($version->current()); $this->assertSame('0.2.0', $version->release()); $this->assertSame('0.2.0', $version->full()); $version->save('A1B2C3D4E5F60718293a4b5c6d7e8f9012345678'); $this->assertSame('a1b2c3d4e5f60718293a4b5c6d7e8f9012345678', $version->current()); $this->assertSame('a1b2c3d', $version->short()); $this->assertSame('0.2.0 (a1b2c3d)', $version->full()); } public function test_invalid_sha_is_rejected(): void { $version = new InstalledVersion($this->dir.'/.version'); foreach (['', 'main', 'a1b2c3d', str_repeat('g', 40), str_repeat('a', 41)] as $invalid) { try { $version->save($invalid); $this->fail("„{$invalid}“ hätte abgelehnt werden müssen."); } catch (InvalidArgumentException) { $this->assertNull($version->current()); } } file_put_contents($this->dir.'/.version', "kaputt\n"); $this->assertNull($version->current()); } public function test_maintenance_mode_heals_itself_after_expiry(): void { $flag = new MaintenanceFlag($this->dir.'/.maintenance'); $this->assertFalse($flag->isActive()); $flag->activate(600); $this->assertTrue($flag->isActive()); $flag->deactivate(); $this->assertFalse($flag->isActive()); // Absturz mitten im Update: Flag bleibt liegen, ist aber abgelaufen. file_put_contents($this->dir.'/.maintenance', json_encode(['since' => time() - 700, 'until' => time() - 100])); $this->assertFalse($flag->isActive()); $this->assertFileDoesNotExist($this->dir.'/.maintenance'); // Kaputte Datei sperrt ebenfalls nicht. file_put_contents($this->dir.'/.maintenance', 'Müll'); $this->assertFalse($flag->isActive()); } public function test_progress_file_including_error_state(): void { $progress = new UpdateProgress($this->dir.'/.update-progress'); $this->assertSame(['pct' => 0, 'step' => 'idle', 'text' => '', 'ts' => 0], $progress->get()); $this->assertFalse($progress->isRunning()); $progress->set(20, 'download', 'Lädt …'); $this->assertSame(20, $progress->get()['pct']); $this->assertSame('Lädt …', $progress->get()['text']); $this->assertTrue($progress->isRunning()); $progress->fail('Kaputt'); $this->assertSame(-1, $progress->get()['pct']); $this->assertSame('error', $progress->get()['step']); $this->assertFalse($progress->isRunning()); } public function test_stale_run_does_not_block_forever(): void { $progress = new UpdateProgress($this->dir.'/.update-progress'); file_put_contents($this->dir.'/.update-progress', json_encode([ 'pct' => 50, 'step' => 'extract', 'text' => '', 'ts' => time() - UpdateProgress::STALE_AFTER_SECONDS - 1, ])); $this->assertFalse($progress->isRunning()); } }