rob-revisited/ide/tests/SessionImportTest.php

78 lines
4 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Claude\SessionImport;
use PHPUnit\Framework\TestCase;
final class SessionImportTest extends TestCase
{
private string $dir;
protected function setUp(): void
{
$this->dir = sys_get_temp_dir() . '/jph-si-' . bin2hex(random_bytes(4));
mkdir($this->dir . '/project', 0775, true);
}
protected function tearDown(): void
{
exec('rm -rf ' . escapeshellarg($this->dir));
}
public function testWhatWasTypedInTheTerminalAfterTheMarkComesBackAsItemsAndIsWrittenToTheHistory(): void
{
$root = $this->dir . '/project';
$cfg = $this->dir . '/cfg';
$folder = "$cfg/projects/" . preg_replace('/[^A-Za-z0-9]/', '-', $root);
mkdir($folder, 0775, true);
$line = fn (array $e) => json_encode($e) . "\n";
file_put_contents("$folder/abc-1.jsonl", $line(['type' => 'user', 'message' => ['content' => 'old question']]));
$import = new SessionImport($root, $cfg);
$mark = $import->mark('abc-1');
self::assertSame(1, $mark['lines']);
file_put_contents("$folder/abc-1.jsonl",
$line(['type' => 'user', 'message' => ['content' => 'new question']])
. $line(['type' => 'assistant', 'message' => ['model' => 'claude-opus-5-5', 'content' => [['type' => 'text', 'text' => 'An answer'],
['type' => 'tool_use', 'id' => 't1', 'name' => 'Bash', 'input' => ['command' => 'ls']]]]])
. $line(['type' => 'user', 'message' => ['content' => [['type' => 'tool_result', 'tool_use_id' => 't1', 'content' => 'a.txt']]]])
. $line(['type' => 'user', 'message' => ['content' => '<command-name>/exit</command-name>']]), FILE_APPEND);
$r = $import->import('c1', 'abc-1', $mark['lines']);
self::assertSame(1, $r['turns']);
self::assertSame(['user', 'assistant', 'tool'], array_column($r['items'], 'role'));
self::assertSame('new question', $r['items'][0]['text']);
$md = glob("$root/IA/HISTORY/CLAUDE/*/*/*-new-question.md");
self::assertCount(1, $md);
self::assertStringContainsString('An answer', (string)file_get_contents($md[0]));
}
public function testItIsFollowedLiveAndATurnIsWrittenOnceClaudeHasBeenQuiet(): void
{
$root = $this->dir . '/project';
$cfg = $this->dir . '/cfg';
$folder = "$cfg/projects/" . preg_replace('/[^A-Za-z0-9]/', '-', $root);
mkdir($folder, 0775, true);
$line = fn (array $e) => json_encode($e) . "\n";
file_put_contents("$folder/s1.jsonl", $line(['type' => 'user', 'message' => ['content' => 'before']]));
$import = new SessionImport($root, $cfg);
$mark = $import->mark('s1');
$import->start('c1', 's1', $mark['lines'], $mark['at']);
self::assertSame([], $import->poll('c1')['items']);
file_put_contents("$folder/s1.jsonl", $line(['type' => 'user', 'message' => ['content' => 'live question']])
. $line(['type' => 'assistant', 'message' => ['content' => [['type' => 'tool_use', 'id' => 't1', 'name' => 'Read', 'input' => ['file_path' => 'a.php']]]]]), FILE_APPEND);
$first = $import->poll('c1');
self::assertSame(['user', 'tool'], array_column($first['items'], 'role'));
self::assertSame(0, $first['turns']); // still working: nothing written yet
self::assertSame([], glob("$root/IA/HISTORY/CLAUDE/*/*/*.md") ?: []);
file_put_contents("$folder/s1.jsonl", $line(['type' => 'user', 'message' => ['content' => [['type' => 'tool_result', 'tool_use_id' => 't1', 'content' => 'x']]]])
. $line(['type' => 'assistant', 'message' => ['content' => [['type' => 'text', 'text' => 'Done']]]]), FILE_APPEND);
self::assertSame(['assistant'], array_column($import->poll('c1')['items'], 'role'));
$end = $import->stop('c1');
self::assertSame(1, $end['turns']);
self::assertCount(1, glob("$root/IA/HISTORY/CLAUDE/*/*/*-live-question.md"));
self::assertCount(1, glob("$root/IA/HISTORY/CLAUDE/*/*/*-live-question.actions.md"));
}
}