rob-revisited/ide/tests/RecentChangesTest.php

71 lines
3.5 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Ide;
use JumPHP\Ide\Workspace\LocalWorkspace;
use PHPUnit\Framework\TestCase;
final class RecentChangesTest extends TestCase
{
private string $dir;
protected function setUp(): void
{
$this->dir = sys_get_temp_dir() . '/jumphp-recent-' . bin2hex(random_bytes(4));
mkdir("$this->dir/src/Scene", 0777, true);
mkdir("$this->dir/vendor/lib", 0777, true);
$now = time();
foreach (['src/Old.php' => 30 * 86400, 'README.md' => 3 * 86400, 'src/Scene/Play.php' => 2 * 3600, 'src/main.js' => 600, 'vendor/lib/X.php' => 5] as $path => $age) {
file_put_contents("$this->dir/$path", "<?php // $path\n");
touch("$this->dir/$path", $now - $age);
}
}
protected function tearDown(): void
{
exec('rm -rf ' . escapeshellarg($this->dir));
}
public function testTheFilesComeNewestFirstWithTheirFilters(): void
{
$ide = new Ide(new LocalWorkspace($this->dir));
self::assertSame(['src/main.js', 'src/Scene/Play.php', 'README.md', 'src/Old.php'], array_column($ide->handle('files.recent', []), 'path'), 'not vendor/');
self::assertSame(['src/main.js'], array_column($ide->handle('files.recent', ['since' => 3600]), 'path'));
self::assertSame(['src/main.js', 'src/Scene/Play.php'], array_column($ide->handle('files.recent', ['since' => 86400]), 'path'));
self::assertSame(['src/Scene/Play.php', 'src/Old.php'], array_column($ide->handle('files.recent', ['ext' => 'php']), 'path'));
self::assertSame(['src/main.js', 'README.md'], array_column($ide->handle('files.recent', ['ext' => '.js, md']), 'path'));
self::assertSame(['src/main.js'], array_column($ide->handle('files.recent', ['limit' => 1]), 'path'));
$first = $ide->handle('files.recent', [])[0];
self::assertSame(strlen("<?php // src/main.js\n"), $first['size']);
self::assertEqualsWithDelta(time() - 600, $first['mtime'], 2);
self::assertNull($first['origin'], 'the local history does not know it');
}
public function testTheLocalHistoryTellsASaveInTheIdeFromAChangeOutside(): void
{
$ide = new Ide(new LocalWorkspace($this->dir));
$ide->handle('file.write', ['path' => 'src/Old.php', 'content' => "<?php // saved in the IDE\n"]);
$saved = $ide->handle('files.recent', [])[0];
self::assertSame('src/Old.php', $saved['path']);
self::assertSame('ide', $saved['origin']);
// the version before the save: what the file was
self::assertSame("<?php // src/Old.php\n", $ide->handle('history.local.read', ['path' => 'src/Old.php', 'id' => $saved['previous']['id']]));
// Claude changes it afterwards
file_put_contents("$this->dir/src/Old.php", "<?php // by Claude\n");
touch("$this->dir/src/Old.php", time() + 10);
$outside = $ide->handle('files.recent', [])[0];
self::assertSame('outside', $outside['origin']);
self::assertSame("<?php // saved in the IDE\n", $ide->handle('history.local.read', ['path' => 'src/Old.php', 'id' => $outside['previous']['id']]));
}
public function testTheListingHasTheModificationTimes(): void
{
$ide = new Ide(new LocalWorkspace($this->dir));
$files = array_column($ide->handle('files.list', []), null);
$byPath = array_column($ide->handle('files.list', [])['files'], 'mtime', 'path');
self::assertEqualsWithDelta(time() - 600, $byPath['src/main.js'], 2);
self::assertNotEmpty($files);
}
}