rob-revisited/ide/tests/CheckpointsTest.php

89 lines
4.1 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Async\Environment;
use JumPHP\Ide\Async\Processes;
use JumPHP\Ide\Claude\Checkpoints;
use PHPUnit\Framework\TestCase;
final class CheckpointsTest extends TestCase
{
private string $dir;
private string $cfg;
private Processes $processes;
protected function setUp(): void
{
$this->dir = sys_get_temp_dir() . '/jph-cp-' . bin2hex(random_bytes(4));
mkdir("$this->dir/proj/src", 0775, true);
$this->cfg = "$this->dir/cfg";
$this->processes = new Processes();
}
protected function tearDown(): void
{
exec('rm -rf ' . escapeshellarg($this->dir));
}
private function checkpoints(): Checkpoints
{
return new Checkpoints($this->processes, "$this->dir/proj", $this->cfg);
}
public function testGoingBackToAMessagePutsTheFilesBackAsTheyWereBeforeIt(): void
{
if (Environment::which('git') === null) {
self::markTestSkipped('git is not installed');
}
$p = "$this->dir/proj";
file_put_contents("$p/src/Hero.php", "v1\n");
file_put_contents("$p/README.md", "keep\n");
mkdir("$p/vendor");
file_put_contents("$p/vendor/lib.php", "vendor v1\n");
$cp = $this->checkpoints();
self::assertTrue($this->processes->wait($cp->take('cp0001', 'a', 'claude', null)), 'the files are kept');
// what the assistant does after the message
file_put_contents("$p/src/Hero.php", "v2\n");
unlink("$p/README.md");
mkdir("$p/src/Enemy");
file_put_contents("$p/src/Enemy/Bat.php", "new\n");
file_put_contents("$p/vendor/lib.php", "vendor v2\n");
$r = $this->processes->wait($cp->rewind('cp0001'));
self::assertSame("v1\n", file_get_contents("$p/src/Hero.php"), 'a changed file is put back');
self::assertSame("keep\n", file_get_contents("$p/README.md"), 'a removed file comes back');
self::assertFileDoesNotExist("$p/src/Enemy/Bat.php", 'a file made since goes');
self::assertDirectoryDoesNotExist("$p/src/Enemy", 'with the folder it leaves empty');
self::assertSame("vendor v2\n", file_get_contents("$p/vendor/lib.php"), 'vendor/ is never touched');
self::assertSame(3, $r['files']);
self::assertNull($r['session'], 'the first message: the conversation starts anew');
self::assertFileExists("$p/.jumphp/checkpoints/.gitignore");
self::assertDirectoryDoesNotExist("$p/.git", 'the project gets no repository of its own');
}
public function testClaudeGoesOnFromTheLastEntryBeforeTheMessage(): void
{
$sess = '11111111-2222-3333-4444-555555555555';
$folder = $this->cfg . '/projects/' . preg_replace('/[^A-Za-z0-9]/', '-', "$this->dir/proj");
mkdir($folder, 0775, true);
file_put_contents("$folder/$sess.jsonl", '{"type":"user","uuid":"aaaaaaaa-0000-0000-0000-000000000001"}' . "\n"
. '{"type":"assistant","uuid":"aaaaaaaa-0000-0000-0000-000000000002"}' . "\n" . '{"type":"queue-operation","operation":"dequeue"}' . "\n");
$cp = $this->checkpoints();
$this->processes->wait($cp->take('cp0002', 'a', 'claude', $sess));
$r = $this->processes->wait($cp->rewind('cp0002'));
self::assertSame($sess, $r['session']);
self::assertSame('aaaaaaaa-0000-0000-0000-000000000002', $cp->pendingFor('a', $sess));
self::assertNull($cp->pendingFor('a', $sess), 'only once');
}
public function testAnOpenCodeSessionGetsNewIdsKeepingTheirTimePart(): void
{
$json = '{"info":{"id":"ses_e39407396ffeJzKJ2eiDI6ElN0"},"messages":[{"info":{"id":"msg_1c6bf8cab001KBLXqR7t0qkv1v","sessionID":"ses_e39407396ffeJzKJ2eiDI6ElN0"}}]}';
[$out, $session] = Checkpoints::renew($json, 'ses_e39407396ffeJzKJ2eiDI6ElN0');
self::assertNotSame('ses_e39407396ffeJzKJ2eiDI6ElN0', $session);
self::assertStringStartsWith('ses_e39407396ffe', $session);
self::assertSame(2, substr_count($out, $session), 'the same new id everywhere');
self::assertStringNotContainsString('msg_1c6bf8cab001KBLXqR7t0qkv1v', $out);
self::assertStringContainsString('msg_1c6bf8cab001', $out);
}
}